Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / gpu / command_buffer / service / stream_texture_manager_in_process_android.cc
blobe7e3a1f55a5ceaa00d71782631dc2104dfeb1510
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/geometry/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 void Destroy(bool have_context) override;
27 gfx::Size GetSize() override;
28 unsigned GetInternalFormat() override;
29 bool BindTexImage(unsigned target) override;
30 void ReleaseTexImage(unsigned target) override;
31 bool CopyTexSubImage(unsigned target,
32 const gfx::Point& offset,
33 const gfx::Rect& rect) override;
34 void WillUseTexImage() override;
35 void DidUseTexImage() override {}
36 void WillModifyTexImage() override {}
37 void DidModifyTexImage() override {}
38 bool ScheduleOverlayPlane(gfx::AcceleratedWidget widget,
39 int z_order,
40 gfx::OverlayTransform transform,
41 const gfx::Rect& bounds_rect,
42 const gfx::RectF& crop_rect) override;
43 void OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd,
44 uint64_t process_tracing_id,
45 const std::string& dump_name) override;
47 private:
48 ~GLImageImpl() override;
50 scoped_refptr<gfx::SurfaceTexture> surface_texture_;
51 base::Closure release_callback_;
53 DISALLOW_COPY_AND_ASSIGN(GLImageImpl);
56 GLImageImpl::GLImageImpl(
57 const scoped_refptr<gfx::SurfaceTexture>& surface_texture,
58 const base::Closure& release_callback)
59 : surface_texture_(surface_texture), release_callback_(release_callback) {}
61 GLImageImpl::~GLImageImpl() {
62 release_callback_.Run();
65 void GLImageImpl::Destroy(bool have_context) {
66 NOTREACHED();
69 gfx::Size GLImageImpl::GetSize() {
70 return gfx::Size();
73 unsigned GLImageImpl::GetInternalFormat() {
74 return GL_RGBA;
77 bool GLImageImpl::BindTexImage(unsigned target) {
78 NOTREACHED();
79 return false;
82 void GLImageImpl::ReleaseTexImage(unsigned target) {
83 NOTREACHED();
86 bool GLImageImpl::CopyTexSubImage(unsigned target,
87 const gfx::Point& offset,
88 const gfx::Rect& rect) {
89 return false;
92 void GLImageImpl::WillUseTexImage() {
93 surface_texture_->UpdateTexImage();
96 bool GLImageImpl::ScheduleOverlayPlane(gfx::AcceleratedWidget widget,
97 int z_order,
98 gfx::OverlayTransform transform,
99 const gfx::Rect& bounds_rect,
100 const gfx::RectF& crop_rect) {
101 NOTREACHED();
102 return false;
105 void GLImageImpl::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd,
106 uint64_t process_tracing_id,
107 const std::string& dump_name) {
108 // TODO(ericrk): Implement GLImage OnMemoryDump. crbug.com/514914
111 } // anonymous namespace
113 StreamTextureManagerInProcess::StreamTextureManagerInProcess()
114 : next_id_(1), weak_factory_(this) {}
116 StreamTextureManagerInProcess::~StreamTextureManagerInProcess() {
117 if (!textures_.empty()) {
118 LOG(WARNING) << "Undestroyed surface textures while tearing down "
119 "StreamTextureManager.";
123 GLuint StreamTextureManagerInProcess::CreateStreamTexture(
124 uint32 client_texture_id,
125 gles2::TextureManager* texture_manager) {
126 CalledOnValidThread();
128 gles2::TextureRef* texture = texture_manager->GetTexture(client_texture_id);
130 if (!texture || (texture->texture()->target() &&
131 texture->texture()->target() != GL_TEXTURE_EXTERNAL_OES)) {
132 return 0;
135 scoped_refptr<gfx::SurfaceTexture> surface_texture(
136 gfx::SurfaceTexture::Create(texture->service_id()));
138 uint32 stream_id = next_id_++;
139 base::Closure release_callback =
140 base::Bind(&StreamTextureManagerInProcess::OnReleaseStreamTexture,
141 weak_factory_.GetWeakPtr(), stream_id);
142 scoped_refptr<gfx::GLImage> gl_image(new GLImageImpl(surface_texture,
143 release_callback));
145 gfx::Size size = gl_image->GetSize();
146 texture_manager->SetTarget(texture, GL_TEXTURE_EXTERNAL_OES);
147 texture_manager->SetLevelInfo(texture, GL_TEXTURE_EXTERNAL_OES, 0, GL_RGBA,
148 size.width(), size.height(), 1, 0, GL_RGBA,
149 GL_UNSIGNED_BYTE, gfx::Rect(size));
150 texture_manager->SetLevelImage(
151 texture, GL_TEXTURE_EXTERNAL_OES, 0, gl_image.get());
154 base::AutoLock lock(map_lock_);
155 textures_[stream_id] = surface_texture;
158 if (next_id_ == 0)
159 next_id_++;
161 return stream_id;
164 void StreamTextureManagerInProcess::OnReleaseStreamTexture(uint32 stream_id) {
165 CalledOnValidThread();
166 base::AutoLock lock(map_lock_);
167 textures_.erase(stream_id);
170 // This can get called from any thread.
171 scoped_refptr<gfx::SurfaceTexture>
172 StreamTextureManagerInProcess::GetSurfaceTexture(uint32 stream_id) {
173 base::AutoLock lock(map_lock_);
174 TextureMap::const_iterator it = textures_.find(stream_id);
175 if (it != textures_.end())
176 return it->second;
178 return NULL;
181 } // namespace gpu