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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
// 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/compositor_impl_android.h"
#include <android/bitmap.h>
#include <android/native_window_jni.h>
#include <map>
#include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/single_thread_task_runner.h"
#include "base/synchronization/lock.h"
#include "base/threading/thread.h"
#include "cc/input/input_handler.h"
#include "cc/layers/layer.h"
#include "cc/output/compositor_frame.h"
#include "cc/output/context_provider.h"
#include "cc/output/output_surface.h"
#include "cc/resources/scoped_ui_resource.h"
#include "cc/resources/ui_resource_bitmap.h"
#include "cc/trees/layer_tree_host.h"
#include "content/browser/gpu/browser_gpu_channel_host_factory.h"
#include "content/browser/gpu/gpu_surface_tracker.h"
#include "content/common/gpu/client/command_buffer_proxy_impl.h"
#include "content/common/gpu/client/context_provider_command_buffer.h"
#include "content/common/gpu/client/gl_helper.h"
#include "content/common/gpu/client/gpu_channel_host.h"
#include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
#include "content/common/gpu/gpu_process_launch_causes.h"
#include "content/public/browser/android/compositor_client.h"
#include "content/public/common/content_switches.h"
#include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
#include "third_party/khronos/GLES2/gl2.h"
#include "third_party/khronos/GLES2/gl2ext.h"
#include "ui/base/android/window_android.h"
#include "ui/gfx/android/device_display_info.h"
#include "ui/gfx/android/java_bitmap.h"
#include "ui/gfx/frame_time.h"
#include "webkit/common/gpu/context_provider_in_process.h"
#include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
namespace gfx {
class JavaBitmap;
}
namespace {
// Used for drawing directly to the screen. Bypasses resizing and swaps.
class DirectOutputSurface : public cc::OutputSurface {
public:
DirectOutputSurface(
const scoped_refptr<cc::ContextProvider>& context_provider)
: cc::OutputSurface(context_provider) {
capabilities_.adjust_deadline_for_parent = false;
}
virtual void Reshape(gfx::Size size, float scale_factor) OVERRIDE {
surface_size_ = size;
}
virtual void SwapBuffers(cc::CompositorFrame*) OVERRIDE {
context_provider_->Context3d()->shallowFlushCHROMIUM();
}
};
// Used to override capabilities_.adjust_deadline_for_parent to false
class OutputSurfaceWithoutParent : public cc::OutputSurface {
public:
OutputSurfaceWithoutParent(
const scoped_refptr<
content::ContextProviderCommandBuffer>& context_provider)
: cc::OutputSurface(context_provider) {
capabilities_.adjust_deadline_for_parent = false;
}
virtual void SwapBuffers(cc::CompositorFrame* frame) OVERRIDE {
content::WebGraphicsContext3DCommandBufferImpl* command_buffer_context =
static_cast<content::WebGraphicsContext3DCommandBufferImpl*>(
context_provider_->Context3d());
content::CommandBufferProxyImpl* command_buffer_proxy =
command_buffer_context->GetCommandBufferProxy();
DCHECK(command_buffer_proxy);
command_buffer_proxy->SetLatencyInfo(frame->metadata.latency_info);
OutputSurface::SwapBuffers(frame);
}
};
static bool g_initialized = false;
} // anonymous namespace
namespace content {
typedef std::map<int, base::android::ScopedJavaGlobalRef<jobject> >
SurfaceMap;
static base::LazyInstance<SurfaceMap>
g_surface_map = LAZY_INSTANCE_INITIALIZER;
static base::LazyInstance<base::Lock> g_surface_map_lock;
// static
Compositor* Compositor::Create(CompositorClient* client,
gfx::NativeWindow root_window) {
return client ? new CompositorImpl(client, root_window) : NULL;
}
// static
void Compositor::Initialize() {
DCHECK(!CompositorImpl::IsInitialized());
g_initialized = true;
}
// static
bool CompositorImpl::IsInitialized() {
return g_initialized;
}
// static
jobject CompositorImpl::GetSurface(int surface_id) {
base::AutoLock lock(g_surface_map_lock.Get());
SurfaceMap* surfaces = g_surface_map.Pointer();
SurfaceMap::iterator it = surfaces->find(surface_id);
jobject jsurface = it == surfaces->end() ? NULL : it->second.obj();
LOG_IF(WARNING, !jsurface) << "No surface for surface id " << surface_id;
return jsurface;
}
CompositorImpl::CompositorImpl(CompositorClient* client,
gfx::NativeWindow root_window)
: root_layer_(cc::Layer::Create()),
has_transparent_background_(false),
window_(NULL),
surface_id_(0),
client_(client),
root_window_(root_window) {
DCHECK(client);
DCHECK(root_window);
ImageTransportFactoryAndroid::AddObserver(this);
root_window->AttachCompositor();
}
CompositorImpl::~CompositorImpl() {
root_window_->DetachCompositor();
ImageTransportFactoryAndroid::RemoveObserver(this);
// Clean-up any surface references.
SetSurface(NULL);
}
void CompositorImpl::Composite() {
if (host_)
host_->Composite(gfx::FrameTime::Now());
}
void CompositorImpl::SetRootLayer(scoped_refptr<cc::Layer> root_layer) {
root_layer_->RemoveAllChildren();
root_layer_->AddChild(root_layer);
}
void CompositorImpl::SetWindowSurface(ANativeWindow* window) {
GpuSurfaceTracker* tracker = GpuSurfaceTracker::Get();
if (window_) {
tracker->RemoveSurface(surface_id_);
ANativeWindow_release(window_);
window_ = NULL;
surface_id_ = 0;
SetVisible(false);
}
if (window) {
window_ = window;
ANativeWindow_acquire(window);
surface_id_ = tracker->AddSurfaceForNativeWidget(window);
tracker->SetSurfaceHandle(
surface_id_,
gfx::GLSurfaceHandle(gfx::kNullPluginWindow, gfx::NATIVE_DIRECT));
SetVisible(true);
}
}
void CompositorImpl::SetSurface(jobject surface) {
JNIEnv* env = base::android::AttachCurrentThread();
base::android::ScopedJavaLocalRef<jobject> j_surface(env, surface);
// First, cleanup any existing surface references.
if (surface_id_) {
DCHECK(g_surface_map.Get().find(surface_id_) !=
g_surface_map.Get().end());
base::AutoLock lock(g_surface_map_lock.Get());
g_surface_map.Get().erase(surface_id_);
}
SetWindowSurface(NULL);
// Now, set the new surface if we have one.
ANativeWindow* window = NULL;
if (surface)
window = ANativeWindow_fromSurface(env, surface);
if (window) {
SetWindowSurface(window);
ANativeWindow_release(window);
{
base::AutoLock lock(g_surface_map_lock.Get());
g_surface_map.Get().insert(std::make_pair(surface_id_, j_surface));
}
}
}
void CompositorImpl::SetVisible(bool visible) {
if (!visible) {
ui_resource_map_.clear();
host_.reset();
client_->UIResourcesAreInvalid();
} else if (!host_) {
cc::LayerTreeSettings settings;
settings.refresh_rate = 60.0;
settings.impl_side_painting = false;
settings.allow_antialiasing = false;
settings.calculate_top_controls_position = false;
settings.top_controls_height = 0.f;
settings.use_memory_management = false;
settings.highp_threshold_min = 2048;
host_ = cc::LayerTreeHost::CreateSingleThreaded(this, this, NULL, settings);
host_->SetRootLayer(root_layer_);
host_->SetVisible(true);
host_->SetLayerTreeHostClientReady();
host_->SetViewportSize(size_);
host_->set_has_transparent_background(has_transparent_background_);
// Need to recreate the UI resources because a new LayerTreeHost has been
// created.
client_->DidLoseUIResources();
}
}
void CompositorImpl::setDeviceScaleFactor(float factor) {
if (host_)
host_->SetDeviceScaleFactor(factor);
}
void CompositorImpl::SetWindowBounds(const gfx::Size& size) {
if (size_ == size)
return;
size_ = size;
if (host_)
host_->SetViewportSize(size);
root_layer_->SetBounds(size);
}
bool CompositorImpl::CompositeAndReadback(void *pixels, const gfx::Rect& rect) {
if (host_)
return host_->CompositeAndReadback(pixels, rect);
else
return false;
}
cc::UIResourceId CompositorImpl::GenerateUIResource(
const cc::UIResourceBitmap& bitmap) {
if (!host_)
return 0;
scoped_ptr<cc::ScopedUIResource> ui_resource =
cc::ScopedUIResource::Create(host_.get(), bitmap);
cc::UIResourceId id = ui_resource->id();
ui_resource_map_.set(id, ui_resource.Pass());
return id;
}
void CompositorImpl::DeleteUIResource(cc::UIResourceId resource_id) {
UIResourceMap::iterator it = ui_resource_map_.find(resource_id);
if (it != ui_resource_map_.end())
ui_resource_map_.erase(it);
}
blink::WebGLId CompositorImpl::GenerateTexture(gfx::JavaBitmap& bitmap) {
unsigned int texture_id = BuildBasicTexture();
blink::WebGraphicsContext3D* context =
ImageTransportFactoryAndroid::GetInstance()->GetContext3D();
if (texture_id == 0 || context->isContextLost() ||
!context->makeContextCurrent())
return 0;
blink::WebGLId format = GetGLFormatForBitmap(bitmap);
blink::WebGLId type = GetGLTypeForBitmap(bitmap);
context->texImage2D(GL_TEXTURE_2D,
0,
format,
bitmap.size().width(),
bitmap.size().height(),
0,
format,
type,
bitmap.pixels());
context->shallowFlushCHROMIUM();
return texture_id;
}
blink::WebGLId CompositorImpl::GenerateCompressedTexture(gfx::Size& size,
int data_size,
void* data) {
unsigned int texture_id = BuildBasicTexture();
blink::WebGraphicsContext3D* context =
ImageTransportFactoryAndroid::GetInstance()->GetContext3D();
if (texture_id == 0 || context->isContextLost() ||
!context->makeContextCurrent())
return 0;
context->compressedTexImage2D(GL_TEXTURE_2D,
0,
GL_ETC1_RGB8_OES,
size.width(),
size.height(),
0,
data_size,
data);
context->shallowFlushCHROMIUM();
return texture_id;
}
void CompositorImpl::DeleteTexture(blink::WebGLId texture_id) {
blink::WebGraphicsContext3D* context =
ImageTransportFactoryAndroid::GetInstance()->GetContext3D();
if (context->isContextLost() || !context->makeContextCurrent())
return;
context->deleteTexture(texture_id);
context->shallowFlushCHROMIUM();
}
bool CompositorImpl::CopyTextureToBitmap(blink::WebGLId texture_id,
gfx::JavaBitmap& bitmap) {
return CopyTextureToBitmap(texture_id, gfx::Rect(bitmap.size()), bitmap);
}
bool CompositorImpl::CopyTextureToBitmap(blink::WebGLId texture_id,
const gfx::Rect& sub_rect,
gfx::JavaBitmap& bitmap) {
// The sub_rect should match the bitmap size.
DCHECK(bitmap.size() == sub_rect.size());
if (bitmap.size() != sub_rect.size() || texture_id == 0) return false;
GLHelper* helper = ImageTransportFactoryAndroid::GetInstance()->GetGLHelper();
helper->ReadbackTextureSync(texture_id,
sub_rect,
static_cast<unsigned char*> (bitmap.pixels()));
return true;
}
static scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
CreateGpuProcessViewContext(
const blink::WebGraphicsContext3D::Attributes attributes,
int surface_id) {
BrowserGpuChannelHostFactory* factory =
BrowserGpuChannelHostFactory::instance();
CauseForGpuLaunch cause =
CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE;
scoped_refptr<GpuChannelHost> gpu_channel_host(
factory->EstablishGpuChannelSync(cause));
if (!gpu_channel_host)
return scoped_ptr<WebGraphicsContext3DCommandBufferImpl>();
GURL url("chrome://gpu/Compositor::createContext3D");
static const size_t kBytesPerPixel = 4;
gfx::DeviceDisplayInfo display_info;
size_t full_screen_texture_size_in_bytes =
display_info.GetDisplayHeight() *
display_info.GetDisplayWidth() *
kBytesPerPixel;
WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits limits;
limits.command_buffer_size = 64 * 1024;
limits.start_transfer_buffer_size = 64 * 1024;
limits.min_transfer_buffer_size = 64 * 1024;
limits.max_transfer_buffer_size = std::min(
3 * full_screen_texture_size_in_bytes, kDefaultMaxTransferBufferSize);
limits.mapped_memory_reclaim_limit = 2 * 1024 * 1024;
bool use_echo_for_swap_ack = true;
return make_scoped_ptr(
new WebGraphicsContext3DCommandBufferImpl(surface_id,
url,
gpu_channel_host.get(),
use_echo_for_swap_ack,
attributes,
false,
limits));
}
scoped_ptr<cc::OutputSurface> CompositorImpl::CreateOutputSurface(
bool fallback) {
blink::WebGraphicsContext3D::Attributes attrs;
attrs.shareResources = true;
attrs.noAutomaticFlushes = true;
DCHECK(window_);
DCHECK(surface_id_);
scoped_refptr<ContextProviderCommandBuffer> context_provider =
ContextProviderCommandBuffer::Create(
CreateGpuProcessViewContext(attrs, surface_id_), "BrowserCompositor");
if (!context_provider.get()) {
LOG(ERROR) << "Failed to create 3D context for compositor.";
return scoped_ptr<cc::OutputSurface>();
}
return scoped_ptr<cc::OutputSurface>(
new OutputSurfaceWithoutParent(context_provider));
}
void CompositorImpl::OnLostResources() {
client_->DidLoseResources();
}
scoped_refptr<cc::ContextProvider> CompositorImpl::OffscreenContextProvider() {
// There is no support for offscreen contexts, or compositor filters that
// would require them in this compositor instance. If they are needed,
// then implement a context provider that provides contexts from
// ImageTransportSurfaceAndroid.
return NULL;
}
void CompositorImpl::DidCompleteSwapBuffers() {
client_->OnSwapBuffersCompleted();
}
void CompositorImpl::ScheduleComposite() {
client_->ScheduleComposite();
}
void CompositorImpl::ScheduleAnimation() {
ScheduleComposite();
}
void CompositorImpl::DidPostSwapBuffers() {
TRACE_EVENT0("compositor", "CompositorImpl::DidPostSwapBuffers");
client_->OnSwapBuffersPosted();
}
void CompositorImpl::DidAbortSwapBuffers() {
TRACE_EVENT0("compositor", "CompositorImpl::DidAbortSwapBuffers");
client_->OnSwapBuffersCompleted();
}
blink::WebGLId CompositorImpl::BuildBasicTexture() {
blink::WebGraphicsContext3D* context =
ImageTransportFactoryAndroid::GetInstance()->GetContext3D();
if (context->isContextLost() || !context->makeContextCurrent())
return 0;
blink::WebGLId texture_id = context->createTexture();
context->bindTexture(GL_TEXTURE_2D, texture_id);
context->texParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
context->texParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
context->texParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
context->texParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
return texture_id;
}
blink::WGC3Denum CompositorImpl::GetGLFormatForBitmap(
gfx::JavaBitmap& bitmap) {
switch (bitmap.format()) {
case ANDROID_BITMAP_FORMAT_A_8:
return GL_ALPHA;
break;
case ANDROID_BITMAP_FORMAT_RGBA_4444:
return GL_RGBA;
break;
case ANDROID_BITMAP_FORMAT_RGBA_8888:
return GL_RGBA;
break;
case ANDROID_BITMAP_FORMAT_RGB_565:
default:
return GL_RGB;
}
}
blink::WGC3Denum CompositorImpl::GetGLTypeForBitmap(gfx::JavaBitmap& bitmap) {
switch (bitmap.format()) {
case ANDROID_BITMAP_FORMAT_A_8:
return GL_UNSIGNED_BYTE;
break;
case ANDROID_BITMAP_FORMAT_RGBA_4444:
return GL_UNSIGNED_SHORT_4_4_4_4;
break;
case ANDROID_BITMAP_FORMAT_RGBA_8888:
return GL_UNSIGNED_BYTE;
break;
case ANDROID_BITMAP_FORMAT_RGB_565:
default:
return GL_UNSIGNED_SHORT_5_6_5;
}
}
void CompositorImpl::DidCommit() {
root_window_->OnCompositingDidCommit();
}
} // namespace content
|