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::BufferFormat::RGBA_8888
, gfx::BufferUsage::MAP
}};
19 GpuMemoryBufferFactorySurfaceTexture::GpuMemoryBufferFactorySurfaceTexture() {
22 GpuMemoryBufferFactorySurfaceTexture::~GpuMemoryBufferFactorySurfaceTexture() {
26 bool GpuMemoryBufferFactorySurfaceTexture::
27 IsGpuMemoryBufferConfigurationSupported(gfx::BufferFormat format
,
28 gfx::BufferUsage usage
) {
29 for (auto& configuration
: kSupportedConfigurations
) {
30 if (configuration
.format
== format
&& configuration
.usage
== usage
)
37 void GpuMemoryBufferFactorySurfaceTexture::
38 GetSupportedGpuMemoryBufferConfigurations(
39 std::vector
<Configuration
>* configurations
) {
40 configurations
->assign(
41 kSupportedConfigurations
,
42 kSupportedConfigurations
+ arraysize(kSupportedConfigurations
));
45 gfx::GpuMemoryBufferHandle
46 GpuMemoryBufferFactorySurfaceTexture::CreateGpuMemoryBuffer(
47 gfx::GpuMemoryBufferId id
,
48 const gfx::Size
& size
,
49 gfx::BufferFormat format
,
50 gfx::BufferUsage usage
,
52 gfx::PluginWindowHandle surface_handle
) {
53 // Note: this needs to be 0 as the surface texture implemenation will take
54 // ownership of the texture and call glDeleteTextures when the GPU service
55 // attaches the surface texture to a real texture id. glDeleteTextures
56 // silently ignores 0.
57 const int kDummyTextureId
= 0;
58 scoped_refptr
<gfx::SurfaceTexture
> surface_texture
=
59 gfx::SurfaceTexture::Create(kDummyTextureId
);
60 if (!surface_texture
.get())
61 return gfx::GpuMemoryBufferHandle();
63 SurfaceTextureManager::GetInstance()->RegisterSurfaceTexture(
64 id
, client_id
, surface_texture
.get());
67 base::AutoLock
lock(surface_textures_lock_
);
69 SurfaceTextureMapKey
key(id
, client_id
);
70 DCHECK(surface_textures_
.find(key
) == surface_textures_
.end());
71 surface_textures_
[key
] = surface_texture
;
74 gfx::GpuMemoryBufferHandle handle
;
75 handle
.type
= gfx::SURFACE_TEXTURE_BUFFER
;
80 void GpuMemoryBufferFactorySurfaceTexture::DestroyGpuMemoryBuffer(
81 gfx::GpuMemoryBufferId id
,
84 base::AutoLock
lock(surface_textures_lock_
);
86 SurfaceTextureMapKey
key(id
, client_id
);
87 DCHECK(surface_textures_
.find(key
) != surface_textures_
.end());
88 surface_textures_
.erase(key
);
91 SurfaceTextureManager::GetInstance()->UnregisterSurfaceTexture(id
, client_id
);
94 gpu::ImageFactory
* GpuMemoryBufferFactorySurfaceTexture::AsImageFactory() {
98 scoped_refptr
<gfx::GLImage
>
99 GpuMemoryBufferFactorySurfaceTexture::CreateImageForGpuMemoryBuffer(
100 const gfx::GpuMemoryBufferHandle
& handle
,
101 const gfx::Size
& size
,
102 gfx::BufferFormat format
,
103 unsigned internalformat
,
105 base::AutoLock
lock(surface_textures_lock_
);
107 DCHECK_EQ(handle
.type
, gfx::SURFACE_TEXTURE_BUFFER
);
109 SurfaceTextureMapKey
key(handle
.id
, client_id
);
110 SurfaceTextureMap::iterator it
= surface_textures_
.find(key
);
111 if (it
== surface_textures_
.end())
112 return scoped_refptr
<gfx::GLImage
>();
114 scoped_refptr
<gfx::GLImageSurfaceTexture
> image(
115 new gfx::GLImageSurfaceTexture(size
));
116 if (!image
->Initialize(it
->second
.get()))
117 return scoped_refptr
<gfx::GLImage
>();
122 } // namespace content