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"
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"
19 // Simply wraps a SurfaceTexture reference as a GLImage.
20 class GLImageImpl
: public gfx::GLImage
{
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
,
40 gfx::OverlayTransform transform
,
41 const gfx::Rect
& bounds_rect
,
42 const gfx::RectF
& crop_rect
) override
;
45 ~GLImageImpl() override
;
47 scoped_refptr
<gfx::SurfaceTexture
> surface_texture_
;
48 base::Closure release_callback_
;
50 DISALLOW_COPY_AND_ASSIGN(GLImageImpl
);
53 GLImageImpl::GLImageImpl(
54 const scoped_refptr
<gfx::SurfaceTexture
>& surface_texture
,
55 const base::Closure
& release_callback
)
56 : surface_texture_(surface_texture
), release_callback_(release_callback
) {}
58 GLImageImpl::~GLImageImpl() {
59 release_callback_
.Run();
62 void GLImageImpl::Destroy(bool have_context
) {
66 gfx::Size
GLImageImpl::GetSize() {
70 unsigned GLImageImpl::GetInternalFormat() {
74 bool GLImageImpl::BindTexImage(unsigned target
) {
79 void GLImageImpl::ReleaseTexImage(unsigned target
) {
83 bool GLImageImpl::CopyTexSubImage(unsigned target
,
84 const gfx::Point
& offset
,
85 const gfx::Rect
& rect
) {
89 void GLImageImpl::WillUseTexImage() {
90 surface_texture_
->UpdateTexImage();
93 bool GLImageImpl::ScheduleOverlayPlane(gfx::AcceleratedWidget widget
,
95 gfx::OverlayTransform transform
,
96 const gfx::Rect
& bounds_rect
,
97 const gfx::RectF
& crop_rect
) {
102 } // anonymous namespace
104 StreamTextureManagerInProcess::StreamTextureManagerInProcess()
105 : next_id_(1), weak_factory_(this) {}
107 StreamTextureManagerInProcess::~StreamTextureManagerInProcess() {
108 if (!textures_
.empty()) {
109 LOG(WARNING
) << "Undestroyed surface textures while tearing down "
110 "StreamTextureManager.";
114 GLuint
StreamTextureManagerInProcess::CreateStreamTexture(
115 uint32 client_texture_id
,
116 gles2::TextureManager
* texture_manager
) {
117 CalledOnValidThread();
119 gles2::TextureRef
* texture
= texture_manager
->GetTexture(client_texture_id
);
121 if (!texture
|| (texture
->texture()->target() &&
122 texture
->texture()->target() != GL_TEXTURE_EXTERNAL_OES
)) {
126 scoped_refptr
<gfx::SurfaceTexture
> surface_texture(
127 gfx::SurfaceTexture::Create(texture
->service_id()));
129 uint32 stream_id
= next_id_
++;
130 base::Closure release_callback
=
131 base::Bind(&StreamTextureManagerInProcess::OnReleaseStreamTexture
,
132 weak_factory_
.GetWeakPtr(), stream_id
);
133 scoped_refptr
<gfx::GLImage
> gl_image(new GLImageImpl(surface_texture
,
136 gfx::Size size
= gl_image
->GetSize();
137 texture_manager
->SetTarget(texture
, GL_TEXTURE_EXTERNAL_OES
);
138 texture_manager
->SetLevelInfo(texture
,
139 GL_TEXTURE_EXTERNAL_OES
,
149 texture_manager
->SetLevelImage(
150 texture
, GL_TEXTURE_EXTERNAL_OES
, 0, gl_image
.get());
153 base::AutoLock
lock(map_lock_
);
154 textures_
[stream_id
] = surface_texture
;
163 void StreamTextureManagerInProcess::OnReleaseStreamTexture(uint32 stream_id
) {
164 CalledOnValidThread();
165 base::AutoLock
lock(map_lock_
);
166 textures_
.erase(stream_id
);
169 // This can get called from any thread.
170 scoped_refptr
<gfx::SurfaceTexture
>
171 StreamTextureManagerInProcess::GetSurfaceTexture(uint32 stream_id
) {
172 base::AutoLock
lock(map_lock_
);
173 TextureMap::const_iterator it
= textures_
.find(stream_id
);
174 if (it
!= textures_
.end())