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 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
,
37 gfx::OverlayTransform transform
,
38 const gfx::Rect
& bounds_rect
,
39 const gfx::RectF
& crop_rect
) OVERRIDE
;
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
) {
63 gfx::Size
GLImageImpl::GetSize() {
67 bool GLImageImpl::BindTexImage(unsigned target
) {
72 void GLImageImpl::ReleaseTexImage(unsigned target
) {
76 bool GLImageImpl::CopyTexImage(unsigned target
) {
80 void GLImageImpl::WillUseTexImage() {
81 surface_texture_
->UpdateTexImage();
84 bool GLImageImpl::ScheduleOverlayPlane(gfx::AcceleratedWidget widget
,
86 gfx::OverlayTransform transform
,
87 const gfx::Rect
& bounds_rect
,
88 const gfx::RectF
& crop_rect
) {
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
)) {
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
,
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
,
140 texture_manager
->SetLevelImage(texture
, GL_TEXTURE_EXTERNAL_OES
, 0, gl_image
);
143 base::AutoLock
lock(map_lock_
);
144 textures_
[stream_id
] = surface_texture
;
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())