1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/compositor_impl_android.h"
7 #include <android/bitmap.h>
8 #include <android/native_window_jni.h>
11 #include "base/android/jni_android.h"
12 #include "base/android/scoped_java_ref.h"
13 #include "base/bind.h"
14 #include "base/command_line.h"
15 #include "base/lazy_instance.h"
16 #include "base/logging.h"
17 #include "base/single_thread_task_runner.h"
18 #include "base/synchronization/lock.h"
19 #include "base/threading/thread.h"
20 #include "cc/input/input_handler.h"
21 #include "cc/layers/layer.h"
22 #include "cc/output/compositor_frame.h"
23 #include "cc/output/context_provider.h"
24 #include "cc/output/output_surface.h"
25 #include "cc/resources/scoped_ui_resource.h"
26 #include "cc/resources/ui_resource_bitmap.h"
27 #include "cc/trees/layer_tree_host.h"
28 #include "content/browser/gpu/browser_gpu_channel_host_factory.h"
29 #include "content/browser/gpu/gpu_surface_tracker.h"
30 #include "content/common/gpu/client/command_buffer_proxy_impl.h"
31 #include "content/common/gpu/client/context_provider_command_buffer.h"
32 #include "content/common/gpu/client/gl_helper.h"
33 #include "content/common/gpu/client/gpu_channel_host.h"
34 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
35 #include "content/common/gpu/gpu_process_launch_causes.h"
36 #include "content/public/browser/android/compositor_client.h"
37 #include "content/public/common/content_switches.h"
38 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
39 #include "third_party/khronos/GLES2/gl2.h"
40 #include "third_party/khronos/GLES2/gl2ext.h"
41 #include "ui/gfx/android/device_display_info.h"
42 #include "ui/gfx/android/java_bitmap.h"
43 #include "ui/gfx/frame_time.h"
44 #include "webkit/common/gpu/context_provider_in_process.h"
45 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
53 // Used for drawing directly to the screen. Bypasses resizing and swaps.
54 class DirectOutputSurface : public cc::OutputSurface {
57 const scoped_refptr<cc::ContextProvider>& context_provider)
58 : cc::OutputSurface(context_provider) {
59 capabilities_.adjust_deadline_for_parent = false;
62 virtual void Reshape(gfx::Size size, float scale_factor) OVERRIDE {
65 virtual void SwapBuffers(cc::CompositorFrame*) OVERRIDE {
66 context_provider_->Context3d()->shallowFlushCHROMIUM();
70 // Used to override capabilities_.adjust_deadline_for_parent to false
71 class OutputSurfaceWithoutParent : public cc::OutputSurface {
73 OutputSurfaceWithoutParent(
75 content::ContextProviderCommandBuffer>& context_provider)
76 : cc::OutputSurface(context_provider) {
77 capabilities_.adjust_deadline_for_parent = false;
80 virtual void SwapBuffers(cc::CompositorFrame* frame) OVERRIDE {
81 content::WebGraphicsContext3DCommandBufferImpl* command_buffer_context =
82 static_cast<content::WebGraphicsContext3DCommandBufferImpl*>(
83 context_provider_->Context3d());
84 content::CommandBufferProxyImpl* command_buffer_proxy =
85 command_buffer_context->GetCommandBufferProxy();
86 DCHECK(command_buffer_proxy);
87 command_buffer_proxy->SetLatencyInfo(frame->metadata.latency_info);
89 OutputSurface::SwapBuffers(frame);
93 static bool g_initialized = false;
94 static base::Thread* g_impl_thread = NULL;
96 } // anonymous namespace
100 typedef std::map<int, base::android::ScopedJavaGlobalRef<jobject> >
102 static base::LazyInstance<SurfaceMap>
103 g_surface_map = LAZY_INSTANCE_INITIALIZER;
104 static base::LazyInstance<base::Lock> g_surface_map_lock;
107 Compositor* Compositor::Create(CompositorClient* client) {
108 return client ? new CompositorImpl(client) : NULL;
112 void Compositor::Initialize() {
113 DCHECK(!CompositorImpl::IsInitialized());
114 g_initialized = true;
118 bool CompositorImpl::IsInitialized() {
119 return g_initialized;
123 bool CompositorImpl::IsThreadingEnabled() {
124 return g_impl_thread;
128 jobject CompositorImpl::GetSurface(int surface_id) {
129 base::AutoLock lock(g_surface_map_lock.Get());
130 SurfaceMap* surfaces = g_surface_map.Pointer();
131 SurfaceMap::iterator it = surfaces->find(surface_id);
132 jobject jsurface = it == surfaces->end() ? NULL : it->second.obj();
134 LOG_IF(WARNING, !jsurface) << "No surface for surface id " << surface_id;
138 CompositorImpl::CompositorImpl(CompositorClient* client)
139 : root_layer_(cc::Layer::Create()),
140 has_transparent_background_(false),
144 weak_factory_(this) {
146 ImageTransportFactoryAndroid::AddObserver(this);
149 CompositorImpl::~CompositorImpl() {
150 ImageTransportFactoryAndroid::RemoveObserver(this);
151 // Clean-up any surface references.
155 void CompositorImpl::Composite() {
157 host_->Composite(gfx::FrameTime::Now());
160 void CompositorImpl::SetRootLayer(scoped_refptr<cc::Layer> root_layer) {
161 root_layer_->RemoveAllChildren();
162 root_layer_->AddChild(root_layer);
165 void CompositorImpl::SetWindowSurface(ANativeWindow* window) {
166 GpuSurfaceTracker* tracker = GpuSurfaceTracker::Get();
169 tracker->RemoveSurface(surface_id_);
170 ANativeWindow_release(window_);
178 ANativeWindow_acquire(window);
179 surface_id_ = tracker->AddSurfaceForNativeWidget(window);
180 tracker->SetSurfaceHandle(
182 gfx::GLSurfaceHandle(gfx::kNullPluginWindow, gfx::NATIVE_DIRECT));
187 void CompositorImpl::SetSurface(jobject surface) {
188 JNIEnv* env = base::android::AttachCurrentThread();
189 base::android::ScopedJavaLocalRef<jobject> j_surface(env, surface);
191 // First, cleanup any existing surface references.
193 DCHECK(g_surface_map.Get().find(surface_id_) !=
194 g_surface_map.Get().end());
195 base::AutoLock lock(g_surface_map_lock.Get());
196 g_surface_map.Get().erase(surface_id_);
198 SetWindowSurface(NULL);
200 // Now, set the new surface if we have one.
201 ANativeWindow* window = NULL;
203 window = ANativeWindow_fromSurface(env, surface);
205 SetWindowSurface(window);
206 ANativeWindow_release(window);
208 base::AutoLock lock(g_surface_map_lock.Get());
209 g_surface_map.Get().insert(std::make_pair(surface_id_, j_surface));
214 void CompositorImpl::SetVisible(bool visible) {
216 ui_resource_map_.clear();
218 client_->UIResourcesAreInvalid();
220 cc::LayerTreeSettings settings;
221 settings.refresh_rate = 60.0;
222 settings.impl_side_painting = false;
223 settings.allow_antialiasing = false;
224 settings.calculate_top_controls_position = false;
225 settings.top_controls_height = 0.f;
226 settings.use_memory_management = false;
227 settings.highp_threshold_min = 2048;
229 scoped_refptr<base::SingleThreadTaskRunner> impl_thread_task_runner =
230 g_impl_thread ? g_impl_thread->message_loop()->message_loop_proxy()
233 host_ = cc::LayerTreeHost::Create(
234 this, NULL, settings, impl_thread_task_runner);
235 host_->SetRootLayer(root_layer_);
237 host_->SetVisible(true);
238 host_->SetLayerTreeHostClientReady();
239 host_->SetViewportSize(size_);
240 host_->set_has_transparent_background(has_transparent_background_);
241 // Need to recreate the UI resources because a new LayerTreeHost has been
243 client_->DidLoseUIResources();
247 void CompositorImpl::setDeviceScaleFactor(float factor) {
249 host_->SetDeviceScaleFactor(factor);
252 void CompositorImpl::SetWindowBounds(const gfx::Size& size) {
258 host_->SetViewportSize(size);
259 root_layer_->SetBounds(size);
262 bool CompositorImpl::CompositeAndReadback(void *pixels, const gfx::Rect& rect) {
264 return host_->CompositeAndReadback(pixels, rect);
269 cc::UIResourceId CompositorImpl::GenerateUIResource(
270 const cc::UIResourceBitmap& bitmap) {
273 scoped_ptr<cc::ScopedUIResource> ui_resource =
274 cc::ScopedUIResource::Create(host_.get(), bitmap);
275 cc::UIResourceId id = ui_resource->id();
276 ui_resource_map_.set(id, ui_resource.Pass());
280 void CompositorImpl::DeleteUIResource(cc::UIResourceId resource_id) {
281 UIResourceMap::iterator it = ui_resource_map_.find(resource_id);
282 if (it != ui_resource_map_.end())
283 ui_resource_map_.erase(it);
286 WebKit::WebGLId CompositorImpl::GenerateTexture(gfx::JavaBitmap& bitmap) {
287 unsigned int texture_id = BuildBasicTexture();
288 WebKit::WebGraphicsContext3D* context =
289 ImageTransportFactoryAndroid::GetInstance()->GetContext3D();
290 if (texture_id == 0 || context->isContextLost() ||
291 !context->makeContextCurrent())
293 WebKit::WebGLId format = GetGLFormatForBitmap(bitmap);
294 WebKit::WebGLId type = GetGLTypeForBitmap(bitmap);
296 context->texImage2D(GL_TEXTURE_2D,
299 bitmap.size().width(),
300 bitmap.size().height(),
305 context->shallowFlushCHROMIUM();
309 WebKit::WebGLId CompositorImpl::GenerateCompressedTexture(gfx::Size& size,
312 unsigned int texture_id = BuildBasicTexture();
313 WebKit::WebGraphicsContext3D* context =
314 ImageTransportFactoryAndroid::GetInstance()->GetContext3D();
315 if (texture_id == 0 || context->isContextLost() ||
316 !context->makeContextCurrent())
318 context->compressedTexImage2D(GL_TEXTURE_2D,
326 context->shallowFlushCHROMIUM();
330 void CompositorImpl::DeleteTexture(WebKit::WebGLId texture_id) {
331 WebKit::WebGraphicsContext3D* context =
332 ImageTransportFactoryAndroid::GetInstance()->GetContext3D();
333 if (context->isContextLost() || !context->makeContextCurrent())
335 context->deleteTexture(texture_id);
336 context->shallowFlushCHROMIUM();
339 bool CompositorImpl::CopyTextureToBitmap(WebKit::WebGLId texture_id,
340 gfx::JavaBitmap& bitmap) {
341 return CopyTextureToBitmap(texture_id, gfx::Rect(bitmap.size()), bitmap);
344 bool CompositorImpl::CopyTextureToBitmap(WebKit::WebGLId texture_id,
345 const gfx::Rect& sub_rect,
346 gfx::JavaBitmap& bitmap) {
347 // The sub_rect should match the bitmap size.
348 DCHECK(bitmap.size() == sub_rect.size());
349 if (bitmap.size() != sub_rect.size() || texture_id == 0) return false;
351 GLHelper* helper = ImageTransportFactoryAndroid::GetInstance()->GetGLHelper();
352 helper->ReadbackTextureSync(texture_id,
354 static_cast<unsigned char*> (bitmap.pixels()));
358 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
359 CreateGpuProcessViewContext(
360 const WebKit::WebGraphicsContext3D::Attributes attributes,
362 base::WeakPtr<CompositorImpl> compositor_impl) {
363 BrowserGpuChannelHostFactory* factory =
364 BrowserGpuChannelHostFactory::instance();
365 scoped_refptr<GpuChannelHost> gpu_channel_host(factory->EstablishGpuChannelSync(
366 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE));
367 if (!gpu_channel_host)
368 return scoped_ptr<WebGraphicsContext3DCommandBufferImpl>();
370 GURL url("chrome://gpu/Compositor::createContext3D");
371 static const size_t kBytesPerPixel = 4;
372 gfx::DeviceDisplayInfo display_info;
373 size_t full_screen_texture_size_in_bytes =
374 display_info.GetDisplayHeight() *
375 display_info.GetDisplayWidth() *
377 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits limits;
378 limits.command_buffer_size = 64 * 1024;
379 limits.start_transfer_buffer_size = 64 * 1024;
380 limits.min_transfer_buffer_size = 64 * 1024;
381 limits.max_transfer_buffer_size = std::min(
382 3 * full_screen_texture_size_in_bytes, kDefaultMaxTransferBufferSize);
383 limits.mapped_memory_reclaim_limit = 2 * 1024 * 1024;
384 return make_scoped_ptr(
385 new WebGraphicsContext3DCommandBufferImpl(surface_id,
387 gpu_channel_host.get(),
395 scoped_ptr<cc::OutputSurface> CompositorImpl::CreateOutputSurface(
397 WebKit::WebGraphicsContext3D::Attributes attrs;
398 attrs.shareResources = true;
399 attrs.noAutomaticFlushes = true;
404 scoped_refptr<ContextProviderCommandBuffer> context_provider =
405 ContextProviderCommandBuffer::Create(CreateGpuProcessViewContext(
406 attrs, surface_id_, weak_factory_.GetWeakPtr()), "BrowserCompositor");
407 if (!context_provider.get()) {
408 LOG(ERROR) << "Failed to create 3D context for compositor.";
409 return scoped_ptr<cc::OutputSurface>();
412 return scoped_ptr<cc::OutputSurface>(
413 new OutputSurfaceWithoutParent(context_provider));
416 void CompositorImpl::OnLostResources() {
417 client_->DidLoseResources();
420 void CompositorImpl::DidCompleteSwapBuffers() {
421 client_->OnSwapBuffersCompleted();
424 void CompositorImpl::ScheduleComposite() {
425 client_->ScheduleComposite();
428 scoped_refptr<cc::ContextProvider> CompositorImpl::OffscreenContextProvider() {
429 // There is no support for offscreen contexts, or compositor filters that
430 // would require them in this compositor instance. If they are needed,
431 // then implement a context provider that provides contexts from
432 // ImageTransportSurfaceAndroid.
436 void CompositorImpl::OnViewContextSwapBuffersPosted() {
437 TRACE_EVENT0("compositor", "CompositorImpl::OnViewContextSwapBuffersPosted");
438 client_->OnSwapBuffersPosted();
441 void CompositorImpl::OnViewContextSwapBuffersComplete() {
442 TRACE_EVENT0("compositor",
443 "CompositorImpl::OnViewContextSwapBuffersComplete");
444 client_->OnSwapBuffersCompleted();
447 void CompositorImpl::OnViewContextSwapBuffersAborted() {
448 TRACE_EVENT0("compositor", "CompositorImpl::OnViewContextSwapBuffersAborted");
449 client_->OnSwapBuffersCompleted();
452 WebKit::WebGLId CompositorImpl::BuildBasicTexture() {
453 WebKit::WebGraphicsContext3D* context =
454 ImageTransportFactoryAndroid::GetInstance()->GetContext3D();
455 if (context->isContextLost() || !context->makeContextCurrent())
457 WebKit::WebGLId texture_id = context->createTexture();
458 context->bindTexture(GL_TEXTURE_2D, texture_id);
459 context->texParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
460 context->texParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
461 context->texParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
462 context->texParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
466 WebKit::WGC3Denum CompositorImpl::GetGLFormatForBitmap(
467 gfx::JavaBitmap& bitmap) {
468 switch (bitmap.format()) {
469 case ANDROID_BITMAP_FORMAT_A_8:
472 case ANDROID_BITMAP_FORMAT_RGBA_4444:
475 case ANDROID_BITMAP_FORMAT_RGBA_8888:
478 case ANDROID_BITMAP_FORMAT_RGB_565:
484 WebKit::WGC3Denum CompositorImpl::GetGLTypeForBitmap(gfx::JavaBitmap& bitmap) {
485 switch (bitmap.format()) {
486 case ANDROID_BITMAP_FORMAT_A_8:
487 return GL_UNSIGNED_BYTE;
489 case ANDROID_BITMAP_FORMAT_RGBA_4444:
490 return GL_UNSIGNED_SHORT_4_4_4_4;
492 case ANDROID_BITMAP_FORMAT_RGBA_8888:
493 return GL_UNSIGNED_BYTE;
495 case ANDROID_BITMAP_FORMAT_RGB_565:
497 return GL_UNSIGNED_SHORT_5_6_5;
501 } // namespace content