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/debug/trace_event.h"
8 #include "ui/gl/android/surface_texture.h"
9 #include "ui/gl/android/surface_texture_tracker.h"
13 GLImageSurfaceTexture::GLImageSurfaceTexture(gfx::Size size
)
14 : size_(size
), texture_id_(0) {}
16 GLImageSurfaceTexture::~GLImageSurfaceTexture() { Destroy(); }
18 bool GLImageSurfaceTexture::Initialize(gfx::GpuMemoryBufferHandle buffer
) {
19 DCHECK(!surface_texture_
);
21 SurfaceTextureTracker::GetInstance()->AcquireSurfaceTexture(
22 buffer
.surface_texture_id
.primary_id
,
23 buffer
.surface_texture_id
.secondary_id
);
24 return !!surface_texture_
;
27 void GLImageSurfaceTexture::Destroy() {
28 surface_texture_
= NULL
;
32 gfx::Size
GLImageSurfaceTexture::GetSize() { return size_
; }
34 bool GLImageSurfaceTexture::BindTexImage(unsigned target
) {
35 TRACE_EVENT0("gpu", "GLImageSurfaceTexture::BindTexImage");
37 if (target
!= GL_TEXTURE_EXTERNAL_OES
) {
39 << "Surface texture can only be bound to TEXTURE_EXTERNAL_OES target";
44 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES
, &texture_id
);
47 if (texture_id_
&& texture_id_
!= texture_id
) {
48 LOG(ERROR
) << "Surface texture can only be bound to one texture ID";
52 DCHECK(surface_texture_
);
53 if (texture_id
!= texture_id_
) {
54 // Note: Surface textures used as gpu memory buffers are created with an
55 // initial dummy texture id of 0. We need to call DetachFromGLContext() here
56 // to detach from the dummy texture before we can attach to a real texture
57 // id. DetachFromGLContext() will delete the texture for the current
58 // attachment point so it's important that this is never called when
59 // attached to a real texture id. Detaching from the dummy texture id should
60 // not cause any problems as the GL should silently ignore 0 when passed to
62 DCHECK_EQ(0, texture_id_
);
63 surface_texture_
->DetachFromGLContext();
65 // This will attach the surface texture to the texture currently bound to
66 // GL_TEXTURE_EXTERNAL_OES target.
67 surface_texture_
->AttachToGLContext();
68 texture_id_
= texture_id
;
71 surface_texture_
->UpdateTexImage();