TextureHelper.h (4396B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "Types.h" 8 #include "gfxImageSurface.h" 9 #include "gfxPlatform.h" 10 #include "mozilla/RefPtr.h" 11 #include "mozilla/gfx/Point.h" 12 #include "mozilla/layers/BufferTexture.h" 13 #include "mozilla/layers/LayersTypes.h" 14 #include "mozilla/layers/TextureClient.h" 15 #include "mozilla/layers/TextureHost.h" 16 #ifdef XP_WIN 17 # include "IMFYCbCrImage.h" 18 # include "mozilla/gfx/DeviceManagerDx.h" 19 # include "mozilla/layers/D3D11YCbCrImage.h" 20 # include "mozilla/layers/TextureD3D11.h" 21 #endif 22 23 namespace mozilla { 24 namespace layers { 25 26 using gfx::BackendType; 27 using gfx::IntSize; 28 using gfx::SurfaceFormat; 29 30 /** 31 * Create a YCbCrTextureClient according to the given backend. 32 */ 33 static already_AddRefed<TextureClient> CreateYCbCrTextureClientWithBackend( 34 LayersBackend aLayersBackend) { 35 TextureData* data = nullptr; 36 IntSize size = IntSize(200, 150); 37 IntSize ySize = IntSize(400, 300); 38 39 RefPtr<gfxImageSurface> ySurface = 40 new gfxImageSurface(ySize, SurfaceFormat::A8); 41 RefPtr<gfxImageSurface> cbSurface = 42 new gfxImageSurface(size, SurfaceFormat::A8); 43 RefPtr<gfxImageSurface> crSurface = 44 new gfxImageSurface(size, SurfaceFormat::A8); 45 46 PlanarYCbCrData clientData; 47 clientData.mYChannel = ySurface->Data(); 48 clientData.mCbChannel = cbSurface->Data(); 49 clientData.mCrChannel = crSurface->Data(); 50 clientData.mPictureRect = IntRect(IntPoint(0, 0), ySurface->GetSize()); 51 clientData.mYStride = ySurface->Stride(); 52 clientData.mCbCrStride = cbSurface->Stride(); 53 clientData.mStereoMode = StereoMode::MONO; 54 clientData.mChromaSubsampling = ChromaSubsampling::HALF_WIDTH_AND_HEIGHT; 55 clientData.mYSkip = 0; 56 clientData.mCbSkip = 0; 57 clientData.mCrSkip = 0; 58 clientData.mCrSkip = 0; 59 60 // Create YCbCrTexture for basic backend. 61 if (aLayersBackend == LayersBackend::LAYERS_BASIC) { 62 return TextureClient::CreateForYCbCr( 63 nullptr, clientData.mPictureRect, clientData.YDataSize(), 64 clientData.mYStride, clientData.CbCrDataSize(), clientData.mCbCrStride, 65 StereoMode::MONO, gfx::ColorDepth::COLOR_8, gfx::YUVColorSpace::BT601, 66 gfx::ColorRange::LIMITED, clientData.mChromaSubsampling, 67 TextureFlags::DEALLOCATE_CLIENT); 68 } 69 70 if (data) { 71 return MakeAndAddRef<TextureClient>(data, TextureFlags::DEALLOCATE_CLIENT, 72 nullptr); 73 } 74 75 return nullptr; 76 } 77 78 /** 79 * Create a TextureClient according to the given backend. 80 */ 81 static already_AddRefed<TextureClient> CreateTextureClientWithBackend( 82 LayersBackend aLayersBackend) { 83 TextureData* data = nullptr; 84 SurfaceFormat format = gfxPlatform::GetPlatform()->Optimal2DFormatForContent( 85 gfxContentType::COLOR_ALPHA); 86 BackendType moz2DBackend = 87 gfxPlatform::GetPlatform()->GetContentBackendFor(aLayersBackend); 88 TextureAllocationFlags allocFlags = TextureAllocationFlags::ALLOC_DEFAULT; 89 IntSize size = IntSize(400, 300); 90 TextureFlags textureFlags = TextureFlags::DEALLOCATE_CLIENT; 91 92 if (!gfx::Factory::AllowedSurfaceSize(size)) { 93 return nullptr; 94 } 95 96 if (!data && aLayersBackend == LayersBackend::LAYERS_BASIC) { 97 // Create BufferTextureData. 98 data = BufferTextureData::Create(size, format, moz2DBackend, aLayersBackend, 99 textureFlags, allocFlags, nullptr); 100 } 101 102 if (data) { 103 return MakeAndAddRef<TextureClient>(data, textureFlags, nullptr); 104 } 105 106 return nullptr; 107 } 108 109 /** 110 * Create a TextureHost according to the given TextureClient. 111 */ 112 already_AddRefed<TextureHost> CreateTextureHostWithBackend( 113 TextureClient* aClient, ISurfaceAllocator* aDeallocator, 114 LayersBackend& aLayersBackend) { 115 if (!aClient) { 116 return nullptr; 117 } 118 119 // client serialization 120 SurfaceDescriptor descriptor; 121 ReadLockDescriptor readLock = null_t(); 122 RefPtr<TextureHost> textureHost; 123 124 aClient->ToSurfaceDescriptor(descriptor); 125 126 wr::MaybeExternalImageId id = Nothing(); 127 return TextureHost::Create(descriptor, readLock, aDeallocator, aLayersBackend, 128 aClient->GetFlags(), id); 129 } 130 131 } // namespace layers 132 } // namespace mozilla