Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / content / common / gpu / client / gpu_memory_buffer_impl_io_surface.cc
index 029ea4f..81cc718 100644 (file)
 
 #include "content/common/gpu/client/gpu_memory_buffer_impl_io_surface.h"
 
+#include "base/atomic_sequence_num.h"
+#include "base/bind.h"
 #include "base/logging.h"
+#include "content/common/gpu/client/gpu_memory_buffer_factory_host.h"
 #include "ui/gl/gl_bindings.h"
 
 namespace content {
+namespace {
+
+base::StaticAtomicSequenceNumber g_next_buffer_id;
+
+void Noop() {
+}
+
+void GpuMemoryBufferCreated(
+    const gfx::Size& size,
+    unsigned internalformat,
+    const GpuMemoryBufferImpl::CreationCallback& callback,
+    const gfx::GpuMemoryBufferHandle& handle) {
+  DCHECK_EQ(gfx::IO_SURFACE_BUFFER, handle.type);
+
+  callback.Run(GpuMemoryBufferImplIOSurface::CreateFromHandle(
+      handle, size, internalformat, base::Bind(&Noop)));
+}
+
+void GpuMemoryBufferCreatedForChildProcess(
+    const GpuMemoryBufferImpl::AllocationCallback& callback,
+    const gfx::GpuMemoryBufferHandle& handle) {
+  DCHECK_EQ(gfx::IO_SURFACE_BUFFER, handle.type);
+
+  callback.Run(handle);
+}
+
+}  // namespace
 
 GpuMemoryBufferImplIOSurface::GpuMemoryBufferImplIOSurface(
     const gfx::Size& size,
-    unsigned internalformat)
-    : GpuMemoryBufferImpl(size, internalformat) {}
+    unsigned internalformat,
+    const DestructionCallback& callback,
+    IOSurfaceRef io_surface)
+    : GpuMemoryBufferImpl(size, internalformat, callback),
+      io_surface_(io_surface) {
+}
 
-GpuMemoryBufferImplIOSurface::~GpuMemoryBufferImplIOSurface() {}
+GpuMemoryBufferImplIOSurface::~GpuMemoryBufferImplIOSurface() {
+}
+
+// static
+void GpuMemoryBufferImplIOSurface::Create(const gfx::Size& size,
+                                          unsigned internalformat,
+                                          unsigned usage,
+                                          int client_id,
+                                          const CreationCallback& callback) {
+  gfx::GpuMemoryBufferHandle handle;
+  handle.type = gfx::IO_SURFACE_BUFFER;
+  handle.global_id.primary_id = g_next_buffer_id.GetNext();
+  handle.global_id.secondary_id = client_id;
+  GpuMemoryBufferFactoryHost::GetInstance()->CreateGpuMemoryBuffer(
+      handle,
+      size,
+      internalformat,
+      usage,
+      base::Bind(&GpuMemoryBufferCreated, size, internalformat, callback));
+}
+
+// static
+void GpuMemoryBufferImplIOSurface::AllocateForChildProcess(
+    const gfx::Size& size,
+    unsigned internalformat,
+    unsigned usage,
+    int child_client_id,
+    const AllocationCallback& callback) {
+  gfx::GpuMemoryBufferHandle handle;
+  handle.type = gfx::IO_SURFACE_BUFFER;
+  handle.global_id.primary_id = g_next_buffer_id.GetNext();
+  handle.global_id.secondary_id = child_client_id;
+  GpuMemoryBufferFactoryHost::GetInstance()->CreateGpuMemoryBuffer(
+      handle,
+      size,
+      internalformat,
+      usage,
+      base::Bind(&GpuMemoryBufferCreatedForChildProcess, callback));
+}
+
+// static
+scoped_ptr<GpuMemoryBufferImpl> GpuMemoryBufferImplIOSurface::CreateFromHandle(
+    const gfx::GpuMemoryBufferHandle& handle,
+    const gfx::Size& size,
+    unsigned internalformat,
+    const DestructionCallback& callback) {
+  DCHECK(IsFormatSupported(internalformat));
+
+  base::ScopedCFTypeRef<IOSurfaceRef> io_surface(
+      IOSurfaceLookup(handle.io_surface_id));
+  if (!io_surface)
+    return scoped_ptr<GpuMemoryBufferImpl>();
+
+  return make_scoped_ptr<GpuMemoryBufferImpl>(new GpuMemoryBufferImplIOSurface(
+      size, internalformat, callback, io_surface.get()));
+}
 
 // static
 bool GpuMemoryBufferImplIOSurface::IsFormatSupported(unsigned internalformat) {
@@ -54,18 +143,6 @@ uint32 GpuMemoryBufferImplIOSurface::PixelFormat(unsigned internalformat) {
   }
 }
 
-bool GpuMemoryBufferImplIOSurface::InitializeFromHandle(
-    const gfx::GpuMemoryBufferHandle& handle) {
-  DCHECK(IsFormatSupported(internalformat_));
-  io_surface_.reset(IOSurfaceLookup(handle.io_surface_id));
-  if (!io_surface_) {
-    VLOG(1) << "IOSurface lookup failed";
-    return false;
-  }
-
-  return true;
-}
-
 void* GpuMemoryBufferImplIOSurface::Map() {
   DCHECK(!mapped_);
   IOSurfaceLock(io_surface_, 0, NULL);