[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 | |
[email protected] | d2eaf52f | 2014-07-31 15:01:24 | [diff] [blame] | 17 | GLImageSurfaceTexture::~GLImageSurfaceTexture() { |
| 18 | DCHECK(!surface_texture_); |
| 19 | DCHECK_EQ(0, texture_id_); |
| 20 | } |
[email protected] | 82efc0d | 2014-03-31 06:45:02 | [diff] [blame] | 21 | |
[email protected] | b63f1d6 | 2014-07-18 15:40:59 | [diff] [blame] | 22 | bool GLImageSurfaceTexture::Initialize( |
| 23 | const gfx::GpuMemoryBufferHandle& handle) { |
[email protected] | 82efc0d | 2014-03-31 06:45:02 | [diff] [blame] | 24 | DCHECK(!surface_texture_); |
| 25 | surface_texture_ = |
| 26 | SurfaceTextureTracker::GetInstance()->AcquireSurfaceTexture( |
[email protected] | b63f1d6 | 2014-07-18 15:40:59 | [diff] [blame] | 27 | handle.surface_texture_id.primary_id, |
| 28 | handle.surface_texture_id.secondary_id); |
[email protected] | 82efc0d | 2014-03-31 06:45:02 | [diff] [blame] | 29 | return !!surface_texture_; |
| 30 | } |
| 31 | |
[email protected] | d2eaf52f | 2014-07-31 15:01:24 | [diff] [blame] | 32 | void GLImageSurfaceTexture::Destroy(bool have_context) { |
[email protected] | 82efc0d | 2014-03-31 06:45:02 | [diff] [blame] | 33 | surface_texture_ = NULL; |
| 34 | texture_id_ = 0; |
| 35 | } |
| 36 | |
| 37 | gfx::Size GLImageSurfaceTexture::GetSize() { return size_; } |
| 38 | |
| 39 | bool GLImageSurfaceTexture::BindTexImage(unsigned target) { |
| 40 | TRACE_EVENT0("gpu", "GLImageSurfaceTexture::BindTexImage"); |
| 41 | |
| 42 | if (target != GL_TEXTURE_EXTERNAL_OES) { |
| 43 | LOG(ERROR) |
| 44 | << "Surface texture can only be bound to TEXTURE_EXTERNAL_OES target"; |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | GLint texture_id; |
| 49 | glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); |
| 50 | DCHECK(texture_id); |
| 51 | |
| 52 | if (texture_id_ && texture_id_ != texture_id) { |
| 53 | LOG(ERROR) << "Surface texture can only be bound to one texture ID"; |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | DCHECK(surface_texture_); |
| 58 | if (texture_id != texture_id_) { |
| 59 | // Note: Surface textures used as gpu memory buffers are created with an |
| 60 | // initial dummy texture id of 0. We need to call DetachFromGLContext() here |
| 61 | // to detach from the dummy texture before we can attach to a real texture |
| 62 | // id. DetachFromGLContext() will delete the texture for the current |
| 63 | // attachment point so it's important that this is never called when |
| 64 | // attached to a real texture id. Detaching from the dummy texture id should |
| 65 | // not cause any problems as the GL should silently ignore 0 when passed to |
| 66 | // glDeleteTextures. |
| 67 | DCHECK_EQ(0, texture_id_); |
| 68 | surface_texture_->DetachFromGLContext(); |
| 69 | |
| 70 | // This will attach the surface texture to the texture currently bound to |
| 71 | // GL_TEXTURE_EXTERNAL_OES target. |
| 72 | surface_texture_->AttachToGLContext(); |
| 73 | texture_id_ = texture_id; |
| 74 | } |
| 75 | |
| 76 | surface_texture_->UpdateTexImage(); |
| 77 | return true; |
| 78 | } |
| 79 | |
reveman | ce8fbe8 | 2014-09-05 02:19:52 | [diff] [blame^] | 80 | bool GLImageSurfaceTexture::CopyTexImage(unsigned target) { |
| 81 | return false; |
| 82 | } |
| 83 | |
[email protected] | 5e57db7 | 2014-08-01 21:50:17 | [diff] [blame] | 84 | bool GLImageSurfaceTexture::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, |
| 85 | int z_order, |
| 86 | OverlayTransform transform, |
| 87 | const Rect& bounds_rect, |
| 88 | const RectF& crop_rect) { |
| 89 | return false; |
| 90 | } |
| 91 | |
[email protected] | 82efc0d | 2014-03-31 06:45:02 | [diff] [blame] | 92 | } // namespace gfx |