OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/android/gl_image_stream.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "ui/gfx/size.h" |
| 9 #include "ui/gl/android/surface_texture_bridge.h" |
| 10 #include "ui/gl/gl_bindings.h" |
| 11 |
| 12 namespace gfx { |
| 13 |
| 14 GLImageStream::GLImageStream() |
| 15 : has_updated_(false), |
| 16 weak_factory_(this) { |
| 17 memset(¤t_matrix_, 0, sizeof(current_matrix_)); |
| 18 } |
| 19 |
| 20 GLImageStream::~GLImageStream() {} |
| 21 |
| 22 void GLImageStream::Destroy() { |
| 23 } |
| 24 |
| 25 gfx::Size GLImageStream::GetSize() { |
| 26 return size_; |
| 27 } |
| 28 |
| 29 void GLImageStream::WillUseTexImage() { |
| 30 GLint texture_id = 0; |
| 31 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); |
| 32 surface_texture_bridge_->UpdateTexImage(); |
| 33 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_id); |
| 34 if (matrix_callback_.is_null()) |
| 35 return; |
| 36 |
| 37 Matrix new_matrix; |
| 38 surface_texture_bridge_->GetTransformMatrix(new_matrix.components); |
| 39 |
| 40 // Only query the matrix once we have bound a valid frame. |
| 41 if (has_updated_ && |
| 42 memcmp(¤t_matrix_, &new_matrix, sizeof(new_matrix)) != 0) { |
| 43 memcpy(¤t_matrix_, &new_matrix, sizeof(new_matrix)); |
| 44 matrix_callback_.Run(new_matrix); |
| 45 } |
| 46 } |
| 47 |
| 48 GLImageStream* GLImageStream::AsGLImageStream() { |
| 49 return this; |
| 50 } |
| 51 |
| 52 SurfaceTextureBridge* GLImageStream::GetSurfaceTexture() { |
| 53 return surface_texture_bridge_; |
| 54 } |
| 55 |
| 56 void GLImageStream::SetFrameAvailableCallback(const base::Closure& callback) { |
| 57 frame_callback_ = callback; |
| 58 // Use a weak pointer here to avoid a circular reference between |
| 59 // |this| and surface_texture_bridge_. |
| 60 base::Closure frame_cb = |
| 61 base::Bind(&GLImageStream::OnFrameAvailable, weak_factory_.GetWeakPtr()); |
| 62 surface_texture_bridge_->SetFrameAvailableCallback(frame_cb); |
| 63 } |
| 64 |
| 65 void GLImageStream::OnFrameAvailable() { |
| 66 has_updated_ = true; |
| 67 frame_callback_.Run(); |
| 68 } |
| 69 |
| 70 bool GLImageStream::BindTexImage() { |
| 71 if (!surface_texture_bridge_) { |
| 72 GLint texture_id = 0; |
| 73 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); |
| 74 if (!texture_id) |
| 75 return false; |
| 76 |
| 77 surface_texture_bridge_(new gfx::SurfaceTextureBridge(texture_id)); |
| 78 return true; |
| 79 } |
| 80 surface_texture_bridge_->AttachToGLContext(); |
| 81 return true; |
| 82 } |
| 83 |
| 84 void GLImageStream::ReleaseTexImage() { |
| 85 surface_texture_bridge_->DetachFromGLContext(); |
| 86 } |
| 87 |
| 88 } // namespace gfx |
OLD | NEW |