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
|
// Copyright 2014 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 "content/common/gpu/image_transport_surface_iosurface_mac.h"
#include "content/common/gpu/gpu_messages.h"
#include "content/common/gpu/surface_handle_types_mac.h"
namespace content {
namespace {
// IOSurface dimensions will be rounded up to a multiple of this value in order
// to reduce memory thrashing during resize. This must be a power of 2.
const uint32 kIOSurfaceDimensionRoundup = 64;
int RoundUpSurfaceDimension(int number) {
DCHECK(number >= 0);
// Cast into unsigned space for portable bitwise ops.
uint32 unsigned_number = static_cast<uint32>(number);
uint32 roundup_sub_1 = kIOSurfaceDimensionRoundup - 1;
unsigned_number = (unsigned_number + roundup_sub_1) & ~roundup_sub_1;
return static_cast<int>(unsigned_number);
}
void AddBooleanValue(CFMutableDictionaryRef dictionary,
const CFStringRef key,
bool value) {
CFDictionaryAddValue(dictionary, key,
(value ? kCFBooleanTrue : kCFBooleanFalse));
}
void AddIntegerValue(CFMutableDictionaryRef dictionary,
const CFStringRef key,
int32 value) {
base::ScopedCFTypeRef<CFNumberRef> number(
CFNumberCreate(NULL, kCFNumberSInt32Type, &value));
CFDictionaryAddValue(dictionary, key, number.get());
}
} // namespace
IOSurfaceStorageProvider::IOSurfaceStorageProvider(
ImageTransportSurfaceFBO* transport_surface)
: transport_surface_(transport_surface) {}
IOSurfaceStorageProvider::~IOSurfaceStorageProvider() {
DCHECK(!io_surface_);
}
gfx::Size IOSurfaceStorageProvider::GetRoundedSize(gfx::Size size) {
return gfx::Size(RoundUpSurfaceDimension(size.width()),
RoundUpSurfaceDimension(size.height()));
}
bool IOSurfaceStorageProvider::AllocateColorBufferStorage(
CGLContextObj context, GLuint texture,
gfx::Size pixel_size, float scale_factor) {
// Allocate a new IOSurface, which is the GPU resource that can be
// shared across processes.
base::ScopedCFTypeRef<CFMutableDictionaryRef> properties;
properties.reset(CFDictionaryCreateMutable(kCFAllocatorDefault,
0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks));
AddIntegerValue(properties,
kIOSurfaceWidth,
pixel_size.width());
AddIntegerValue(properties,
kIOSurfaceHeight,
pixel_size.height());
AddIntegerValue(properties,
kIOSurfaceBytesPerElement, 4);
AddBooleanValue(properties,
kIOSurfaceIsGlobal, true);
// I believe we should be able to unreference the IOSurfaces without
// synchronizing with the browser process because they are
// ultimately reference counted by the operating system.
io_surface_.reset(IOSurfaceCreate(properties));
io_surface_id_ = IOSurfaceGetID(io_surface_);
// Don't think we need to identify a plane.
GLuint plane = 0;
CGLError cglerror = CGLTexImageIOSurface2D(
context,
GL_TEXTURE_RECTANGLE_ARB,
GL_RGBA,
pixel_size.width(),
pixel_size.height(),
GL_BGRA,
GL_UNSIGNED_INT_8_8_8_8_REV,
io_surface_.get(),
plane);
if (cglerror != kCGLNoError) {
DLOG(ERROR) << "CGLTexImageIOSurface2D failed with CGL error: " << cglerror;
return false;
}
glFlush();
return true;
}
void IOSurfaceStorageProvider::FreeColorBufferStorage() {
io_surface_.reset();
io_surface_id_ = 0;
}
void IOSurfaceStorageProvider::SwapBuffers(
const gfx::Size& size, float scale_factor) {
// The browser compositor will throttle itself, so we are free to unblock the
// context immediately. Make sure that the browser is doing its throttling
// appropriately by ensuring that the previous swap was acknowledged before
// we get another swap.
DCHECK(pending_swapped_surfaces_.empty());
pending_swapped_surfaces_.push_back(io_surface_);
transport_surface_->SendSwapBuffers(
SurfaceHandleFromIOSurfaceID(io_surface_id_), size, scale_factor);
}
void IOSurfaceStorageProvider::WillWriteToBackbuffer() {
}
void IOSurfaceStorageProvider::DiscardBackbuffer() {
}
void IOSurfaceStorageProvider::SwapBuffersAckedByBrowser() {
DCHECK(!pending_swapped_surfaces_.empty());
pending_swapped_surfaces_.pop_front();
}
} // namespace content
|