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
|
// 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 "base/bind.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "content/browser/gpu/browser_gpu_channel_host_factory.h"
#include "content/browser/gpu/gpu_surface_tracker.h"
#include "content/browser/renderer_host/image_transport_factory_android.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/common/content_switches.h"
#include "third_party/khronos/GLES2/gl2.h"
#include "third_party/khronos/GLES2/gl2ext.h"
#include "third_party/WebKit/Source/Platform/chromium/public/Platform.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebCompositorSupport.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebCompositorOutputSurface.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3D.h"
#include "ui/gfx/android/java_bitmap.h"
namespace gfx {
class JavaBitmap;
}
namespace {
static bool g_initialized = false;
// Adapts a pure WebGraphicsContext3D into a WebCompositorOutputSurface.
class WebGraphicsContextToOutputSurfaceAdapter :
public WebKit::WebCompositorOutputSurface {
public:
explicit WebGraphicsContextToOutputSurfaceAdapter(
WebKit::WebGraphicsContext3D* context)
: m_context3D(context)
, m_client(0)
{
}
virtual bool bindToClient(
WebKit::WebCompositorOutputSurfaceClient* client) OVERRIDE
{
DCHECK(client);
if (!m_context3D->makeContextCurrent())
return false;
m_client = client;
return true;
}
virtual const Capabilities& capabilities() const OVERRIDE
{
return m_capabilities;
}
virtual WebKit::WebGraphicsContext3D* context3D() const OVERRIDE
{
return m_context3D.get();
}
virtual void sendFrameToParentCompositor(
const WebKit::WebCompositorFrame&) OVERRIDE
{
}
private:
scoped_ptr<WebKit::WebGraphicsContext3D> m_context3D;
Capabilities m_capabilities;
WebKit::WebCompositorOutputSurfaceClient* m_client;
};
} // anonymous namespace
namespace content {
// static
Compositor* Compositor::Create(Client* client) {
return client ? new CompositorImpl(client) : NULL;
}
// static
void Compositor::Initialize() {
g_initialized = true;
// Android WebView runs in single process, and depends on the renderer to
// perform WebKit::Platform initialization for the entire process. The
// renderer, however, does that lazily which in practice means it waits
// until the first page load request.
// The WebView-specific rendering code isn't ready yet so we only want to
// trick the rest of it into thinking the Compositor is initialized, which
// keeps us from crashing.
// See BUG 152904.
if (WebKit::Platform::current() == NULL) {
LOG(WARNING) << "CompositorImpl(Android)::Initialize(): WebKit::Platform "
<< "is not initialized, COMPOSITOR IS NOT INITIALIZED "
<< "(this is OK and expected if you're running Android"
<< "WebView tests).";
// We only ever want to run this hack in single process mode.
CHECK(CommandLine::ForCurrentProcess()->HasSwitch(
switches::kSingleProcess));
return;
}
WebKit::Platform::current()->compositorSupport()->initialize(NULL);
}
// static
bool CompositorImpl::IsInitialized() {
return g_initialized;
}
CompositorImpl::CompositorImpl(Compositor::Client* client)
: window_(NULL),
surface_id_(0),
client_(client) {
DCHECK(client);
root_layer_.reset(
WebKit::Platform::current()->compositorSupport()->createLayer());
}
CompositorImpl::~CompositorImpl() {
}
void CompositorImpl::Composite() {
if (host_.get())
host_->composite();
}
void CompositorImpl::SetRootLayer(WebKit::WebLayer* 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;
host_.reset();
}
if (window) {
window_ = window;
ANativeWindow_acquire(window);
surface_id_ = tracker->AddSurfaceForNativeWidget(window);
tracker->SetSurfaceHandle(
surface_id_,
gfx::GLSurfaceHandle(gfx::kDummyPluginWindow, false));
DCHECK(!host_.get());
WebKit::WebLayerTreeView::Settings settings;
settings.refreshRate = 60.0;
WebKit::WebCompositorSupport* compositor_support =
WebKit::Platform::current()->compositorSupport();
host_.reset(
compositor_support->createLayerTreeView(this, *root_layer_, settings));
host_->setVisible(true);
host_->setSurfaceReady();
host_->setViewportSize(size_);
}
}
void CompositorImpl::SetWindowBounds(const gfx::Size& size) {
if (size_ == size)
return;
size_ = size;
host_->setViewportSize(size);
root_layer_->setBounds(size);
}
bool CompositorImpl::CompositeAndReadback(void *pixels, const gfx::Rect& rect) {
if (host_.get())
return host_->compositeAndReadback(pixels, rect);
else
return false;
}
WebKit::WebGLId CompositorImpl::GenerateTexture(gfx::JavaBitmap& bitmap) {
unsigned int texture_id = BuildBasicTexture();
WebKit::WebGraphicsContext3D* context =
ImageTransportFactoryAndroid::GetInstance()->GetContext3D();
if (texture_id == 0 || context->isContextLost())
return 0;
WebKit::WebGLId format = GetGLFormatForBitmap(bitmap);
WebKit::WebGLId type = GetGLTypeForBitmap(bitmap);
context->texImage2D(GL_TEXTURE_2D,
0,
format,
bitmap.size().width(),
bitmap.size().height(),
0,
format,
type,
bitmap.pixels());
DCHECK(context->getError() == GL_NO_ERROR);
return texture_id;
}
WebKit::WebGLId CompositorImpl::GenerateCompressedTexture(gfx::Size& size,
int data_size,
void* data) {
unsigned int texture_id = BuildBasicTexture();
WebKit::WebGraphicsContext3D* context =
ImageTransportFactoryAndroid::GetInstance()->GetContext3D();
if (texture_id == 0 || context->isContextLost())
return 0;
context->compressedTexImage2D(GL_TEXTURE_2D,
0,
GL_ETC1_RGB8_OES,
size.width(),
size.height(),
0,
data_size,
data);
DCHECK(context->getError() == GL_NO_ERROR);
return texture_id;
}
void CompositorImpl::DeleteTexture(WebKit::WebGLId texture_id) {
WebKit::WebGraphicsContext3D* context =
ImageTransportFactoryAndroid::GetInstance()->GetContext3D();
if (context->isContextLost())
return;
context->deleteTexture(texture_id);
DCHECK(context->getError() == GL_NO_ERROR);
}
void CompositorImpl::CopyTextureToBitmap(WebKit::WebGLId texture_id,
gfx::JavaBitmap& bitmap) {
GLHelper* helper = ImageTransportFactoryAndroid::GetInstance()->GetGLHelper();
helper->ReadbackTextureSync(texture_id,
bitmap.size(),
static_cast<unsigned char*> (bitmap.pixels()));
}
void CompositorImpl::updateAnimations(double frameBeginTime) {
}
void CompositorImpl::layout() {
}
void CompositorImpl::applyScrollAndScale(const WebKit::WebSize& scrollDelta,
float scaleFactor) {
}
WebKit::WebCompositorOutputSurface* CompositorImpl::createOutputSurface() {
DCHECK(window_ && surface_id_);
WebKit::WebGraphicsContext3D::Attributes attrs;
attrs.shareResources = true;
attrs.noAutomaticFlushes = true;
GpuChannelHostFactory* factory = BrowserGpuChannelHostFactory::instance();
GURL url("chrome://gpu/Compositor::createContext3D");
base::WeakPtr<WebGraphicsContext3DSwapBuffersClient> swap_client;
scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context(
new WebGraphicsContext3DCommandBufferImpl(
surface_id_,
url,
factory,
swap_client));
if (!context->Initialize(
attrs,
false,
CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE)) {
LOG(ERROR) << "Failed to create 3D context for compositor.";
return NULL;
}
return new WebGraphicsContextToOutputSurfaceAdapter(context.release());
}
void CompositorImpl::didRecreateOutputSurface(bool success) {
}
void CompositorImpl::didCommit() {
}
void CompositorImpl::didCommitAndDrawFrame() {
}
void CompositorImpl::didCompleteSwapBuffers() {
client_->OnSwapBuffersCompleted();
}
void CompositorImpl::scheduleComposite() {
client_->ScheduleComposite();
}
WebKit::WebGLId CompositorImpl::BuildBasicTexture() {
WebKit::WebGraphicsContext3D* context =
ImageTransportFactoryAndroid::GetInstance()->GetContext3D();
if (context->isContextLost())
return 0;
WebKit::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);
DCHECK(context->getError() == GL_NO_ERROR);
return texture_id;
}
WebKit::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;
}
}
WebKit::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;
}
}
} // namespace content
|