Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / content / common / gpu / gpu_memory_buffer_factory_surface_texture.cc
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.
4
5 #include "content/common/gpu/gpu_memory_buffer_factory_surface_texture.h"
6
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"
10
11 namespace content {
12
13 GpuMemoryBufferFactorySurfaceTexture::GpuMemoryBufferFactorySurfaceTexture() {
14 }
15
16 GpuMemoryBufferFactorySurfaceTexture::~GpuMemoryBufferFactorySurfaceTexture() {
17 }
18
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();
34
35   SurfaceTextureManager::GetInstance()->RegisterSurfaceTexture(
36       id, client_id, surface_texture.get());
37
38   SurfaceTextureMapKey key(id, client_id);
39   DCHECK(surface_textures_.find(key) == surface_textures_.end());
40   surface_textures_[key] = surface_texture;
41
42   gfx::GpuMemoryBufferHandle handle;
43   handle.type = gfx::SURFACE_TEXTURE_BUFFER;
44   handle.id = id;
45   return handle;
46 }
47
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);
55
56   SurfaceTextureManager::GetInstance()->UnregisterSurfaceTexture(id, client_id);
57 }
58
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>();
69
70   scoped_refptr<gfx::GLImageSurfaceTexture> image(
71       new gfx::GLImageSurfaceTexture(size));
72   if (!image->Initialize(it->second.get()))
73     return scoped_refptr<gfx::GLImage>();
74
75   return image;
76 }
77
78 }  // namespace content