Chromium Code Reviews| 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( | |
|
no sievers
2014/03/18 20:58:13
This handle comes from the client, so we also need
reveman
2014/03/24 18:35:07
Gpu memory buffers are global on other platforms.
| |
| 59 buffer.surface_texture_id); | |
| 60 return !!surface_texture_; | |
| 61 } | |
| 62 | |
| 63 void GLImageSurfaceTexture::Destroy() { | |
| 64 surface_texture_ = NULL; | |
| 65 texture_id_ = 0; | |
| 66 } | |
| 67 | |
| 68 gfx::Size GLImageSurfaceTexture::GetSize() { return size_; } | |
| 69 | |
| 70 bool GLImageSurfaceTexture::BindTexImage(unsigned target) { | |
| 71 TRACE_EVENT0("gpu", "GLImageSurfaceTexture::BindTexImage"); | |
| 72 | |
| 73 if (target != GL_TEXTURE_EXTERNAL_OES) { | |
| 74 LOG(ERROR) | |
| 75 << "Surface texture can only be bound to TEXTURE_EXTERNAL_OES target"; | |
| 76 return false; | |
| 77 } | |
| 78 | |
| 79 int texture_id; | |
| 80 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); | |
| 81 DCHECK(texture_id); | |
| 82 | |
| 83 if (texture_id_ && texture_id_ != texture_id) { | |
| 84 LOG(ERROR) << "Surface texture can only be bound to one texture ID"; | |
| 85 return false; | |
| 86 } | |
| 87 | |
| 88 DCHECK(surface_texture_); | |
| 89 if (texture_id != texture_id_) { | |
| 90 DetachFromGLContext(surface_texture_.get()); | |
| 91 surface_texture_->AttachToGLContext(); | |
| 92 texture_id_ = texture_id; | |
| 93 } | |
| 94 | |
| 95 surface_texture_->UpdateTexImage(); | |
|
no sievers
2014/03/18 20:58:13
So whenever we updated the contents from the clien
reveman
2014/03/24 18:35:07
Yes, no guarantee that updated contents will be us
| |
| 96 return true; | |
| 97 } | |
| 98 | |
| 99 } // namespace gfx | |
| OLD | NEW |