1 // Copyright 2014 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 "content/common/gpu/gpu_memory_buffer_factory_surface_texture.h"
7 #include "content/common/android/surface_texture_manager.h"
8 #include "ui/gl/android/surface_texture.h"
9 #include "ui/gl/gl_image_surface_texture.h"
14 const GpuMemoryBufferFactory::Configuration kSupportedConfigurations
[] = {
15 { gfx::GpuMemoryBuffer::RGBA_8888
, gfx::GpuMemoryBuffer::MAP
}
20 GpuMemoryBufferFactorySurfaceTexture::GpuMemoryBufferFactorySurfaceTexture() {
23 GpuMemoryBufferFactorySurfaceTexture::~GpuMemoryBufferFactorySurfaceTexture() {
27 bool GpuMemoryBufferFactorySurfaceTexture::
28 IsGpuMemoryBufferConfigurationSupported(gfx::GpuMemoryBuffer::Format format
,
29 gfx::GpuMemoryBuffer::Usage usage
) {
30 for (auto& configuration
: kSupportedConfigurations
) {
31 if (configuration
.format
== format
&& configuration
.usage
== usage
)
38 void GpuMemoryBufferFactorySurfaceTexture::
39 GetSupportedGpuMemoryBufferConfigurations(
40 std::vector
<Configuration
>* configurations
) {
41 configurations
->assign(
42 kSupportedConfigurations
,
43 kSupportedConfigurations
+ arraysize(kSupportedConfigurations
));
46 gfx::GpuMemoryBufferHandle
47 GpuMemoryBufferFactorySurfaceTexture::CreateGpuMemoryBuffer(
48 gfx::GpuMemoryBufferId id
,
49 const gfx::Size
& size
,
50 gfx::GpuMemoryBuffer::Format format
,
51 gfx::GpuMemoryBuffer::Usage usage
,
53 gfx::PluginWindowHandle surface_handle
) {
54 // Note: this needs to be 0 as the surface texture implemenation will take
55 // ownership of the texture and call glDeleteTextures when the GPU service
56 // attaches the surface texture to a real texture id. glDeleteTextures
57 // silently ignores 0.
58 const int kDummyTextureId
= 0;
59 scoped_refptr
<gfx::SurfaceTexture
> surface_texture
=
60 gfx::SurfaceTexture::Create(kDummyTextureId
);
61 if (!surface_texture
.get())
62 return gfx::GpuMemoryBufferHandle();
64 SurfaceTextureManager::GetInstance()->RegisterSurfaceTexture(
65 id
, client_id
, surface_texture
.get());
68 base::AutoLock
lock(surface_textures_lock_
);
70 SurfaceTextureMapKey
key(id
, client_id
);
71 DCHECK(surface_textures_
.find(key
) == surface_textures_
.end());
72 surface_textures_
[key
] = surface_texture
;
75 gfx::GpuMemoryBufferHandle handle
;
76 handle
.type
= gfx::SURFACE_TEXTURE_BUFFER
;
81 void GpuMemoryBufferFactorySurfaceTexture::DestroyGpuMemoryBuffer(
82 gfx::GpuMemoryBufferId id
,
85 base::AutoLock
lock(surface_textures_lock_
);
87 SurfaceTextureMapKey
key(id
, client_id
);
88 SurfaceTextureMap::iterator it
= surface_textures_
.find(key
);
89 if (it
!= surface_textures_
.end())
90 surface_textures_
.erase(it
);
93 SurfaceTextureManager::GetInstance()->UnregisterSurfaceTexture(id
, client_id
);
96 gpu::ImageFactory
* GpuMemoryBufferFactorySurfaceTexture::AsImageFactory() {
100 scoped_refptr
<gfx::GLImage
>
101 GpuMemoryBufferFactorySurfaceTexture::CreateImageForGpuMemoryBuffer(
102 const gfx::GpuMemoryBufferHandle
& handle
,
103 const gfx::Size
& size
,
104 gfx::GpuMemoryBuffer::Format format
,
105 unsigned internalformat
,
107 base::AutoLock
lock(surface_textures_lock_
);
109 DCHECK_EQ(handle
.type
, gfx::SURFACE_TEXTURE_BUFFER
);
111 SurfaceTextureMapKey
key(handle
.id
, client_id
);
112 SurfaceTextureMap::iterator it
= surface_textures_
.find(key
);
113 if (it
== surface_textures_
.end())
114 return scoped_refptr
<gfx::GLImage
>();
116 scoped_refptr
<gfx::GLImageSurfaceTexture
> image(
117 new gfx::GLImageSurfaceTexture(size
));
118 if (!image
->Initialize(it
->second
.get()))
119 return scoped_refptr
<gfx::GLImage
>();
124 } // namespace content