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
;
21 using gpu::gles2::GLES2Interface
;
25 MailboxOutputSurface::MailboxOutputSurface(
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
,
36 worker_context_provider
,
37 software_device
.Pass(),
38 swap_frame_message_queue
,
41 is_backbuffer_discarded_(false),
43 pending_textures_
.push_back(TransferableFrame());
44 capabilities_
.max_frames_pending
= 1;
45 capabilities_
.uses_default_gl_framebuffer
= false;
48 MailboxOutputSurface::~MailboxOutputSurface() {
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();
76 gl
->DeleteTextures(1, &texture
.texture_id
);
77 returned_textures_
.pop();
80 if (!current_backing_
.texture_id
) {
81 gl
->GenTextures(1, ¤t_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_
),
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, ¤t_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();
120 gl
->BindFramebuffer(GL_FRAMEBUFFER
, fbo_
);
121 gl
->DeleteFramebuffers(1, &fbo_
);
126 void MailboxOutputSurface::Reshape(const 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 GLES2Interface
* gl
= context_provider_
->ContextGL();
143 gl
->GenFramebuffers(1, &fbo_
);
144 gl
->BindFramebuffer(GL_FRAMEBUFFER
, fbo_
);
145 gl
->FramebufferTexture2D(GL_FRAMEBUFFER
,
146 GL_COLOR_ATTACHMENT0
,
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
);
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
);
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
);
180 context_provider_
->ContextGL()->DeleteTextures(1, &it
->texture_id
);
183 pending_textures_
.erase(it
);
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
;
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