OLD | NEW |
(Empty) | |
| 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 #include "ui/gl/gl_bindings.h" |
| 11 |
| 12 namespace gfx { |
| 13 namespace { |
| 14 |
| 15 // This is admittedly a bit ugly. SurfaceTexture API takes ownership of texture |
| 16 // ids when AttachToGLContext() is called and will delete the texture unless |
| 17 // DetachFromGLContext() is called with no context current. This helper class |
| 18 // can be used to temporarily make no context current for the purpose of |
| 19 // stealing the texture id of a given surface texture. |
| 20 class ScopedNoContextCurrent { |
| 21 public: |
| 22 ScopedNoContextCurrent() |
| 23 : display_(eglGetCurrentDisplay()), |
| 24 context_(eglGetCurrentContext()), |
| 25 draw_surface_(eglGetCurrentSurface(EGL_DRAW)), |
| 26 read_surface_(eglGetCurrentSurface(EGL_READ)) { |
| 27 eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 28 } |
| 29 |
| 30 ~ScopedNoContextCurrent() { |
| 31 eglMakeCurrent(display_, draw_surface_, read_surface_, context_); |
| 32 } |
| 33 |
| 34 private: |
| 35 EGLDisplay display_; |
| 36 EGLContext context_; |
| 37 EGLSurface draw_surface_; |
| 38 EGLSurface read_surface_; |
| 39 }; |
| 40 |
| 41 void DetachFromGLContext(SurfaceTexture* surface_texture) { |
| 42 // Detach with no context current to prevent SurfaceTexture from deleting |
| 43 // currently attached texture id. |
| 44 ScopedNoContextCurrent no_context_current; |
| 45 surface_texture->DetachFromGLContext(); |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
| 50 GLImageSurfaceTexture::GLImageSurfaceTexture(gfx::Size size) |
| 51 : size_(size), texture_id_(0) {} |
| 52 |
| 53 GLImageSurfaceTexture::~GLImageSurfaceTexture() { Destroy(); } |
| 54 |
| 55 bool GLImageSurfaceTexture::Initialize(gfx::GpuMemoryBufferHandle buffer) { |
| 56 DCHECK(!surface_texture_); |
| 57 surface_texture_ = |
| 58 SurfaceTextureTracker::GetInstance()->AcquireSurfaceTexture( |
| 59 buffer.surface_texture_id.primary_id, |
| 60 buffer.surface_texture_id.secondary_id); |
| 61 return !!surface_texture_; |
| 62 } |
| 63 |
| 64 void GLImageSurfaceTexture::Destroy() { |
| 65 surface_texture_ = NULL; |
| 66 texture_id_ = 0; |
| 67 } |
| 68 |
| 69 gfx::Size GLImageSurfaceTexture::GetSize() { return size_; } |
| 70 |
| 71 bool GLImageSurfaceTexture::BindTexImage(unsigned target) { |
| 72 TRACE_EVENT0("gpu", "GLImageSurfaceTexture::BindTexImage"); |
| 73 |
| 74 if (target != GL_TEXTURE_EXTERNAL_OES) { |
| 75 LOG(ERROR) |
| 76 << "Surface texture can only be bound to TEXTURE_EXTERNAL_OES target"; |
| 77 return false; |
| 78 } |
| 79 |
| 80 int texture_id; |
| 81 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); |
| 82 DCHECK(texture_id); |
| 83 |
| 84 if (texture_id_ && texture_id_ != texture_id) { |
| 85 LOG(ERROR) << "Surface texture can only be bound to one texture ID"; |
| 86 return false; |
| 87 } |
| 88 |
| 89 DCHECK(surface_texture_); |
| 90 if (texture_id != texture_id_) { |
| 91 DetachFromGLContext(surface_texture_.get()); |
| 92 surface_texture_->AttachToGLContext(); |
| 93 texture_id_ = texture_id; |
| 94 } |
| 95 |
| 96 surface_texture_->UpdateTexImage(); |
| 97 return true; |
| 98 } |
| 99 |
| 100 } // namespace gfx |
OLD | NEW |