MacIOSurfaceTextureClientOGL.cpp (4031B)
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 "MacIOSurfaceTextureClientOGL.h" 8 #include "mozilla/gfx/MacIOSurface.h" 9 #include "MacIOSurfaceHelpers.h" 10 #include "gfxPlatform.h" 11 12 namespace mozilla { 13 namespace layers { 14 15 using namespace gfx; 16 17 MacIOSurfaceTextureData::MacIOSurfaceTextureData(MacIOSurface* aSurface, 18 BackendType aBackend) 19 : mSurface(aSurface), mBackend(aBackend) { 20 MOZ_ASSERT(mSurface); 21 } 22 23 MacIOSurfaceTextureData::~MacIOSurfaceTextureData() = default; 24 25 // static 26 MacIOSurfaceTextureData* MacIOSurfaceTextureData::Create(MacIOSurface* aSurface, 27 BackendType aBackend) { 28 MOZ_ASSERT(aSurface); 29 if (!aSurface) { 30 return nullptr; 31 } 32 return new MacIOSurfaceTextureData(aSurface, aBackend); 33 } 34 35 MacIOSurfaceTextureData* MacIOSurfaceTextureData::Create(const IntSize& aSize, 36 SurfaceFormat aFormat, 37 BackendType aBackend) { 38 if (aFormat != SurfaceFormat::B8G8R8A8 && 39 aFormat != SurfaceFormat::B8G8R8X8) { 40 return nullptr; 41 } 42 43 RefPtr<MacIOSurface> surf = MacIOSurface::CreateIOSurface( 44 aSize.width, aSize.height, aFormat == SurfaceFormat::B8G8R8A8); 45 if (!surf) { 46 return nullptr; 47 } 48 49 return Create(surf, aBackend); 50 } 51 52 bool MacIOSurfaceTextureData::Serialize(SurfaceDescriptor& aOutDescriptor) { 53 RefPtr<layers::GpuFence> gpuFence; 54 aOutDescriptor = SurfaceDescriptorMacIOSurface( 55 mSurface->GetIOSurfaceID(), !mSurface->HasAlpha(), 56 mSurface->GetYUVColorSpace(), std::move(gpuFence)); 57 return true; 58 } 59 60 void MacIOSurfaceTextureData::GetSubDescriptor( 61 RemoteDecoderVideoSubDescriptor* const aOutDesc) { 62 RefPtr<layers::GpuFence> gpuFence; 63 *aOutDesc = SurfaceDescriptorMacIOSurface( 64 mSurface->GetIOSurfaceID(), !mSurface->HasAlpha(), 65 mSurface->GetYUVColorSpace(), std::move(gpuFence)); 66 } 67 68 void MacIOSurfaceTextureData::FillInfo(TextureData::Info& aInfo) const { 69 aInfo.size = gfx::IntSize(mSurface->GetDevicePixelWidth(), 70 mSurface->GetDevicePixelHeight()); 71 aInfo.format = 72 mSurface->HasAlpha() ? SurfaceFormat::B8G8R8A8 : SurfaceFormat::B8G8R8X8; 73 aInfo.hasSynchronization = false; 74 aInfo.supportsMoz2D = true; 75 aInfo.canExposeMappedData = false; 76 } 77 78 bool MacIOSurfaceTextureData::Lock(OpenMode) { return mSurface->Lock(false); } 79 80 void MacIOSurfaceTextureData::Unlock() { mSurface->Unlock(false); } 81 82 already_AddRefed<DataSourceSurface> MacIOSurfaceTextureData::GetAsSurface() { 83 RefPtr<SourceSurface> surf = CreateSourceSurfaceFromMacIOSurface(mSurface); 84 return surf->GetDataSurface(); 85 } 86 87 already_AddRefed<DrawTarget> MacIOSurfaceTextureData::BorrowDrawTarget() { 88 MOZ_ASSERT(mBackend != BackendType::NONE); 89 if (mBackend == BackendType::NONE) { 90 // shouldn't happen, but degrade gracefully 91 return nullptr; 92 } 93 return Factory::CreateDrawTargetForData( 94 mBackend, (unsigned char*)mSurface->GetBaseAddress(), 95 IntSize(mSurface->GetWidth(), mSurface->GetHeight()), 96 mSurface->GetBytesPerRow(), 97 mSurface->HasAlpha() ? SurfaceFormat::B8G8R8A8 : SurfaceFormat::B8G8R8X8, 98 true); 99 } 100 101 void MacIOSurfaceTextureData::Deallocate(LayersIPCChannel*) { 102 mSurface = nullptr; 103 } 104 105 void MacIOSurfaceTextureData::Forget(LayersIPCChannel*) { mSurface = nullptr; } 106 107 bool MacIOSurfaceTextureData::UpdateFromSurface(gfx::SourceSurface* aSurface) { 108 RefPtr<DrawTarget> dt = BorrowDrawTarget(); 109 if (!dt) { 110 return false; 111 } 112 113 dt->CopySurface(aSurface, IntRect(IntPoint(), aSurface->GetSize()), 114 IntPoint()); 115 return true; 116 } 117 118 } // namespace layers 119 } // namespace mozilla