Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / gpu / command_buffer / service / stream_texture_manager_in_process_android.cc
blobddff2e901e13bf14e7f5426126cb148456f0771d
1 // Copyright 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.
5 #include "gpu/command_buffer/service/stream_texture_manager_in_process_android.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "gpu/command_buffer/service/texture_manager.h"
10 #include "ui/gfx/size.h"
11 #include "ui/gl/android/surface_texture.h"
12 #include "ui/gl/gl_bindings.h"
13 #include "ui/gl/gl_image.h"
15 namespace gpu {
17 namespace {
19 // Simply wraps a SurfaceTexture reference as a GLImage.
20 class GLImageImpl : public gfx::GLImage {
21 public:
22 GLImageImpl(const scoped_refptr<gfx::SurfaceTexture>& surface_texture,
23 const base::Closure& release_callback);
25 // implement gfx::GLImage
26 virtual void Destroy(bool have_context) OVERRIDE;
27 virtual gfx::Size GetSize() OVERRIDE;
28 virtual bool BindTexImage(unsigned target) OVERRIDE;
29 virtual void ReleaseTexImage(unsigned target) OVERRIDE;
30 virtual void WillUseTexImage() OVERRIDE;
31 virtual void DidUseTexImage() OVERRIDE {}
32 virtual void WillModifyTexImage() OVERRIDE {}
33 virtual void DidModifyTexImage() OVERRIDE {}
34 virtual bool ScheduleOverlayPlane(gfx::AcceleratedWidget widget,
35 int z_order,
36 gfx::OverlayTransform transform,
37 const gfx::Rect& bounds_rect,
38 const gfx::RectF& crop_rect) OVERRIDE;
40 private:
41 virtual ~GLImageImpl();
43 scoped_refptr<gfx::SurfaceTexture> surface_texture_;
44 base::Closure release_callback_;
46 DISALLOW_COPY_AND_ASSIGN(GLImageImpl);
49 GLImageImpl::GLImageImpl(
50 const scoped_refptr<gfx::SurfaceTexture>& surface_texture,
51 const base::Closure& release_callback)
52 : surface_texture_(surface_texture), release_callback_(release_callback) {}
54 GLImageImpl::~GLImageImpl() {
55 release_callback_.Run();
58 void GLImageImpl::Destroy(bool have_context) {
59 NOTREACHED();
62 gfx::Size GLImageImpl::GetSize() {
63 return gfx::Size();
66 bool GLImageImpl::BindTexImage(unsigned target) {
67 NOTREACHED();
68 return false;
71 void GLImageImpl::ReleaseTexImage(unsigned target) {
72 NOTREACHED();
75 void GLImageImpl::WillUseTexImage() {
76 surface_texture_->UpdateTexImage();
79 bool GLImageImpl::ScheduleOverlayPlane(gfx::AcceleratedWidget widget,
80 int z_order,
81 gfx::OverlayTransform transform,
82 const gfx::Rect& bounds_rect,
83 const gfx::RectF& crop_rect) {
84 NOTREACHED();
85 return false;
88 } // anonymous namespace
90 StreamTextureManagerInProcess::StreamTextureManagerInProcess()
91 : next_id_(1), weak_factory_(this) {}
93 StreamTextureManagerInProcess::~StreamTextureManagerInProcess() {
94 if (!textures_.empty()) {
95 LOG(WARNING) << "Undestroyed surface textures while tearing down "
96 "StreamTextureManager.";
100 GLuint StreamTextureManagerInProcess::CreateStreamTexture(
101 uint32 client_texture_id,
102 gles2::TextureManager* texture_manager) {
103 CalledOnValidThread();
105 gles2::TextureRef* texture = texture_manager->GetTexture(client_texture_id);
107 if (!texture || (texture->texture()->target() &&
108 texture->texture()->target() != GL_TEXTURE_EXTERNAL_OES)) {
109 return 0;
112 scoped_refptr<gfx::SurfaceTexture> surface_texture(
113 gfx::SurfaceTexture::Create(texture->service_id()));
115 uint32 stream_id = next_id_++;
116 base::Closure release_callback =
117 base::Bind(&StreamTextureManagerInProcess::OnReleaseStreamTexture,
118 weak_factory_.GetWeakPtr(), stream_id);
119 scoped_refptr<gfx::GLImage> gl_image(new GLImageImpl(surface_texture,
120 release_callback));
122 gfx::Size size = gl_image->GetSize();
123 texture_manager->SetTarget(texture, GL_TEXTURE_EXTERNAL_OES);
124 texture_manager->SetLevelInfo(texture,
125 GL_TEXTURE_EXTERNAL_OES,
127 GL_RGBA,
128 size.width(),
129 size.height(),
132 GL_RGBA,
133 GL_UNSIGNED_BYTE,
134 true);
135 texture_manager->SetLevelImage(texture, GL_TEXTURE_EXTERNAL_OES, 0, gl_image);
138 base::AutoLock lock(map_lock_);
139 textures_[stream_id] = surface_texture;
142 if (next_id_ == 0)
143 next_id_++;
145 return stream_id;
148 void StreamTextureManagerInProcess::OnReleaseStreamTexture(uint32 stream_id) {
149 CalledOnValidThread();
150 base::AutoLock lock(map_lock_);
151 textures_.erase(stream_id);
154 // This can get called from any thread.
155 scoped_refptr<gfx::SurfaceTexture>
156 StreamTextureManagerInProcess::GetSurfaceTexture(uint32 stream_id) {
157 base::AutoLock lock(map_lock_);
158 TextureMap::const_iterator it = textures_.find(stream_id);
159 if (it != textures_.end())
160 return it->second;
162 return NULL;
165 } // namespace gpu