1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "ui/gl/gl_image_surface_texture.h"
6 
7 #include "base/trace_event/trace_event.h"
8 #include "ui/gl/android/surface_texture.h"
9 
10 namespace gl {
11 
GLImageSurfaceTexture(const gfx::Size & size)12 GLImageSurfaceTexture::GLImageSurfaceTexture(const gfx::Size& size)
13     : size_(size), texture_id_(0) {}
14 
~GLImageSurfaceTexture()15 GLImageSurfaceTexture::~GLImageSurfaceTexture() {
16   DCHECK(thread_checker_.CalledOnValidThread());
17 }
18 
Initialize(SurfaceTexture * surface_texture)19 bool GLImageSurfaceTexture::Initialize(SurfaceTexture* surface_texture) {
20   DCHECK(thread_checker_.CalledOnValidThread());
21   DCHECK(!surface_texture_.get());
22   surface_texture_ = surface_texture;
23   return true;
24 }
25 
GetSize()26 gfx::Size GLImageSurfaceTexture::GetSize() {
27   return size_;
28 }
29 
GetInternalFormat()30 unsigned GLImageSurfaceTexture::GetInternalFormat() { return GL_RGBA; }
31 
GetDataType()32 unsigned GLImageSurfaceTexture::GetDataType() {
33   return GL_UNSIGNED_BYTE;
34 }
35 
BindTexImage(unsigned target)36 bool GLImageSurfaceTexture::BindTexImage(unsigned target) {
37   TRACE_EVENT0("gpu", "GLImageSurfaceTexture::BindTexImage");
38   DCHECK(thread_checker_.CalledOnValidThread());
39 
40   if (target != GL_TEXTURE_EXTERNAL_OES) {
41     LOG(ERROR)
42         << "Surface texture can only be bound to TEXTURE_EXTERNAL_OES target";
43     return false;
44   }
45 
46   GLint texture_id;
47   glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id);
48   DCHECK(texture_id);
49 
50   if (texture_id_ && texture_id_ != texture_id) {
51     LOG(ERROR) << "Surface texture can only be bound to one texture ID";
52     return false;
53   }
54 
55   DCHECK(surface_texture_.get());
56   if (texture_id != texture_id_) {
57     // Note: Surface textures used as gpu memory buffers are created with an
58     // initial dummy texture id of 0. We need to call DetachFromGLContext() here
59     // to detach from the dummy texture before we can attach to a real texture
60     // id. DetachFromGLContext() will delete the texture for the current
61     // attachment point so it's important that this is never called when
62     // attached to a real texture id. Detaching from the dummy texture id should
63     // not cause any problems as the GL should silently ignore 0 when passed to
64     // glDeleteTextures.
65     DCHECK_EQ(0, texture_id_);
66     surface_texture_->DetachFromGLContext();
67 
68     // This will attach the surface texture to the texture currently bound to
69     // GL_TEXTURE_EXTERNAL_OES target.
70     surface_texture_->AttachToGLContext();
71     texture_id_ = texture_id;
72   }
73 
74   surface_texture_->UpdateTexImage();
75   return true;
76 }
77 
CopyTexImage(unsigned target)78 bool GLImageSurfaceTexture::CopyTexImage(unsigned target) {
79   return false;
80 }
81 
CopyTexSubImage(unsigned target,const gfx::Point & offset,const gfx::Rect & rect)82 bool GLImageSurfaceTexture::CopyTexSubImage(unsigned target,
83                                             const gfx::Point& offset,
84                                             const gfx::Rect& rect) {
85   return false;
86 }
87 
ScheduleOverlayPlane(gfx::AcceleratedWidget widget,int z_order,gfx::OverlayTransform transform,const gfx::Rect & bounds_rect,const gfx::RectF & crop_rect,bool enable_blend,std::unique_ptr<gfx::GpuFence> gpu_fence)88 bool GLImageSurfaceTexture::ScheduleOverlayPlane(
89     gfx::AcceleratedWidget widget,
90     int z_order,
91     gfx::OverlayTransform transform,
92     const gfx::Rect& bounds_rect,
93     const gfx::RectF& crop_rect,
94     bool enable_blend,
95     std::unique_ptr<gfx::GpuFence> gpu_fence) {
96   return false;
97 }
98 
OnMemoryDump(base::trace_event::ProcessMemoryDump * pmd,uint64_t process_tracing_id,const std::string & dump_name)99 void GLImageSurfaceTexture::OnMemoryDump(
100     base::trace_event::ProcessMemoryDump* pmd,
101     uint64_t process_tracing_id,
102     const std::string& dump_name) {
103   // TODO(ericrk): Add OnMemoryDump for GLImages. crbug.com/514914
104 }
105 
106 }  // namespace gl
107