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/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 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
,
36 gfx::OverlayTransform transform
,
37 const gfx::Rect
& bounds_rect
,
38 const gfx::RectF
& crop_rect
) OVERRIDE
;
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
) {
62 gfx::Size
GLImageImpl::GetSize() {
66 bool GLImageImpl::BindTexImage(unsigned target
) {
71 void GLImageImpl::ReleaseTexImage(unsigned target
) {
75 void GLImageImpl::WillUseTexImage() {
76 surface_texture_
->UpdateTexImage();
79 bool GLImageImpl::ScheduleOverlayPlane(gfx::AcceleratedWidget widget
,
81 gfx::OverlayTransform transform
,
82 const gfx::Rect
& bounds_rect
,
83 const gfx::RectF
& crop_rect
) {
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
)) {
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
,
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
,
135 texture_manager
->SetLevelImage(texture
, GL_TEXTURE_EXTERNAL_OES
, 0, gl_image
);
138 base::AutoLock
lock(map_lock_
);
139 textures_
[stream_id
] = surface_texture
;
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())