SharedSurfaceIO.cpp (3382B)
1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 4; -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #include "SharedSurfaceIO.h" 7 8 #include "GLContextCGL.h" 9 #include "MozFramebuffer.h" 10 #include "mozilla/gfx/MacIOSurface.h" 11 #include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor, etc 12 #include "mozilla/layers/LayersTypes.h" 13 #include "ScopedGLHelpers.h" 14 15 namespace mozilla { 16 namespace gl { 17 18 // - 19 // Factory 20 21 SurfaceFactory_IOSurface::SurfaceFactory_IOSurface(GLContext& gl) 22 : SurfaceFactory({&gl, SharedSurfaceType::IOSurface, 23 layers::TextureType::MacIOSurface, true}), 24 mMaxDims(gfx::IntSize::Truncate(MacIOSurface::GetMaxWidth(), 25 MacIOSurface::GetMaxHeight())) {} 26 27 // - 28 // Surface 29 30 static bool BackTextureWithIOSurf(GLContext* const gl, const GLuint tex, 31 MacIOSurface* const ioSurf) { 32 MOZ_ASSERT(gl->IsCurrent()); 33 34 ScopedBindTexture texture(gl, tex, LOCAL_GL_TEXTURE_RECTANGLE_ARB); 35 36 gl->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB, 37 LOCAL_GL_TEXTURE_MIN_FILTER, LOCAL_GL_LINEAR); 38 gl->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB, 39 LOCAL_GL_TEXTURE_MAG_FILTER, LOCAL_GL_LINEAR); 40 gl->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB, LOCAL_GL_TEXTURE_WRAP_S, 41 LOCAL_GL_CLAMP_TO_EDGE); 42 gl->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB, LOCAL_GL_TEXTURE_WRAP_T, 43 LOCAL_GL_CLAMP_TO_EDGE); 44 45 return ioSurf->BindTexImage(gl, 0); 46 } 47 48 /*static*/ 49 UniquePtr<SharedSurface_IOSurface> SharedSurface_IOSurface::Create( 50 const SharedSurfaceDesc& desc) { 51 const auto& size = desc.size; 52 const RefPtr<MacIOSurface> ioSurf = 53 MacIOSurface::CreateIOSurface(size.width, size.height, true); 54 if (!ioSurf) { 55 NS_WARNING("Failed to create MacIOSurface."); 56 return nullptr; 57 } 58 59 ioSurf->SetColorSpace(desc.colorSpace); 60 61 // - 62 63 auto tex = MakeUnique<Texture>(*desc.gl); 64 if (!BackTextureWithIOSurf(desc.gl, tex->name, ioSurf)) { 65 return nullptr; 66 } 67 68 const GLenum target = LOCAL_GL_TEXTURE_RECTANGLE; 69 auto fb = MozFramebuffer::CreateForBacking(desc.gl, desc.size, 0, false, 70 target, tex->name); 71 if (!fb) return nullptr; 72 73 return AsUnique( 74 new SharedSurface_IOSurface(desc, std::move(fb), std::move(tex), ioSurf)); 75 } 76 77 SharedSurface_IOSurface::SharedSurface_IOSurface( 78 const SharedSurfaceDesc& desc, UniquePtr<MozFramebuffer> fb, 79 UniquePtr<Texture> tex, const RefPtr<MacIOSurface>& ioSurf) 80 : SharedSurface(desc, std::move(fb)), 81 mTex(std::move(tex)), 82 mIOSurf(ioSurf) {} 83 84 SharedSurface_IOSurface::~SharedSurface_IOSurface() = default; 85 86 void SharedSurface_IOSurface::ProducerReleaseImpl() { 87 const auto& gl = mDesc.gl; 88 if (!gl) return; 89 gl->MakeCurrent(); 90 gl->fFlush(); 91 } 92 93 Maybe<layers::SurfaceDescriptor> 94 SharedSurface_IOSurface::ToSurfaceDescriptor() { 95 const bool isOpaque = false; // RGBA 96 return Some(layers::SurfaceDescriptorMacIOSurface( 97 mIOSurf->GetIOSurfaceID(), isOpaque, mIOSurf->GetYUVColorSpace(), 98 (layers::GpuFence*)nullptr)); 99 } 100 101 } // namespace gl 102 } // namespace mozilla