Bug 1890689 accumulate input in LargerReceiverBlockSizeThanDesiredBuffering GTest...
[gecko.git] / gfx / gl / SharedSurfaceIO.cpp
blob459faa64b3587dca5194f0f1f4320e4a067de6dc
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 bool 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 return ioSurf->BindTexImage(gl, 0);
49 /*static*/
50 UniquePtr<SharedSurface_IOSurface> SharedSurface_IOSurface::Create(
51 const SharedSurfaceDesc& desc) {
52 const auto& size = desc.size;
53 const RefPtr<MacIOSurface> ioSurf =
54 MacIOSurface::CreateIOSurface(size.width, size.height, true);
55 if (!ioSurf) {
56 NS_WARNING("Failed to create MacIOSurface.");
57 return nullptr;
60 ioSurf->SetColorSpace(desc.colorSpace);
62 // -
64 auto tex = MakeUnique<Texture>(*desc.gl);
65 if (!BackTextureWithIOSurf(desc.gl, tex->name, ioSurf)) {
66 return nullptr;
69 const GLenum target = LOCAL_GL_TEXTURE_RECTANGLE;
70 auto fb = MozFramebuffer::CreateForBacking(desc.gl, desc.size, 0, false,
71 target, tex->name);
72 if (!fb) return nullptr;
74 return AsUnique(
75 new SharedSurface_IOSurface(desc, std::move(fb), std::move(tex), ioSurf));
78 SharedSurface_IOSurface::SharedSurface_IOSurface(
79 const SharedSurfaceDesc& desc, UniquePtr<MozFramebuffer> fb,
80 UniquePtr<Texture> tex, const RefPtr<MacIOSurface>& ioSurf)
81 : SharedSurface(desc, std::move(fb)),
82 mTex(std::move(tex)),
83 mIOSurf(ioSurf) {}
85 SharedSurface_IOSurface::~SharedSurface_IOSurface() = default;
87 void SharedSurface_IOSurface::ProducerReleaseImpl() {
88 const auto& gl = mDesc.gl;
89 if (!gl) return;
90 gl->MakeCurrent();
91 gl->fFlush();
94 Maybe<layers::SurfaceDescriptor>
95 SharedSurface_IOSurface::ToSurfaceDescriptor() {
96 const bool isOpaque = false; // RGBA
97 return Some(layers::SurfaceDescriptorMacIOSurface(
98 mIOSurf->GetIOSurfaceID(), isOpaque, mIOSurf->GetYUVColorSpace()));
101 } // namespace gl
102 } // namespace mozilla