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
|
// Copyright (c) 2012 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.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/win/windows_version.h"
#include "content/common/gpu/gpu_messages.h"
#include "content/public/common/content_switches.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gl/gl_bindings.h"
#include "ui/gl/gl_context.h"
#include "ui/gl/gl_implementation.h"
#include "ui/gl/gl_surface_egl.h"
namespace content {
namespace {
// We are backed by an Pbuffer offscreen surface through which ANGLE provides
// a handle to the corresponding render target texture through an extension.
class PbufferImageTransportSurface
: public gfx::GLSurfaceAdapter,
public ImageTransportSurface,
public base::SupportsWeakPtr<PbufferImageTransportSurface> {
public:
PbufferImageTransportSurface(GpuChannelManager* manager,
GpuCommandBufferStub* stub);
// gfx::GLSurface implementation
virtual bool Initialize() OVERRIDE;
virtual void Destroy() OVERRIDE;
virtual bool DeferDraws() OVERRIDE;
virtual bool IsOffscreen() OVERRIDE;
virtual bool SwapBuffers() OVERRIDE;
virtual bool PostSubBuffer(int x, int y, int width, int height) OVERRIDE;
virtual std::string GetExtensions() OVERRIDE;
virtual bool SetBackbufferAllocation(bool allocated) OVERRIDE;
virtual void SetFrontbufferAllocation(bool allocated) OVERRIDE;
protected:
// ImageTransportSurface implementation
virtual void OnBufferPresented(
const AcceleratedSurfaceMsg_BufferPresented_Params& params) OVERRIDE;
virtual void OnResizeViewACK() OVERRIDE;
virtual void OnResize(gfx::Size size, float scale_factor) OVERRIDE;
virtual void SetLatencyInfo(const ui::LatencyInfo&) OVERRIDE;
virtual gfx::Size GetSize() OVERRIDE;
private:
virtual ~PbufferImageTransportSurface();
void SendBuffersSwapped();
void DestroySurface();
// Tracks the current buffer allocation state.
bool backbuffer_suggested_allocation_;
bool frontbuffer_suggested_allocation_;
// Whether a SwapBuffers is pending.
bool is_swap_buffers_pending_;
// Whether we unscheduled command buffer because of pending SwapBuffers.
bool did_unschedule_;
// Size to resize to when the surface becomes visible.
gfx::Size visible_size_;
ui::LatencyInfo latency_info_;
scoped_ptr<ImageTransportHelper> helper_;
DISALLOW_COPY_AND_ASSIGN(PbufferImageTransportSurface);
};
PbufferImageTransportSurface::PbufferImageTransportSurface(
GpuChannelManager* manager,
GpuCommandBufferStub* stub)
: GLSurfaceAdapter(new gfx::PbufferGLSurfaceEGL(gfx::Size(1, 1))),
backbuffer_suggested_allocation_(true),
frontbuffer_suggested_allocation_(true),
is_swap_buffers_pending_(false),
did_unschedule_(false) {
helper_.reset(new ImageTransportHelper(this,
manager,
stub,
gfx::kNullPluginWindow));
}
PbufferImageTransportSurface::~PbufferImageTransportSurface() {
Destroy();
}
bool PbufferImageTransportSurface::Initialize() {
// Only support this path if the GL implementation is ANGLE.
// IO surfaces will not work with, for example, OSMesa software renderer
// GL contexts.
if (gfx::GetGLImplementation() != gfx::kGLImplementationEGLGLES2)
return false;
if (!helper_->Initialize())
return false;
return GLSurfaceAdapter::Initialize();
}
void PbufferImageTransportSurface::Destroy() {
helper_->Destroy();
GLSurfaceAdapter::Destroy();
}
bool PbufferImageTransportSurface::DeferDraws() {
// The command buffer hit a draw/clear command that could clobber the
// IOSurface in use by an earlier SwapBuffers. If a Swap is pending, abort
// processing of the command by returning true and unschedule until the Swap
// Ack arrives.
if (did_unschedule_)
return true;
if (is_swap_buffers_pending_) {
did_unschedule_ = true;
helper_->SetScheduled(false);
return true;
}
return false;
}
bool PbufferImageTransportSurface::IsOffscreen() {
return false;
}
bool PbufferImageTransportSurface::SwapBuffers() {
DCHECK(backbuffer_suggested_allocation_);
if (!frontbuffer_suggested_allocation_)
return true;
HANDLE surface_handle = GetShareHandle();
if (!surface_handle)
return false;
// Don't send the surface to the browser until we hit the fence that
// indicates the drawing to the surface has been completed.
// TODO(jbates) unscheduling should be deferred until draw commands from the
// next frame -- otherwise the GPU is potentially sitting idle.
helper_->DeferToFence(base::Bind(
&PbufferImageTransportSurface::SendBuffersSwapped,
AsWeakPtr()));
return true;
}
bool PbufferImageTransportSurface::PostSubBuffer(
int x, int y, int width, int height) {
NOTREACHED();
return false;
}
bool PbufferImageTransportSurface::SetBackbufferAllocation(bool allocation) {
if (backbuffer_suggested_allocation_ == allocation)
return true;
backbuffer_suggested_allocation_ = allocation;
DestroySurface();
if (backbuffer_suggested_allocation_ && visible_size_.GetArea() != 0)
return Resize(visible_size_);
else
return Resize(gfx::Size(1, 1));
}
void PbufferImageTransportSurface::SetFrontbufferAllocation(bool allocation) {
if (frontbuffer_suggested_allocation_ == allocation)
return;
frontbuffer_suggested_allocation_ = allocation;
// We recreate frontbuffer by recreating backbuffer and swapping.
// But we release frontbuffer by telling UI to release its handle on it.
if (!frontbuffer_suggested_allocation_)
helper_->Suspend();
}
void PbufferImageTransportSurface::DestroySurface() {
GpuHostMsg_AcceleratedSurfaceRelease_Params params;
helper_->SendAcceleratedSurfaceRelease(params);
}
std::string PbufferImageTransportSurface::GetExtensions() {
std::string extensions = gfx::GLSurface::GetExtensions();
extensions += extensions.empty() ? "" : " ";
extensions += "GL_CHROMIUM_front_buffer_cached";
return extensions;
}
void PbufferImageTransportSurface::SendBuffersSwapped() {
GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params params;
params.surface_handle = reinterpret_cast<int64>(GetShareHandle());
CHECK(params.surface_handle);
params.size = GetSize();
params.latency_info = latency_info_;
helper_->SendAcceleratedSurfaceBuffersSwapped(params);
DCHECK(!is_swap_buffers_pending_);
is_swap_buffers_pending_ = true;
}
void PbufferImageTransportSurface::OnBufferPresented(
const AcceleratedSurfaceMsg_BufferPresented_Params& params) {
if (!params.vsync_timebase.is_null() &&
params.vsync_interval != base::TimeDelta()) {
helper_->SendUpdateVSyncParameters(params.vsync_timebase,
params.vsync_interval);
}
is_swap_buffers_pending_ = false;
if (did_unschedule_) {
did_unschedule_ = false;
helper_->SetScheduled(true);
}
}
void PbufferImageTransportSurface::OnResizeViewACK() {
NOTREACHED();
}
void PbufferImageTransportSurface::OnResize(gfx::Size size,
float scale_factor) {
DCHECK(backbuffer_suggested_allocation_);
DCHECK(frontbuffer_suggested_allocation_);
Resize(size);
DestroySurface();
visible_size_ = size;
}
void PbufferImageTransportSurface::SetLatencyInfo(
const ui::LatencyInfo& latency_info) {
latency_info_ = latency_info;
}
gfx::Size PbufferImageTransportSurface::GetSize() {
return GLSurfaceAdapter::GetSize();
}
} // namespace anonymous
// static
scoped_refptr<gfx::GLSurface> ImageTransportSurface::CreateNativeSurface(
GpuChannelManager* manager,
GpuCommandBufferStub* stub,
const gfx::GLSurfaceHandle& handle) {
DCHECK(handle.handle);
DCHECK(handle.transport_type == gfx::NATIVE_DIRECT ||
handle.transport_type == gfx::NATIVE_TRANSPORT);
if (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2 &&
!CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableImageTransportSurface)) {
// This path handles two different cases.
//
// For post-Vista regular Windows, this surface will be used for
// renderer compositors.
//
// For Aura Windows, this will be the surface for the browser compositor
// (and the renderer compositors surface's will be
// TextureImageTransportSurface above).
const char* extensions = eglQueryString(
gfx::GLSurfaceEGL::GetHardwareDisplay(), EGL_EXTENSIONS);
if (extensions &&
strstr(extensions, "EGL_ANGLE_query_surface_pointer") &&
strstr(extensions, "EGL_ANGLE_surface_d3d_texture_2d_share_handle")) {
return scoped_refptr<gfx::GLSurface>(
new PbufferImageTransportSurface(manager, stub));
}
}
scoped_refptr<gfx::GLSurface> surface =
gfx::GLSurface::CreateViewGLSurface(handle.handle);
if (!surface)
return surface;
return scoped_refptr<gfx::GLSurface>(new PassThroughImageTransportSurface(
manager, stub, surface.get(), handle.is_transport()));
}
} // namespace content
|