Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / service / in_process_command_buffer.cc
index 97ec15e..20766db 100644 (file)
@@ -24,6 +24,7 @@
 #include "base/sequence_checker.h"
 #include "base/synchronization/condition_variable.h"
 #include "base/threading/thread.h"
+#include "gpu/command_buffer/client/gpu_memory_buffer_factory.h"
 #include "gpu/command_buffer/service/command_buffer_service.h"
 #include "gpu/command_buffer/service/context_group.h"
 #include "gpu/command_buffer/service/gl_context_virtual.h"
@@ -73,11 +74,14 @@ class GpuInProcessThread
   virtual void ScheduleTask(const base::Closure& task) OVERRIDE;
   virtual void ScheduleIdleWork(const base::Closure& callback) OVERRIDE;
   virtual bool UseVirtualizedGLContexts() OVERRIDE { return false; }
+  virtual scoped_refptr<gles2::ShaderTranslatorCache> shader_translator_cache()
+      OVERRIDE;
 
  private:
   virtual ~GpuInProcessThread();
   friend class base::RefCountedThreadSafe<GpuInProcessThread>;
 
+  scoped_refptr<gpu::gles2::ShaderTranslatorCache> shader_translator_cache_;
   DISALLOW_COPY_AND_ASSIGN(GpuInProcessThread);
 };
 
@@ -98,6 +102,13 @@ void GpuInProcessThread::ScheduleIdleWork(const base::Closure& callback) {
       FROM_HERE, callback, base::TimeDelta::FromMilliseconds(5));
 }
 
+scoped_refptr<gles2::ShaderTranslatorCache>
+GpuInProcessThread::shader_translator_cache() {
+  if (!shader_translator_cache_.get())
+    shader_translator_cache_ = new gpu::gles2::ShaderTranslatorCache;
+  return shader_translator_cache_;
+}
+
 base::LazyInstance<std::set<InProcessCommandBuffer*> > default_thread_clients_ =
     LAZY_INSTANCE_INITIALIZER;
 base::LazyInstance<base::Lock> default_thread_clients_lock_ =
@@ -286,8 +297,11 @@ bool InProcessCommandBuffer::Initialize(
       base::Bind(&RunTaskWithResult<bool>, init_task, &result, &completion));
   completion.Wait();
 
-  if (result)
+  if (result) {
     capabilities_ = capabilities;
+    capabilities_.map_image =
+        capabilities_.map_image && g_gpu_memory_buffer_factory;
+  }
   return result;
 }
 
@@ -325,12 +339,14 @@ bool InProcessCommandBuffer::InitializeOnGpuThread(
 
   bool bind_generates_resource = false;
   decoder_.reset(gles2::GLES2Decoder::Create(
-      params.context_group ? params.context_group->decoder_->GetContextGroup()
-                    : new gles2::ContextGroup(NULL,
-                                              NULL,
-                                              NULL,
-                                              NULL,
-                                              bind_generates_resource)));
+      params.context_group
+          ? params.context_group->decoder_->GetContextGroup()
+          : new gles2::ContextGroup(NULL,
+                                    NULL,
+                                    NULL,
+                                    service_->shader_translator_cache(),
+                                    NULL,
+                                    bind_generates_resource)));
 
   gpu_scheduler_.reset(
       new GpuScheduler(command_buffer.get(), decoder_.get(), decoder_.get()));
@@ -397,15 +413,11 @@ bool InProcessCommandBuffer::InitializeOnGpuThread(
     DestroyOnGpuThread();
     return false;
   }
+  *params.capabilities = decoder_->GetCapabilities();
 
   gpu_control_.reset(
       new GpuControlService(decoder_->GetContextGroup()->image_manager(),
-                            g_gpu_memory_buffer_factory,
-                            decoder_->GetContextGroup()->mailbox_manager(),
-                            decoder_->GetQueryManager(),
-                            decoder_->GetCapabilities()));
-
-  *params.capabilities = gpu_control_->GetCapabilities();
+                            decoder_->GetQueryManager()));
 
   if (!params.is_offscreen) {
     decoder_->SetResizeCallback(base::Bind(
@@ -471,11 +483,6 @@ CommandBuffer::State InProcessCommandBuffer::GetStateFast() {
   return last_state_;
 }
 
-CommandBuffer::State InProcessCommandBuffer::GetState() {
-  CheckSequencedThread();
-  return GetStateFast();
-}
-
 CommandBuffer::State InProcessCommandBuffer::GetLastState() {
   CheckSequencedThread();
   return last_state_;
@@ -495,7 +502,7 @@ void InProcessCommandBuffer::FlushOnGpuThread(int32 put_offset) {
   {
     // Update state before signaling the flush event.
     base::AutoLock lock(state_after_last_flush_lock_);
-    state_after_last_flush_ = command_buffer_->GetState();
+    state_after_last_flush_ = command_buffer_->GetLastState();
   }
   DCHECK((!error::IsError(state_after_last_flush_.error) && !context_lost_) ||
          (error::IsError(state_after_last_flush_.error) && context_lost_));
@@ -566,7 +573,7 @@ void InProcessCommandBuffer::SetGetBuffer(int32 shm_id) {
   }
   {
     base::AutoLock lock(state_after_last_flush_lock_);
-    state_after_last_flush_ = command_buffer_->GetState();
+    state_after_last_flush_ = command_buffer_->GetLastState();
   }
 }
 
@@ -579,16 +586,17 @@ scoped_refptr<Buffer> InProcessCommandBuffer::CreateTransferBuffer(size_t size,
 
 void InProcessCommandBuffer::DestroyTransferBuffer(int32 id) {
   CheckSequencedThread();
-  base::Closure task = base::Bind(&CommandBuffer::DestroyTransferBuffer,
-                                  base::Unretained(command_buffer_.get()),
-                                  id);
+  base::Closure task =
+      base::Bind(&InProcessCommandBuffer::DestroyTransferBufferOnGputhread,
+                 base::Unretained(this),
+                 id);
 
   QueueTask(task);
 }
 
-scoped_refptr<gpu::Buffer> InProcessCommandBuffer::GetTransferBuffer(int32 id) {
-  NOTREACHED();
-  return NULL;
+void InProcessCommandBuffer::DestroyTransferBufferOnGputhread(int32 id) {
+  base::AutoLock lock(command_buffer_lock_);
+  command_buffer_->DestroyTransferBuffer(id);
 }
 
 gpu::Capabilities InProcessCommandBuffer::GetCapabilities() {
@@ -599,18 +607,40 @@ gfx::GpuMemoryBuffer* InProcessCommandBuffer::CreateGpuMemoryBuffer(
     size_t width,
     size_t height,
     unsigned internalformat,
+    unsigned usage,
     int32* id) {
   CheckSequencedThread();
-  base::AutoLock lock(command_buffer_lock_);
-  return gpu_control_->CreateGpuMemoryBuffer(width,
-                                             height,
-                                             internalformat,
-                                             id);
+
+  *id = -1;
+  linked_ptr<gfx::GpuMemoryBuffer> buffer =
+      make_linked_ptr(g_gpu_memory_buffer_factory->CreateGpuMemoryBuffer(
+          width, height, internalformat, usage));
+  if (!buffer.get())
+    return NULL;
+
+  static int32 next_id = 1;
+  *id = next_id++;
+
+  base::Closure task = base::Bind(&GpuControlService::RegisterGpuMemoryBuffer,
+                                  base::Unretained(gpu_control_.get()),
+                                  *id,
+                                  buffer->GetHandle(),
+                                  width,
+                                  height,
+                                  internalformat);
+
+  QueueTask(task);
+
+  gpu_memory_buffers_[*id] = buffer;
+  return buffer.get();
 }
 
 void InProcessCommandBuffer::DestroyGpuMemoryBuffer(int32 id) {
   CheckSequencedThread();
-  base::Closure task = base::Bind(&GpuControl::DestroyGpuMemoryBuffer,
+  GpuMemoryBufferMap::iterator it = gpu_memory_buffers_.find(id);
+  if (it != gpu_memory_buffers_.end())
+    gpu_memory_buffers_.erase(it);
+  base::Closure task = base::Bind(&GpuControlService::UnregisterGpuMemoryBuffer,
                                   base::Unretained(gpu_control_.get()),
                                   id);
 
@@ -659,7 +689,7 @@ void InProcessCommandBuffer::SignalSyncPointOnGpuThread(
 void InProcessCommandBuffer::SignalQuery(unsigned query,
                                          const base::Closure& callback) {
   CheckSequencedThread();
-  QueueTask(base::Bind(&GpuControl::SignalQuery,
+  QueueTask(base::Bind(&GpuControlService::SignalQuery,
                        base::Unretained(gpu_control_.get()),
                        query,
                        WrapCallback(callback)));
@@ -708,19 +738,6 @@ bool InProcessCommandBuffer::Initialize() {
   return false;
 }
 
-void InProcessCommandBuffer::SetGetOffset(int32 get_offset) { NOTREACHED(); }
-
-void InProcessCommandBuffer::SetToken(int32 token) { NOTREACHED(); }
-
-void InProcessCommandBuffer::SetParseError(gpu::error::Error error) {
-  NOTREACHED();
-}
-
-void InProcessCommandBuffer::SetContextLostReason(
-    gpu::error::ContextLostReason reason) {
-  NOTREACHED();
-}
-
 namespace {
 
 void PostCallback(const scoped_refptr<base::MessageLoopProxy>& loop,