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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
// 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/browser/renderer_host/image_transport_factory.h"
#include <algorithm>
#include <map>
#include "base/bind.h"
#include "base/command_line.h"
#include "base/memory/ref_counted.h"
#include "base/observer_list.h"
#include "content/browser/gpu/gpu_surface_tracker.h"
#include "content/browser/gpu/browser_gpu_channel_host_factory.h"
#include "content/browser/renderer_host/image_transport_client.h"
#include "content/common/gpu/client/command_buffer_proxy.h"
#include "content/common/gpu/client/gpu_channel_host.h"
#include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
#include "content/public/common/content_switches.h"
#include "ui/gfx/compositor/compositor.h"
#include "ui/gfx/compositor/compositor_setup.h"
#include "ui/gfx/gl/gl_context.h"
#include "ui/gfx/gl/gl_surface.h"
#include "ui/gfx/gl/scoped_make_current.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gfx/size.h"
#include "webkit/gpu/webgraphicscontext3d_in_process_impl.h"
using content::BrowserGpuChannelHostFactory;
namespace {
ImageTransportFactory* g_factory;
class DefaultTransportFactory
: public ui::DefaultContextFactory,
public ImageTransportFactory {
public:
DefaultTransportFactory() {
ui::DefaultContextFactory::Initialize();
}
virtual ui::ContextFactory* AsContextFactory() OVERRIDE {
return this;
}
virtual gfx::GLSurfaceHandle CreateSharedSurfaceHandle(
ui::Compositor* compositor) OVERRIDE {
return gfx::GLSurfaceHandle();
}
virtual void DestroySharedSurfaceHandle(
gfx::GLSurfaceHandle surface) OVERRIDE {
}
virtual scoped_refptr<ImageTransportClient> CreateTransportClient(
const gfx::Size& size,
uint64* transport_handle) OVERRIDE {
return NULL;
}
virtual gfx::ScopedMakeCurrent* GetScopedMakeCurrent() OVERRIDE {
return NULL;
}
// We don't generate lost context events, so we don't need to keep track of
// observers
virtual void AddObserver(ImageTransportFactoryObserver* observer) OVERRIDE {
}
virtual void RemoveObserver(
ImageTransportFactoryObserver* observer) OVERRIDE {
}
private:
DISALLOW_COPY_AND_ASSIGN(DefaultTransportFactory);
};
class TestTransportFactory : public DefaultTransportFactory {
public:
TestTransportFactory() {}
virtual gfx::GLSurfaceHandle CreateSharedSurfaceHandle(
ui::Compositor* compositor) OVERRIDE {
return gfx::GLSurfaceHandle(gfx::kNullPluginWindow, true);
}
virtual scoped_refptr<ImageTransportClient> CreateTransportClient(
const gfx::Size& size,
uint64* transport_handle) OVERRIDE {
#if defined(UI_COMPOSITOR_IMAGE_TRANSPORT)
scoped_refptr<ImageTransportClient> surface(
ImageTransportClient::Create(this, size));
if (!surface || !surface->Initialize(transport_handle)) {
LOG(ERROR) << "Failed to create ImageTransportClient";
return NULL;
}
return surface;
#else
return NULL;
#endif
}
private:
DISALLOW_COPY_AND_ASSIGN(TestTransportFactory);
};
#if defined(UI_COMPOSITOR_IMAGE_TRANSPORT)
class InProcessTransportFactory : public DefaultTransportFactory {
public:
InProcessTransportFactory() {
surface_ = gfx::GLSurface::CreateOffscreenGLSurface(false, gfx::Size(1, 1));
CHECK(surface_.get()) << "Unable to create compositor GL surface.";
context_ = gfx::GLContext::CreateGLContext(
NULL, surface_.get(), gfx::PreferIntegratedGpu);
CHECK(context_.get()) <<"Unable to create compositor GL context.";
set_share_group(context_->share_group());
}
virtual ~InProcessTransportFactory() {}
virtual gfx::ScopedMakeCurrent* GetScopedMakeCurrent() OVERRIDE {
return new gfx::ScopedMakeCurrent(context_.get(), surface_.get());
}
virtual gfx::GLSurfaceHandle CreateSharedSurfaceHandle(
ui::Compositor* compositor) OVERRIDE {
return gfx::GLSurfaceHandle(gfx::kNullPluginWindow, true);
}
virtual scoped_refptr<ImageTransportClient> CreateTransportClient(
const gfx::Size& size,
uint64* transport_handle) OVERRIDE {
scoped_refptr<ImageTransportClient> surface(
ImageTransportClient::Create(this, size));
if (!surface || !surface->Initialize(transport_handle)) {
LOG(ERROR) << "Failed to create ImageTransportClient";
return NULL;
}
return surface;
}
// We don't generate lost context events, so we don't need to keep track of
// observers
virtual void AddObserver(ImageTransportFactoryObserver* observer) OVERRIDE {
}
virtual void RemoveObserver(
ImageTransportFactoryObserver* observer) OVERRIDE {
}
private:
scoped_refptr<gfx::GLContext> context_;
scoped_refptr<gfx::GLSurface> surface_;
DISALLOW_COPY_AND_ASSIGN(InProcessTransportFactory);
};
#endif
class ImageTransportClientTexture : public ImageTransportClient {
public:
explicit ImageTransportClientTexture(const gfx::Size& size)
: ImageTransportClient(true, size) {
}
virtual bool Initialize(uint64* surface_id) OVERRIDE {
set_texture_id(*surface_id);
return true;
}
virtual ~ImageTransportClientTexture() {}
virtual void Update() OVERRIDE {}
virtual TransportDIB::Handle Handle() const OVERRIDE {
return TransportDIB::DefaultHandleValue();
}
private:
DISALLOW_COPY_AND_ASSIGN(ImageTransportClientTexture);
};
class GpuProcessTransportFactory;
class CompositorSwapClient
: public base::SupportsWeakPtr<CompositorSwapClient>,
public WebGraphicsContext3DSwapBuffersClient {
public:
CompositorSwapClient(ui::Compositor* compositor,
GpuProcessTransportFactory* factory)
: compositor_(compositor),
factory_(factory) {
}
~CompositorSwapClient() {
}
virtual void OnViewContextSwapBuffersPosted() OVERRIDE {
compositor_->OnSwapBuffersPosted();
}
virtual void OnViewContextSwapBuffersComplete() OVERRIDE {
compositor_->OnSwapBuffersComplete();
}
virtual void OnViewContextSwapBuffersAborted() OVERRIDE {
// Recreating contexts directly from here causes issues, so post a task
// instead.
// TODO(piman): Fix the underlying issues.
MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(&CompositorSwapClient::OnLostContext, this->AsWeakPtr()));
}
private:
void OnLostContext();
ui::Compositor* compositor_;
GpuProcessTransportFactory* factory_;
DISALLOW_COPY_AND_ASSIGN(CompositorSwapClient);
};
class GpuProcessTransportFactory : public ui::ContextFactory,
public ImageTransportFactory {
public:
GpuProcessTransportFactory() {}
virtual ~GpuProcessTransportFactory() {
DCHECK(per_compositor_data_.empty());
}
virtual WebKit::WebGraphicsContext3D* CreateContext(
ui::Compositor* compositor) OVERRIDE {
PerCompositorData* data = per_compositor_data_[compositor];
if (!data)
data = CreatePerCompositorData(compositor);
WebKit::WebGraphicsContext3D::Attributes attrs;
attrs.shareResources = true;
GpuChannelHostFactory* factory = BrowserGpuChannelHostFactory::instance();
scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context(
new WebGraphicsContext3DCommandBufferImpl(
data->surface_id, GURL(), factory, data->swap_client->AsWeakPtr()));
if (!context->Initialize(attrs))
return NULL;
return context.release();
}
virtual void RemoveCompositor(ui::Compositor* compositor) OVERRIDE {
PerCompositorDataMap::iterator it = per_compositor_data_.find(compositor);
if (it == per_compositor_data_.end())
return;
PerCompositorData* data = it->second;
DCHECK(data);
GpuSurfaceTracker::Get()->RemoveSurface(data->surface_id);
delete data;
per_compositor_data_.erase(it);
}
virtual ui::ContextFactory* AsContextFactory() OVERRIDE {
return this;
}
virtual gfx::GLSurfaceHandle CreateSharedSurfaceHandle(
ui::Compositor* compositor) OVERRIDE {
PerCompositorData* data = per_compositor_data_[compositor];
if (!data)
data = CreatePerCompositorData(compositor);
gfx::GLSurfaceHandle handle = gfx::GLSurfaceHandle(
gfx::kNullPluginWindow, true);
ContentGLContext* context = data->shared_context->content_gl_context();
handle.parent_gpu_process_id = context->GetGPUProcessID();
handle.parent_client_id = context->GetChannelID();
handle.parent_context_id = context->GetContextID();
handle.parent_texture_id[0] = data->shared_context->createTexture();
handle.parent_texture_id[1] = data->shared_context->createTexture();
// Finish is overkill, but flush semantics don't apply cross-channel.
// TODO(piman): Use a cross-channel synchronization mechanism instead.
data->shared_context->finish();
return handle;
}
virtual void DestroySharedSurfaceHandle(
gfx::GLSurfaceHandle surface) OVERRIDE {
for (PerCompositorDataMap::iterator it = per_compositor_data_.begin();
it != per_compositor_data_.end(); ++it) {
PerCompositorData* data = it->second;
DCHECK(data);
ContentGLContext* context = data->shared_context->content_gl_context();
int gpu_process_id = context->GetGPUProcessID();
uint32 client_id = context->GetChannelID();
uint32 context_id = context->GetContextID();
if (surface.parent_gpu_process_id == gpu_process_id &&
surface.parent_client_id == client_id &&
surface.parent_context_id == context_id) {
data->shared_context->deleteTexture(surface.parent_texture_id[0]);
data->shared_context->deleteTexture(surface.parent_texture_id[1]);
break;
}
}
}
virtual scoped_refptr<ImageTransportClient> CreateTransportClient(
const gfx::Size& size,
uint64* transport_handle) {
scoped_refptr<ImageTransportClientTexture> image(
new ImageTransportClientTexture(size));
image->Initialize(transport_handle);
return image;
}
virtual gfx::ScopedMakeCurrent* GetScopedMakeCurrent() { return NULL; }
virtual void AddObserver(ImageTransportFactoryObserver* observer) {
observer_list_.AddObserver(observer);
}
virtual void RemoveObserver(ImageTransportFactoryObserver* observer) {
observer_list_.RemoveObserver(observer);
}
void OnLostContext(ui::Compositor* compositor) {
LOG(ERROR) << "Lost UI compositor context.";
PerCompositorData* data = per_compositor_data_[compositor];
DCHECK(data);
// Note: this has the effect of recreating the swap_client, which means we
// won't get more reports of lost context from the same gpu process. It's a
// good thing.
CreateSharedContext(compositor);
FOR_EACH_OBSERVER(ImageTransportFactoryObserver,
observer_list_,
OnLostResources(compositor));
compositor->OnSwapBuffersAborted();
}
private:
struct PerCompositorData {
int surface_id;
scoped_ptr<CompositorSwapClient> swap_client;
scoped_ptr<WebGraphicsContext3DCommandBufferImpl> shared_context;
};
PerCompositorData* CreatePerCompositorData(ui::Compositor* compositor) {
DCHECK(!per_compositor_data_[compositor]);
gfx::AcceleratedWidget widget = compositor->widget();
GpuSurfaceTracker* tracker = GpuSurfaceTracker::Get();
PerCompositorData* data = new PerCompositorData;
data->surface_id = tracker->AddSurfaceForNativeWidget(widget);
tracker->SetSurfaceHandle(
data->surface_id,
gfx::GLSurfaceHandle(widget, false));
per_compositor_data_[compositor] = data;
CreateSharedContext(compositor);
return data;
}
void CreateSharedContext(ui::Compositor* compositor) {
PerCompositorData* data = per_compositor_data_[compositor];
DCHECK(data);
data->swap_client.reset(new CompositorSwapClient(compositor, this));
GpuChannelHostFactory* factory = BrowserGpuChannelHostFactory::instance();
WebKit::WebGraphicsContext3D::Attributes attrs;
attrs.shareResources = true;
data->shared_context.reset(new WebGraphicsContext3DCommandBufferImpl(
data->surface_id, GURL(), factory, data->swap_client->AsWeakPtr()));
if (!data->shared_context->Initialize(attrs)) {
// If we can't recreate contexts, we won't be able to show the UI. Better
// crash at this point.
LOG(FATAL) << "Failed to initialize compositor shared context.";
}
if (!data->shared_context->makeContextCurrent()) {
// If we can't recreate contexts, we won't be able to show the UI. Better
// crash at this point.
LOG(FATAL) << "Failed to make compositor shared context current.";
}
}
typedef std::map<ui::Compositor*, PerCompositorData*> PerCompositorDataMap;
PerCompositorDataMap per_compositor_data_;
ObserverList<ImageTransportFactoryObserver> observer_list_;
DISALLOW_COPY_AND_ASSIGN(GpuProcessTransportFactory);
};
void CompositorSwapClient::OnLostContext() {
factory_->OnLostContext(compositor_);
// Note: previous line destroyed this. Don't access members from now on.
}
} // anonymous namespace
// static
void ImageTransportFactory::Initialize() {
CommandLine* command_line = CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kTestCompositor)) {
ui::SetupTestCompositor();
}
if (ui::IsTestCompositorEnabled()) {
g_factory = new TestTransportFactory();
} else if (command_line->HasSwitch(switches::kUIUseGPUProcess)) {
g_factory = new GpuProcessTransportFactory();
} else {
#if defined(UI_COMPOSITOR_IMAGE_TRANSPORT)
g_factory = new InProcessTransportFactory();
#else
g_factory = new DefaultTransportFactory();
#endif
}
ui::ContextFactory::SetInstance(g_factory->AsContextFactory());
}
// static
void ImageTransportFactory::Terminate() {
ui::ContextFactory::SetInstance(NULL);
delete g_factory;
g_factory = NULL;
}
// static
ImageTransportFactory* ImageTransportFactory::GetInstance() {
return g_factory;
}
|