| 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/renderer/gpu/mailbox_output_surface.h" | 5 #include "content/renderer/gpu/mailbox_output_surface.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "cc/output/compositor_frame.h" | 8 #include "cc/output/compositor_frame.h" |
| 9 #include "cc/output/compositor_frame_ack.h" | 9 #include "cc/output/compositor_frame_ack.h" |
| 10 #include "cc/output/gl_frame_data.h" | 10 #include "cc/output/gl_frame_data.h" |
| 11 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h" | 11 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h" |
| 12 #include "third_party/khronos/GLES2/gl2.h" | 12 #include "third_party/khronos/GLES2/gl2.h" |
| 13 #include "third_party/khronos/GLES2/gl2ext.h" | 13 #include "third_party/khronos/GLES2/gl2ext.h" |
| 14 | 14 |
| 15 using cc::CompositorFrame; | 15 using cc::CompositorFrame; |
| 16 using cc::GLFrameData; | 16 using cc::GLFrameData; |
| 17 using gpu::Mailbox; | 17 using gpu::Mailbox; |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 | 20 |
| 21 MailboxOutputSurface::MailboxOutputSurface( | 21 MailboxOutputSurface::MailboxOutputSurface( |
| 22 int32 routing_id, | 22 int32 routing_id, |
| 23 uint32 output_surface_id, | 23 uint32 output_surface_id, |
| 24 WebGraphicsContext3DCommandBufferImpl* context3D, | 24 const scoped_refptr<ContextProviderCommandBuffer>& context_provider, |
| 25 cc::SoftwareOutputDevice* software_device) | 25 scoped_ptr<cc::SoftwareOutputDevice> software_device) |
| 26 : CompositorOutputSurface(routing_id, | 26 : CompositorOutputSurface(routing_id, |
| 27 output_surface_id, | 27 output_surface_id, |
| 28 context3D, | 28 context_provider, |
| 29 software_device, | 29 software_device.Pass(), |
| 30 true), | 30 true), |
| 31 fbo_(0), | 31 fbo_(0), |
| 32 is_backbuffer_discarded_(false) { | 32 is_backbuffer_discarded_(false) { |
| 33 pending_textures_.push_back(TransferableFrame()); | 33 pending_textures_.push_back(TransferableFrame()); |
| 34 capabilities_.max_frames_pending = 1; | 34 capabilities_.max_frames_pending = 1; |
| 35 } | 35 } |
| 36 | 36 |
| 37 MailboxOutputSurface::~MailboxOutputSurface() { | 37 MailboxOutputSurface::~MailboxOutputSurface() { |
| 38 DiscardBackbuffer(); | 38 DiscardBackbuffer(); |
| 39 while (!pending_textures_.empty()) { | 39 while (!pending_textures_.empty()) { |
| 40 if (pending_textures_.front().texture_id) | 40 if (pending_textures_.front().texture_id) { |
| 41 context3d_->deleteTexture(pending_textures_.front().texture_id); | 41 context_provider_->Context3d()->deleteTexture( |
| 42 pending_textures_.front().texture_id); |
| 43 } |
| 42 pending_textures_.pop_front(); | 44 pending_textures_.pop_front(); |
| 43 } | 45 } |
| 44 } | 46 } |
| 45 | 47 |
| 46 void MailboxOutputSurface::EnsureBackbuffer() { | 48 void MailboxOutputSurface::EnsureBackbuffer() { |
| 47 is_backbuffer_discarded_ = false; | 49 is_backbuffer_discarded_ = false; |
| 48 | 50 |
| 51 WebKit::WebGraphicsContext3D* context3d = context_provider_->Context3d(); |
| 52 |
| 49 if (!current_backing_.texture_id) { | 53 if (!current_backing_.texture_id) { |
| 50 // Find a texture of matching size to recycle. | 54 // Find a texture of matching size to recycle. |
| 51 while (!returned_textures_.empty()) { | 55 while (!returned_textures_.empty()) { |
| 52 TransferableFrame& texture = returned_textures_.front(); | 56 TransferableFrame& texture = returned_textures_.front(); |
| 53 if (texture.size == surface_size_) { | 57 if (texture.size == surface_size_) { |
| 54 current_backing_ = texture; | 58 current_backing_ = texture; |
| 55 if (current_backing_.sync_point) | 59 if (current_backing_.sync_point) |
| 56 context3d_->waitSyncPoint(current_backing_.sync_point); | 60 context3d->waitSyncPoint(current_backing_.sync_point); |
| 57 returned_textures_.pop(); | 61 returned_textures_.pop(); |
| 58 break; | 62 break; |
| 59 } | 63 } |
| 60 | 64 |
| 61 context3d_->deleteTexture(texture.texture_id); | 65 context3d->deleteTexture(texture.texture_id); |
| 62 returned_textures_.pop(); | 66 returned_textures_.pop(); |
| 63 } | 67 } |
| 64 | 68 |
| 65 if (!current_backing_.texture_id) { | 69 if (!current_backing_.texture_id) { |
| 66 current_backing_.texture_id = context3d_->createTexture(); | 70 current_backing_.texture_id = context3d->createTexture(); |
| 67 current_backing_.size = surface_size_; | 71 current_backing_.size = surface_size_; |
| 68 context3d_->bindTexture(GL_TEXTURE_2D, current_backing_.texture_id); | 72 context3d->bindTexture(GL_TEXTURE_2D, current_backing_.texture_id); |
| 69 context3d_->texParameteri( | 73 context3d->texParameteri( |
| 70 GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 74 GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 71 context3d_->texParameteri( | 75 context3d->texParameteri( |
| 72 GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | 76 GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 73 context3d_->texParameteri( | 77 context3d->texParameteri( |
| 74 GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 78 GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 75 context3d_->texParameteri( | 79 context3d->texParameteri( |
| 76 GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 80 GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 77 context3d_->texImage2D( | 81 context3d->texImage2D( |
| 78 GL_TEXTURE_2D, 0, GL_RGBA, | 82 GL_TEXTURE_2D, 0, GL_RGBA, |
| 79 surface_size_.width(), surface_size_.height(), 0, | 83 surface_size_.width(), surface_size_.height(), 0, |
| 80 GL_RGBA, GL_UNSIGNED_BYTE, NULL); | 84 GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 81 context3d_->genMailboxCHROMIUM(current_backing_.mailbox.name); | 85 context3d->genMailboxCHROMIUM(current_backing_.mailbox.name); |
| 82 context3d_->produceTextureCHROMIUM( | 86 context3d->produceTextureCHROMIUM( |
| 83 GL_TEXTURE_2D, current_backing_.mailbox.name); | 87 GL_TEXTURE_2D, current_backing_.mailbox.name); |
| 84 } | 88 } |
| 85 } | 89 } |
| 86 } | 90 } |
| 87 | 91 |
| 88 void MailboxOutputSurface::DiscardBackbuffer() { | 92 void MailboxOutputSurface::DiscardBackbuffer() { |
| 89 is_backbuffer_discarded_ = true; | 93 is_backbuffer_discarded_ = true; |
| 90 | 94 |
| 95 WebKit::WebGraphicsContext3D* context3d = context_provider_->Context3d(); |
| 96 |
| 91 if (current_backing_.texture_id) { | 97 if (current_backing_.texture_id) { |
| 92 context3d_->deleteTexture(current_backing_.texture_id); | 98 context3d->deleteTexture(current_backing_.texture_id); |
| 93 current_backing_ = TransferableFrame(); | 99 current_backing_ = TransferableFrame(); |
| 94 } | 100 } |
| 95 | 101 |
| 96 while (!returned_textures_.empty()) { | 102 while (!returned_textures_.empty()) { |
| 97 const TransferableFrame& frame = returned_textures_.front(); | 103 const TransferableFrame& frame = returned_textures_.front(); |
| 98 context3d_->deleteTexture(frame.texture_id); | 104 context3d->deleteTexture(frame.texture_id); |
| 99 returned_textures_.pop(); | 105 returned_textures_.pop(); |
| 100 } | 106 } |
| 101 | 107 |
| 102 if (fbo_) { | 108 if (fbo_) { |
| 103 context3d_->bindFramebuffer(GL_FRAMEBUFFER, fbo_); | 109 context3d->bindFramebuffer(GL_FRAMEBUFFER, fbo_); |
| 104 context3d_->deleteFramebuffer(fbo_); | 110 context3d->deleteFramebuffer(fbo_); |
| 105 fbo_ = 0; | 111 fbo_ = 0; |
| 106 } | 112 } |
| 107 } | 113 } |
| 108 | 114 |
| 109 void MailboxOutputSurface::Reshape(gfx::Size size, float scale_factor) { | 115 void MailboxOutputSurface::Reshape(gfx::Size size, float scale_factor) { |
| 110 if (size == surface_size_) | 116 if (size == surface_size_) |
| 111 return; | 117 return; |
| 112 | 118 |
| 113 surface_size_ = size; | 119 surface_size_ = size; |
| 114 device_scale_factor_ = scale_factor; | 120 device_scale_factor_ = scale_factor; |
| 115 DiscardBackbuffer(); | 121 DiscardBackbuffer(); |
| 116 EnsureBackbuffer(); | 122 EnsureBackbuffer(); |
| 117 } | 123 } |
| 118 | 124 |
| 119 void MailboxOutputSurface::BindFramebuffer() { | 125 void MailboxOutputSurface::BindFramebuffer() { |
| 120 EnsureBackbuffer(); | 126 EnsureBackbuffer(); |
| 121 DCHECK(current_backing_.texture_id); | 127 DCHECK(current_backing_.texture_id); |
| 122 | 128 |
| 129 WebKit::WebGraphicsContext3D* context3d = context_provider_->Context3d(); |
| 130 |
| 123 if (!fbo_) | 131 if (!fbo_) |
| 124 fbo_ = context3d_->createFramebuffer(); | 132 fbo_ = context3d->createFramebuffer(); |
| 125 context3d_->bindFramebuffer(GL_FRAMEBUFFER, fbo_); | 133 context3d->bindFramebuffer(GL_FRAMEBUFFER, fbo_); |
| 126 context3d_->framebufferTexture2D( | 134 context3d->framebufferTexture2D( |
| 127 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, | 135 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
| 128 current_backing_.texture_id, 0); | 136 current_backing_.texture_id, 0); |
| 129 } | 137 } |
| 130 | 138 |
| 131 void MailboxOutputSurface::OnSwapAck(uint32 output_surface_id, | 139 void MailboxOutputSurface::OnSwapAck(uint32 output_surface_id, |
| 132 const cc::CompositorFrameAck& ack) { | 140 const cc::CompositorFrameAck& ack) { |
| 133 // Ignore message if it's a stale one coming from a different output surface | 141 // Ignore message if it's a stale one coming from a different output surface |
| 134 // (e.g. after a lost context). | 142 // (e.g. after a lost context). |
| 135 if (output_surface_id != output_surface_id_) { | 143 if (output_surface_id != output_surface_id_) { |
| 136 CompositorOutputSurface::OnSwapAck(output_surface_id, ack); | 144 CompositorOutputSurface::OnSwapAck(output_surface_id, ack); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 149 DCHECK(it->size == ack.gl_frame_data->size); | 157 DCHECK(it->size == ack.gl_frame_data->size); |
| 150 break; | 158 break; |
| 151 } | 159 } |
| 152 } | 160 } |
| 153 DCHECK(it != pending_textures_.end()); | 161 DCHECK(it != pending_textures_.end()); |
| 154 it->sync_point = ack.gl_frame_data->sync_point; | 162 it->sync_point = ack.gl_frame_data->sync_point; |
| 155 | 163 |
| 156 if (!is_backbuffer_discarded_) { | 164 if (!is_backbuffer_discarded_) { |
| 157 returned_textures_.push(*it); | 165 returned_textures_.push(*it); |
| 158 } else { | 166 } else { |
| 159 context3d_->deleteTexture(it->texture_id); | 167 context_provider_->Context3d()->deleteTexture(it->texture_id); |
| 160 } | 168 } |
| 161 | 169 |
| 162 pending_textures_.erase(it); | 170 pending_textures_.erase(it); |
| 163 } else { | 171 } else { |
| 164 DCHECK(!pending_textures_.empty()); | 172 DCHECK(!pending_textures_.empty()); |
| 165 // The browser always keeps one texture as the frontbuffer. | 173 // The browser always keeps one texture as the frontbuffer. |
| 166 // If it does not return a mailbox, it discarded the frontbuffer which is | 174 // If it does not return a mailbox, it discarded the frontbuffer which is |
| 167 // the oldest texture we sent. | 175 // the oldest texture we sent. |
| 168 uint32 texture_id = pending_textures_.front().texture_id; | 176 uint32 texture_id = pending_textures_.front().texture_id; |
| 169 if (texture_id) | 177 if (texture_id) |
| 170 context3d_->deleteTexture(texture_id); | 178 context_provider_->Context3d()->deleteTexture(texture_id); |
| 171 pending_textures_.pop_front(); | 179 pending_textures_.pop_front(); |
| 172 } | 180 } |
| 173 CompositorOutputSurface::OnSwapAck(output_surface_id, ack); | 181 CompositorOutputSurface::OnSwapAck(output_surface_id, ack); |
| 174 } | 182 } |
| 175 | 183 |
| 176 void MailboxOutputSurface::SwapBuffers(cc::CompositorFrame* frame) { | 184 void MailboxOutputSurface::SwapBuffers(cc::CompositorFrame* frame) { |
| 177 DCHECK(frame->gl_frame_data); | 185 DCHECK(frame->gl_frame_data); |
| 178 DCHECK(!surface_size_.IsEmpty()); | 186 DCHECK(!surface_size_.IsEmpty()); |
| 179 DCHECK(surface_size_ == current_backing_.size); | 187 DCHECK(surface_size_ == current_backing_.size); |
| 180 DCHECK(frame->gl_frame_data->size == current_backing_.size); | 188 DCHECK(frame->gl_frame_data->size == current_backing_.size); |
| 181 DCHECK(!current_backing_.mailbox.IsZero() || context3d_->isContextLost()); | 189 DCHECK(!current_backing_.mailbox.IsZero() || |
| 190 context_provider_->Context3d()->isContextLost()); |
| 182 | 191 |
| 183 frame->gl_frame_data->mailbox = current_backing_.mailbox; | 192 frame->gl_frame_data->mailbox = current_backing_.mailbox; |
| 184 context3d_->flush(); | 193 context_provider_->Context3d()->flush(); |
| 185 frame->gl_frame_data->sync_point = context3d_->insertSyncPoint(); | 194 frame->gl_frame_data->sync_point = |
| 195 context_provider_->Context3d()->insertSyncPoint(); |
| 186 CompositorOutputSurface::SwapBuffers(frame); | 196 CompositorOutputSurface::SwapBuffers(frame); |
| 187 | 197 |
| 188 pending_textures_.push_back(current_backing_); | 198 pending_textures_.push_back(current_backing_); |
| 189 current_backing_ = TransferableFrame(); | 199 current_backing_ = TransferableFrame(); |
| 190 } | 200 } |
| 191 | 201 |
| 192 size_t MailboxOutputSurface::GetNumAcksPending() { | 202 size_t MailboxOutputSurface::GetNumAcksPending() { |
| 193 DCHECK(pending_textures_.size()); | 203 DCHECK(pending_textures_.size()); |
| 194 return pending_textures_.size() - 1; | 204 return pending_textures_.size() - 1; |
| 195 } | 205 } |
| 196 | 206 |
| 197 } // namespace content | 207 } // namespace content |
| OLD | NEW |