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"
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())) {}
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();
49 ioSurf
->CGLTexImageIOSurface2D(gl
, cgl
, 0);
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);
59 NS_WARNING("Failed to create MacIOSurface.");
63 ioSurf
->SetColorSpace(desc
.colorSpace
);
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,
73 if (!fb
) return nullptr;
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
)),
86 SharedSurface_IOSurface::~SharedSurface_IOSurface() = default;
88 void SharedSurface_IOSurface::ProducerReleaseImpl() {
89 const auto& gl
= mDesc
.gl
;
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()));
103 } // namespace mozilla