Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / service / texture_definition.cc
index 3703f14..9959059 100644 (file)
@@ -4,12 +4,18 @@
 
 #include "gpu/command_buffer/service/texture_definition.h"
 
+#include <list>
+
+#include "base/memory/linked_ptr.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/synchronization/lock.h"
 #include "gpu/command_buffer/service/texture_manager.h"
 #include "ui/gl/gl_image.h"
 #include "ui/gl/gl_implementation.h"
 #include "ui/gl/scoped_binders.h"
 
 #if !defined(OS_MACOSX)
+#include "ui/gl/gl_fence_egl.h"
 #include "ui/gl/gl_surface_egl.h"
 #endif
 
@@ -24,15 +30,19 @@ class GLImageSync : public gfx::GLImage {
                        const gfx::Size& size);
 
   // Implement GLImage.
-  virtual void Destroy() OVERRIDE;
+  virtual void Destroy(bool have_context) OVERRIDE;
   virtual gfx::Size GetSize() OVERRIDE;
   virtual bool BindTexImage(unsigned target) OVERRIDE;
   virtual void ReleaseTexImage(unsigned target) OVERRIDE;
   virtual void WillUseTexImage() OVERRIDE;
   virtual void WillModifyTexImage() OVERRIDE;
   virtual void DidModifyTexImage() OVERRIDE;
-
   virtual void DidUseTexImage() OVERRIDE;
+  virtual bool ScheduleOverlayPlane(gfx::AcceleratedWidget widget,
+                                    int z_order,
+                                    gfx::OverlayTransform transform,
+                                    const gfx::Rect& bounds_rect,
+                                    const gfx::RectF& crop_rect) OVERRIDE;
   virtual void SetReleaseAfterUse() OVERRIDE;
 
  protected:
@@ -57,7 +67,8 @@ GLImageSync::~GLImageSync() {
     buffer_->RemoveClient(this);
 }
 
-void GLImageSync::Destroy() {}
+void GLImageSync::Destroy(bool have_context) {
+}
 
 gfx::Size GLImageSync::GetSize() {
   return size_;
@@ -92,6 +103,15 @@ void GLImageSync::DidModifyTexImage() {
     buffer_->DidWrite(this);
 }
 
+bool GLImageSync::ScheduleOverlayPlane(gfx::AcceleratedWidget widget,
+                                       int z_order,
+                                       gfx::OverlayTransform transform,
+                                       const gfx::Rect& bounds_rect,
+                                       const gfx::RectF& crop_rect) {
+  NOTREACHED();
+  return false;
+}
+
 void GLImageSync::SetReleaseAfterUse() {
   NOTREACHED();
 }
@@ -102,15 +122,34 @@ class NativeImageBufferEGL : public NativeImageBuffer {
   static scoped_refptr<NativeImageBufferEGL> Create(GLuint texture_id);
 
  private:
-  NativeImageBufferEGL(scoped_ptr<gfx::GLFence> write_fence,
-                       EGLDisplay display,
-                       EGLImageKHR image);
+  NativeImageBufferEGL(EGLDisplay display, EGLImageKHR image);
   virtual ~NativeImageBufferEGL();
+  virtual void AddClient(gfx::GLImage* client) OVERRIDE;
+  virtual void RemoveClient(gfx::GLImage* client) OVERRIDE;
+  virtual bool IsClient(gfx::GLImage* client) OVERRIDE;
   virtual void BindToTexture(GLenum target) OVERRIDE;
+  virtual void WillRead(gfx::GLImage* client) OVERRIDE;
+  virtual void WillWrite(gfx::GLImage* client) OVERRIDE;
+  virtual void DidRead(gfx::GLImage* client) OVERRIDE;
+  virtual void DidWrite(gfx::GLImage* client) OVERRIDE;
 
   EGLDisplay egl_display_;
   EGLImageKHR egl_image_;
 
+  base::Lock lock_;
+
+  struct ClientInfo {
+    ClientInfo(gfx::GLImage* client);
+    ~ClientInfo();
+
+    gfx::GLImage* client;
+    bool needs_wait_before_read;
+    linked_ptr<gfx::GLFence> read_fence;
+  };
+  std::list<ClientInfo> client_infos_;
+  scoped_ptr<gfx::GLFence> write_fence_;
+  gfx::GLImage* write_client_;
+
   DISALLOW_COPY_AND_ASSIGN(NativeImageBufferEGL);
 };
 
@@ -139,80 +178,37 @@ scoped_refptr<NativeImageBufferEGL> NativeImageBufferEGL::Create(
   if (egl_image == EGL_NO_IMAGE_KHR)
     return NULL;
 
-  return new NativeImageBufferEGL(
-      make_scoped_ptr(gfx::GLFence::Create()), egl_display, egl_image);
+  return new NativeImageBufferEGL(egl_display, egl_image);
 }
 
-NativeImageBufferEGL::NativeImageBufferEGL(scoped_ptr<gfx::GLFence> write_fence,
-                                           EGLDisplay display,
+NativeImageBufferEGL::ClientInfo::ClientInfo(gfx::GLImage* client)
+    : client(client), needs_wait_before_read(true) {}
+
+NativeImageBufferEGL::ClientInfo::~ClientInfo() {}
+
+NativeImageBufferEGL::NativeImageBufferEGL(EGLDisplay display,
                                            EGLImageKHR image)
-    : NativeImageBuffer(write_fence.Pass()),
+    : NativeImageBuffer(),
       egl_display_(display),
-      egl_image_(image) {
+      egl_image_(image),
+      write_fence_(new gfx::GLFenceEGL(true)),
+      write_client_(NULL) {
   DCHECK(egl_display_ != EGL_NO_DISPLAY);
   DCHECK(egl_image_ != EGL_NO_IMAGE_KHR);
 }
 
 NativeImageBufferEGL::~NativeImageBufferEGL() {
+  DCHECK(client_infos_.empty());
   if (egl_image_ != EGL_NO_IMAGE_KHR)
     eglDestroyImageKHR(egl_display_, egl_image_);
 }
 
-void NativeImageBufferEGL::BindToTexture(GLenum target) {
-  DCHECK(egl_image_ != EGL_NO_IMAGE_KHR);
-  glEGLImageTargetTexture2DOES(target, egl_image_);
-  DCHECK_EQ(static_cast<EGLint>(EGL_SUCCESS), eglGetError());
-  DCHECK_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
-}
-#endif
-
-class NativeImageBufferStub : public NativeImageBuffer {
- public:
-  NativeImageBufferStub() : NativeImageBuffer(scoped_ptr<gfx::GLFence>()) {}
-
- private:
-  virtual ~NativeImageBufferStub() {}
-  virtual void BindToTexture(GLenum target) OVERRIDE {}
-
-  DISALLOW_COPY_AND_ASSIGN(NativeImageBufferStub);
-};
-
-}  // anonymous namespace
-
-// static
-scoped_refptr<NativeImageBuffer> NativeImageBuffer::Create(GLuint texture_id) {
-  switch (gfx::GetGLImplementation()) {
-#if !defined(OS_MACOSX)
-    case gfx::kGLImplementationEGLGLES2:
-      return NativeImageBufferEGL::Create(texture_id);
-#endif
-    case gfx::kGLImplementationMockGL:
-      return new NativeImageBufferStub;
-    default:
-      NOTREACHED();
-      return NULL;
-  }
-}
-
-NativeImageBuffer::ClientInfo::ClientInfo(gfx::GLImage* client)
-    : client(client), needs_wait_before_read(true) {}
-
-NativeImageBuffer::ClientInfo::~ClientInfo() {}
-
-NativeImageBuffer::NativeImageBuffer(scoped_ptr<gfx::GLFence> write_fence)
-    : write_fence_(write_fence.Pass()), write_client_(NULL) {
-}
-
-NativeImageBuffer::~NativeImageBuffer() {
-  DCHECK(client_infos_.empty());
-}
-
-void NativeImageBuffer::AddClient(gfx::GLImage* client) {
+void NativeImageBufferEGL::AddClient(gfx::GLImage* client) {
   base::AutoLock lock(lock_);
   client_infos_.push_back(ClientInfo(client));
 }
 
-void NativeImageBuffer::RemoveClient(gfx::GLImage* client) {
+void NativeImageBufferEGL::RemoveClient(gfx::GLImage* client) {
   base::AutoLock lock(lock_);
   if (write_client_ == client)
     write_client_ = NULL;
@@ -227,7 +223,7 @@ void NativeImageBuffer::RemoveClient(gfx::GLImage* client) {
   NOTREACHED();
 }
 
-bool NativeImageBuffer::IsClient(gfx::GLImage* client) {
+bool NativeImageBufferEGL::IsClient(gfx::GLImage* client) {
   base::AutoLock lock(lock_);
   for (std::list<ClientInfo>::iterator it = client_infos_.begin();
        it != client_infos_.end();
@@ -238,7 +234,14 @@ bool NativeImageBuffer::IsClient(gfx::GLImage* client) {
   return false;
 }
 
-void NativeImageBuffer::WillRead(gfx::GLImage* client) {
+void NativeImageBufferEGL::BindToTexture(GLenum target) {
+  DCHECK(egl_image_ != EGL_NO_IMAGE_KHR);
+  glEGLImageTargetTexture2DOES(target, egl_image_);
+  DCHECK_EQ(static_cast<EGLint>(EGL_SUCCESS), eglGetError());
+  DCHECK_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
+}
+
+void NativeImageBufferEGL::WillRead(gfx::GLImage* client) {
   base::AutoLock lock(lock_);
   if (!write_fence_.get() || write_client_ == client)
     return;
@@ -257,7 +260,7 @@ void NativeImageBuffer::WillRead(gfx::GLImage* client) {
   NOTREACHED();
 }
 
-void NativeImageBuffer::WillWrite(gfx::GLImage* client) {
+void NativeImageBufferEGL::WillWrite(gfx::GLImage* client) {
   base::AutoLock lock(lock_);
   if (write_client_ != client)
     write_fence_->ServerWait();
@@ -270,24 +273,24 @@ void NativeImageBuffer::WillWrite(gfx::GLImage* client) {
   }
 }
 
-void NativeImageBuffer::DidRead(gfx::GLImage* client) {
+void NativeImageBufferEGL::DidRead(gfx::GLImage* client) {
   base::AutoLock lock(lock_);
   for (std::list<ClientInfo>::iterator it = client_infos_.begin();
        it != client_infos_.end();
        it++) {
     if (it->client == client) {
-      it->read_fence = make_linked_ptr(gfx::GLFence::Create());
+      it->read_fence = make_linked_ptr(new gfx::GLFenceEGL(true));
       return;
     }
   }
   NOTREACHED();
 }
 
-void NativeImageBuffer::DidWrite(gfx::GLImage* client) {
+void NativeImageBufferEGL::DidWrite(gfx::GLImage* client) {
   base::AutoLock lock(lock_);
   // Sharing semantics require the client to flush in order to make changes
   // visible to other clients.
-  write_fence_.reset(gfx::GLFence::CreateWithoutFlush());
+  write_fence_.reset(new gfx::GLFenceEGL(false));
   write_client_ = client;
   for (std::list<ClientInfo>::iterator it = client_infos_.begin();
        it != client_infos_.end();
@@ -296,6 +299,43 @@ void NativeImageBuffer::DidWrite(gfx::GLImage* client) {
   }
 }
 
+#endif
+
+class NativeImageBufferStub : public NativeImageBuffer {
+ public:
+  NativeImageBufferStub() : NativeImageBuffer() {}
+
+ private:
+  virtual ~NativeImageBufferStub() {}
+  virtual void AddClient(gfx::GLImage* client) OVERRIDE {}
+  virtual void RemoveClient(gfx::GLImage* client) OVERRIDE {}
+  virtual bool IsClient(gfx::GLImage* client) OVERRIDE { return true; }
+  virtual void BindToTexture(GLenum target) OVERRIDE {}
+  virtual void WillRead(gfx::GLImage* client) OVERRIDE {}
+  virtual void WillWrite(gfx::GLImage* client) OVERRIDE {}
+  virtual void DidRead(gfx::GLImage* client) OVERRIDE {}
+  virtual void DidWrite(gfx::GLImage* client) OVERRIDE {}
+
+  DISALLOW_COPY_AND_ASSIGN(NativeImageBufferStub);
+};
+
+}  // anonymous namespace
+
+// static
+scoped_refptr<NativeImageBuffer> NativeImageBuffer::Create(GLuint texture_id) {
+  switch (gfx::GetGLImplementation()) {
+#if !defined(OS_MACOSX)
+    case gfx::kGLImplementationEGLGLES2:
+      return NativeImageBufferEGL::Create(texture_id);
+#endif
+    case gfx::kGLImplementationMockGL:
+      return new NativeImageBufferStub;
+    default:
+      NOTREACHED();
+      return NULL;
+  }
+}
+
 TextureDefinition::LevelInfo::LevelInfo(GLenum target,
                                         GLenum internal_format,
                                         GLsizei width,