OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/texture_image_transport_surface.h" | 5 #include "content/common/gpu/texture_image_transport_surface.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "content/common/gpu/gl_scoped_binders.h" | 11 #include "content/common/gpu/gl_scoped_binders.h" |
12 #include "content/common/gpu/gpu_channel.h" | 12 #include "content/common/gpu/gpu_channel.h" |
13 #include "content/common/gpu/gpu_channel_manager.h" | 13 #include "content/common/gpu/gpu_channel_manager.h" |
14 #include "content/common/gpu/gpu_messages.h" | 14 #include "content/common/gpu/gpu_messages.h" |
15 #include "content/common/gpu/sync_point_manager.h" | 15 #include "content/common/gpu/sync_point_manager.h" |
16 #include "content/public/common/content_switches.h" | 16 #include "content/public/common/content_switches.h" |
17 #include "gpu/command_buffer/service/context_group.h" | 17 #include "gpu/command_buffer/service/context_group.h" |
18 #include "gpu/command_buffer/service/gpu_scheduler.h" | 18 #include "gpu/command_buffer/service/gpu_scheduler.h" |
19 #include "gpu/command_buffer/service/texture_manager.h" | 19 #include "gpu/command_buffer/service/texture_definition.h" |
20 | 20 |
21 using gpu::gles2::ContextGroup; | 21 using gpu::gles2::ContextGroup; |
| 22 using gpu::gles2::MailboxManager; |
| 23 using gpu::gles2::MailboxName; |
| 24 using gpu::gles2::TextureDefinition; |
22 using gpu::gles2::TextureManager; | 25 using gpu::gles2::TextureManager; |
23 typedef TextureManager::TextureInfo TextureInfo; | |
24 | 26 |
25 namespace content { | 27 namespace content { |
26 | 28 |
27 TextureImageTransportSurface::Texture::Texture() | 29 TextureImageTransportSurface::Texture::Texture() |
28 : client_id(0), | 30 : service_id(0), |
29 sent_to_client(false) { | 31 surface_handle(0) { |
30 } | 32 } |
31 | 33 |
32 TextureImageTransportSurface::Texture::~Texture() { | 34 TextureImageTransportSurface::Texture::~Texture() { |
33 } | 35 } |
34 | 36 |
35 TextureImageTransportSurface::TextureImageTransportSurface( | 37 TextureImageTransportSurface::TextureImageTransportSurface( |
36 GpuChannelManager* manager, | 38 GpuChannelManager* manager, |
37 GpuCommandBufferStub* stub, | 39 GpuCommandBufferStub* stub, |
38 const gfx::GLSurfaceHandle& handle) | 40 const gfx::GLSurfaceHandle& handle) |
39 : fbo_id_(0), | 41 : fbo_id_(0), |
40 front_(0), | |
41 stub_destroyed_(false), | 42 stub_destroyed_(false), |
42 backbuffer_suggested_allocation_(true), | 43 backbuffer_suggested_allocation_(true), |
43 frontbuffer_suggested_allocation_(true), | 44 frontbuffer_suggested_allocation_(true), |
44 frontbuffer_is_protected_(true), | |
45 protection_state_id_(0), | |
46 handle_(handle), | 45 handle_(handle), |
47 parent_stub_(NULL), | |
48 is_swap_buffers_pending_(false), | 46 is_swap_buffers_pending_(false), |
49 did_unschedule_(false), | 47 did_unschedule_(false) { |
50 did_flip_(false) { | |
51 helper_.reset(new ImageTransportHelper(this, | 48 helper_.reset(new ImageTransportHelper(this, |
52 manager, | 49 manager, |
53 stub, | 50 stub, |
54 gfx::kNullPluginWindow)); | 51 gfx::kNullPluginWindow)); |
55 } | 52 } |
56 | 53 |
57 TextureImageTransportSurface::~TextureImageTransportSurface() { | 54 TextureImageTransportSurface::~TextureImageTransportSurface() { |
58 DCHECK(stub_destroyed_); | 55 DCHECK(stub_destroyed_); |
59 Destroy(); | 56 Destroy(); |
60 } | 57 } |
61 | 58 |
62 bool TextureImageTransportSurface::Initialize() { | 59 bool TextureImageTransportSurface::Initialize() { |
| 60 mailbox_manager_ = |
| 61 helper_->stub()->decoder()->GetContextGroup()->mailbox_manager(); |
| 62 |
| 63 backbuffer_.surface_handle = 1; |
| 64 |
63 GpuChannelManager* manager = helper_->manager(); | 65 GpuChannelManager* manager = helper_->manager(); |
64 GpuChannel* parent_channel = manager->LookupChannel(handle_.parent_client_id); | |
65 if (!parent_channel) | |
66 return false; | |
67 | |
68 parent_stub_ = parent_channel->LookupCommandBuffer(handle_.parent_context_id); | |
69 if (!parent_stub_) | |
70 return false; | |
71 | |
72 parent_stub_->AddDestructionObserver(this); | |
73 TextureManager* texture_manager = | |
74 parent_stub_->decoder()->GetContextGroup()->texture_manager(); | |
75 DCHECK(texture_manager); | |
76 | |
77 for (int i = 0; i < 2; ++i) { | |
78 Texture& texture = textures_[i]; | |
79 texture.client_id = handle_.parent_texture_id[i]; | |
80 texture.info = texture_manager->GetTextureInfo(texture.client_id); | |
81 if (!texture.info) | |
82 return false; | |
83 | |
84 if (!texture.info->target()) | |
85 texture_manager->SetInfoTarget(texture.info, GL_TEXTURE_2D); | |
86 texture_manager->SetParameter( | |
87 texture.info, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
88 texture_manager->SetParameter( | |
89 texture.info, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
90 texture_manager->SetParameter( | |
91 texture.info, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
92 texture_manager->SetParameter( | |
93 texture.info, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
94 } | |
95 | |
96 surface_ = manager->GetDefaultOffscreenSurface(); | 66 surface_ = manager->GetDefaultOffscreenSurface(); |
97 if (!surface_.get()) | 67 if (!surface_.get()) |
98 return false; | 68 return false; |
99 | 69 |
100 if (!helper_->Initialize()) | 70 if (!helper_->Initialize()) |
101 return false; | 71 return false; |
102 | 72 |
103 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 73 GpuChannel* parent_channel = manager->LookupChannel(handle_.parent_client_id); |
104 if (command_line->HasSwitch(switches::kUIPrioritizeInGpuProcess)) | 74 if (parent_channel) { |
105 helper_->SetPreemptByCounter(parent_channel->MessagesPendingCount()); | 75 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 76 if (command_line->HasSwitch(switches::kUIPrioritizeInGpuProcess)) |
| 77 helper_->SetPreemptByCounter(parent_channel->MessagesPendingCount()); |
| 78 } |
106 | 79 |
107 return true; | 80 return true; |
108 } | 81 } |
109 | 82 |
110 void TextureImageTransportSurface::Destroy() { | 83 void TextureImageTransportSurface::Destroy() { |
111 if (parent_stub_) { | |
112 parent_stub_->decoder()->MakeCurrent(); | |
113 ReleaseParentStub(); | |
114 } | |
115 | |
116 if (surface_.get()) | 84 if (surface_.get()) |
117 surface_ = NULL; | 85 surface_ = NULL; |
118 | 86 |
119 helper_->Destroy(); | 87 helper_->Destroy(); |
120 } | 88 } |
121 | 89 |
122 bool TextureImageTransportSurface::DeferDraws() { | 90 bool TextureImageTransportSurface::DeferDraws() { |
123 // The command buffer hit a draw/clear command that could clobber the | 91 // The command buffer hit a draw/clear command that could clobber the |
124 // texture in use by the UI compositor. If a Swap is pending, abort | 92 // texture in use by the UI compositor. If a Swap is pending, abort |
125 // processing of the command by returning true and unschedule until the Swap | 93 // processing of the command by returning true and unschedule until the Swap |
(...skipping 16 matching lines...) Expand all Loading... |
142 } | 110 } |
143 | 111 |
144 bool TextureImageTransportSurface::OnMakeCurrent(gfx::GLContext* context) { | 112 bool TextureImageTransportSurface::OnMakeCurrent(gfx::GLContext* context) { |
145 if (stub_destroyed_) { | 113 if (stub_destroyed_) { |
146 // Early-exit so that we don't recreate the fbo. We still want to return | 114 // Early-exit so that we don't recreate the fbo. We still want to return |
147 // true, so that the context is made current and the GLES2DecoderImpl can | 115 // true, so that the context is made current and the GLES2DecoderImpl can |
148 // release its own resources. | 116 // release its own resources. |
149 return true; | 117 return true; |
150 } | 118 } |
151 | 119 |
| 120 if (!context_.get()) { |
| 121 DCHECK(helper_->stub()); |
| 122 context_ = helper_->stub()->decoder()->GetGLContext(); |
| 123 } |
| 124 |
152 if (!fbo_id_) { | 125 if (!fbo_id_) { |
153 glGenFramebuffersEXT(1, &fbo_id_); | 126 glGenFramebuffersEXT(1, &fbo_id_); |
154 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo_id_); | 127 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo_id_); |
155 CreateBackTexture(gfx::Size(1, 1)); | 128 current_size_ = gfx::Size(1, 1); |
| 129 helper_->stub()->AddDestructionObserver(this); |
| 130 } |
| 131 |
| 132 // We could be receiving non-deferred GL commands, that is anything that does |
| 133 // not need a framebuffer. |
| 134 if (!backbuffer_.service_id && !is_swap_buffers_pending_ && |
| 135 backbuffer_suggested_allocation_) { |
| 136 CreateBackTexture(); |
156 | 137 |
157 #ifndef NDEBUG | 138 #ifndef NDEBUG |
158 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER); | 139 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER); |
159 if (status != GL_FRAMEBUFFER_COMPLETE) { | 140 if (status != GL_FRAMEBUFFER_COMPLETE) { |
160 DLOG(ERROR) << "Framebuffer incomplete."; | 141 DLOG(ERROR) << "Framebuffer incomplete."; |
161 glDeleteFramebuffersEXT(1, &fbo_id_); | 142 glDeleteFramebuffersEXT(1, &fbo_id_); |
162 fbo_id_ = 0; | 143 fbo_id_ = 0; |
163 return false; | 144 return false; |
164 } | 145 } |
165 #endif | 146 #endif |
166 DCHECK(helper_->stub()); | |
167 helper_->stub()->AddDestructionObserver(this); | |
168 } | 147 } |
169 | |
170 return true; | 148 return true; |
171 } | 149 } |
172 | 150 |
173 unsigned int TextureImageTransportSurface::GetBackingFrameBufferObject() { | 151 unsigned int TextureImageTransportSurface::GetBackingFrameBufferObject() { |
174 return fbo_id_; | 152 return fbo_id_; |
175 } | 153 } |
176 | 154 |
177 void TextureImageTransportSurface::SetBackbufferAllocation(bool allocation) { | 155 void TextureImageTransportSurface::SetBackbufferAllocation(bool allocation) { |
178 DCHECK(!is_swap_buffers_pending_); | 156 DCHECK(!is_swap_buffers_pending_); |
179 if (backbuffer_suggested_allocation_ == allocation) | 157 if (backbuffer_suggested_allocation_ == allocation) |
180 return; | 158 return; |
181 backbuffer_suggested_allocation_ = allocation; | 159 backbuffer_suggested_allocation_ = allocation; |
182 | 160 |
183 if (!helper_->MakeCurrent()) | |
184 return; | |
185 | |
186 if (backbuffer_suggested_allocation_) { | 161 if (backbuffer_suggested_allocation_) { |
187 DCHECK(!textures_[back()].info->service_id() || | 162 DCHECK(!backbuffer_.service_id); |
188 !textures_[back()].sent_to_client); | 163 CreateBackTexture(); |
189 CreateBackTexture(textures_[back()].size); | |
190 } else { | 164 } else { |
191 ReleaseTexture(back()); | 165 ReleaseBackTexture(); |
192 } | 166 } |
193 } | 167 } |
194 | 168 |
195 void TextureImageTransportSurface::SetFrontbufferAllocation(bool allocation) { | 169 void TextureImageTransportSurface::SetFrontbufferAllocation(bool allocation) { |
196 if (frontbuffer_suggested_allocation_ == allocation) | 170 if (frontbuffer_suggested_allocation_ == allocation) |
197 return; | 171 return; |
198 frontbuffer_suggested_allocation_ = allocation; | 172 frontbuffer_suggested_allocation_ = allocation; |
199 AdjustFrontBufferAllocation(); | |
200 } | |
201 | 173 |
202 void TextureImageTransportSurface::AdjustFrontBufferAllocation() { | 174 if (!frontbuffer_suggested_allocation_) { |
203 if (!helper_->MakeCurrent()) | 175 GpuHostMsg_AcceleratedSurfaceRelease_Params params; |
204 return; | 176 helper_->SendAcceleratedSurfaceRelease(params); |
205 | |
206 if (!frontbuffer_suggested_allocation_ && !frontbuffer_is_protected_ && | |
207 textures_[front()].info->service_id()) { | |
208 ReleaseTexture(front()); | |
209 if (textures_[front()].sent_to_client) { | |
210 GpuHostMsg_AcceleratedSurfaceRelease_Params params; | |
211 params.identifier = textures_[front()].client_id; | |
212 helper_->SendAcceleratedSurfaceRelease(params); | |
213 textures_[front()].sent_to_client = false; | |
214 } | |
215 } | 177 } |
216 } | 178 } |
217 | 179 |
218 void* TextureImageTransportSurface::GetShareHandle() { | 180 void* TextureImageTransportSurface::GetShareHandle() { |
219 return GetHandle(); | 181 return GetHandle(); |
220 } | 182 } |
221 | 183 |
222 void* TextureImageTransportSurface::GetDisplay() { | 184 void* TextureImageTransportSurface::GetDisplay() { |
223 return surface_.get() ? surface_->GetDisplay() : NULL; | 185 return surface_.get() ? surface_->GetDisplay() : NULL; |
224 } | 186 } |
225 | 187 |
226 void* TextureImageTransportSurface::GetConfig() { | 188 void* TextureImageTransportSurface::GetConfig() { |
227 return surface_.get() ? surface_->GetConfig() : NULL; | 189 return surface_.get() ? surface_->GetConfig() : NULL; |
228 } | 190 } |
229 | 191 |
230 void TextureImageTransportSurface::OnResize(gfx::Size size) { | 192 void TextureImageTransportSurface::OnResize(gfx::Size size) { |
231 CreateBackTexture(size); | 193 current_size_ = size; |
| 194 CreateBackTexture(); |
232 } | 195 } |
233 | 196 |
234 void TextureImageTransportSurface::OnWillDestroyStub( | 197 void TextureImageTransportSurface::OnWillDestroyStub( |
235 GpuCommandBufferStub* stub) { | 198 GpuCommandBufferStub* stub) { |
236 if (stub == parent_stub_) { | 199 DCHECK(stub == helper_->stub()); |
237 ReleaseParentStub(); | 200 stub->RemoveDestructionObserver(this); |
238 helper_->SetPreemptByCounter(NULL); | |
239 } else { | |
240 DCHECK(stub == helper_->stub()); | |
241 stub->RemoveDestructionObserver(this); | |
242 | 201 |
243 // We are losing the stub owning us, this is our last chance to clean up the | 202 GpuHostMsg_AcceleratedSurfaceRelease_Params params; |
244 // resources we allocated in the stub's context. | 203 helper_->SendAcceleratedSurfaceRelease(params); |
245 if (fbo_id_) { | |
246 glDeleteFramebuffersEXT(1, &fbo_id_); | |
247 CHECK_GL_ERROR(); | |
248 fbo_id_ = 0; | |
249 } | |
250 | 204 |
251 stub_destroyed_ = true; | 205 ReleaseBackTexture(); |
| 206 |
| 207 // We are losing the stub owning us, this is our last chance to clean up the |
| 208 // resources we allocated in the stub's context. |
| 209 if (fbo_id_) { |
| 210 glDeleteFramebuffersEXT(1, &fbo_id_); |
| 211 CHECK_GL_ERROR(); |
| 212 fbo_id_ = 0; |
252 } | 213 } |
| 214 |
| 215 stub_destroyed_ = true; |
253 } | 216 } |
254 | 217 |
255 bool TextureImageTransportSurface::SwapBuffers() { | 218 bool TextureImageTransportSurface::SwapBuffers() { |
256 DCHECK(backbuffer_suggested_allocation_); | 219 DCHECK(backbuffer_suggested_allocation_); |
257 if (!frontbuffer_suggested_allocation_ || !frontbuffer_is_protected_) | 220 if (!frontbuffer_suggested_allocation_) |
258 return true; | 221 return true; |
259 if (!parent_stub_) { | |
260 LOG(ERROR) << "SwapBuffers failed because no parent stub."; | |
261 return false; | |
262 } | |
263 | 222 |
264 glFlush(); | 223 glFlush(); |
265 front_ = back(); | 224 ProduceTexture(backbuffer_); |
266 previous_damage_rect_ = gfx::Rect(textures_[front()].size); | |
267 | 225 |
268 DCHECK(textures_[front()].client_id != 0); | 226 // Do not allow destruction while we are still waiting for a swap ACK, |
| 227 // so we do not leak a texture in the mailbox. |
| 228 AddRef(); |
269 | 229 |
| 230 DCHECK(backbuffer_.size == current_size_); |
270 GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params params; | 231 GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params params; |
271 params.surface_handle = textures_[front()].client_id; | 232 params.surface_handle = backbuffer_.surface_handle; |
272 params.size = textures_[front()].size; | 233 params.size = backbuffer_.size; |
273 params.protection_state_id = protection_state_id_; | |
274 params.skip_ack = false; | |
275 helper_->SendAcceleratedSurfaceBuffersSwapped(params); | 234 helper_->SendAcceleratedSurfaceBuffersSwapped(params); |
276 | 235 |
277 DCHECK(!is_swap_buffers_pending_); | 236 DCHECK(!is_swap_buffers_pending_); |
278 is_swap_buffers_pending_ = true; | 237 is_swap_buffers_pending_ = true; |
279 return true; | 238 return true; |
280 } | 239 } |
281 | 240 |
282 bool TextureImageTransportSurface::PostSubBuffer( | 241 bool TextureImageTransportSurface::PostSubBuffer( |
283 int x, int y, int width, int height) { | 242 int x, int y, int width, int height) { |
284 DCHECK(backbuffer_suggested_allocation_); | 243 DCHECK(backbuffer_suggested_allocation_); |
285 DCHECK(textures_[back()].info->service_id()); | 244 if (!frontbuffer_suggested_allocation_) |
286 if (!frontbuffer_suggested_allocation_ || !frontbuffer_is_protected_) | |
287 return true; | 245 return true; |
288 // If we are recreating the frontbuffer with this swap, make sure we are | |
289 // drawing a full frame. | |
290 DCHECK(textures_[front()].info->service_id() || | |
291 (!x && !y && gfx::Size(width, height) == textures_[back()].size)); | |
292 if (!parent_stub_) { | |
293 LOG(ERROR) << "PostSubBuffer failed because no parent stub."; | |
294 return false; | |
295 } | |
296 | |
297 const gfx::Rect new_damage_rect(x, y, width, height); | 246 const gfx::Rect new_damage_rect(x, y, width, height); |
| 247 DCHECK(gfx::Rect(gfx::Point(), current_size_).Contains(new_damage_rect)); |
298 | 248 |
299 // An empty damage rect is a successful no-op. | 249 // An empty damage rect is a successful no-op. |
300 if (new_damage_rect.IsEmpty()) | 250 if (new_damage_rect.IsEmpty()) |
301 return true; | 251 return true; |
302 | 252 |
303 int back_texture_service_id = textures_[back()].info->service_id(); | 253 glFlush(); |
304 int front_texture_service_id = textures_[front()].info->service_id(); | 254 ProduceTexture(backbuffer_); |
305 | 255 |
306 gfx::Size expected_size = textures_[back()].size; | 256 // Do not allow destruction while we are still waiting for a swap ACK, |
307 bool surfaces_same_size = textures_[front()].size == expected_size; | 257 // so we do not leak a texture in the mailbox. |
| 258 AddRef(); |
308 | 259 |
309 if (surfaces_same_size) { | 260 DCHECK(current_size_ == backbuffer_.size); |
310 std::vector<gfx::Rect> regions_to_copy; | |
311 GetRegionsToCopy(previous_damage_rect_, new_damage_rect, ®ions_to_copy); | |
312 | |
313 ScopedFrameBufferBinder fbo_binder(fbo_id_); | |
314 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, | |
315 GL_COLOR_ATTACHMENT0, | |
316 GL_TEXTURE_2D, | |
317 front_texture_service_id, | |
318 0); | |
319 ScopedTextureBinder texture_binder(back_texture_service_id); | |
320 | |
321 for (size_t i = 0; i < regions_to_copy.size(); ++i) { | |
322 const gfx::Rect& region_to_copy = regions_to_copy[i]; | |
323 if (!region_to_copy.IsEmpty()) { | |
324 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, region_to_copy.x(), | |
325 region_to_copy.y(), region_to_copy.x(), region_to_copy.y(), | |
326 region_to_copy.width(), region_to_copy.height()); | |
327 } | |
328 } | |
329 } else if (!surfaces_same_size && did_flip_) { | |
330 DCHECK(new_damage_rect == gfx::Rect(expected_size)); | |
331 } | |
332 | |
333 glFlush(); | |
334 front_ = back(); | |
335 previous_damage_rect_ = new_damage_rect; | |
336 | |
337 DCHECK(textures_[front()].client_id); | |
338 | 261 |
339 GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params params; | 262 GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params params; |
340 params.surface_handle = textures_[front()].client_id; | 263 params.surface_handle = backbuffer_.surface_handle; |
341 params.surface_size = textures_[front()].size; | 264 params.surface_size = backbuffer_.size; |
342 params.x = x; | 265 params.x = x; |
343 params.y = y; | 266 params.y = y; |
344 params.width = width; | 267 params.width = width; |
345 params.height = height; | 268 params.height = height; |
346 params.protection_state_id = protection_state_id_; | |
347 helper_->SendAcceleratedSurfacePostSubBuffer(params); | 269 helper_->SendAcceleratedSurfacePostSubBuffer(params); |
348 | 270 |
349 DCHECK(!is_swap_buffers_pending_); | 271 DCHECK(!is_swap_buffers_pending_); |
350 is_swap_buffers_pending_ = true; | 272 is_swap_buffers_pending_ = true; |
351 return true; | 273 return true; |
352 } | 274 } |
353 | 275 |
354 std::string TextureImageTransportSurface::GetExtensions() { | 276 std::string TextureImageTransportSurface::GetExtensions() { |
355 std::string extensions = gfx::GLSurface::GetExtensions(); | 277 std::string extensions = gfx::GLSurface::GetExtensions(); |
356 extensions += extensions.empty() ? "" : " "; | 278 extensions += extensions.empty() ? "" : " "; |
357 extensions += "GL_CHROMIUM_front_buffer_cached "; | 279 extensions += "GL_CHROMIUM_front_buffer_cached "; |
358 extensions += "GL_CHROMIUM_post_sub_buffer"; | 280 extensions += "GL_CHROMIUM_post_sub_buffer"; |
359 return extensions; | 281 return extensions; |
360 } | 282 } |
361 | 283 |
362 gfx::Size TextureImageTransportSurface::GetSize() { | 284 gfx::Size TextureImageTransportSurface::GetSize() { |
363 gfx::Size size = textures_[back()].size; | 285 gfx::Size size = current_size_; |
364 | 286 |
365 // OSMesa expects a non-zero size. | 287 // OSMesa expects a non-zero size. |
366 return gfx::Size(size.width() == 0 ? 1 : size.width(), | 288 return gfx::Size(size.width() == 0 ? 1 : size.width(), |
367 size.height() == 0 ? 1 : size.height()); | 289 size.height() == 0 ? 1 : size.height()); |
368 } | 290 } |
369 | 291 |
370 void* TextureImageTransportSurface::GetHandle() { | 292 void* TextureImageTransportSurface::GetHandle() { |
371 return surface_.get() ? surface_->GetHandle() : NULL; | 293 return surface_.get() ? surface_->GetHandle() : NULL; |
372 } | 294 } |
373 | 295 |
374 unsigned TextureImageTransportSurface::GetFormat() { | 296 unsigned TextureImageTransportSurface::GetFormat() { |
375 return surface_.get() ? surface_->GetFormat() : 0; | 297 return surface_.get() ? surface_->GetFormat() : 0; |
376 } | 298 } |
377 | 299 |
378 void TextureImageTransportSurface::OnSetFrontSurfaceIsProtected( | 300 void TextureImageTransportSurface::OnBufferPresented(uint64 surface_handle, |
379 bool is_protected, uint32 protection_state_id) { | |
380 protection_state_id_ = protection_state_id; | |
381 if (frontbuffer_is_protected_ == is_protected) | |
382 return; | |
383 frontbuffer_is_protected_ = is_protected; | |
384 AdjustFrontBufferAllocation(); | |
385 | |
386 // If surface is set to protected, and we haven't actually released it yet, | |
387 // we can set the ui surface handle now just by sending a swap message. | |
388 if (is_protected && textures_[front()].info->service_id() && | |
389 textures_[front()].sent_to_client) { | |
390 GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params params; | |
391 params.surface_handle = textures_[front()].client_id; | |
392 params.size = textures_[front()].size; | |
393 params.protection_state_id = protection_state_id_; | |
394 params.skip_ack = true; | |
395 helper_->SendAcceleratedSurfaceBuffersSwapped(params); | |
396 } | |
397 } | |
398 | |
399 void TextureImageTransportSurface::OnBufferPresented(bool presented, | |
400 uint32 sync_point) { | 301 uint32 sync_point) { |
401 if (sync_point == 0) { | 302 if (sync_point == 0) { |
402 BufferPresentedImpl(presented); | 303 BufferPresentedImpl(surface_handle); |
403 } else { | 304 } else { |
404 helper_->manager()->sync_point_manager()->AddSyncPointCallback( | 305 helper_->manager()->sync_point_manager()->AddSyncPointCallback( |
405 sync_point, | 306 sync_point, |
406 base::Bind(&TextureImageTransportSurface::BufferPresentedImpl, | 307 base::Bind(&TextureImageTransportSurface::BufferPresentedImpl, |
407 this->AsWeakPtr(), | 308 this, |
408 presented)); | 309 surface_handle)); |
409 } | 310 } |
| 311 |
| 312 // Careful, we might get deleted now if we were only waiting for |
| 313 // a final swap ACK. |
| 314 Release(); |
410 } | 315 } |
411 | 316 |
412 void TextureImageTransportSurface::BufferPresentedImpl(bool presented) { | 317 void TextureImageTransportSurface::BufferPresentedImpl(uint64 surface_handle) { |
| 318 DCHECK(!backbuffer_.service_id); |
| 319 if (surface_handle) { |
| 320 DCHECK(surface_handle == 1 || surface_handle == 2); |
| 321 backbuffer_.surface_handle = surface_handle; |
| 322 ConsumeTexture(backbuffer_); |
| 323 } else { |
| 324 // We didn't get back a texture, so allocate 'the other' buffer. |
| 325 backbuffer_.surface_handle = (backbuffer_.surface_handle == 1) ? 2 : 1; |
| 326 mailbox_name(backbuffer_.surface_handle) = MailboxName(); |
| 327 } |
| 328 |
| 329 if (stub_destroyed_ && backbuffer_.service_id) { |
| 330 // TODO(sievers): Remove this after changes to the mailbox to take ownership |
| 331 // of the service ids. |
| 332 DCHECK(context_.get() && surface_.get()); |
| 333 if (context_->MakeCurrent(surface_.get())) |
| 334 glDeleteTextures(1, &backbuffer_.service_id); |
| 335 |
| 336 return; |
| 337 } |
| 338 |
413 DCHECK(is_swap_buffers_pending_); | 339 DCHECK(is_swap_buffers_pending_); |
414 is_swap_buffers_pending_ = false; | 340 is_swap_buffers_pending_ = false; |
415 | 341 |
416 if (presented) { | 342 // We should not have allowed the backbuffer to be discarded while the ack |
417 // If we had not flipped, the two frame damage tracking is inconsistent. | 343 // was pending. |
418 // So conservatively take the whole frame. | 344 DCHECK(backbuffer_suggested_allocation_); |
419 if (!did_flip_) | |
420 previous_damage_rect_ = gfx::Rect(textures_[front()].size); | |
421 } else { | |
422 front_ = back(); | |
423 previous_damage_rect_ = gfx::Rect(0, 0, 0, 0); | |
424 } | |
425 | |
426 did_flip_ = presented; | |
427 | 345 |
428 // We're relying on the fact that the parent context is | 346 // We're relying on the fact that the parent context is |
429 // finished with it's context when it inserts the sync point that | 347 // finished with it's context when it inserts the sync point that |
430 // triggers this callback. | 348 // triggers this callback. |
431 if (helper_->MakeCurrent()) { | 349 if (helper_->MakeCurrent()) { |
432 if ((presented && textures_[front()].size != textures_[back()].size) || | 350 if (backbuffer_.size != current_size_ || !backbuffer_.service_id) |
433 !textures_[back()].info->service_id() || | 351 CreateBackTexture(); |
434 !textures_[back()].sent_to_client) { | 352 else |
435 // We may get an ACK from a stale swap just to reschedule. In that case, | |
436 // we may not have a backbuffer suggestion and should not recreate one. | |
437 if (backbuffer_suggested_allocation_) | |
438 CreateBackTexture(textures_[front()].size); | |
439 } else { | |
440 AttachBackTextureToFBO(); | 353 AttachBackTextureToFBO(); |
441 } | |
442 } | 354 } |
443 | 355 |
444 // Even if MakeCurrent fails, schedule anyway, to trigger the lost context | 356 // Even if MakeCurrent fails, schedule anyway, to trigger the lost context |
445 // logic. | 357 // logic. |
446 if (did_unschedule_) { | 358 if (did_unschedule_) { |
447 did_unschedule_ = false; | 359 did_unschedule_ = false; |
448 helper_->SetScheduled(true); | 360 helper_->SetScheduled(true); |
449 } | 361 } |
450 } | 362 } |
451 | 363 |
452 void TextureImageTransportSurface::OnResizeViewACK() { | 364 void TextureImageTransportSurface::OnResizeViewACK() { |
453 NOTREACHED(); | 365 NOTREACHED(); |
454 } | 366 } |
455 | 367 |
456 void TextureImageTransportSurface::ReleaseTexture(int id) { | 368 void TextureImageTransportSurface::ReleaseBackTexture() { |
457 if (!parent_stub_) | 369 if (!backbuffer_.service_id) |
458 return; | 370 return; |
459 Texture& texture = textures_[id]; | |
460 TextureInfo* info = texture.info; | |
461 DCHECK(info); | |
462 | 371 |
463 GLuint service_id = info->service_id(); | 372 glDeleteTextures(1, &backbuffer_.service_id); |
464 if (!service_id) | 373 backbuffer_.service_id = 0; |
465 return; | 374 mailbox_name(backbuffer_.surface_handle) = MailboxName(); |
466 info->SetServiceId(0); | |
467 | |
468 { | |
469 ScopedFrameBufferBinder fbo_binder(fbo_id_); | |
470 glDeleteTextures(1, &service_id); | |
471 } | |
472 glFlush(); | 375 glFlush(); |
473 CHECK_GL_ERROR(); | 376 CHECK_GL_ERROR(); |
474 } | 377 } |
475 | 378 |
476 void TextureImageTransportSurface::CreateBackTexture(const gfx::Size& size) { | 379 void TextureImageTransportSurface::CreateBackTexture() { |
477 if (!parent_stub_) | 380 // If |is_swap_buffers_pending| we are waiting for our backbuffer |
478 return; | 381 // in the mailbox, so we shouldn't be reallocating it now. |
479 Texture& texture = textures_[back()]; | 382 DCHECK(!is_swap_buffers_pending_); |
480 TextureInfo* info = texture.info; | |
481 DCHECK(info); | |
482 | 383 |
483 GLuint service_id = info->service_id(); | 384 if (backbuffer_.service_id && backbuffer_.size == current_size_) |
484 | |
485 if (service_id && texture.size == size && texture.sent_to_client) | |
486 return; | 385 return; |
487 | 386 |
488 if (!service_id) { | 387 if (!backbuffer_.service_id) { |
489 glGenTextures(1, &service_id); | 388 MailboxName new_mailbox_name; |
490 info->SetServiceId(service_id); | 389 MailboxName& name = mailbox_name(backbuffer_.surface_handle); |
| 390 // This slot should be uninitialized. |
| 391 DCHECK(!memcmp(&name, &new_mailbox_name, sizeof(MailboxName))); |
| 392 mailbox_manager_->GenerateMailboxName(&new_mailbox_name); |
| 393 name = new_mailbox_name; |
| 394 glGenTextures(1, &backbuffer_.service_id); |
491 } | 395 } |
492 | 396 |
493 if (size != texture.size) { | 397 backbuffer_.size = current_size_; |
494 texture.size = size; | |
495 TextureManager* texture_manager = | |
496 parent_stub_->decoder()->GetContextGroup()->texture_manager(); | |
497 texture_manager->SetLevelInfo( | |
498 info, | |
499 GL_TEXTURE_2D, | |
500 0, | |
501 GL_RGBA, | |
502 size.width(), | |
503 size.height(), | |
504 1, | |
505 0, | |
506 GL_RGBA, | |
507 GL_UNSIGNED_BYTE, | |
508 true); | |
509 } | |
510 | 398 |
511 { | 399 { |
512 ScopedTextureBinder texture_binder(service_id); | 400 ScopedTextureBinder texture_binder(backbuffer_.service_id); |
513 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 401 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
514 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | 402 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
515 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 403 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
516 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 404 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
517 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, | 405 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, |
518 size.width(), size.height(), 0, | 406 current_size_.width(), current_size_.height(), 0, |
519 GL_RGBA, GL_UNSIGNED_BYTE, NULL); | 407 GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
520 CHECK_GL_ERROR(); | 408 CHECK_GL_ERROR(); |
521 } | 409 } |
522 | 410 |
523 AttachBackTextureToFBO(); | 411 AttachBackTextureToFBO(); |
524 | 412 |
| 413 const MailboxName& name = mailbox_name(backbuffer_.surface_handle); |
| 414 |
525 GpuHostMsg_AcceleratedSurfaceNew_Params params; | 415 GpuHostMsg_AcceleratedSurfaceNew_Params params; |
526 params.width = size.width(); | 416 params.width = current_size_.width(); |
527 params.height = size.height(); | 417 params.height = current_size_.height(); |
528 params.surface_handle = texture.client_id; | 418 params.surface_handle = backbuffer_.surface_handle; |
| 419 params.mailbox_name.append( |
| 420 reinterpret_cast<const char*>(&name), sizeof(name)); |
529 helper_->SendAcceleratedSurfaceNew(params); | 421 helper_->SendAcceleratedSurfaceNew(params); |
530 texture.sent_to_client = true; | |
531 } | 422 } |
532 | 423 |
533 void TextureImageTransportSurface::AttachBackTextureToFBO() { | 424 void TextureImageTransportSurface::AttachBackTextureToFBO() { |
534 if (!parent_stub_) | 425 DCHECK(backbuffer_.service_id); |
535 return; | |
536 TextureInfo* info = textures_[back()].info; | |
537 DCHECK(info); | |
538 | |
539 ScopedFrameBufferBinder fbo_binder(fbo_id_); | 426 ScopedFrameBufferBinder fbo_binder(fbo_id_); |
540 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, | 427 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, |
541 GL_COLOR_ATTACHMENT0, | 428 GL_COLOR_ATTACHMENT0, |
542 GL_TEXTURE_2D, | 429 GL_TEXTURE_2D, |
543 info->service_id(), | 430 backbuffer_.service_id, |
544 0); | 431 0); |
545 glFlush(); | 432 glFlush(); |
546 CHECK_GL_ERROR(); | 433 CHECK_GL_ERROR(); |
547 | 434 |
548 #ifndef NDEBUG | 435 #ifndef NDEBUG |
549 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER); | 436 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER); |
550 if (status != GL_FRAMEBUFFER_COMPLETE) { | 437 if (status != GL_FRAMEBUFFER_COMPLETE) { |
551 DLOG(ERROR) << "Framebuffer incomplete."; | 438 DLOG(FATAL) << "Framebuffer incomplete: " << status; |
552 } | 439 } |
553 #endif | 440 #endif |
554 } | 441 } |
555 | 442 |
556 void TextureImageTransportSurface::ReleaseParentStub() { | 443 void TextureImageTransportSurface::ConsumeTexture(Texture& texture) { |
557 DCHECK(parent_stub_); | 444 DCHECK(!texture.service_id); |
558 parent_stub_->RemoveDestructionObserver(this); | 445 DCHECK(texture.surface_handle == 1 || texture.surface_handle == 2); |
559 for (int i = 0; i < 2; ++i) { | 446 |
560 Texture& texture = textures_[i]; | 447 scoped_ptr<TextureDefinition> definition(mailbox_manager_->ConsumeTexture( |
561 texture.info = NULL; | 448 GL_TEXTURE_2D, mailbox_name(texture.surface_handle))); |
562 if (!texture.sent_to_client) | 449 if (definition.get()) { |
563 continue; | 450 texture.service_id = definition->ReleaseServiceId(); |
564 GpuHostMsg_AcceleratedSurfaceRelease_Params params; | 451 texture.size = gfx::Size(definition->level_infos()[0][0].width, |
565 params.identifier = texture.client_id; | 452 definition->level_infos()[0][0].height); |
566 helper_->SendAcceleratedSurfaceRelease(params); | |
567 } | 453 } |
568 parent_stub_ = NULL; | 454 } |
| 455 |
| 456 void TextureImageTransportSurface::ProduceTexture(Texture& texture) { |
| 457 DCHECK(texture.service_id); |
| 458 DCHECK(texture.surface_handle == 1 || texture.surface_handle == 2); |
| 459 TextureManager* texture_manager = |
| 460 helper_->stub()->decoder()->GetContextGroup()->texture_manager(); |
| 461 DCHECK(texture.size.width() > 0 && texture.size.height() > 0); |
| 462 TextureDefinition::LevelInfo info( |
| 463 GL_TEXTURE_2D, GL_RGBA, texture.size.width(), texture.size.height(), 1, |
| 464 0, GL_RGBA, GL_UNSIGNED_BYTE, true); |
| 465 |
| 466 TextureDefinition::LevelInfos level_infos; |
| 467 level_infos.resize(1); |
| 468 level_infos[0].resize(texture_manager->MaxLevelsForTarget(GL_TEXTURE_2D)); |
| 469 level_infos[0][0] = info; |
| 470 scoped_ptr<TextureDefinition> definition(new TextureDefinition( |
| 471 GL_TEXTURE_2D, |
| 472 texture.service_id, |
| 473 GL_LINEAR, |
| 474 GL_LINEAR, |
| 475 GL_CLAMP_TO_EDGE, |
| 476 GL_CLAMP_TO_EDGE, |
| 477 GL_NONE, |
| 478 true, |
| 479 level_infos)); |
| 480 // Pass NULL as |owner| here to avoid errors from glConsumeTextureCHROMIUM() |
| 481 // when the renderer context group goes away before the RWHV handles a pending |
| 482 // ACK. We avoid leaking a texture in the mailbox by waiting for the final ACK |
| 483 // at which point we consume the correct texture back. |
| 484 mailbox_manager_->ProduceTexture( |
| 485 GL_TEXTURE_2D, |
| 486 mailbox_name(texture.surface_handle), |
| 487 definition.release(), |
| 488 NULL); |
| 489 texture.service_id = 0; |
569 } | 490 } |
570 | 491 |
571 } // namespace content | 492 } // namespace content |
OLD | NEW |