-
Notifications
You must be signed in to change notification settings - Fork 54
/
MacIOSurface.cpp
319 lines (268 loc) · 11.1 KB
/
MacIOSurface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "MacIOSurface.h"
#include <OpenGL/gl.h>
#include <OpenGL/CGLIOSurface.h>
#include <QuartzCore/QuartzCore.h>
#include "GLConsts.h"
#include "GLContextCGL.h"
#include "mozilla/Assertions.h"
#include "mozilla/RefPtr.h"
using namespace mozilla;
MacIOSurface::MacIOSurface(CFTypeRefPtr<IOSurfaceRef> aIOSurfaceRef,
double aContentsScaleFactor, bool aHasAlpha,
gfx::YUVColorSpace aColorSpace)
: mIOSurfaceRef(std::move(aIOSurfaceRef)),
mContentsScaleFactor(aContentsScaleFactor),
mHasAlpha(aHasAlpha),
mColorSpace(aColorSpace) {
IncrementUseCount();
}
MacIOSurface::~MacIOSurface() {
MOZ_RELEASE_ASSERT(!IsLocked(), "Destroying locked surface");
DecrementUseCount();
}
/* static */
already_AddRefed<MacIOSurface> MacIOSurface::CreateIOSurface(
int aWidth, int aHeight, double aContentsScaleFactor, bool aHasAlpha) {
if (aContentsScaleFactor <= 0) return nullptr;
CFMutableDictionaryRef props = ::CFDictionaryCreateMutable(
kCFAllocatorDefault, 4, &kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
if (!props) return nullptr;
MOZ_ASSERT((size_t)aWidth <= GetMaxWidth());
MOZ_ASSERT((size_t)aHeight <= GetMaxHeight());
int32_t bytesPerElem = 4;
size_t intScaleFactor = ceil(aContentsScaleFactor);
aWidth *= intScaleFactor;
aHeight *= intScaleFactor;
CFNumberRef cfWidth = ::CFNumberCreate(nullptr, kCFNumberSInt32Type, &aWidth);
CFNumberRef cfHeight =
::CFNumberCreate(nullptr, kCFNumberSInt32Type, &aHeight);
CFNumberRef cfBytesPerElem =
::CFNumberCreate(nullptr, kCFNumberSInt32Type, &bytesPerElem);
::CFDictionaryAddValue(props, kIOSurfaceWidth, cfWidth);
::CFRelease(cfWidth);
::CFDictionaryAddValue(props, kIOSurfaceHeight, cfHeight);
::CFRelease(cfHeight);
::CFDictionaryAddValue(props, kIOSurfaceBytesPerElement, cfBytesPerElem);
::CFRelease(cfBytesPerElem);
::CFDictionaryAddValue(props, kIOSurfaceIsGlobal, kCFBooleanTrue);
CFTypeRefPtr<IOSurfaceRef> surfaceRef =
CFTypeRefPtr<IOSurfaceRef>::WrapUnderCreateRule(::IOSurfaceCreate(props));
::CFRelease(props);
if (!surfaceRef) {
return nullptr;
}
RefPtr<MacIOSurface> ioSurface =
new MacIOSurface(std::move(surfaceRef), aContentsScaleFactor, aHasAlpha);
return ioSurface.forget();
}
/* static */
already_AddRefed<MacIOSurface> MacIOSurface::LookupSurface(
IOSurfaceID aIOSurfaceID, double aContentsScaleFactor, bool aHasAlpha,
gfx::YUVColorSpace aColorSpace) {
if (aContentsScaleFactor <= 0) return nullptr;
CFTypeRefPtr<IOSurfaceRef> surfaceRef =
CFTypeRefPtr<IOSurfaceRef>::WrapUnderCreateRule(
::IOSurfaceLookup(aIOSurfaceID));
if (!surfaceRef) return nullptr;
RefPtr<MacIOSurface> ioSurface = new MacIOSurface(
std::move(surfaceRef), aContentsScaleFactor, aHasAlpha, aColorSpace);
return ioSurface.forget();
}
IOSurfaceID MacIOSurface::GetIOSurfaceID() const {
return ::IOSurfaceGetID(mIOSurfaceRef.get());
}
void* MacIOSurface::GetBaseAddress() const {
return ::IOSurfaceGetBaseAddress(mIOSurfaceRef.get());
}
void* MacIOSurface::GetBaseAddressOfPlane(size_t aPlaneIndex) const {
return ::IOSurfaceGetBaseAddressOfPlane(mIOSurfaceRef.get(), aPlaneIndex);
}
size_t MacIOSurface::GetWidth(size_t plane) const {
size_t intScaleFactor = ceil(mContentsScaleFactor);
return GetDevicePixelWidth(plane) / intScaleFactor;
}
size_t MacIOSurface::GetHeight(size_t plane) const {
size_t intScaleFactor = ceil(mContentsScaleFactor);
return GetDevicePixelHeight(plane) / intScaleFactor;
}
size_t MacIOSurface::GetPlaneCount() const {
return ::IOSurfaceGetPlaneCount(mIOSurfaceRef.get());
}
/*static*/
size_t MacIOSurface::GetMaxWidth() {
return ::IOSurfaceGetPropertyMaximum(kIOSurfaceWidth);
}
/*static*/
size_t MacIOSurface::GetMaxHeight() {
return ::IOSurfaceGetPropertyMaximum(kIOSurfaceHeight);
}
size_t MacIOSurface::GetDevicePixelWidth(size_t plane) const {
return ::IOSurfaceGetWidthOfPlane(mIOSurfaceRef.get(), plane);
}
size_t MacIOSurface::GetDevicePixelHeight(size_t plane) const {
return ::IOSurfaceGetHeightOfPlane(mIOSurfaceRef.get(), plane);
}
size_t MacIOSurface::GetBytesPerRow(size_t plane) const {
return ::IOSurfaceGetBytesPerRowOfPlane(mIOSurfaceRef.get(), plane);
}
OSType MacIOSurface::GetPixelFormat() const {
return ::IOSurfaceGetPixelFormat(mIOSurfaceRef.get());
}
void MacIOSurface::IncrementUseCount() {
::IOSurfaceIncrementUseCount(mIOSurfaceRef.get());
}
void MacIOSurface::DecrementUseCount() {
::IOSurfaceDecrementUseCount(mIOSurfaceRef.get());
}
void MacIOSurface::Lock(bool aReadOnly) {
MOZ_RELEASE_ASSERT(!mIsLocked, "double MacIOSurface lock");
::IOSurfaceLock(mIOSurfaceRef.get(), aReadOnly ? kIOSurfaceLockReadOnly : 0,
nullptr);
mIsLocked = true;
}
void MacIOSurface::Unlock(bool aReadOnly) {
MOZ_RELEASE_ASSERT(mIsLocked, "MacIOSurface unlock without being locked");
::IOSurfaceUnlock(mIOSurfaceRef.get(), aReadOnly ? kIOSurfaceLockReadOnly : 0,
nullptr);
mIsLocked = false;
}
using mozilla::gfx::IntSize;
using mozilla::gfx::SourceSurface;
using mozilla::gfx::SurfaceFormat;
static void MacIOSurfaceBufferDeallocator(void* aClosure) {
MOZ_ASSERT(aClosure);
delete[] static_cast<unsigned char*>(aClosure);
}
already_AddRefed<SourceSurface> MacIOSurface::GetAsSurface() {
Lock();
size_t bytesPerRow = GetBytesPerRow();
size_t ioWidth = GetDevicePixelWidth();
size_t ioHeight = GetDevicePixelHeight();
unsigned char* ioData = (unsigned char*)GetBaseAddress();
auto* dataCpy =
new unsigned char[bytesPerRow * ioHeight / sizeof(unsigned char)];
for (size_t i = 0; i < ioHeight; i++) {
memcpy(dataCpy + i * bytesPerRow, ioData + i * bytesPerRow, ioWidth * 4);
}
Unlock();
SurfaceFormat format = HasAlpha() ? mozilla::gfx::SurfaceFormat::B8G8R8A8
: mozilla::gfx::SurfaceFormat::B8G8R8X8;
RefPtr<mozilla::gfx::DataSourceSurface> surf =
mozilla::gfx::Factory::CreateWrappingDataSourceSurface(
dataCpy, bytesPerRow, IntSize(ioWidth, ioHeight), format,
&MacIOSurfaceBufferDeallocator, static_cast<void*>(dataCpy));
return surf.forget();
}
already_AddRefed<mozilla::gfx::DrawTarget> MacIOSurface::GetAsDrawTargetLocked(
mozilla::gfx::BackendType aBackendType) {
MOZ_RELEASE_ASSERT(
IsLocked(),
"Only call GetAsDrawTargetLocked while the surface is locked.");
size_t bytesPerRow = GetBytesPerRow();
size_t ioWidth = GetDevicePixelWidth();
size_t ioHeight = GetDevicePixelHeight();
unsigned char* ioData = (unsigned char*)GetBaseAddress();
SurfaceFormat format = GetFormat();
return mozilla::gfx::Factory::CreateDrawTargetForData(
aBackendType, ioData, IntSize(ioWidth, ioHeight), bytesPerRow, format);
}
SurfaceFormat MacIOSurface::GetFormat() const {
switch (GetPixelFormat()) {
case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange:
return SurfaceFormat::NV12;
case kCVPixelFormatType_422YpCbCr8:
return SurfaceFormat::YUV422;
case kCVPixelFormatType_32BGRA:
return HasAlpha() ? SurfaceFormat::B8G8R8A8 : SurfaceFormat::B8G8R8X8;
default:
return HasAlpha() ? SurfaceFormat::R8G8B8A8 : SurfaceFormat::R8G8B8X8;
}
}
SurfaceFormat MacIOSurface::GetReadFormat() const {
SurfaceFormat format = GetFormat();
if (format == SurfaceFormat::YUV422) {
return SurfaceFormat::R8G8B8X8;
}
return format;
}
CGLError MacIOSurface::CGLTexImageIOSurface2D(CGLContextObj ctx, GLenum target,
GLenum internalFormat,
GLsizei width, GLsizei height,
GLenum format, GLenum type,
GLuint plane) const {
return ::CGLTexImageIOSurface2D(ctx, target, internalFormat, width, height,
format, type, mIOSurfaceRef.get(), plane);
}
CGLError MacIOSurface::CGLTexImageIOSurface2D(
mozilla::gl::GLContext* aGL, CGLContextObj ctx, size_t plane,
mozilla::gfx::SurfaceFormat* aOutReadFormat) {
MOZ_ASSERT(plane >= 0);
bool isCompatibilityProfile = aGL->IsCompatibilityProfile();
OSType pixelFormat = GetPixelFormat();
GLenum internalFormat;
GLenum format;
GLenum type;
if (pixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange ||
pixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange) {
MOZ_ASSERT(GetPlaneCount() == 2);
MOZ_ASSERT(plane < 2);
// The LOCAL_GL_LUMINANCE and LOCAL_GL_LUMINANCE_ALPHA are the deprecated
// format. So, use LOCAL_GL_RED and LOCAL_GL_RB if we use core profile.
// https://www.khronos.org/opengl/wiki/Image_Format#Legacy_Image_Formats
if (plane == 0) {
internalFormat = format =
(isCompatibilityProfile) ? (LOCAL_GL_LUMINANCE) : (LOCAL_GL_RED);
} else {
internalFormat = format =
(isCompatibilityProfile) ? (LOCAL_GL_LUMINANCE_ALPHA) : (LOCAL_GL_RG);
}
type = LOCAL_GL_UNSIGNED_BYTE;
if (aOutReadFormat) {
*aOutReadFormat = mozilla::gfx::SurfaceFormat::NV12;
}
} else if (pixelFormat == kCVPixelFormatType_422YpCbCr8) {
MOZ_ASSERT(plane == 0);
// The YCBCR_422_APPLE ext is only available in compatibility profile. So,
// we should use RGB_422_APPLE for core profile. The difference between
// YCBCR_422_APPLE and RGB_422_APPLE is that the YCBCR_422_APPLE converts
// the YCbCr value to RGB with REC 601 conversion. But the RGB_422_APPLE
// doesn't contain color conversion. You should do the color conversion by
// yourself for RGB_422_APPLE.
//
// https://www.khronos.org/registry/OpenGL/extensions/APPLE/APPLE_ycbcr_422.txt
// https://www.khronos.org/registry/OpenGL/extensions/APPLE/APPLE_rgb_422.txt
if (isCompatibilityProfile) {
format = LOCAL_GL_YCBCR_422_APPLE;
if (aOutReadFormat) {
*aOutReadFormat = mozilla::gfx::SurfaceFormat::R8G8B8X8;
}
} else {
format = LOCAL_GL_RGB_422_APPLE;
if (aOutReadFormat) {
*aOutReadFormat = mozilla::gfx::SurfaceFormat::YUV422;
}
}
internalFormat = LOCAL_GL_RGB;
type = LOCAL_GL_UNSIGNED_SHORT_8_8_APPLE;
} else {
MOZ_ASSERT(plane == 0);
internalFormat = HasAlpha() ? LOCAL_GL_RGBA : LOCAL_GL_RGB;
format = LOCAL_GL_BGRA;
type = LOCAL_GL_UNSIGNED_INT_8_8_8_8_REV;
if (aOutReadFormat) {
*aOutReadFormat = HasAlpha() ? mozilla::gfx::SurfaceFormat::R8G8B8A8
: mozilla::gfx::SurfaceFormat::R8G8B8X8;
}
}
return CGLTexImageIOSurface2D(ctx, LOCAL_GL_TEXTURE_RECTANGLE_ARB,
internalFormat, GetDevicePixelWidth(plane),
GetDevicePixelHeight(plane), format, type,
plane);
}