OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/renderer/gpu/mailbox_output_surface.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "cc/compositor_frame.h" |
| 9 #include "cc/compositor_frame_ack.h" |
| 10 #include "cc/gl_frame_data.h" |
| 11 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3
D.h" |
| 12 #include "third_party/khronos/GLES2/gl2.h" |
| 13 #include "third_party/khronos/GLES2/gl2ext.h" |
| 14 |
| 15 using cc::CompositorFrame; |
| 16 using cc::GLFrameData; |
| 17 using cc::Mailbox; |
| 18 using WebKit::WebGraphicsContext3D; |
| 19 |
| 20 namespace content { |
| 21 |
| 22 MailboxOutputSurface::MailboxOutputSurface( |
| 23 int32 routing_id, |
| 24 WebGraphicsContext3D* context3D, |
| 25 cc::SoftwareOutputDevice* software_device) |
| 26 : CompositorOutputSurface(routing_id, |
| 27 context3D, |
| 28 software_device), |
| 29 fbo_(0) { |
| 30 } |
| 31 |
| 32 MailboxOutputSurface::~MailboxOutputSurface() { |
| 33 DiscardBackbuffer(); |
| 34 } |
| 35 |
| 36 void MailboxOutputSurface::EnsureBackbuffer() { |
| 37 if (!current_backing_.texture_id) { |
| 38 // Find a texture of matching size to recycle. |
| 39 while (!returned_textures_.empty()) { |
| 40 TransferableFrame& texture = returned_textures_.front(); |
| 41 if (texture.size == size_) { |
| 42 current_backing_ = texture; |
| 43 returned_textures_.pop(); |
| 44 break; |
| 45 } |
| 46 |
| 47 context3d_->deleteTexture(texture.texture_id); |
| 48 returned_textures_.pop(); |
| 49 } |
| 50 |
| 51 if (!current_backing_.texture_id) { |
| 52 current_backing_.texture_id = context3d_->createTexture(); |
| 53 current_backing_.size = size_; |
| 54 context3d_->genMailboxCHROMIUM(current_backing_.mailbox.name); |
| 55 context3d_->bindTexture(GL_TEXTURE_2D, current_backing_.texture_id); |
| 56 context3d_->texParameteri( |
| 57 GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 58 context3d_->texParameteri( |
| 59 GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 60 context3d_->texParameteri( |
| 61 GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 62 context3d_->texParameteri( |
| 63 GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 64 context3d_->texImage2D( |
| 65 GL_TEXTURE_2D, 0, GL_RGBA, size_.width(), size_.height(), 0, |
| 66 GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 67 } |
| 68 } |
| 69 } |
| 70 |
| 71 void MailboxOutputSurface::DiscardBackbuffer() { |
| 72 if (current_backing_.texture_id) { |
| 73 context3d_->deleteTexture(current_backing_.texture_id); |
| 74 current_backing_ = TransferableFrame(); |
| 75 } |
| 76 |
| 77 while (!returned_textures_.empty()) { |
| 78 context3d_->deleteTexture(returned_textures_.front().texture_id); |
| 79 returned_textures_.pop(); |
| 80 } |
| 81 |
| 82 if (fbo_) { |
| 83 context3d_->bindFramebuffer(GL_FRAMEBUFFER, fbo_); |
| 84 context3d_->deleteFramebuffer(fbo_); |
| 85 fbo_ = 0; |
| 86 } |
| 87 } |
| 88 |
| 89 void MailboxOutputSurface::Reshape(gfx::Size size) { |
| 90 if (size == size_) |
| 91 return; |
| 92 |
| 93 size_ = size; |
| 94 DiscardBackbuffer(); |
| 95 EnsureBackbuffer(); |
| 96 } |
| 97 |
| 98 void MailboxOutputSurface::BindFramebuffer() { |
| 99 EnsureBackbuffer(); |
| 100 DCHECK(current_backing_.texture_id); |
| 101 |
| 102 if (!fbo_) |
| 103 fbo_ = context3d_->createFramebuffer(); |
| 104 context3d_->bindFramebuffer(GL_FRAMEBUFFER, fbo_); |
| 105 context3d_->framebufferTexture2D( |
| 106 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
| 107 current_backing_.texture_id, 0); |
| 108 } |
| 109 |
| 110 void MailboxOutputSurface::SendFrameToParentCompositor( |
| 111 cc::CompositorFrame* frame) { |
| 112 frame->gl_frame_data.reset(new GLFrameData()); |
| 113 |
| 114 DCHECK(!size_.IsEmpty()); |
| 115 DCHECK(size_ == current_backing_.size); |
| 116 DCHECK(!current_backing_.mailbox.isZero()); |
| 117 |
| 118 context3d_->framebufferTexture2D( |
| 119 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); |
| 120 context3d_->bindTexture(GL_TEXTURE_2D, current_backing_.texture_id); |
| 121 context3d_->produceTextureCHROMIUM( |
| 122 GL_TEXTURE_2D, current_backing_.mailbox.name); |
| 123 frame->gl_frame_data->mailbox = current_backing_.mailbox; |
| 124 frame->gl_frame_data->size = current_backing_.size; |
| 125 context3d_->flush(); |
| 126 frame->gl_frame_data->sync_point = context3d_->insertSyncPoint(); |
| 127 CompositorOutputSurface::SendFrameToParentCompositor(frame); |
| 128 |
| 129 // TODO(sievers): Reuse the texture. |
| 130 context3d_->deleteTexture(current_backing_.texture_id); |
| 131 current_backing_ = TransferableFrame(); |
| 132 } |
| 133 |
| 134 void MailboxOutputSurface::OnSwapAck(const cc::CompositorFrameAck& ack) { |
| 135 if (!ack.gl_frame_data->mailbox.isZero()) { |
| 136 DCHECK(!ack.gl_frame_data->size.IsEmpty()); |
| 137 uint32 texture_id = context3d_->createTexture(); |
| 138 TransferableFrame texture( |
| 139 texture_id, ack.gl_frame_data->mailbox, ack.gl_frame_data->size); |
| 140 |
| 141 context3d_->bindTexture(GL_TEXTURE_2D, texture_id); |
| 142 |
| 143 // If the consumer is bouncing back the same texture (i.e. skipping the |
| 144 // frame), we don't have to synchronize here (sync_point == 0). |
| 145 // TODO: Consider delaying the wait and consume until BindFramebuffer. |
| 146 if (ack.gl_frame_data->sync_point) |
| 147 context3d_->waitSyncPoint(ack.gl_frame_data->sync_point); |
| 148 |
| 149 context3d_->consumeTextureCHROMIUM( |
| 150 GL_TEXTURE_2D, ack.gl_frame_data->mailbox.name); |
| 151 returned_textures_.push(texture); |
| 152 } |
| 153 CompositorOutputSurface::OnSwapAck(ack); |
| 154 } |
| 155 |
| 156 void MailboxOutputSurface::SwapBuffers() { |
| 157 } |
| 158 |
| 159 void MailboxOutputSurface::PostSubBuffer(gfx::Rect rect) { |
| 160 NOTIMPLEMENTED() |
| 161 << "Partial swap not supported with composite-to-mailbox yet."; |
| 162 } |
| 163 |
| 164 } // namespace content |
OLD | NEW |