Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / gpu / mailbox_output_surface.cc
blobdbf7acbf7bb92c7bb0f845eedb1176184133d8e2
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 "content/renderer/gpu/frame_swap_message_queue.h"
13 #include "gpu/command_buffer/client/gles2_interface.h"
14 #include "third_party/khronos/GLES2/gl2.h"
15 #include "third_party/khronos/GLES2/gl2ext.h"
17 using cc::CompositorFrame;
18 using cc::GLFrameData;
19 using cc::ResourceProvider;
20 using gpu::Mailbox;
21 using gpu::gles2::GLES2Interface;
23 namespace content {
25 MailboxOutputSurface::MailboxOutputSurface(
26 int32 routing_id,
27 uint32 output_surface_id,
28 const scoped_refptr<ContextProviderCommandBuffer>& context_provider,
29 const scoped_refptr<ContextProviderCommandBuffer>& worker_context_provider,
30 scoped_ptr<cc::SoftwareOutputDevice> software_device,
31 scoped_refptr<FrameSwapMessageQueue> swap_frame_message_queue,
32 cc::ResourceFormat format)
33 : CompositorOutputSurface(routing_id,
34 output_surface_id,
35 context_provider,
36 worker_context_provider,
37 software_device.Pass(),
38 swap_frame_message_queue,
39 true),
40 fbo_(0),
41 is_backbuffer_discarded_(false),
42 format_(format) {
43 pending_textures_.push_back(TransferableFrame());
44 capabilities_.max_frames_pending = 1;
45 capabilities_.uses_default_gl_framebuffer = false;
48 MailboxOutputSurface::~MailboxOutputSurface() {
49 DiscardBackbuffer();
50 while (!pending_textures_.empty()) {
51 if (pending_textures_.front().texture_id) {
52 context_provider_->ContextGL()->DeleteTextures(
53 1, &pending_textures_.front().texture_id);
55 pending_textures_.pop_front();
59 void MailboxOutputSurface::EnsureBackbuffer() {
60 is_backbuffer_discarded_ = false;
62 GLES2Interface* gl = context_provider_->ContextGL();
64 if (!current_backing_.texture_id) {
65 // Find a texture of matching size to recycle.
66 while (!returned_textures_.empty()) {
67 TransferableFrame& texture = returned_textures_.front();
68 if (texture.size == surface_size_) {
69 current_backing_ = texture;
70 if (current_backing_.sync_point)
71 gl->WaitSyncPointCHROMIUM(current_backing_.sync_point);
72 returned_textures_.pop();
73 break;
76 gl->DeleteTextures(1, &texture.texture_id);
77 returned_textures_.pop();
80 if (!current_backing_.texture_id) {
81 gl->GenTextures(1, &current_backing_.texture_id);
82 current_backing_.size = surface_size_;
83 gl->BindTexture(GL_TEXTURE_2D, current_backing_.texture_id);
84 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
85 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
86 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
87 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
88 gl->TexImage2D(GL_TEXTURE_2D,
90 GLInternalFormat(format_),
91 surface_size_.width(),
92 surface_size_.height(),
94 GLDataFormat(format_),
95 GLDataType(format_),
96 NULL);
97 gl->GenMailboxCHROMIUM(current_backing_.mailbox.name);
98 gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, current_backing_.mailbox.name);
103 void MailboxOutputSurface::DiscardBackbuffer() {
104 is_backbuffer_discarded_ = true;
106 GLES2Interface* gl = context_provider_->ContextGL();
108 if (current_backing_.texture_id) {
109 gl->DeleteTextures(1, &current_backing_.texture_id);
110 current_backing_ = TransferableFrame();
113 while (!returned_textures_.empty()) {
114 const TransferableFrame& frame = returned_textures_.front();
115 gl->DeleteTextures(1, &frame.texture_id);
116 returned_textures_.pop();
119 if (fbo_) {
120 gl->BindFramebuffer(GL_FRAMEBUFFER, fbo_);
121 gl->DeleteFramebuffers(1, &fbo_);
122 fbo_ = 0;
126 void MailboxOutputSurface::Reshape(const gfx::Size& size, float scale_factor) {
127 if (size == surface_size_)
128 return;
130 surface_size_ = size;
131 device_scale_factor_ = scale_factor;
132 DiscardBackbuffer();
133 EnsureBackbuffer();
136 void MailboxOutputSurface::BindFramebuffer() {
137 EnsureBackbuffer();
138 DCHECK(current_backing_.texture_id);
140 GLES2Interface* gl = context_provider_->ContextGL();
142 if (!fbo_)
143 gl->GenFramebuffers(1, &fbo_);
144 gl->BindFramebuffer(GL_FRAMEBUFFER, fbo_);
145 gl->FramebufferTexture2D(GL_FRAMEBUFFER,
146 GL_COLOR_ATTACHMENT0,
147 GL_TEXTURE_2D,
148 current_backing_.texture_id,
152 void MailboxOutputSurface::OnSwapAck(uint32 output_surface_id,
153 const cc::CompositorFrameAck& ack) {
154 // Ignore message if it's a stale one coming from a different output surface
155 // (e.g. after a lost context).
156 if (output_surface_id != output_surface_id_) {
157 CompositorOutputSurface::OnSwapAck(output_surface_id, ack);
158 return;
160 if (!ack.gl_frame_data->mailbox.IsZero()) {
161 DCHECK(!ack.gl_frame_data->size.IsEmpty());
162 // The browser could be returning the oldest or any other pending texture
163 // if it decided to skip a frame.
164 std::deque<TransferableFrame>::iterator it;
165 for (it = pending_textures_.begin(); it != pending_textures_.end(); it++) {
166 DCHECK(!it->mailbox.IsZero());
167 if (!memcmp(it->mailbox.name,
168 ack.gl_frame_data->mailbox.name,
169 sizeof(it->mailbox.name))) {
170 DCHECK(it->size == ack.gl_frame_data->size);
171 break;
174 DCHECK(it != pending_textures_.end());
175 it->sync_point = ack.gl_frame_data->sync_point;
177 if (!is_backbuffer_discarded_) {
178 returned_textures_.push(*it);
179 } else {
180 context_provider_->ContextGL()->DeleteTextures(1, &it->texture_id);
183 pending_textures_.erase(it);
184 } else {
185 DCHECK(!pending_textures_.empty());
186 // The browser always keeps one texture as the frontbuffer.
187 // If it does not return a mailbox, it discarded the frontbuffer which is
188 // the oldest texture we sent.
189 uint32 texture_id = pending_textures_.front().texture_id;
190 if (texture_id)
191 context_provider_->ContextGL()->DeleteTextures(1, &texture_id);
192 pending_textures_.pop_front();
194 CompositorOutputSurface::OnSwapAck(output_surface_id, ack);
197 void MailboxOutputSurface::SwapBuffers(cc::CompositorFrame* frame) {
198 DCHECK(frame->gl_frame_data);
199 DCHECK(!surface_size_.IsEmpty());
200 DCHECK(surface_size_ == current_backing_.size);
201 DCHECK(frame->gl_frame_data->size == current_backing_.size);
202 DCHECK(!current_backing_.mailbox.IsZero() ||
203 context_provider_->IsContextLost());
205 frame->gl_frame_data->mailbox = current_backing_.mailbox;
206 context_provider_->ContextGL()->Flush();
207 frame->gl_frame_data->sync_point =
208 context_provider_->ContextGL()->InsertSyncPointCHROMIUM();
209 CompositorOutputSurface::SwapBuffers(frame);
211 pending_textures_.push_back(current_backing_);
212 current_backing_ = TransferableFrame();
215 size_t MailboxOutputSurface::GetNumAcksPending() {
216 DCHECK(pending_textures_.size());
217 return pending_textures_.size() - 1;
220 } // namespace content