Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / renderer / gpu / mailbox_output_surface.cc
blob4a598d4c60647d3e24225d10c56895c28fd86912
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_.uses_default_gl_framebuffer = false;
47 MailboxOutputSurface::~MailboxOutputSurface() {
48 DiscardBackbuffer();
49 while (!pending_textures_.empty()) {
50 if (pending_textures_.front().texture_id) {
51 context_provider_->ContextGL()->DeleteTextures(
52 1, &pending_textures_.front().texture_id);
54 pending_textures_.pop_front();
58 void MailboxOutputSurface::EnsureBackbuffer() {
59 is_backbuffer_discarded_ = false;
61 GLES2Interface* gl = context_provider_->ContextGL();
63 if (!current_backing_.texture_id) {
64 // Find a texture of matching size to recycle.
65 while (!returned_textures_.empty()) {
66 TransferableFrame& texture = returned_textures_.front();
67 if (texture.size == surface_size_) {
68 current_backing_ = texture;
69 if (current_backing_.sync_point)
70 gl->WaitSyncPointCHROMIUM(current_backing_.sync_point);
71 returned_textures_.pop();
72 break;
75 gl->DeleteTextures(1, &texture.texture_id);
76 returned_textures_.pop();
79 if (!current_backing_.texture_id) {
80 gl->GenTextures(1, &current_backing_.texture_id);
81 current_backing_.size = surface_size_;
82 gl->BindTexture(GL_TEXTURE_2D, current_backing_.texture_id);
83 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
84 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
85 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
86 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
87 gl->TexImage2D(GL_TEXTURE_2D,
89 GLInternalFormat(format_),
90 surface_size_.width(),
91 surface_size_.height(),
93 GLDataFormat(format_),
94 GLDataType(format_),
95 NULL);
96 gl->GenMailboxCHROMIUM(current_backing_.mailbox.name);
97 gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, current_backing_.mailbox.name);
102 void MailboxOutputSurface::DiscardBackbuffer() {
103 is_backbuffer_discarded_ = true;
105 GLES2Interface* gl = context_provider_->ContextGL();
107 if (current_backing_.texture_id) {
108 gl->DeleteTextures(1, &current_backing_.texture_id);
109 current_backing_ = TransferableFrame();
112 while (!returned_textures_.empty()) {
113 const TransferableFrame& frame = returned_textures_.front();
114 gl->DeleteTextures(1, &frame.texture_id);
115 returned_textures_.pop();
118 if (fbo_) {
119 gl->BindFramebuffer(GL_FRAMEBUFFER, fbo_);
120 gl->DeleteFramebuffers(1, &fbo_);
121 fbo_ = 0;
125 void MailboxOutputSurface::Reshape(const gfx::Size& size, float scale_factor) {
126 if (size == surface_size_)
127 return;
129 surface_size_ = size;
130 device_scale_factor_ = scale_factor;
131 DiscardBackbuffer();
132 EnsureBackbuffer();
135 void MailboxOutputSurface::BindFramebuffer() {
136 EnsureBackbuffer();
137 DCHECK(current_backing_.texture_id);
139 GLES2Interface* gl = context_provider_->ContextGL();
141 if (!fbo_)
142 gl->GenFramebuffers(1, &fbo_);
143 gl->BindFramebuffer(GL_FRAMEBUFFER, fbo_);
144 gl->FramebufferTexture2D(GL_FRAMEBUFFER,
145 GL_COLOR_ATTACHMENT0,
146 GL_TEXTURE_2D,
147 current_backing_.texture_id,
151 void MailboxOutputSurface::OnSwapAck(uint32 output_surface_id,
152 const cc::CompositorFrameAck& ack) {
153 // Ignore message if it's a stale one coming from a different output surface
154 // (e.g. after a lost context).
155 if (output_surface_id != output_surface_id_) {
156 CompositorOutputSurface::OnSwapAck(output_surface_id, ack);
157 return;
159 if (!ack.gl_frame_data->mailbox.IsZero()) {
160 DCHECK(!ack.gl_frame_data->size.IsEmpty());
161 // The browser could be returning the oldest or any other pending texture
162 // if it decided to skip a frame.
163 std::deque<TransferableFrame>::iterator it;
164 for (it = pending_textures_.begin(); it != pending_textures_.end(); it++) {
165 DCHECK(!it->mailbox.IsZero());
166 if (!memcmp(it->mailbox.name,
167 ack.gl_frame_data->mailbox.name,
168 sizeof(it->mailbox.name))) {
169 DCHECK(it->size == ack.gl_frame_data->size);
170 break;
173 DCHECK(it != pending_textures_.end());
174 it->sync_point = ack.gl_frame_data->sync_point;
176 if (!is_backbuffer_discarded_) {
177 returned_textures_.push(*it);
178 } else {
179 context_provider_->ContextGL()->DeleteTextures(1, &it->texture_id);
182 pending_textures_.erase(it);
183 } else {
184 DCHECK(!pending_textures_.empty());
185 // The browser always keeps one texture as the frontbuffer.
186 // If it does not return a mailbox, it discarded the frontbuffer which is
187 // the oldest texture we sent.
188 uint32 texture_id = pending_textures_.front().texture_id;
189 if (texture_id)
190 context_provider_->ContextGL()->DeleteTextures(1, &texture_id);
191 pending_textures_.pop_front();
193 CompositorOutputSurface::OnSwapAck(output_surface_id, ack);
196 void MailboxOutputSurface::SwapBuffers(cc::CompositorFrame* frame) {
197 DCHECK(frame->gl_frame_data);
198 DCHECK(!surface_size_.IsEmpty());
199 DCHECK(surface_size_ == current_backing_.size);
200 DCHECK(frame->gl_frame_data->size == current_backing_.size);
201 DCHECK_IMPLIES(current_backing_.mailbox.IsZero(),
202 context_provider_->ContextGL()->GetGraphicsResetStatusKHR() !=
203 GL_NO_ERROR);
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