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.
5 #include "content/renderer/gpu/mailbox_output_surface.h"
7 #include "base/logging.h"
8 #include "cc/output/compositor_frame.h"
9 #include "cc/output/compositor_frame_ack.h"
10 #include "cc/output/gl_frame_data.h"
11 #include "cc/resources/resource_provider.h"
12 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
13 #include "third_party/khronos/GLES2/gl2.h"
14 #include "third_party/khronos/GLES2/gl2ext.h"
16 using cc::CompositorFrame;
17 using cc::GLFrameData;
18 using cc::ResourceProvider;
23 MailboxOutputSurface::MailboxOutputSurface(
25 uint32 output_surface_id,
26 const scoped_refptr<ContextProviderCommandBuffer>& context_provider,
27 scoped_ptr<cc::SoftwareOutputDevice> software_device,
28 cc::ResourceFormat format)
29 : CompositorOutputSurface(routing_id,
32 software_device.Pass(),
35 is_backbuffer_discarded_(false),
37 pending_textures_.push_back(TransferableFrame());
38 capabilities_.max_frames_pending = 1;
39 capabilities_.uses_default_gl_framebuffer = false;
42 MailboxOutputSurface::~MailboxOutputSurface() {
44 while (!pending_textures_.empty()) {
45 if (pending_textures_.front().texture_id) {
46 context_provider_->Context3d()->deleteTexture(
47 pending_textures_.front().texture_id);
49 pending_textures_.pop_front();
53 void MailboxOutputSurface::EnsureBackbuffer() {
54 is_backbuffer_discarded_ = false;
56 WebKit::WebGraphicsContext3D* context3d = context_provider_->Context3d();
58 if (!current_backing_.texture_id) {
59 // Find a texture of matching size to recycle.
60 while (!returned_textures_.empty()) {
61 TransferableFrame& texture = returned_textures_.front();
62 if (texture.size == surface_size_) {
63 current_backing_ = texture;
64 if (current_backing_.sync_point)
65 context3d->waitSyncPoint(current_backing_.sync_point);
66 returned_textures_.pop();
70 context3d->deleteTexture(texture.texture_id);
71 returned_textures_.pop();
74 if (!current_backing_.texture_id) {
75 current_backing_.texture_id = context3d->createTexture();
76 current_backing_.size = surface_size_;
77 context3d->bindTexture(GL_TEXTURE_2D, current_backing_.texture_id);
78 context3d->texParameteri(
79 GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
80 context3d->texParameteri(
81 GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
82 context3d->texParameteri(
83 GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
84 context3d->texParameteri(
85 GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
86 context3d->texImage2D(
89 GLInternalFormat(format_),
90 surface_size_.width(),
91 surface_size_.height(),
93 GLDataFormat(format_),
96 context3d->genMailboxCHROMIUM(current_backing_.mailbox.name);
97 context3d->produceTextureCHROMIUM(
98 GL_TEXTURE_2D, current_backing_.mailbox.name);
103 void MailboxOutputSurface::DiscardBackbuffer() {
104 is_backbuffer_discarded_ = true;
106 WebKit::WebGraphicsContext3D* context3d = context_provider_->Context3d();
108 if (current_backing_.texture_id) {
109 context3d->deleteTexture(current_backing_.texture_id);
110 current_backing_ = TransferableFrame();
113 while (!returned_textures_.empty()) {
114 const TransferableFrame& frame = returned_textures_.front();
115 context3d->deleteTexture(frame.texture_id);
116 returned_textures_.pop();
120 context3d->bindFramebuffer(GL_FRAMEBUFFER, fbo_);
121 context3d->deleteFramebuffer(fbo_);
126 void MailboxOutputSurface::Reshape(gfx::Size size, float scale_factor) {
127 if (size == surface_size_)
130 surface_size_ = size;
131 device_scale_factor_ = scale_factor;
136 void MailboxOutputSurface::BindFramebuffer() {
138 DCHECK(current_backing_.texture_id);
140 WebKit::WebGraphicsContext3D* context3d = context_provider_->Context3d();
143 fbo_ = context3d->createFramebuffer();
144 context3d->bindFramebuffer(GL_FRAMEBUFFER, fbo_);
145 context3d->framebufferTexture2D(
146 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
147 current_backing_.texture_id, 0);
150 void MailboxOutputSurface::OnSwapAck(uint32 output_surface_id,
151 const cc::CompositorFrameAck& ack) {
152 // Ignore message if it's a stale one coming from a different output surface
153 // (e.g. after a lost context).
154 if (output_surface_id != output_surface_id_) {
155 CompositorOutputSurface::OnSwapAck(output_surface_id, ack);
158 if (!ack.gl_frame_data->mailbox.IsZero()) {
159 DCHECK(!ack.gl_frame_data->size.IsEmpty());
160 // The browser could be returning the oldest or any other pending texture
161 // if it decided to skip a frame.
162 std::deque<TransferableFrame>::iterator it;
163 for (it = pending_textures_.begin(); it != pending_textures_.end(); it++) {
164 DCHECK(!it->mailbox.IsZero());
165 if (!memcmp(it->mailbox.name,
166 ack.gl_frame_data->mailbox.name,
167 sizeof(it->mailbox.name))) {
168 DCHECK(it->size == ack.gl_frame_data->size);
172 DCHECK(it != pending_textures_.end());
173 it->sync_point = ack.gl_frame_data->sync_point;
175 if (!is_backbuffer_discarded_) {
176 returned_textures_.push(*it);
178 context_provider_->Context3d()->deleteTexture(it->texture_id);
181 pending_textures_.erase(it);
183 DCHECK(!pending_textures_.empty());
184 // The browser always keeps one texture as the frontbuffer.
185 // If it does not return a mailbox, it discarded the frontbuffer which is
186 // the oldest texture we sent.
187 uint32 texture_id = pending_textures_.front().texture_id;
189 context_provider_->Context3d()->deleteTexture(texture_id);
190 pending_textures_.pop_front();
192 CompositorOutputSurface::OnSwapAck(output_surface_id, ack);
195 void MailboxOutputSurface::SwapBuffers(cc::CompositorFrame* frame) {
196 DCHECK(frame->gl_frame_data);
197 DCHECK(!surface_size_.IsEmpty());
198 DCHECK(surface_size_ == current_backing_.size);
199 DCHECK(frame->gl_frame_data->size == current_backing_.size);
200 DCHECK(!current_backing_.mailbox.IsZero() ||
201 context_provider_->Context3d()->isContextLost());
203 frame->gl_frame_data->mailbox = current_backing_.mailbox;
204 context_provider_->Context3d()->flush();
205 frame->gl_frame_data->sync_point =
206 context_provider_->Context3d()->insertSyncPoint();
207 CompositorOutputSurface::SwapBuffers(frame);
209 pending_textures_.push_back(current_backing_);
210 current_backing_ = TransferableFrame();
213 size_t MailboxOutputSurface::GetNumAcksPending() {
214 DCHECK(pending_textures_.size());
215 return pending_textures_.size() - 1;
218 } // namespace content