[email protected] | 82efc0d | 2014-03-31 06:45:02 | [diff] [blame] | 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/debug/trace_event.h" |
| 8 | #include "ui/gl/android/surface_texture.h" |
| 9 | #include "ui/gl/android/surface_texture_tracker.h" |
| 10 | |
| 11 | namespace gfx { |
| 12 | |
[email protected] | b63f1d6 | 2014-07-18 15:40:59 | [diff] [blame^] | 13 | GLImageSurfaceTexture::GLImageSurfaceTexture(const gfx::Size& size) |
| 14 | : size_(size), texture_id_(0) { |
| 15 | } |
[email protected] | 82efc0d | 2014-03-31 06:45:02 | [diff] [blame] | 16 | |
| 17 | GLImageSurfaceTexture::~GLImageSurfaceTexture() { Destroy(); } |
| 18 | |
[email protected] | b63f1d6 | 2014-07-18 15:40:59 | [diff] [blame^] | 19 | bool GLImageSurfaceTexture::Initialize( |
| 20 | const gfx::GpuMemoryBufferHandle& handle) { |
[email protected] | 82efc0d | 2014-03-31 06:45:02 | [diff] [blame] | 21 | DCHECK(!surface_texture_); |
| 22 | surface_texture_ = |
| 23 | SurfaceTextureTracker::GetInstance()->AcquireSurfaceTexture( |
[email protected] | b63f1d6 | 2014-07-18 15:40:59 | [diff] [blame^] | 24 | handle.surface_texture_id.primary_id, |
| 25 | handle.surface_texture_id.secondary_id); |
[email protected] | 82efc0d | 2014-03-31 06:45:02 | [diff] [blame] | 26 | return !!surface_texture_; |
| 27 | } |
| 28 | |
| 29 | void GLImageSurfaceTexture::Destroy() { |
| 30 | surface_texture_ = NULL; |
| 31 | texture_id_ = 0; |
| 32 | } |
| 33 | |
| 34 | gfx::Size GLImageSurfaceTexture::GetSize() { return size_; } |
| 35 | |
| 36 | bool GLImageSurfaceTexture::BindTexImage(unsigned target) { |
| 37 | TRACE_EVENT0("gpu", "GLImageSurfaceTexture::BindTexImage"); |
| 38 | |
| 39 | if (target != GL_TEXTURE_EXTERNAL_OES) { |
| 40 | LOG(ERROR) |
| 41 | << "Surface texture can only be bound to TEXTURE_EXTERNAL_OES target"; |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | GLint texture_id; |
| 46 | glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); |
| 47 | DCHECK(texture_id); |
| 48 | |
| 49 | if (texture_id_ && texture_id_ != texture_id) { |
| 50 | LOG(ERROR) << "Surface texture can only be bound to one texture ID"; |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | DCHECK(surface_texture_); |
| 55 | if (texture_id != texture_id_) { |
| 56 | // Note: Surface textures used as gpu memory buffers are created with an |
| 57 | // initial dummy texture id of 0. We need to call DetachFromGLContext() here |
| 58 | // to detach from the dummy texture before we can attach to a real texture |
| 59 | // id. DetachFromGLContext() will delete the texture for the current |
| 60 | // attachment point so it's important that this is never called when |
| 61 | // attached to a real texture id. Detaching from the dummy texture id should |
| 62 | // not cause any problems as the GL should silently ignore 0 when passed to |
| 63 | // glDeleteTextures. |
| 64 | DCHECK_EQ(0, texture_id_); |
| 65 | surface_texture_->DetachFromGLContext(); |
| 66 | |
| 67 | // This will attach the surface texture to the texture currently bound to |
| 68 | // GL_TEXTURE_EXTERNAL_OES target. |
| 69 | surface_texture_->AttachToGLContext(); |
| 70 | texture_id_ = texture_id; |
| 71 | } |
| 72 | |
| 73 | surface_texture_->UpdateTexImage(); |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | } // namespace gfx |