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 bool BindTexImage(unsigned target
) override
;
29 void ReleaseTexImage(unsigned target
) override
;
30 bool CopyTexImage(unsigned target
) override
;
31 void WillUseTexImage() override
;
32 void DidUseTexImage() override
{}
33 void WillModifyTexImage() override
{}
34 void DidModifyTexImage() override
{}
35 bool ScheduleOverlayPlane(gfx::AcceleratedWidget widget
,
37 gfx::OverlayTransform transform
,
38 const gfx::Rect
& bounds_rect
,
39 const gfx::RectF
& crop_rect
) override
;
42 ~GLImageImpl() override
;
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(
141 texture
, GL_TEXTURE_EXTERNAL_OES
, 0, gl_image
.get());
144 base::AutoLock
lock(map_lock_
);
145 textures_
[stream_id
] = surface_texture
;
154 void StreamTextureManagerInProcess::OnReleaseStreamTexture(uint32 stream_id
) {
155 CalledOnValidThread();
156 base::AutoLock
lock(map_lock_
);
157 textures_
.erase(stream_id
);
160 // This can get called from any thread.
161 scoped_refptr
<gfx::SurfaceTexture
>
162 StreamTextureManagerInProcess::GetSurfaceTexture(uint32 stream_id
) {
163 base::AutoLock
lock(map_lock_
);
164 TextureMap::const_iterator it
= textures_
.find(stream_id
);
165 if (it
!= textures_
.end())