OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/common/gpu/gpu_memory_buffer_factory_surface_texture.h" | 5 #include "content/common/gpu/gpu_memory_buffer_factory_surface_texture.h" |
6 | 6 |
7 #include "content/common/android/surface_texture_manager.h" | 7 #include "content/common/android/surface_texture_manager.h" |
8 #include "ui/gl/android/surface_texture.h" | 8 #include "ui/gl/android/surface_texture.h" |
| 9 #include "ui/gl/gl_bindings.h" |
9 #include "ui/gl/gl_image_surface_texture.h" | 10 #include "ui/gl/gl_image_surface_texture.h" |
| 11 #include "ui/gl/scoped_binders.h" |
10 | 12 |
11 namespace content { | 13 namespace content { |
12 | 14 |
13 GpuMemoryBufferFactorySurfaceTexture::GpuMemoryBufferFactorySurfaceTexture() { | 15 GpuMemoryBufferFactorySurfaceTexture::GpuMemoryBufferFactorySurfaceTexture() { |
14 } | 16 } |
15 | 17 |
16 GpuMemoryBufferFactorySurfaceTexture::~GpuMemoryBufferFactorySurfaceTexture() { | 18 GpuMemoryBufferFactorySurfaceTexture::~GpuMemoryBufferFactorySurfaceTexture() { |
17 } | 19 } |
18 | 20 |
19 // static | 21 // static |
20 bool GpuMemoryBufferFactorySurfaceTexture:: | 22 bool GpuMemoryBufferFactorySurfaceTexture:: |
21 IsGpuMemoryBufferConfigurationSupported(gfx::BufferFormat format, | 23 IsGpuMemoryBufferConfigurationSupported(gfx::BufferFormat format, |
22 gfx::BufferUsage usage) { | 24 gfx::BufferUsage usage) { |
23 switch (usage) { | 25 switch (usage) { |
24 case gfx::BufferUsage::GPU_READ: | 26 case gfx::BufferUsage::GPU_READ: |
25 case gfx::BufferUsage::SCANOUT: | 27 case gfx::BufferUsage::SCANOUT: |
| 28 return false; |
26 case gfx::BufferUsage::GPU_READ_CPU_READ_WRITE_PERSISTENT: | 29 case gfx::BufferUsage::GPU_READ_CPU_READ_WRITE_PERSISTENT: |
27 return false; | |
28 case gfx::BufferUsage::GPU_READ_CPU_READ_WRITE: | 30 case gfx::BufferUsage::GPU_READ_CPU_READ_WRITE: |
29 return format == gfx::BufferFormat::RGBA_8888; | 31 return format == gfx::BufferFormat::RGBA_8888; |
30 } | 32 } |
31 NOTREACHED(); | 33 NOTREACHED(); |
32 return false; | 34 return false; |
33 } | 35 } |
34 | 36 |
35 gfx::GpuMemoryBufferHandle | 37 gfx::GpuMemoryBufferHandle |
36 GpuMemoryBufferFactorySurfaceTexture::CreateGpuMemoryBuffer( | 38 GpuMemoryBufferFactorySurfaceTexture::CreateGpuMemoryBuffer( |
37 gfx::GpuMemoryBufferId id, | 39 gfx::GpuMemoryBufferId id, |
38 const gfx::Size& size, | 40 const gfx::Size& size, |
39 gfx::BufferFormat format, | 41 gfx::BufferFormat format, |
40 gfx::BufferUsage usage, | 42 gfx::BufferUsage usage, |
41 int client_id, | 43 int client_id, |
42 gfx::PluginWindowHandle surface_handle) { | 44 gfx::PluginWindowHandle surface_handle) { |
43 // Note: this needs to be 0 as the surface texture implemenation will take | 45 // This can be called on a thread without a GL context current so use a dummy |
44 // ownership of the texture and call glDeleteTextures when the GPU service | 46 // texture id instead of a real one. |
45 // attaches the surface texture to a real texture id. glDeleteTextures | 47 // Note: This needs to be 0 as the surface texture implemenation will take |
46 // silently ignores 0. | 48 // ownership of the texture and call glDeleteTextures when we attach a real |
| 49 // texture id to it. See CreateImageForGpuMemoryBuffer() below. |
47 const int kDummyTextureId = 0; | 50 const int kDummyTextureId = 0; |
48 scoped_refptr<gfx::SurfaceTexture> surface_texture = | 51 scoped_refptr<gfx::SurfaceTexture> surface_texture = |
49 gfx::SurfaceTexture::Create(kDummyTextureId); | 52 gfx::SurfaceTexture::CreateSingleBuffered(kDummyTextureId); |
50 if (!surface_texture.get()) | 53 if (!surface_texture.get()) |
51 return gfx::GpuMemoryBufferHandle(); | 54 return gfx::GpuMemoryBufferHandle(); |
52 | 55 |
53 SurfaceTextureManager::GetInstance()->RegisterSurfaceTexture( | 56 SurfaceTextureManager::GetInstance()->RegisterSurfaceTexture( |
54 id.id, client_id, surface_texture.get()); | 57 id.id, client_id, surface_texture.get()); |
55 | 58 |
56 { | 59 { |
57 base::AutoLock lock(surface_textures_lock_); | 60 base::AutoLock lock(surface_textures_lock_); |
58 | 61 |
59 SurfaceTextureMapKey key(id.id, client_id); | 62 SurfaceTextureMapKey key(id.id, client_id); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 gfx::BufferFormat format, | 107 gfx::BufferFormat format, |
105 unsigned internalformat, | 108 unsigned internalformat, |
106 int client_id) { | 109 int client_id) { |
107 base::AutoLock lock(surface_textures_lock_); | 110 base::AutoLock lock(surface_textures_lock_); |
108 | 111 |
109 DCHECK_EQ(handle.type, gfx::SURFACE_TEXTURE_BUFFER); | 112 DCHECK_EQ(handle.type, gfx::SURFACE_TEXTURE_BUFFER); |
110 | 113 |
111 SurfaceTextureMapKey key(handle.id.id, client_id); | 114 SurfaceTextureMapKey key(handle.id.id, client_id); |
112 SurfaceTextureMap::iterator it = surface_textures_.find(key); | 115 SurfaceTextureMap::iterator it = surface_textures_.find(key); |
113 if (it == surface_textures_.end()) | 116 if (it == surface_textures_.end()) |
114 return scoped_refptr<gl::GLImage>(); | 117 return nullptr; |
| 118 |
| 119 gfx::SurfaceTexture* surface_texture = it->second.get(); |
| 120 // Note: Surface textures used as gpu memory buffers are created with an |
| 121 // initial dummy texture id of 0. We need to call DetachFromGLContext() here |
| 122 // to detach from the dummy texture before we can attach to a real texture |
| 123 // id. DetachFromGLContext() will delete the texture for the current |
| 124 // attachment point. Detaching from the dummy texture id should not cause any |
| 125 // problems as the GL should silently ignore 0 when passed to |
| 126 // glDeleteTextures. |
| 127 surface_texture->DetachFromGLContext(); |
| 128 |
| 129 // Create a real texture. |
| 130 GLuint texture_id = 0; |
| 131 glGenTextures(1, &texture_id); |
| 132 DCHECK(texture_id); |
| 133 { |
| 134 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_EXTERNAL_OES, |
| 135 texture_id); |
| 136 // This will attach the surface texture to the texture currently bound to |
| 137 // GL_TEXTURE_EXTERNAL_OES target and take ownership of the texture. |
| 138 surface_texture->AttachToGLContext(); |
| 139 } |
115 | 140 |
116 scoped_refptr<gl::GLImageSurfaceTexture> image( | 141 scoped_refptr<gl::GLImageSurfaceTexture> image( |
117 new gl::GLImageSurfaceTexture(size)); | 142 new gl::GLImageSurfaceTexture(size)); |
118 if (!image->Initialize(it->second.get())) | 143 if (!image->Initialize(surface_texture)) |
119 return scoped_refptr<gl::GLImage>(); | 144 return nullptr; |
120 | 145 |
121 return image; | 146 return image; |
122 } | 147 } |
123 | 148 |
124 } // namespace content | 149 } // namespace content |
OLD | NEW |