Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / content / common / gpu / client / gpu_memory_buffer_impl_shared_memory.cc
index b038dc3..b4ee128 100644 (file)
@@ -4,40 +4,90 @@
 
 #include "content/common/gpu/client/gpu_memory_buffer_impl_shared_memory.h"
 
+#include "base/bind.h"
 #include "base/numerics/safe_math.h"
 #include "ui/gl/gl_bindings.h"
 
 namespace content {
+namespace {
+
+void Noop() {
+}
+
+}  // namespace
 
 GpuMemoryBufferImplSharedMemory::GpuMemoryBufferImplSharedMemory(
     const gfx::Size& size,
-    unsigned internalformat)
-    : GpuMemoryBufferImpl(size, internalformat) {
+    unsigned internalformat,
+    const DestructionCallback& callback,
+    scoped_ptr<base::SharedMemory> shared_memory)
+    : GpuMemoryBufferImpl(size, internalformat, callback),
+      shared_memory_(shared_memory.Pass()) {
 }
 
 GpuMemoryBufferImplSharedMemory::~GpuMemoryBufferImplSharedMemory() {
 }
 
 // static
-void GpuMemoryBufferImplSharedMemory::AllocateSharedMemoryForChildProcess(
+void GpuMemoryBufferImplSharedMemory::Create(const gfx::Size& size,
+                                             unsigned internalformat,
+                                             unsigned usage,
+                                             const CreationCallback& callback) {
+  DCHECK(GpuMemoryBufferImplSharedMemory::IsConfigurationSupported(
+      size, internalformat, usage));
+
+  scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory());
+  if (!shared_memory->CreateAnonymous(size.GetArea() *
+                                      BytesPerPixel(internalformat))) {
+    callback.Run(scoped_ptr<GpuMemoryBufferImpl>());
+    return;
+  }
+
+  callback.Run(
+      make_scoped_ptr<GpuMemoryBufferImpl>(new GpuMemoryBufferImplSharedMemory(
+          size, internalformat, base::Bind(&Noop), shared_memory.Pass())));
+}
+
+// static
+void GpuMemoryBufferImplSharedMemory::AllocateForChildProcess(
     const gfx::Size& size,
     unsigned internalformat,
     base::ProcessHandle child_process,
     const AllocationCallback& callback) {
   DCHECK(IsLayoutSupported(size, internalformat));
-  gfx::GpuMemoryBufferHandle handle;
   base::SharedMemory shared_memory;
   if (!shared_memory.CreateAnonymous(size.GetArea() *
                                      BytesPerPixel(internalformat))) {
-    handle.type = gfx::EMPTY_BUFFER;
+    callback.Run(gfx::GpuMemoryBufferHandle());
     return;
   }
+  gfx::GpuMemoryBufferHandle handle;
   handle.type = gfx::SHARED_MEMORY_BUFFER;
   shared_memory.GiveToProcess(child_process, &handle.handle);
   callback.Run(handle);
 }
 
 // static
+scoped_ptr<GpuMemoryBufferImpl>
+GpuMemoryBufferImplSharedMemory::CreateFromHandle(
+    const gfx::GpuMemoryBufferHandle& handle,
+    const gfx::Size& size,
+    unsigned internalformat,
+    const DestructionCallback& callback) {
+  DCHECK(IsLayoutSupported(size, internalformat));
+
+  if (!base::SharedMemory::IsHandleValid(handle.handle))
+    return scoped_ptr<GpuMemoryBufferImpl>();
+
+  return make_scoped_ptr<GpuMemoryBufferImpl>(
+      new GpuMemoryBufferImplSharedMemory(
+          size,
+          internalformat,
+          callback,
+          make_scoped_ptr(new base::SharedMemory(handle.handle, false))));
+}
+
+// static
 bool GpuMemoryBufferImplSharedMemory::IsLayoutSupported(
     const gfx::Size& size,
     unsigned internalformat) {
@@ -65,27 +115,6 @@ bool GpuMemoryBufferImplSharedMemory::IsConfigurationSupported(
   return IsLayoutSupported(size, internalformat) && IsUsageSupported(usage);
 }
 
-bool GpuMemoryBufferImplSharedMemory::Initialize() {
-  DCHECK(IsLayoutSupported(size_, internalformat_));
-  scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory());
-  if (!shared_memory->CreateAnonymous(size_.GetArea() *
-                                      BytesPerPixel(internalformat_)))
-    return false;
-  shared_memory_ = shared_memory.Pass();
-  DCHECK(!shared_memory_->memory());
-  return true;
-}
-
-bool GpuMemoryBufferImplSharedMemory::InitializeFromHandle(
-    const gfx::GpuMemoryBufferHandle& handle) {
-  DCHECK(IsLayoutSupported(size_, internalformat_));
-  if (!base::SharedMemory::IsHandleValid(handle.handle))
-    return false;
-  shared_memory_.reset(new base::SharedMemory(handle.handle, false));
-  DCHECK(!shared_memory_->memory());
-  return true;
-}
-
 void* GpuMemoryBufferImplSharedMemory::Map() {
   DCHECK(!mapped_);
   if (!shared_memory_->Map(size_.GetArea() * BytesPerPixel(internalformat_)))