- add sources.
[platform/framework/web/crosswalk.git] / src / content / renderer / gpu / mailbox_output_surface.cc
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/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"
15
16 using cc::CompositorFrame;
17 using cc::GLFrameData;
18 using cc::ResourceProvider;
19 using gpu::Mailbox;
20
21 namespace content {
22
23 MailboxOutputSurface::MailboxOutputSurface(
24     int32 routing_id,
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,
30                               output_surface_id,
31                               context_provider,
32                               software_device.Pass(),
33                               true),
34       fbo_(0),
35       is_backbuffer_discarded_(false),
36       format_(format) {
37   pending_textures_.push_back(TransferableFrame());
38   capabilities_.max_frames_pending = 1;
39   capabilities_.uses_default_gl_framebuffer = false;
40 }
41
42 MailboxOutputSurface::~MailboxOutputSurface() {
43   DiscardBackbuffer();
44   while (!pending_textures_.empty()) {
45     if (pending_textures_.front().texture_id) {
46       context_provider_->Context3d()->deleteTexture(
47           pending_textures_.front().texture_id);
48     }
49     pending_textures_.pop_front();
50   }
51 }
52
53 void MailboxOutputSurface::EnsureBackbuffer() {
54   is_backbuffer_discarded_ = false;
55
56   WebKit::WebGraphicsContext3D* context3d = context_provider_->Context3d();
57
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();
67         break;
68       }
69
70       context3d->deleteTexture(texture.texture_id);
71       returned_textures_.pop();
72     }
73
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(
87           GL_TEXTURE_2D,
88           0,
89           GLInternalFormat(format_),
90           surface_size_.width(),
91           surface_size_.height(),
92           0,
93           GLDataFormat(format_),
94           GLDataType(format_),
95           NULL);
96       context3d->genMailboxCHROMIUM(current_backing_.mailbox.name);
97       context3d->produceTextureCHROMIUM(
98           GL_TEXTURE_2D, current_backing_.mailbox.name);
99     }
100   }
101 }
102
103 void MailboxOutputSurface::DiscardBackbuffer() {
104   is_backbuffer_discarded_ = true;
105
106   WebKit::WebGraphicsContext3D* context3d = context_provider_->Context3d();
107
108   if (current_backing_.texture_id) {
109     context3d->deleteTexture(current_backing_.texture_id);
110     current_backing_ = TransferableFrame();
111   }
112
113   while (!returned_textures_.empty()) {
114     const TransferableFrame& frame = returned_textures_.front();
115     context3d->deleteTexture(frame.texture_id);
116     returned_textures_.pop();
117   }
118
119   if (fbo_) {
120     context3d->bindFramebuffer(GL_FRAMEBUFFER, fbo_);
121     context3d->deleteFramebuffer(fbo_);
122     fbo_ = 0;
123   }
124 }
125
126 void MailboxOutputSurface::Reshape(gfx::Size size, float scale_factor) {
127   if (size == surface_size_)
128     return;
129
130   surface_size_ = size;
131   device_scale_factor_ = scale_factor;
132   DiscardBackbuffer();
133   EnsureBackbuffer();
134 }
135
136 void MailboxOutputSurface::BindFramebuffer() {
137   EnsureBackbuffer();
138   DCHECK(current_backing_.texture_id);
139
140   WebKit::WebGraphicsContext3D* context3d = context_provider_->Context3d();
141
142   if (!fbo_)
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);
148 }
149
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);
156     return;
157   }
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);
169         break;
170       }
171     }
172     DCHECK(it != pending_textures_.end());
173     it->sync_point = ack.gl_frame_data->sync_point;
174
175     if (!is_backbuffer_discarded_) {
176       returned_textures_.push(*it);
177     } else {
178       context_provider_->Context3d()->deleteTexture(it->texture_id);
179     }
180
181     pending_textures_.erase(it);
182   } else {
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;
188     if (texture_id)
189       context_provider_->Context3d()->deleteTexture(texture_id);
190     pending_textures_.pop_front();
191   }
192   CompositorOutputSurface::OnSwapAck(output_surface_id, ack);
193 }
194
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());
202
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);
208
209   pending_textures_.push_back(current_backing_);
210   current_backing_ = TransferableFrame();
211 }
212
213 size_t MailboxOutputSurface::GetNumAcksPending() {
214   DCHECK(pending_textures_.size());
215   return pending_textures_.size() - 1;
216 }
217
218 }  // namespace content