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
;
43 void OnMemoryDump(base::trace_event::ProcessMemoryDump
* pmd
,
44 uint64_t process_tracing_id
,
45 const std::string
& dump_name
) override
;
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
) {
69 gfx::Size
GLImageImpl::GetSize() {
73 unsigned GLImageImpl::GetInternalFormat() {
77 bool GLImageImpl::BindTexImage(unsigned target
) {
82 void GLImageImpl::ReleaseTexImage(unsigned target
) {
86 bool GLImageImpl::CopyTexSubImage(unsigned target
,
87 const gfx::Point
& offset
,
88 const gfx::Rect
& rect
) {
92 void GLImageImpl::WillUseTexImage() {
93 surface_texture_
->UpdateTexImage();
96 bool GLImageImpl::ScheduleOverlayPlane(gfx::AcceleratedWidget widget
,
98 gfx::OverlayTransform transform
,
99 const gfx::Rect
& bounds_rect
,
100 const gfx::RectF
& crop_rect
) {
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
)) {
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
,
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
;
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())