Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / browser / android / in_process / synchronous_compositor_factory_impl.cc
index 3e5df8b..af50011 100644 (file)
@@ -6,7 +6,9 @@
 
 #include "content/browser/android/in_process/synchronous_compositor_output_surface.h"
 #include "content/public/browser/browser_thread.h"
+#include "content/renderer/gpu/frame_swap_message_queue.h"
 #include "gpu/command_buffer/client/gl_in_process_context.h"
+#include "gpu/command_buffer/common/gles2_cmd_utils.h"
 #include "ui/gl/android/surface_texture.h"
 #include "ui/gl/gl_surface.h"
 #include "ui/gl/gl_surface_stub.h"
@@ -31,33 +33,50 @@ blink::WebGraphicsContext3D::Attributes GetDefaultAttribs() {
 }
 
 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl;
+using webkit::gpu::WebGraphicsContext3DImpl;
 
-scoped_ptr<gpu::GLInProcessContext> CreateContextWithAttributes(
-    scoped_refptr<gfx::GLSurface> surface,
-    scoped_refptr<gpu::InProcessCommandBuffer::Service> service,
-    gpu::GLInProcessContext* share_context,
+scoped_ptr<gpu::GLInProcessContext> CreateOffscreenContext(
     const blink::WebGraphicsContext3D::Attributes& attributes) {
   const gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu;
 
-  if (!surface)
-    surface = gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size(1, 1));
-
-  gpu::GLInProcessContextAttribs in_process_attribs;
-  WebGraphicsContext3DInProcessCommandBufferImpl::ConvertAttributes(
+  gpu::gles2::ContextCreationAttribHelper in_process_attribs;
+  WebGraphicsContext3DImpl::ConvertAttributes(
       attributes, &in_process_attribs);
-  in_process_attribs.lose_context_when_out_of_memory = 1;
+  in_process_attribs.lose_context_when_out_of_memory = true;
+
   scoped_ptr<gpu::GLInProcessContext> context(
-      gpu::GLInProcessContext::CreateWithSurface(
-          surface, service, share_context, in_process_attribs, gpu_preference));
+      gpu::GLInProcessContext::Create(NULL /* service */,
+                                      NULL /* surface */,
+                                      true /* is_offscreen */,
+                                      gfx::kNullAcceleratedWidget,
+                                      gfx::Size(1, 1),
+                                      NULL /* share_context */,
+                                      false /* share_resources */,
+                                      in_process_attribs,
+                                      gpu_preference));
   return context.Pass();
 }
 
 scoped_ptr<gpu::GLInProcessContext> CreateContext(
-    scoped_refptr<gfx::GLSurface> surface,
     scoped_refptr<gpu::InProcessCommandBuffer::Service> service,
     gpu::GLInProcessContext* share_context) {
-  return CreateContextWithAttributes(
-      surface, service, share_context, GetDefaultAttribs());
+  const gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu;
+  gpu::gles2::ContextCreationAttribHelper in_process_attribs;
+  WebGraphicsContext3DImpl::ConvertAttributes(
+      GetDefaultAttribs(), &in_process_attribs);
+  in_process_attribs.lose_context_when_out_of_memory = true;
+
+  scoped_ptr<gpu::GLInProcessContext> context(
+      gpu::GLInProcessContext::Create(service,
+                                      NULL /* surface */,
+                                      false /* is_offscreen */,
+                                      gfx::kNullAcceleratedWidget,
+                                      gfx::Size(1, 1),
+                                      share_context,
+                                      false /* share_resources */,
+                                      in_process_attribs,
+                                      gpu_preference));
+  return context.Pass();
 }
 
 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> WrapContext(
@@ -70,6 +89,18 @@ scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> WrapContext(
           context.Pass(), GetDefaultAttribs()));
 }
 
+scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>
+WrapContextWithAttributes(
+    scoped_ptr<gpu::GLInProcessContext> context,
+    const blink::WebGraphicsContext3D::Attributes& attributes) {
+  if (!context.get())
+    return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>();
+
+  return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>(
+      WebGraphicsContext3DInProcessCommandBufferImpl::WrapContext(
+          context.Pass(), attributes));
+}
+
 class VideoContextProvider
     : public StreamTextureFactorySynchronousImpl::ContextProvider {
  public:
@@ -107,7 +138,7 @@ class VideoContextProvider
 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl;
 
 SynchronousCompositorFactoryImpl::SynchronousCompositorFactoryImpl()
-    : wrapped_gl_context_for_compositor_thread_(NULL),
+    : record_full_layer_(true),
       num_hardware_compositors_(0) {
   SynchronousCompositorFactory::SetInstance(this);
 }
@@ -119,10 +150,18 @@ SynchronousCompositorFactoryImpl::GetCompositorMessageLoop() {
   return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);
 }
 
+bool
+SynchronousCompositorFactoryImpl::RecordFullLayer() {
+  return record_full_layer_;
+}
+
 scoped_ptr<cc::OutputSurface>
-SynchronousCompositorFactoryImpl::CreateOutputSurface(int routing_id) {
+SynchronousCompositorFactoryImpl::CreateOutputSurface(
+    int routing_id,
+    scoped_refptr<content::FrameSwapMessageQueue> frame_swap_message_queue) {
   scoped_ptr<SynchronousCompositorOutputSurface> output_surface(
-      new SynchronousCompositorOutputSurface(routing_id));
+      new SynchronousCompositorOutputSurface(routing_id,
+                                             frame_swap_message_queue));
   return output_surface.PassAs<cc::OutputSurface>();
 }
 
@@ -131,82 +170,48 @@ SynchronousCompositorFactoryImpl::GetInputHandlerManagerClient() {
   return synchronous_input_event_filter();
 }
 
-scoped_refptr<ContextProviderWebContext> SynchronousCompositorFactoryImpl::
-    GetSharedOffscreenContextProviderForMainThread() {
-  bool failed = false;
-  if ((!offscreen_context_for_main_thread_.get() ||
-       offscreen_context_for_main_thread_->DestroyedOnMainThread())) {
-    scoped_ptr<gpu::GLInProcessContext> context =
-        CreateContext(NULL, NULL, NULL);
-    offscreen_context_for_main_thread_ =
-        webkit::gpu::ContextProviderInProcess::Create(
-            WrapContext(context.Pass()),
-            "Compositor-Offscreen-main-thread");
-    failed = !offscreen_context_for_main_thread_.get() ||
-             !offscreen_context_for_main_thread_->BindToCurrentThread();
-  }
-
-  if (failed) {
-    offscreen_context_for_main_thread_ = NULL;
-  }
-  return offscreen_context_for_main_thread_;
-}
-
-// This is called on the renderer compositor impl thread (InitializeHwDraw) in
-// order to support Android WebView synchronously enable and disable hardware
-// mode multiple times in the same task.
-scoped_refptr<cc::ContextProvider> SynchronousCompositorFactoryImpl::
-    GetOffscreenContextProviderForCompositorThread() {
-  DCHECK(service_);
-  bool failed = false;
-  if (!offscreen_context_for_compositor_thread_.get() ||
-      offscreen_context_for_compositor_thread_->DestroyedOnMainThread()) {
-    scoped_ptr<gpu::GLInProcessContext> context =
-        CreateContext(new gfx::GLSurfaceStub, service_, NULL);
-    wrapped_gl_context_for_compositor_thread_ = context.get();
-    offscreen_context_for_compositor_thread_ =
-        webkit::gpu::ContextProviderInProcess::Create(
-            WrapContext(context.Pass()),
-            "Compositor-Offscreen-compositor-thread");
-    failed = !offscreen_context_for_compositor_thread_.get() ||
-             !offscreen_context_for_compositor_thread_->BindToCurrentThread();
-  }
-  if (failed) {
-    offscreen_context_for_compositor_thread_ = NULL;
-    wrapped_gl_context_for_compositor_thread_ = NULL;
-  }
-  return offscreen_context_for_compositor_thread_;
+scoped_refptr<ContextProviderWebContext>
+SynchronousCompositorFactoryImpl::CreateOffscreenContextProvider(
+    const blink::WebGraphicsContext3D::Attributes& attributes,
+    const std::string& debug_name) {
+  scoped_ptr<gpu::GLInProcessContext> context =
+      CreateOffscreenContext(attributes);
+  return webkit::gpu::ContextProviderInProcess::Create(
+      WrapContext(context.Pass()), debug_name);
 }
 
 scoped_refptr<cc::ContextProvider> SynchronousCompositorFactoryImpl::
-    CreateOnscreenContextProviderForCompositorThread(
-        scoped_refptr<gfx::GLSurface> surface) {
-  DCHECK(surface);
+    CreateOnscreenContextProviderForCompositorThread() {
   DCHECK(service_);
-  DCHECK(wrapped_gl_context_for_compositor_thread_);
 
+  if (!share_context_.get())
+    share_context_ = CreateContext(service_, NULL);
   return webkit::gpu::ContextProviderInProcess::Create(
-      WrapContext(CreateContext(
-          surface, service_, wrapped_gl_context_for_compositor_thread_)),
-      "Compositor-Onscreen");
+      WrapContext(CreateContext(service_, share_context_.get())),
+      "Child-Compositor");
+}
+
+gpu::GLInProcessContext* SynchronousCompositorFactoryImpl::GetShareContext() {
+  DCHECK(share_context_.get());
+  return share_context_.get();
 }
 
 scoped_refptr<StreamTextureFactory>
-SynchronousCompositorFactoryImpl::CreateStreamTextureFactory(int view_id) {
+SynchronousCompositorFactoryImpl::CreateStreamTextureFactory(int frame_id) {
   scoped_refptr<StreamTextureFactorySynchronousImpl> factory(
       StreamTextureFactorySynchronousImpl::Create(
           base::Bind(
               &SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory,
               base::Unretained(this)),
-          view_id));
+          frame_id));
   return factory;
 }
 
 blink::WebGraphicsContext3D*
 SynchronousCompositorFactoryImpl::CreateOffscreenGraphicsContext3D(
     const blink::WebGraphicsContext3D::Attributes& attributes) {
-  return WrapContext(CreateContextWithAttributes(NULL, NULL, NULL, attributes))
-      .release();
+  return WrapContextWithAttributes(CreateOffscreenContext(attributes),
+                                   attributes).release();
 }
 
 void SynchronousCompositorFactoryImpl::CompositorInitializedHardwareDraw() {
@@ -218,6 +223,11 @@ void SynchronousCompositorFactoryImpl::CompositorReleasedHardwareDraw() {
   base::AutoLock lock(num_hardware_compositor_lock_);
   DCHECK_GT(num_hardware_compositors_, 0u);
   num_hardware_compositors_--;
+  if (num_hardware_compositors_ == 0) {
+    // Nullify the video_context_provider_ now so that it is not null only if
+    // there is at least 1 hardware compositor
+    video_context_provider_ = NULL;
+  }
 }
 
 bool SynchronousCompositorFactoryImpl::CanCreateMainThreadContext() {
@@ -231,16 +241,14 @@ SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory() {
       context_provider;
   // This check only guarantees the main thread context is created after
   // a compositor did successfully initialize hardware draw in the past.
-  // In particular this does not guarantee that the main thread context
-  // will fail creation when all compositors release hardware draw.
+  // When all compositors have released hardware draw, main thread context
+  // creation is guaranteed to fail.
   if (CanCreateMainThreadContext() && !video_context_provider_) {
     DCHECK(service_);
-    DCHECK(wrapped_gl_context_for_compositor_thread_);
+    DCHECK(share_context_.get());
 
     video_context_provider_ = new VideoContextProvider(
-        CreateContext(new gfx::GLSurfaceStub,
-                      service_,
-                      wrapped_gl_context_for_compositor_thread_));
+        CreateContext(service_, share_context_.get()));
   }
   return video_context_provider_;
 }
@@ -251,4 +259,9 @@ void SynchronousCompositorFactoryImpl::SetDeferredGpuService(
   service_ = service;
 }
 
+void SynchronousCompositorFactoryImpl::SetRecordFullDocument(
+    bool record_full_document) {
+  record_full_layer_ = record_full_document;
+}
+
 }  // namespace content