Bug 1685183 Part 2: Remove scale factor from SurfaceDescriptorMacIOSurface. r=jgilbert
[gecko.git] / gfx / layers / opengl / MacIOSurfaceTextureHostOGL.cpp
blob65c43c1351720413948d1021e9a998cae727e6e1
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/. */
7 #include "MacIOSurfaceTextureHostOGL.h"
8 #include "mozilla/gfx/gfxVars.h"
9 #include "mozilla/gfx/MacIOSurface.h"
10 #include "mozilla/webrender/RenderMacIOSurfaceTextureHost.h"
11 #include "mozilla/webrender/RenderThread.h"
12 #include "mozilla/webrender/WebRenderAPI.h"
13 #include "GLContextCGL.h"
15 namespace mozilla {
16 namespace layers {
18 MacIOSurfaceTextureHostOGL::MacIOSurfaceTextureHostOGL(
19 TextureFlags aFlags, const SurfaceDescriptorMacIOSurface& aDescriptor)
20 : TextureHost(aFlags) {
21 MOZ_COUNT_CTOR(MacIOSurfaceTextureHostOGL);
22 mSurface = MacIOSurface::LookupSurface(aDescriptor.surfaceId(),
23 !aDescriptor.isOpaque(),
24 aDescriptor.yUVColorSpace());
25 if (!mSurface) {
26 gfxCriticalNote << "Failed to look up MacIOSurface";
30 MacIOSurfaceTextureHostOGL::~MacIOSurfaceTextureHostOGL() {
31 MOZ_COUNT_DTOR(MacIOSurfaceTextureHostOGL);
34 GLTextureSource* MacIOSurfaceTextureHostOGL::CreateTextureSourceForPlane(
35 size_t aPlane) {
36 MOZ_ASSERT(mSurface);
38 GLuint textureHandle;
39 gl::GLContext* gl = mProvider->GetGLContext();
40 gl->fGenTextures(1, &textureHandle);
41 gl->fBindTexture(LOCAL_GL_TEXTURE_RECTANGLE_ARB, textureHandle);
42 gl->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB, LOCAL_GL_TEXTURE_WRAP_T,
43 LOCAL_GL_CLAMP_TO_EDGE);
44 gl->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB, LOCAL_GL_TEXTURE_WRAP_S,
45 LOCAL_GL_CLAMP_TO_EDGE);
47 gfx::SurfaceFormat readFormat = gfx::SurfaceFormat::UNKNOWN;
48 mSurface->CGLTexImageIOSurface2D(
49 gl, gl::GLContextCGL::Cast(gl)->GetCGLContext(), aPlane, &readFormat);
50 // With compositorOGL, we doesn't support the yuv interleaving format yet.
51 MOZ_ASSERT(readFormat != gfx::SurfaceFormat::YUV422);
53 return new GLTextureSource(
54 mProvider, textureHandle, LOCAL_GL_TEXTURE_RECTANGLE_ARB,
55 gfx::IntSize(mSurface->GetDevicePixelWidth(aPlane),
56 mSurface->GetDevicePixelHeight(aPlane)),
57 readFormat);
60 bool MacIOSurfaceTextureHostOGL::Lock() {
61 if (!gl() || !gl()->MakeCurrent() || !mSurface) {
62 return false;
65 if (!mTextureSource) {
66 mTextureSource = CreateTextureSourceForPlane(0);
68 RefPtr<TextureSource> prev = mTextureSource;
69 for (size_t i = 1; i < mSurface->GetPlaneCount(); i++) {
70 RefPtr<TextureSource> next = CreateTextureSourceForPlane(i);
71 prev->SetNextSibling(next);
72 prev = next;
75 return true;
78 void MacIOSurfaceTextureHostOGL::SetTextureSourceProvider(
79 TextureSourceProvider* aProvider) {
80 if (!aProvider || !aProvider->GetGLContext()) {
81 mTextureSource = nullptr;
82 mProvider = nullptr;
83 return;
86 if (mProvider != aProvider) {
87 // Cannot share GL texture identifiers across compositors.
88 mTextureSource = nullptr;
91 mProvider = aProvider;
94 gfx::SurfaceFormat MacIOSurfaceTextureHostOGL::GetFormat() const {
95 if (!mSurface) {
96 return gfx::SurfaceFormat::UNKNOWN;
98 return mSurface->GetFormat();
101 gfx::SurfaceFormat MacIOSurfaceTextureHostOGL::GetReadFormat() const {
102 if (!mSurface) {
103 return gfx::SurfaceFormat::UNKNOWN;
105 return mSurface->GetReadFormat();
108 gfx::IntSize MacIOSurfaceTextureHostOGL::GetSize() const {
109 if (!mSurface) {
110 return gfx::IntSize();
112 return gfx::IntSize(mSurface->GetDevicePixelWidth(),
113 mSurface->GetDevicePixelHeight());
116 gl::GLContext* MacIOSurfaceTextureHostOGL::gl() const {
117 return mProvider ? mProvider->GetGLContext() : nullptr;
120 gfx::YUVColorSpace MacIOSurfaceTextureHostOGL::GetYUVColorSpace() const {
121 if (!mSurface) {
122 return gfx::YUVColorSpace::Identity;
124 return mSurface->GetYUVColorSpace();
127 gfx::ColorRange MacIOSurfaceTextureHostOGL::GetColorRange() const {
128 if (!mSurface) {
129 return gfx::ColorRange::LIMITED;
131 return mSurface->IsFullRange() ? gfx::ColorRange::FULL
132 : gfx::ColorRange::LIMITED;
135 void MacIOSurfaceTextureHostOGL::CreateRenderTexture(
136 const wr::ExternalImageId& aExternalImageId) {
137 RefPtr<wr::RenderTextureHost> texture =
138 new wr::RenderMacIOSurfaceTextureHost(GetMacIOSurface());
140 wr::RenderThread::Get()->RegisterExternalImage(wr::AsUint64(aExternalImageId),
141 texture.forget());
144 uint32_t MacIOSurfaceTextureHostOGL::NumSubTextures() {
145 if (!mSurface) {
146 return 0;
149 switch (GetFormat()) {
150 case gfx::SurfaceFormat::R8G8B8X8:
151 case gfx::SurfaceFormat::R8G8B8A8:
152 case gfx::SurfaceFormat::B8G8R8A8:
153 case gfx::SurfaceFormat::B8G8R8X8:
154 case gfx::SurfaceFormat::YUV422: {
155 return 1;
157 case gfx::SurfaceFormat::NV12: {
158 return 2;
160 default: {
161 MOZ_ASSERT_UNREACHABLE("unexpected format");
162 return 1;
167 void MacIOSurfaceTextureHostOGL::PushResourceUpdates(
168 wr::TransactionBuilder& aResources, ResourceUpdateOp aOp,
169 const Range<wr::ImageKey>& aImageKeys, const wr::ExternalImageId& aExtID) {
170 MOZ_ASSERT(mSurface);
172 auto method = aOp == TextureHost::ADD_IMAGE
173 ? &wr::TransactionBuilder::AddExternalImage
174 : &wr::TransactionBuilder::UpdateExternalImage;
175 auto imageType =
176 wr::ExternalImageType::TextureHandle(wr::ImageBufferKind::TextureRect);
178 switch (GetFormat()) {
179 case gfx::SurfaceFormat::B8G8R8A8:
180 case gfx::SurfaceFormat::B8G8R8X8: {
181 MOZ_ASSERT(aImageKeys.length() == 1);
182 MOZ_ASSERT(mSurface->GetPlaneCount() == 0);
183 // The internal pixel format of MacIOSurface is always BGRX or BGRA
184 // format.
185 auto format = GetFormat() == gfx::SurfaceFormat::B8G8R8A8
186 ? gfx::SurfaceFormat::B8G8R8A8
187 : gfx::SurfaceFormat::B8G8R8X8;
188 wr::ImageDescriptor descriptor(GetSize(), format);
189 (aResources.*method)(aImageKeys[0], descriptor, aExtID, imageType, 0);
190 break;
192 case gfx::SurfaceFormat::YUV422: {
193 // This is the special buffer format. The buffer contents could be a
194 // converted RGB interleaving data or a YCbCr interleaving data depending
195 // on the different platform setting. (e.g. It will be RGB at OpenGL 2.1
196 // and YCbCr at OpenGL 3.1)
197 MOZ_ASSERT(aImageKeys.length() == 1);
198 MOZ_ASSERT(mSurface->GetPlaneCount() == 0);
199 wr::ImageDescriptor descriptor(GetSize(), gfx::SurfaceFormat::B8G8R8X8);
200 (aResources.*method)(aImageKeys[0], descriptor, aExtID, imageType, 0);
201 break;
203 case gfx::SurfaceFormat::NV12: {
204 MOZ_ASSERT(aImageKeys.length() == 2);
205 MOZ_ASSERT(mSurface->GetPlaneCount() == 2);
206 wr::ImageDescriptor descriptor0(
207 gfx::IntSize(mSurface->GetDevicePixelWidth(0),
208 mSurface->GetDevicePixelHeight(0)),
209 gfx::SurfaceFormat::A8);
210 wr::ImageDescriptor descriptor1(
211 gfx::IntSize(mSurface->GetDevicePixelWidth(1),
212 mSurface->GetDevicePixelHeight(1)),
213 gfx::SurfaceFormat::R8G8);
214 (aResources.*method)(aImageKeys[0], descriptor0, aExtID, imageType, 0);
215 (aResources.*method)(aImageKeys[1], descriptor1, aExtID, imageType, 1);
216 break;
218 default: {
219 MOZ_ASSERT_UNREACHABLE("unexpected to be called");
224 void MacIOSurfaceTextureHostOGL::PushDisplayItems(
225 wr::DisplayListBuilder& aBuilder, const wr::LayoutRect& aBounds,
226 const wr::LayoutRect& aClip, wr::ImageRendering aFilter,
227 const Range<wr::ImageKey>& aImageKeys, PushDisplayItemFlagSet aFlags) {
228 bool preferCompositorSurface =
229 aFlags.contains(PushDisplayItemFlag::PREFER_COMPOSITOR_SURFACE);
230 switch (GetFormat()) {
231 case gfx::SurfaceFormat::B8G8R8A8:
232 case gfx::SurfaceFormat::B8G8R8X8: {
233 MOZ_ASSERT(aImageKeys.length() == 1);
234 MOZ_ASSERT(mSurface->GetPlaneCount() == 0);
235 // We disable external compositing for RGB surfaces for now until
236 // we've tested support more thoroughly. Bug 1667917.
237 aBuilder.PushImage(aBounds, aClip, true, aFilter, aImageKeys[0],
238 !(mFlags & TextureFlags::NON_PREMULTIPLIED),
239 wr::ColorF{1.0f, 1.0f, 1.0f, 1.0f},
240 preferCompositorSurface,
241 /* aSupportsExternalCompositing */ false);
242 break;
244 case gfx::SurfaceFormat::YUV422: {
245 MOZ_ASSERT(aImageKeys.length() == 1);
246 MOZ_ASSERT(mSurface->GetPlaneCount() == 0);
247 // Those images can only be generated at present by the Apple H264 decoder
248 // which only supports 8 bits color depth.
249 aBuilder.PushYCbCrInterleavedImage(
250 aBounds, aClip, true, aImageKeys[0], wr::ColorDepth::Color8,
251 wr::ToWrYuvColorSpace(GetYUVColorSpace()),
252 wr::ToWrColorRange(GetColorRange()), aFilter, preferCompositorSurface,
253 /* aSupportsExternalCompositing */ true);
254 break;
256 case gfx::SurfaceFormat::NV12: {
257 MOZ_ASSERT(aImageKeys.length() == 2);
258 MOZ_ASSERT(mSurface->GetPlaneCount() == 2);
259 // Those images can only be generated at present by the Apple H264 decoder
260 // which only supports 8 bits color depth.
261 aBuilder.PushNV12Image(
262 aBounds, aClip, true, aImageKeys[0], aImageKeys[1],
263 wr::ColorDepth::Color8, wr::ToWrYuvColorSpace(GetYUVColorSpace()),
264 wr::ToWrColorRange(GetColorRange()), aFilter, preferCompositorSurface,
265 /* aSupportsExternalCompositing */ true);
266 break;
268 default: {
269 MOZ_ASSERT_UNREACHABLE("unexpected to be called");
274 } // namespace layers
275 } // namespace mozilla