Backed out 2 changesets (bug 1881078, bug 1879806) for causing dt failures @ devtools...
[gecko.git] / gfx / gl / SharedSurfaceIO.cpp
blob1fd0f22d31f88ae00e3b32154c32c5ed2ec9812b
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/. */
6 #include "SharedSurfaceIO.h"
8 #include "GLContextCGL.h"
9 #include "MozFramebuffer.h"
10 #include "mozilla/DebugOnly.h"
11 #include "mozilla/gfx/MacIOSurface.h"
12 #include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor, etc
13 #include "mozilla/layers/LayersTypes.h"
14 #include "ScopedGLHelpers.h"
16 namespace mozilla {
17 namespace gl {
19 // -
20 // Factory
22 SurfaceFactory_IOSurface::SurfaceFactory_IOSurface(GLContext& gl)
23 : SurfaceFactory({&gl, SharedSurfaceType::IOSurface,
24 layers::TextureType::MacIOSurface, true}),
25 mMaxDims(gfx::IntSize::Truncate(MacIOSurface::GetMaxWidth(),
26 MacIOSurface::GetMaxHeight())) {}
28 // -
29 // Surface
31 static void BackTextureWithIOSurf(GLContext* const gl, const GLuint tex,
32 MacIOSurface* const ioSurf) {
33 MOZ_ASSERT(gl->IsCurrent());
35 ScopedBindTexture texture(gl, tex, LOCAL_GL_TEXTURE_RECTANGLE_ARB);
37 gl->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
38 LOCAL_GL_TEXTURE_MIN_FILTER, LOCAL_GL_LINEAR);
39 gl->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
40 LOCAL_GL_TEXTURE_MAG_FILTER, LOCAL_GL_LINEAR);
41 gl->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB, LOCAL_GL_TEXTURE_WRAP_S,
42 LOCAL_GL_CLAMP_TO_EDGE);
43 gl->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB, LOCAL_GL_TEXTURE_WRAP_T,
44 LOCAL_GL_CLAMP_TO_EDGE);
46 CGLContextObj cgl = GLContextCGL::Cast(gl)->GetCGLContext();
47 MOZ_ASSERT(cgl);
49 ioSurf->CGLTexImageIOSurface2D(gl, cgl, 0);
52 /*static*/
53 UniquePtr<SharedSurface_IOSurface> SharedSurface_IOSurface::Create(
54 const SharedSurfaceDesc& desc) {
55 const auto& size = desc.size;
56 const RefPtr<MacIOSurface> ioSurf =
57 MacIOSurface::CreateIOSurface(size.width, size.height, true);
58 if (!ioSurf) {
59 NS_WARNING("Failed to create MacIOSurface.");
60 return nullptr;
63 ioSurf->SetColorSpace(desc.colorSpace);
65 // -
67 auto tex = MakeUnique<Texture>(*desc.gl);
68 BackTextureWithIOSurf(desc.gl, tex->name, ioSurf);
70 const GLenum target = LOCAL_GL_TEXTURE_RECTANGLE;
71 auto fb = MozFramebuffer::CreateForBacking(desc.gl, desc.size, 0, false,
72 target, tex->name);
73 if (!fb) return nullptr;
75 return AsUnique(
76 new SharedSurface_IOSurface(desc, std::move(fb), std::move(tex), ioSurf));
79 SharedSurface_IOSurface::SharedSurface_IOSurface(
80 const SharedSurfaceDesc& desc, UniquePtr<MozFramebuffer> fb,
81 UniquePtr<Texture> tex, const RefPtr<MacIOSurface>& ioSurf)
82 : SharedSurface(desc, std::move(fb)),
83 mTex(std::move(tex)),
84 mIOSurf(ioSurf) {}
86 SharedSurface_IOSurface::~SharedSurface_IOSurface() = default;
88 void SharedSurface_IOSurface::ProducerReleaseImpl() {
89 const auto& gl = mDesc.gl;
90 if (!gl) return;
91 gl->MakeCurrent();
92 gl->fFlush();
95 Maybe<layers::SurfaceDescriptor>
96 SharedSurface_IOSurface::ToSurfaceDescriptor() {
97 const bool isOpaque = false; // RGBA
98 return Some(layers::SurfaceDescriptorMacIOSurface(
99 mIOSurf->GetIOSurfaceID(), isOpaque, mIOSurf->GetYUVColorSpace()));
102 } // namespace gl
103 } // namespace mozilla