gpu: Associate all GpuMemoryBuffers with unique IDs.
[chromium-blink-merge.git] / content / common / gpu / gpu_memory_buffer_factory_surface_texture.cc
blob91b81cbd6516ca0be9fb95ec1fd22408faa90aa9
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 {
13 GpuMemoryBufferFactorySurfaceTexture::GpuMemoryBufferFactorySurfaceTexture() {
16 GpuMemoryBufferFactorySurfaceTexture::~GpuMemoryBufferFactorySurfaceTexture() {
19 gfx::GpuMemoryBufferHandle
20 GpuMemoryBufferFactorySurfaceTexture::CreateGpuMemoryBuffer(
21 gfx::GpuMemoryBufferId id,
22 const gfx::Size& size,
23 unsigned internalformat,
24 int client_id) {
25 // Note: this needs to be 0 as the surface texture implemenation will take
26 // ownership of the texture and call glDeleteTextures when the GPU service
27 // attaches the surface texture to a real texture id. glDeleteTextures
28 // silently ignores 0.
29 const int kDummyTextureId = 0;
30 scoped_refptr<gfx::SurfaceTexture> surface_texture =
31 gfx::SurfaceTexture::Create(kDummyTextureId);
32 if (!surface_texture.get())
33 return gfx::GpuMemoryBufferHandle();
35 SurfaceTextureManager::GetInstance()->RegisterSurfaceTexture(
36 id, client_id, surface_texture.get());
38 SurfaceTextureMapKey key(id, client_id);
39 DCHECK(surface_textures_.find(key) == surface_textures_.end());
40 surface_textures_[key] = surface_texture;
42 gfx::GpuMemoryBufferHandle handle;
43 handle.type = gfx::SURFACE_TEXTURE_BUFFER;
44 handle.id = id;
45 return handle;
48 void GpuMemoryBufferFactorySurfaceTexture::DestroyGpuMemoryBuffer(
49 gfx::GpuMemoryBufferId id,
50 int client_id) {
51 SurfaceTextureMapKey key(id, client_id);
52 SurfaceTextureMap::iterator it = surface_textures_.find(key);
53 if (it != surface_textures_.end())
54 surface_textures_.erase(it);
56 SurfaceTextureManager::GetInstance()->UnregisterSurfaceTexture(id, client_id);
59 scoped_refptr<gfx::GLImage>
60 GpuMemoryBufferFactorySurfaceTexture::CreateImageForGpuMemoryBuffer(
61 gfx::GpuMemoryBufferId id,
62 const gfx::Size& size,
63 unsigned internalformat,
64 int client_id) {
65 SurfaceTextureMapKey key(id, client_id);
66 SurfaceTextureMap::iterator it = surface_textures_.find(key);
67 if (it == surface_textures_.end())
68 return scoped_refptr<gfx::GLImage>();
70 scoped_refptr<gfx::GLImageSurfaceTexture> image(
71 new gfx::GLImageSurfaceTexture(size));
72 if (!image->Initialize(it->second.get()))
73 return scoped_refptr<gfx::GLImage>();
75 return image;
78 } // namespace content