Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / content / common / gpu / gpu_memory_buffer_factory_surface_texture.cc
blob490c45e41f03d1376a347efa715027d79d0ea917
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"
11 namespace content {
12 namespace {
14 const GpuMemoryBufferFactory::Configuration kSupportedConfigurations[] = {
15 {gfx::BufferFormat::RGBA_8888, gfx::BufferUsage::MAP}};
17 } // namespace
19 GpuMemoryBufferFactorySurfaceTexture::GpuMemoryBufferFactorySurfaceTexture() {
22 GpuMemoryBufferFactorySurfaceTexture::~GpuMemoryBufferFactorySurfaceTexture() {
25 // static
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)
31 return true;
34 return false;
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,
51 int client_id,
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.id, client_id, surface_texture.get());
67 base::AutoLock lock(surface_textures_lock_);
69 SurfaceTextureMapKey key(id.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;
76 handle.id = id;
77 return handle;
80 void GpuMemoryBufferFactorySurfaceTexture::DestroyGpuMemoryBuffer(
81 gfx::GpuMemoryBufferId id,
82 int client_id) {
84 base::AutoLock lock(surface_textures_lock_);
86 SurfaceTextureMapKey key(id.id, client_id);
87 DCHECK(surface_textures_.find(key) != surface_textures_.end());
88 surface_textures_.erase(key);
91 SurfaceTextureManager::GetInstance()->UnregisterSurfaceTexture(id.id,
92 client_id);
95 gpu::ImageFactory* GpuMemoryBufferFactorySurfaceTexture::AsImageFactory() {
96 return this;
99 scoped_refptr<gfx::GLImage>
100 GpuMemoryBufferFactorySurfaceTexture::CreateImageForGpuMemoryBuffer(
101 const gfx::GpuMemoryBufferHandle& handle,
102 const gfx::Size& size,
103 gfx::BufferFormat format,
104 unsigned internalformat,
105 int client_id) {
106 base::AutoLock lock(surface_textures_lock_);
108 DCHECK_EQ(handle.type, gfx::SURFACE_TEXTURE_BUFFER);
110 SurfaceTextureMapKey key(handle.id.id, client_id);
111 SurfaceTextureMap::iterator it = surface_textures_.find(key);
112 if (it == surface_textures_.end())
113 return scoped_refptr<gfx::GLImage>();
115 scoped_refptr<gfx::GLImageSurfaceTexture> image(
116 new gfx::GLImageSurfaceTexture(size));
117 if (!image->Initialize(it->second.get()))
118 return scoped_refptr<gfx::GLImage>();
120 return image;
123 } // namespace content