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.
5 #include "ui/gl/gl_image_surface_texture.h"
7 #include "base/trace_event/trace_event.h"
8 #include "ui/gl/android/surface_texture.h"
12 GLImageSurfaceTexture::GLImageSurfaceTexture(const gfx::Size
& size
)
13 : size_(size
), texture_id_(0) {
16 GLImageSurfaceTexture::~GLImageSurfaceTexture() {
17 DCHECK(thread_checker_
.CalledOnValidThread());
18 DCHECK(!surface_texture_
.get());
19 DCHECK_EQ(0, texture_id_
);
22 bool GLImageSurfaceTexture::Initialize(SurfaceTexture
* surface_texture
) {
23 DCHECK(thread_checker_
.CalledOnValidThread());
24 DCHECK(!surface_texture_
.get());
25 surface_texture_
= surface_texture
;
29 void GLImageSurfaceTexture::Destroy(bool have_context
) {
30 DCHECK(thread_checker_
.CalledOnValidThread());
31 surface_texture_
= NULL
;
35 gfx::Size
GLImageSurfaceTexture::GetSize() { return size_
; }
37 unsigned GLImageSurfaceTexture::GetInternalFormat() { return GL_RGBA
; }
39 bool GLImageSurfaceTexture::BindTexImage(unsigned target
) {
40 TRACE_EVENT0("gpu", "GLImageSurfaceTexture::BindTexImage");
41 DCHECK(thread_checker_
.CalledOnValidThread());
43 if (target
!= GL_TEXTURE_EXTERNAL_OES
) {
45 << "Surface texture can only be bound to TEXTURE_EXTERNAL_OES target";
50 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES
, &texture_id
);
53 if (texture_id_
&& texture_id_
!= texture_id
) {
54 LOG(ERROR
) << "Surface texture can only be bound to one texture ID";
58 DCHECK(surface_texture_
.get());
59 if (texture_id
!= texture_id_
) {
60 // Note: Surface textures used as gpu memory buffers are created with an
61 // initial dummy texture id of 0. We need to call DetachFromGLContext() here
62 // to detach from the dummy texture before we can attach to a real texture
63 // id. DetachFromGLContext() will delete the texture for the current
64 // attachment point so it's important that this is never called when
65 // attached to a real texture id. Detaching from the dummy texture id should
66 // not cause any problems as the GL should silently ignore 0 when passed to
68 DCHECK_EQ(0, texture_id_
);
69 surface_texture_
->DetachFromGLContext();
71 // This will attach the surface texture to the texture currently bound to
72 // GL_TEXTURE_EXTERNAL_OES target.
73 surface_texture_
->AttachToGLContext();
74 texture_id_
= texture_id
;
77 surface_texture_
->UpdateTexImage();
81 bool GLImageSurfaceTexture::CopyTexSubImage(unsigned target
,
87 bool GLImageSurfaceTexture::ScheduleOverlayPlane(gfx::AcceleratedWidget widget
,
89 OverlayTransform transform
,
90 const Rect
& bounds_rect
,
91 const RectF
& crop_rect
) {