- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / compositor_impl_android.cc
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.
4
5 #include "content/browser/renderer_host/compositor_impl_android.h"
6
7 #include <android/bitmap.h>
8 #include <android/native_window_jni.h>
9 #include <map>
10
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"
46
47 namespace gfx {
48 class JavaBitmap;
49 }
50
51 namespace {
52
53 // Used for drawing directly to the screen. Bypasses resizing and swaps.
54 class DirectOutputSurface : public cc::OutputSurface {
55  public:
56   DirectOutputSurface(
57       const scoped_refptr<cc::ContextProvider>& context_provider)
58       : cc::OutputSurface(context_provider) {
59     capabilities_.adjust_deadline_for_parent = false;
60   }
61
62   virtual void Reshape(gfx::Size size, float scale_factor) OVERRIDE {
63     surface_size_ = size;
64   }
65   virtual void SwapBuffers(cc::CompositorFrame*) OVERRIDE {
66     context_provider_->Context3d()->shallowFlushCHROMIUM();
67   }
68 };
69
70 // Used to override capabilities_.adjust_deadline_for_parent to false
71 class OutputSurfaceWithoutParent : public cc::OutputSurface {
72  public:
73   OutputSurfaceWithoutParent(
74       const scoped_refptr<
75         content::ContextProviderCommandBuffer>& context_provider)
76       : cc::OutputSurface(context_provider) {
77     capabilities_.adjust_deadline_for_parent = false;
78   }
79
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);
88
89     OutputSurface::SwapBuffers(frame);
90   }
91 };
92
93 static bool g_initialized = false;
94 static base::Thread* g_impl_thread = NULL;
95
96 } // anonymous namespace
97
98 namespace content {
99
100 typedef std::map<int, base::android::ScopedJavaGlobalRef<jobject> >
101     SurfaceMap;
102 static base::LazyInstance<SurfaceMap>
103     g_surface_map = LAZY_INSTANCE_INITIALIZER;
104 static base::LazyInstance<base::Lock> g_surface_map_lock;
105
106 // static
107 Compositor* Compositor::Create(CompositorClient* client) {
108   return client ? new CompositorImpl(client) : NULL;
109 }
110
111 // static
112 void Compositor::Initialize() {
113   DCHECK(!CompositorImpl::IsInitialized());
114   g_initialized = true;
115 }
116
117 // static
118 bool CompositorImpl::IsInitialized() {
119   return g_initialized;
120 }
121
122 // static
123 bool CompositorImpl::IsThreadingEnabled() {
124   return g_impl_thread;
125 }
126
127 // static
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();
133
134   LOG_IF(WARNING, !jsurface) << "No surface for surface id " << surface_id;
135   return jsurface;
136 }
137
138 CompositorImpl::CompositorImpl(CompositorClient* client)
139     : root_layer_(cc::Layer::Create()),
140       has_transparent_background_(false),
141       window_(NULL),
142       surface_id_(0),
143       client_(client),
144       weak_factory_(this) {
145   DCHECK(client);
146   ImageTransportFactoryAndroid::AddObserver(this);
147 }
148
149 CompositorImpl::~CompositorImpl() {
150   ImageTransportFactoryAndroid::RemoveObserver(this);
151   // Clean-up any surface references.
152   SetSurface(NULL);
153 }
154
155 void CompositorImpl::Composite() {
156   if (host_)
157     host_->Composite(gfx::FrameTime::Now());
158 }
159
160 void CompositorImpl::SetRootLayer(scoped_refptr<cc::Layer> root_layer) {
161   root_layer_->RemoveAllChildren();
162   root_layer_->AddChild(root_layer);
163 }
164
165 void CompositorImpl::SetWindowSurface(ANativeWindow* window) {
166   GpuSurfaceTracker* tracker = GpuSurfaceTracker::Get();
167
168   if (window_) {
169     tracker->RemoveSurface(surface_id_);
170     ANativeWindow_release(window_);
171     window_ = NULL;
172     surface_id_ = 0;
173     SetVisible(false);
174   }
175
176   if (window) {
177     window_ = window;
178     ANativeWindow_acquire(window);
179     surface_id_ = tracker->AddSurfaceForNativeWidget(window);
180     tracker->SetSurfaceHandle(
181         surface_id_,
182         gfx::GLSurfaceHandle(gfx::kNullPluginWindow, gfx::NATIVE_DIRECT));
183     SetVisible(true);
184   }
185 }
186
187 void CompositorImpl::SetSurface(jobject surface) {
188   JNIEnv* env = base::android::AttachCurrentThread();
189   base::android::ScopedJavaLocalRef<jobject> j_surface(env, surface);
190
191   // First, cleanup any existing surface references.
192   if (surface_id_) {
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_);
197   }
198   SetWindowSurface(NULL);
199
200   // Now, set the new surface if we have one.
201   ANativeWindow* window = NULL;
202   if (surface)
203     window = ANativeWindow_fromSurface(env, surface);
204   if (window) {
205     SetWindowSurface(window);
206     ANativeWindow_release(window);
207     {
208       base::AutoLock lock(g_surface_map_lock.Get());
209       g_surface_map.Get().insert(std::make_pair(surface_id_, j_surface));
210     }
211   }
212 }
213
214 void CompositorImpl::SetVisible(bool visible) {
215   if (!visible) {
216     ui_resource_map_.clear();
217     host_.reset();
218     client_->UIResourcesAreInvalid();
219   } else if (!host_) {
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;
228
229     scoped_refptr<base::SingleThreadTaskRunner> impl_thread_task_runner =
230         g_impl_thread ? g_impl_thread->message_loop()->message_loop_proxy()
231                       : NULL;
232
233     host_ = cc::LayerTreeHost::Create(
234         this, NULL, settings, impl_thread_task_runner);
235     host_->SetRootLayer(root_layer_);
236
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
242     // created.
243     client_->DidLoseUIResources();
244   }
245 }
246
247 void CompositorImpl::setDeviceScaleFactor(float factor) {
248   if (host_)
249     host_->SetDeviceScaleFactor(factor);
250 }
251
252 void CompositorImpl::SetWindowBounds(const gfx::Size& size) {
253   if (size_ == size)
254     return;
255
256   size_ = size;
257   if (host_)
258     host_->SetViewportSize(size);
259   root_layer_->SetBounds(size);
260 }
261
262 bool CompositorImpl::CompositeAndReadback(void *pixels, const gfx::Rect& rect) {
263   if (host_)
264     return host_->CompositeAndReadback(pixels, rect);
265   else
266     return false;
267 }
268
269 cc::UIResourceId CompositorImpl::GenerateUIResource(
270     const cc::UIResourceBitmap& bitmap) {
271   if (!host_)
272     return 0;
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());
277   return id;
278 }
279
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);
284 }
285
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())
292     return 0;
293   WebKit::WebGLId format = GetGLFormatForBitmap(bitmap);
294   WebKit::WebGLId type = GetGLTypeForBitmap(bitmap);
295
296   context->texImage2D(GL_TEXTURE_2D,
297                       0,
298                       format,
299                       bitmap.size().width(),
300                       bitmap.size().height(),
301                       0,
302                       format,
303                       type,
304                       bitmap.pixels());
305   context->shallowFlushCHROMIUM();
306   return texture_id;
307 }
308
309 WebKit::WebGLId CompositorImpl::GenerateCompressedTexture(gfx::Size& size,
310                                                           int data_size,
311                                                           void* data) {
312   unsigned int texture_id = BuildBasicTexture();
313   WebKit::WebGraphicsContext3D* context =
314         ImageTransportFactoryAndroid::GetInstance()->GetContext3D();
315   if (texture_id == 0 || context->isContextLost() ||
316       !context->makeContextCurrent())
317     return 0;
318   context->compressedTexImage2D(GL_TEXTURE_2D,
319                                 0,
320                                 GL_ETC1_RGB8_OES,
321                                 size.width(),
322                                 size.height(),
323                                 0,
324                                 data_size,
325                                 data);
326   context->shallowFlushCHROMIUM();
327   return texture_id;
328 }
329
330 void CompositorImpl::DeleteTexture(WebKit::WebGLId texture_id) {
331   WebKit::WebGraphicsContext3D* context =
332       ImageTransportFactoryAndroid::GetInstance()->GetContext3D();
333   if (context->isContextLost() || !context->makeContextCurrent())
334     return;
335   context->deleteTexture(texture_id);
336   context->shallowFlushCHROMIUM();
337 }
338
339 bool CompositorImpl::CopyTextureToBitmap(WebKit::WebGLId texture_id,
340                                          gfx::JavaBitmap& bitmap) {
341   return CopyTextureToBitmap(texture_id, gfx::Rect(bitmap.size()), bitmap);
342 }
343
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;
350
351   GLHelper* helper = ImageTransportFactoryAndroid::GetInstance()->GetGLHelper();
352   helper->ReadbackTextureSync(texture_id,
353                               sub_rect,
354                               static_cast<unsigned char*> (bitmap.pixels()));
355   return true;
356 }
357
358 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
359 CreateGpuProcessViewContext(
360     const WebKit::WebGraphicsContext3D::Attributes attributes,
361     int surface_id,
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>();
369
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() *
376       kBytesPerPixel;
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,
386                                                 url,
387                                                 gpu_channel_host.get(),
388                                                 compositor_impl,
389                                                 attributes,
390                                                 false,
391                                                 limits,
392                                                 true));
393 }
394
395 scoped_ptr<cc::OutputSurface> CompositorImpl::CreateOutputSurface(
396     bool fallback) {
397   WebKit::WebGraphicsContext3D::Attributes attrs;
398   attrs.shareResources = true;
399   attrs.noAutomaticFlushes = true;
400
401   DCHECK(window_);
402   DCHECK(surface_id_);
403
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>();
410   }
411
412   return scoped_ptr<cc::OutputSurface>(
413       new OutputSurfaceWithoutParent(context_provider));
414 }
415
416 void CompositorImpl::OnLostResources() {
417   client_->DidLoseResources();
418 }
419
420 void CompositorImpl::DidCompleteSwapBuffers() {
421   client_->OnSwapBuffersCompleted();
422 }
423
424 void CompositorImpl::ScheduleComposite() {
425   client_->ScheduleComposite();
426 }
427
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.
433   return NULL;
434 }
435
436 void CompositorImpl::OnViewContextSwapBuffersPosted() {
437   TRACE_EVENT0("compositor", "CompositorImpl::OnViewContextSwapBuffersPosted");
438   client_->OnSwapBuffersPosted();
439 }
440
441 void CompositorImpl::OnViewContextSwapBuffersComplete() {
442   TRACE_EVENT0("compositor",
443                "CompositorImpl::OnViewContextSwapBuffersComplete");
444   client_->OnSwapBuffersCompleted();
445 }
446
447 void CompositorImpl::OnViewContextSwapBuffersAborted() {
448   TRACE_EVENT0("compositor", "CompositorImpl::OnViewContextSwapBuffersAborted");
449   client_->OnSwapBuffersCompleted();
450 }
451
452 WebKit::WebGLId CompositorImpl::BuildBasicTexture() {
453   WebKit::WebGraphicsContext3D* context =
454             ImageTransportFactoryAndroid::GetInstance()->GetContext3D();
455   if (context->isContextLost() || !context->makeContextCurrent())
456     return 0;
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);
463   return texture_id;
464 }
465
466 WebKit::WGC3Denum CompositorImpl::GetGLFormatForBitmap(
467     gfx::JavaBitmap& bitmap) {
468   switch (bitmap.format()) {
469     case ANDROID_BITMAP_FORMAT_A_8:
470       return GL_ALPHA;
471       break;
472     case ANDROID_BITMAP_FORMAT_RGBA_4444:
473       return GL_RGBA;
474       break;
475     case ANDROID_BITMAP_FORMAT_RGBA_8888:
476       return GL_RGBA;
477       break;
478     case ANDROID_BITMAP_FORMAT_RGB_565:
479     default:
480       return GL_RGB;
481   }
482 }
483
484 WebKit::WGC3Denum CompositorImpl::GetGLTypeForBitmap(gfx::JavaBitmap& bitmap) {
485   switch (bitmap.format()) {
486     case ANDROID_BITMAP_FORMAT_A_8:
487       return GL_UNSIGNED_BYTE;
488       break;
489     case ANDROID_BITMAP_FORMAT_RGBA_4444:
490       return GL_UNSIGNED_SHORT_4_4_4_4;
491       break;
492     case ANDROID_BITMAP_FORMAT_RGBA_8888:
493       return GL_UNSIGNED_BYTE;
494       break;
495     case ANDROID_BITMAP_FORMAT_RGB_565:
496     default:
497       return GL_UNSIGNED_SHORT_5_6_5;
498   }
499 }
500
501 } // namespace content