Handle account removal correctly on all platforms.
[chromium-blink-merge.git] / gpu / command_buffer / service / stream_texture_manager_in_process_android.cc
blobb59cf5ce40f46b52562f2e8647601cd258878415
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 bool CopyTexImage(unsigned target) OVERRIDE;
31 virtual void WillUseTexImage() OVERRIDE;
32 virtual void DidUseTexImage() OVERRIDE {}
33 virtual void WillModifyTexImage() OVERRIDE {}
34 virtual void DidModifyTexImage() OVERRIDE {}
35 virtual bool ScheduleOverlayPlane(gfx::AcceleratedWidget widget,
36 int z_order,
37 gfx::OverlayTransform transform,
38 const gfx::Rect& bounds_rect,
39 const gfx::RectF& crop_rect) OVERRIDE;
41 private:
42 virtual ~GLImageImpl();
44 scoped_refptr<gfx::SurfaceTexture> surface_texture_;
45 base::Closure release_callback_;
47 DISALLOW_COPY_AND_ASSIGN(GLImageImpl);
50 GLImageImpl::GLImageImpl(
51 const scoped_refptr<gfx::SurfaceTexture>& surface_texture,
52 const base::Closure& release_callback)
53 : surface_texture_(surface_texture), release_callback_(release_callback) {}
55 GLImageImpl::~GLImageImpl() {
56 release_callback_.Run();
59 void GLImageImpl::Destroy(bool have_context) {
60 NOTREACHED();
63 gfx::Size GLImageImpl::GetSize() {
64 return gfx::Size();
67 bool GLImageImpl::BindTexImage(unsigned target) {
68 NOTREACHED();
69 return false;
72 void GLImageImpl::ReleaseTexImage(unsigned target) {
73 NOTREACHED();
76 bool GLImageImpl::CopyTexImage(unsigned target) {
77 return false;
80 void GLImageImpl::WillUseTexImage() {
81 surface_texture_->UpdateTexImage();
84 bool GLImageImpl::ScheduleOverlayPlane(gfx::AcceleratedWidget widget,
85 int z_order,
86 gfx::OverlayTransform transform,
87 const gfx::Rect& bounds_rect,
88 const gfx::RectF& crop_rect) {
89 NOTREACHED();
90 return false;
93 } // anonymous namespace
95 StreamTextureManagerInProcess::StreamTextureManagerInProcess()
96 : next_id_(1), weak_factory_(this) {}
98 StreamTextureManagerInProcess::~StreamTextureManagerInProcess() {
99 if (!textures_.empty()) {
100 LOG(WARNING) << "Undestroyed surface textures while tearing down "
101 "StreamTextureManager.";
105 GLuint StreamTextureManagerInProcess::CreateStreamTexture(
106 uint32 client_texture_id,
107 gles2::TextureManager* texture_manager) {
108 CalledOnValidThread();
110 gles2::TextureRef* texture = texture_manager->GetTexture(client_texture_id);
112 if (!texture || (texture->texture()->target() &&
113 texture->texture()->target() != GL_TEXTURE_EXTERNAL_OES)) {
114 return 0;
117 scoped_refptr<gfx::SurfaceTexture> surface_texture(
118 gfx::SurfaceTexture::Create(texture->service_id()));
120 uint32 stream_id = next_id_++;
121 base::Closure release_callback =
122 base::Bind(&StreamTextureManagerInProcess::OnReleaseStreamTexture,
123 weak_factory_.GetWeakPtr(), stream_id);
124 scoped_refptr<gfx::GLImage> gl_image(new GLImageImpl(surface_texture,
125 release_callback));
127 gfx::Size size = gl_image->GetSize();
128 texture_manager->SetTarget(texture, GL_TEXTURE_EXTERNAL_OES);
129 texture_manager->SetLevelInfo(texture,
130 GL_TEXTURE_EXTERNAL_OES,
132 GL_RGBA,
133 size.width(),
134 size.height(),
137 GL_RGBA,
138 GL_UNSIGNED_BYTE,
139 true);
140 texture_manager->SetLevelImage(texture, GL_TEXTURE_EXTERNAL_OES, 0, gl_image);
143 base::AutoLock lock(map_lock_);
144 textures_[stream_id] = surface_texture;
147 if (next_id_ == 0)
148 next_id_++;
150 return stream_id;
153 void StreamTextureManagerInProcess::OnReleaseStreamTexture(uint32 stream_id) {
154 CalledOnValidThread();
155 base::AutoLock lock(map_lock_);
156 textures_.erase(stream_id);
159 // This can get called from any thread.
160 scoped_refptr<gfx::SurfaceTexture>
161 StreamTextureManagerInProcess::GetSurfaceTexture(uint32 stream_id) {
162 base::AutoLock lock(map_lock_);
163 TextureMap::const_iterator it = textures_.find(stream_id);
164 if (it != textures_.end())
165 return it->second;
167 return NULL;
170 } // namespace gpu