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
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/mac/io_surface.h"
#include <stddef.h>
#include <stdint.h>
#include "base/command_line.h"
#include "base/logging.h"
#include "base/mac/mac_util.h"
#include "base/mac/mach_logging.h"
#include "base/macros.h"
#include "base/metrics/histogram_macros.h"
#include "base/trace_event/trace_event.h"
#include "ui/gfx/buffer_format_util.h"
#include "ui/gfx/icc_profile.h"
namespace gfx {
namespace {
void AddIntegerValue(CFMutableDictionaryRef dictionary,
const CFStringRef key,
int32_t value) {
base::ScopedCFTypeRef<CFNumberRef> number(
CFNumberCreate(NULL, kCFNumberSInt32Type, &value));
CFDictionaryAddValue(dictionary, key, number.get());
}
int32_t BytesPerElement(gfx::BufferFormat format, int plane) {
switch (format) {
case gfx::BufferFormat::R_8:
DCHECK_EQ(plane, 0);
return 1;
case gfx::BufferFormat::BGRA_8888:
case gfx::BufferFormat::BGRX_8888:
case gfx::BufferFormat::RGBA_8888:
case gfx::BufferFormat::BGRX_1010102:
DCHECK_EQ(plane, 0);
return 4;
case gfx::BufferFormat::RGBA_F16:
DCHECK_EQ(plane, 0);
return 8;
case gfx::BufferFormat::YUV_420_BIPLANAR:
static int32_t bytes_per_element[] = {1, 2};
DCHECK_LT(static_cast<size_t>(plane), arraysize(bytes_per_element));
return bytes_per_element[plane];
case gfx::BufferFormat::R_16:
case gfx::BufferFormat::RG_88:
case gfx::BufferFormat::UYVY_422:
DCHECK_EQ(plane, 0);
return 2;
case gfx::BufferFormat::ATC:
case gfx::BufferFormat::ATCIA:
case gfx::BufferFormat::DXT1:
case gfx::BufferFormat::DXT5:
case gfx::BufferFormat::ETC1:
case gfx::BufferFormat::BGR_565:
case gfx::BufferFormat::RGBA_4444:
case gfx::BufferFormat::RGBX_8888:
case gfx::BufferFormat::RGBX_1010102:
case gfx::BufferFormat::YVU_420:
NOTREACHED();
return 0;
}
NOTREACHED();
return 0;
}
int32_t PixelFormat(gfx::BufferFormat format) {
switch (format) {
case gfx::BufferFormat::R_8:
return 'L008';
case gfx::BufferFormat::BGRX_1010102:
return 'l10r'; // little-endian ARGB2101010 full-range ARGB
case gfx::BufferFormat::BGRA_8888:
case gfx::BufferFormat::BGRX_8888:
case gfx::BufferFormat::RGBA_8888:
return 'BGRA';
case gfx::BufferFormat::RGBA_F16:
return 'RGhA';
case gfx::BufferFormat::YUV_420_BIPLANAR:
return '420v';
case gfx::BufferFormat::UYVY_422:
return '2vuy';
case gfx::BufferFormat::R_16:
case gfx::BufferFormat::RG_88:
case gfx::BufferFormat::ATC:
case gfx::BufferFormat::ATCIA:
case gfx::BufferFormat::DXT1:
case gfx::BufferFormat::DXT5:
case gfx::BufferFormat::ETC1:
case gfx::BufferFormat::BGR_565:
case gfx::BufferFormat::RGBA_4444:
case gfx::BufferFormat::RGBX_8888:
case gfx::BufferFormat::RGBX_1010102:
// Technically RGBX_1010102 should be accepted as 'R10k', but then it won't
// be supported by CGLTexImageIOSurface2D(), so it's best to reject it here.
case gfx::BufferFormat::YVU_420:
NOTREACHED();
return 0;
}
NOTREACHED();
return 0;
}
} // namespace
namespace internal {
// static
mach_port_t IOSurfaceMachPortTraits::Retain(mach_port_t port) {
kern_return_t kr =
mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_SEND, 1);
MACH_LOG_IF(ERROR, kr != KERN_SUCCESS, kr)
<< "IOSurfaceMachPortTraits::Retain mach_port_mod_refs";
return port;
}
// static
void IOSurfaceMachPortTraits::Release(mach_port_t port) {
kern_return_t kr =
mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_SEND, -1);
MACH_LOG_IF(ERROR, kr != KERN_SUCCESS, kr)
<< "IOSurfaceMachPortTraits::Release mach_port_mod_refs";
}
} // namespace internal
IOSurfaceRef CreateIOSurface(const gfx::Size& size,
gfx::BufferFormat format,
bool should_clear) {
TRACE_EVENT0("ui", "CreateIOSurface");
base::TimeTicks start_time = base::TimeTicks::Now();
size_t num_planes = gfx::NumberOfPlanesForBufferFormat(format);
base::ScopedCFTypeRef<CFMutableArrayRef> planes(CFArrayCreateMutable(
kCFAllocatorDefault, num_planes, &kCFTypeArrayCallBacks));
// Don't specify plane information unless there are indeed multiple planes
// because DisplayLink drivers do not support this.
// http://crbug.com/527556
if (num_planes > 1) {
for (size_t plane = 0; plane < num_planes; ++plane) {
size_t factor = gfx::SubsamplingFactorForBufferFormat(format, plane);
base::ScopedCFTypeRef<CFMutableDictionaryRef> plane_info(
CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks));
AddIntegerValue(plane_info, kIOSurfacePlaneWidth, size.width() / factor);
AddIntegerValue(plane_info, kIOSurfacePlaneHeight,
size.height() / factor);
AddIntegerValue(plane_info, kIOSurfacePlaneBytesPerElement,
BytesPerElement(format, plane));
CFArrayAppendValue(planes, plane_info);
}
}
base::ScopedCFTypeRef<CFMutableDictionaryRef> properties(
CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks));
AddIntegerValue(properties, kIOSurfaceWidth, size.width());
AddIntegerValue(properties, kIOSurfaceHeight, size.height());
AddIntegerValue(properties, kIOSurfacePixelFormat, PixelFormat(format));
if (num_planes > 1) {
CFDictionaryAddValue(properties, kIOSurfacePlaneInfo, planes);
} else {
AddIntegerValue(properties, kIOSurfaceBytesPerElement,
BytesPerElement(format, 0));
}
IOSurfaceRef surface = IOSurfaceCreate(properties);
if (!surface) {
LOG(ERROR) << "Failed to allocate IOSurface of size " << size.ToString()
<< ".";
return nullptr;
}
// For unknown reasons, triggering this lock on OS X 10.9, on certain GPUs,
// causes PDFs to render incorrectly. Hopefully this check can be removed once
// pdfium switches to a Skia backend on Mac.
// https://crbug.com/594343.
// IOSurface clearing causes significant performance regression on about half
// of all devices running Yosemite. https://crbug.com/606850#c22.
if (base::mac::IsOS10_9() || base::mac::IsOS10_10())
should_clear = false;
if (should_clear) {
// Zero-initialize the IOSurface. Calling IOSurfaceLock/IOSurfaceUnlock
// appears to be sufficient. https://crbug.com/584760#c17
IOReturn r = IOSurfaceLock(surface, 0, nullptr);
DCHECK_EQ(kIOReturnSuccess, r);
r = IOSurfaceUnlock(surface, 0, nullptr);
DCHECK_EQ(kIOReturnSuccess, r);
}
// Ensure that all IOSurfaces start as sRGB.
CGColorSpaceRef color_space = base::mac::GetSRGBColorSpace();
base::ScopedCFTypeRef<CFDataRef> color_space_icc(
CGColorSpaceCopyICCProfile(color_space));
IOSurfaceSetValue(surface, CFSTR("IOSurfaceColorSpace"), color_space_icc);
UMA_HISTOGRAM_TIMES("GPU.IOSurface.CreateTime",
base::TimeTicks::Now() - start_time);
return surface;
}
void IOSurfaceSetColorSpace(IOSurfaceRef io_surface,
const ColorSpace& color_space) {
// Retrieve the ICC profile data that created this profile, if it exists.
ICCProfile icc_profile = ICCProfile::FromCacheMac(color_space);
// If that fails, generate parametric data.
if (!icc_profile.IsValid()) {
icc_profile =
ICCProfile::FromParametricColorSpace(color_space.GetAsFullRangeRGB());
}
if (!icc_profile.IsValid()) {
if (base::mac::IsAtLeastOS10_12()) {
static const ColorSpace kBt2020(ColorSpace::PrimaryID::BT2020,
ColorSpace::TransferID::SMPTEST2084,
ColorSpace::MatrixID::BT2020_NCL,
ColorSpace::RangeID::LIMITED);
if (color_space == kBt2020) {
base::ScopedCFTypeRef<CGColorSpaceRef> cg_color_space(
CGColorSpaceCreateWithName(kCGColorSpaceITUR_2020));
DCHECK(cg_color_space);
base::ScopedCFTypeRef<CFDataRef> cf_data_icc_profile(
CGColorSpaceCopyICCData(cg_color_space));
DCHECK(cf_data_icc_profile);
IOSurfaceSetValue(io_surface, CFSTR("IOSurfaceColorSpace"),
cf_data_icc_profile);
return;
}
}
DLOG(ERROR) << "Failed to set color space for IOSurface: no ICC profile: "
<< color_space.ToString();
return;
}
std::vector<char> icc_profile_data = icc_profile.GetData();
// Package it as a CFDataRef and send it to the IOSurface.
base::ScopedCFTypeRef<CFDataRef> cf_data_icc_profile(CFDataCreate(
nullptr, reinterpret_cast<const UInt8*>(icc_profile_data.data()),
icc_profile_data.size()));
IOSurfaceSetValue(io_surface, CFSTR("IOSurfaceColorSpace"),
cf_data_icc_profile);
}
} // namespace gfx
|