[M67 Dev][Tizen] Redirect UncheckedMalloc to malloc for Tizen
[platform/framework/web/chromium-efl.git] / tizen_src / chromium_impl / content / browser / compositor / mailbox_output_surface_efl.cc
1 // Copyright (c) 2016 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/browser/compositor/mailbox_output_surface_efl.h"
6
7 #include "base/threading/thread_task_runner_handle.h"
8 #include "build/tizen_version.h"
9 //#include "cc/output/output_surface_client.h"
10 #include "components/viz/service/display/output_surface_client.h"
11 //#include "cc/output/output_surface_frame.h"
12 #include "cc/raster/single_thread_task_graph_runner.h"
13 #include "components/viz/service/display/output_surface_frame.h"
14 #include "content/browser/renderer_host/render_widget_host_impl.h"
15 #include "gpu/command_buffer/client/context_support.h"
16 #include "gpu/command_buffer/client/gles2_interface.h"
17 #include "third_party/khronos/GLES2/gl2.h"
18 #include "third_party/khronos/GLES2/gl2ext.h"
19
20 using gpu::gles2::GLES2Interface;
21
22 namespace content {
23
24 MailboxOutputSurfaceEfl::MailboxOutputSurfaceEfl(
25     const scoped_refptr<ui::ContextProviderCommandBuffer>& context_provider,
26     viz::ResourceFormat format)
27     : viz::OutputSurface(context_provider),
28       swap_buffers_completion_callback_(
29           base::Bind(&MailboxOutputSurfaceEfl::OnSwapBuffersCompleted,
30                      base::Unretained(this))),
31       fbo_(0),
32       is_backbuffer_discarded_(false),
33       texture_upload_pending_(false),
34       format_(format) {
35   capabilities_.max_frames_pending = 1;
36   capabilities_.uses_default_gl_framebuffer = false;
37 }
38
39 MailboxOutputSurfaceEfl::~MailboxOutputSurfaceEfl() {}
40
41 void MailboxOutputSurfaceEfl::EnsureBackbuffer() {
42   is_backbuffer_discarded_ = false;
43
44   GLES2Interface* gl = context_provider_->ContextGL();
45
46   if (!current_backing_.texture_id) {
47     // Find a texture of matching size to recycle.
48     while (!returned_textures_.empty()) {
49       TransferableFrame& texture = returned_textures_.front();
50 #if !defined(EWK_BRINGUP)  // FIXME: m67 bringup
51       // FIXME: EWK_BRINGUP definition should be removed.
52       // surface_size_ variable was removed in
53       // cc/output/output_surface.h
54       if (texture.size == surface_size_) {
55         current_backing_ = texture;
56         if (current_backing_.sync_token.HasData())
57           gl->WaitSyncTokenCHROMIUM(current_backing_.sync_token.GetConstData());
58         returned_textures_.pop();
59         break;
60       }
61 #endif  // !defined(EWK_BRINGUP)
62
63       gl->DeleteTextures(1, &texture.texture_id);
64       returned_textures_.pop();
65     }
66
67     if (!current_backing_.texture_id) {
68       gl->GenTextures(1, &current_backing_.texture_id);
69 #if !defined(EWK_BRINGUP)  // FIXME: m67 bringup
70       // FIXME: EWK_BRINGUP definition should be removed.
71       // surface_size_ variable was removed in
72       // cc/output/output_surface.h
73       current_backing_.size = surface_size_;
74 #endif  // !defined(EWK_BRINGUP)
75       gl->BindTexture(GL_TEXTURE_2D, current_backing_.texture_id);
76       gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
77       gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
78       gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
79       gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
80 #if !defined(EWK_BRINGUP)  // FIXME: m67 bringup
81       // FIXME: EWK_BRINGUP definition should be removed.
82       // surface_size_ variable was removed in
83       // cc/output/output_surface.h
84       gl->TexImage2D(GL_TEXTURE_2D, 0, GLInternalFormat(format_),
85                      surface_size_.width(), surface_size_.height(), 0,
86                      GLDataFormat(format_), GLDataType(format_), NULL);
87 #endif  // !defined(EWK_BRINGUP)
88       gl->GenMailboxCHROMIUM(current_backing_.mailbox.name);
89       gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, current_backing_.mailbox.name);
90       texture_upload_pending_ = true;
91     }
92   }
93 }
94
95 /*void MailboxOutputSurfaceEfl::fvDiscardBackbuffer() {
96   is_backbuffer_discarded_ = true;
97
98   GLES2Interface* gl = context_provider_->ContextGL();
99
100   if (current_backing_.texture_id) {
101     gl->DeleteTextures(1, &current_backing_.texture_id);
102     current_backing_ = TransferableFrame();
103   }
104
105   while (!returned_textures_.empty()) {
106     const TransferableFrame& frame = returned_textures_.front();
107     gl->DeleteTextures(1, &frame.texture_id);
108     returned_textures_.pop();
109   }
110
111   if (fbo_) {
112     gl->BindFramebuffer(GL_FRAMEBUFFER, fbo_);
113     gl->DeleteFramebuffers(1, &fbo_);
114     fbo_ = 0;
115   }
116 }*/
117
118 void MailboxOutputSurfaceEfl::Reshape(const gfx::Size& size,
119                                       float scale_factor,
120                                       const gfx::ColorSpace& color_space,
121                                       bool has_alpha,
122                                       bool use_stencil) {
123 #if !defined(EWK_BRINGUP)  // FIXME: m67 bringup
124   // FIXME: EWK_BRINGUP definition should be removed.
125   // surface_size_ variable was removed in cc/output/output_surface.h
126   if (size == surface_size_)
127     return;
128
129   surface_size_ = size;
130   device_scale_factor_ = scale_factor;
131 #endif  // !defined(EWK_BRINGUP)
132   DiscardBackbuffer();
133   EnsureBackbuffer();
134 }
135
136 void MailboxOutputSurfaceEfl::BindFramebuffer() {
137   EnsureBackbuffer();
138   DCHECK(current_backing_.texture_id);
139
140   GLES2Interface* gl = context_provider_->ContextGL();
141
142   if (!fbo_)
143     gl->GenFramebuffers(1, &fbo_);
144   gl->BindFramebuffer(GL_FRAMEBUFFER, fbo_);
145   gl->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
146                            current_backing_.texture_id, 0);
147 }
148
149 #if !defined(EWK_BRINGUP)  // FIXME: m67 bringup
150 // FIXME: EWK_BRINGUP definition should be removed.
151 // cc::GLFrameData was removed in cc/output/gl_frame_data.h
152 void MailboxOutputSurfaceEfl::DrawTexture(
153     std::unique_ptr<cc::GLFrameData> gl_frame_data) {
154   // Draw texture on RWHV
155   if (gl_frame_data) {
156     compositor_client_->GetTextureFromMailbox(&gl_frame_data->mailbox,
157                                               surface_size_);
158   }
159 }
160
161 void MailboxOutputSurfaceEfl::OnSwapAck(
162     std::unique_ptr<cc::GLFrameData> gl_frame_data) {
163   // Ignore message if it's a stale one coming from a different output surface
164   // (e.g. after a lost context).
165
166   if (!gl_frame_data->mailbox.IsZero()) {
167     DCHECK(!gl_frame_data->size.IsEmpty());
168     // The browser could be returning the oldest or any other pending texture
169     // if it decided to skip a frame.
170     std::deque<TransferableFrame>::iterator it;
171     for (it = pending_textures_.begin(); it != pending_textures_.end(); it++) {
172       DCHECK(!it->mailbox.IsZero());
173       if (!memcmp(it->mailbox.name, gl_frame_data->mailbox.name,
174                   sizeof(it->mailbox.name))) {
175         DCHECK(it->size == gl_frame_data->size);
176         break;
177       }
178     }
179     DCHECK(it != pending_textures_.end());
180     it->sync_token = gl_frame_data->sync_token;
181
182     if (!is_backbuffer_discarded_) {
183       returned_textures_.push(*it);
184     } else {
185       context_provider_->ContextGL()->DeleteTextures(1, &it->texture_id);
186     }
187
188     pending_textures_.erase(it);
189   } else {
190     DCHECK(!pending_textures_.empty());
191     // The browser always keeps one texture as the frontbuffer.
192     // If it does not return a mailbox, it discarded the frontbuffer which is
193     // the oldest texture we sent.
194     uint32_t texture_id = pending_textures_.front().texture_id;
195     if (texture_id)
196       context_provider_->ContextGL()->DeleteTextures(1, &texture_id);
197     pending_textures_.pop_front();
198   }
199
200   if (gl_frame_data) {
201     base::Closure closure =
202         base::Bind(&MailboxOutputSurfaceEfl::DrawTexture,
203                    base::Unretained(this), base::Passed(&gl_frame_data));
204     base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, closure);
205   }
206   client_->DidSwapBuffersComplete();
207 }
208 #endif  // !defined(EWK_BRINGUP)
209
210 void MailboxOutputSurfaceEfl::SwapBuffers(viz::OutputSurfaceFrame frame) {
211 #if !defined(EWK_BRINGUP)  // FIXME: m67 bringup
212   // FIXME: EWK_BRINGUP definition should be removed.
213   // The type of frame was changed, so gl_frame_data isn't member of
214   // frame
215   DCHECK(frame.gl_frame_data);
216   DCHECK(!surface_size_.IsEmpty());
217   DCHECK(surface_size_ == current_backing_.size);
218   DCHECK(frame.gl_frame_data->size == current_backing_.size);
219   DCHECK(!current_backing_.mailbox.IsZero() ||
220          context_provider_->ContextGL()->GetGraphicsResetStatusKHR() !=
221              GL_NO_ERROR);
222   frame.gl_frame_data->mailbox = current_backing_.mailbox;
223
224 // Using glFinish call instead of glFlush, fixes black screen issue with
225 // static pages and IE Fish page.
226 // Black screen issue is seen on page in Tizen 2.4 product TV.
227 #if TIZEN_VERSION_EQ(2, 4, 0) && defined(OS_TIZEN_TV)
228   context_provider_->ContextGL()->Finish();
229 #else
230   if (texture_upload_pending_) {
231     context_provider_->ContextGL()->Finish();
232     texture_upload_pending_ = false;
233   } else {
234     context_provider_->ContextGL()->Flush();
235   }
236 #endif
237
238   gpu::gles2::GLES2Interface* gl = context_provider()->ContextGL();
239   const uint64_t fence_sync = gl->InsertFenceSyncCHROMIUM();
240   gl->Flush();
241
242   gpu::SyncToken sync_token;
243   gl->GenSyncTokenCHROMIUM(fence_sync, sync_token.GetData());
244
245   frame.gl_frame_data->sync_token = sync_token;
246
247   base::Closure closure =
248       base::Bind(&MailboxOutputSurfaceEfl::OnSwapAck, base::Unretained(this),
249                  base::Passed(&frame.gl_frame_data));
250
251   context_provider()->ContextSupport()->SignalSyncToken(sync_token, closure);
252
253   client_->DidSwapBuffers();
254
255   pending_textures_.push_back(current_backing_);
256   current_backing_ = TransferableFrame();
257 #endif  // !defined(EWK_BRINGUP)
258 }
259
260 uint32_t MailboxOutputSurfaceEfl::GetFramebufferCopyTextureFormat() {
261   auto* gl =
262       static_cast<ui::ContextProviderCommandBuffer*>(context_provider_.get());
263   return gl->GetCopyTextureInternalFormat();
264 }
265
266 void MailboxOutputSurfaceEfl::BindToClient(viz::OutputSurfaceClient* client) {
267   OutputSurface::BindToClient(client);
268   GetCommandBufferProxy()->SetSwapBuffersCompletionCallback(
269       swap_buffers_completion_callback_.callback());
270   return;
271 }
272
273 gpu::CommandBufferProxyImpl* MailboxOutputSurfaceEfl::GetCommandBufferProxy() {
274   ui::ContextProviderCommandBuffer* provider_command_buffer =
275       static_cast<ui::ContextProviderCommandBuffer*>(context_provider_.get());
276   gpu::CommandBufferProxyImpl* command_buffer_proxy =
277       provider_command_buffer->GetCommandBufferProxy();
278   DCHECK(command_buffer_proxy);
279   return command_buffer_proxy;
280 }
281
282 /*void MailboxOutputSurfaceEfl::OnSwapBuffersCompleted(
283     const std::vector<ui::LatencyInfo>& latency_info,
284     gfx::SwapResult result,
285     const gpu::GpuProcessHostedCALayerTreeParamsMac* params_mac) {
286   RenderWidgetHostImpl::CompositorFrameDrawn(latency_info);
287 #if !defined(EWK_BRINGUP)  // FIXME: m67 bringup
288   // FIXME: EWK_BRINGUP definition should be removed.
289   // OnSwapBuffersComplete was removed in cc/output/output_surface.h
290   OutputSurface::OnSwapBuffersComplete();
291 #endif  // !defined(EWK_BRINGUP)
292 }*/
293
294 MailboxOutputSurfaceEfl::TransferableFrame::TransferableFrame()
295     : texture_id(0) {}
296
297 MailboxOutputSurfaceEfl::TransferableFrame::TransferableFrame(
298     uint32_t texture_id,
299     const gpu::Mailbox& mailbox,
300     const gfx::Size size)
301     : texture_id(texture_id), mailbox(mailbox), size(size) {}
302
303 void MailboxOutputSurfaceEfl::SetDrawRectangle(const gfx::Rect& rect) {}
304
305 bool MailboxOutputSurfaceEfl::HasExternalStencilTest() const {
306   NOTIMPLEMENTED();
307   return false;
308 }
309
310 void MailboxOutputSurfaceEfl::ApplyExternalStencil() {
311   NOTIMPLEMENTED();
312 }
313
314 viz::OverlayCandidateValidator*
315 MailboxOutputSurfaceEfl::GetOverlayCandidateValidator() const {
316   return nullptr;
317 }
318
319 bool MailboxOutputSurfaceEfl::IsDisplayedAsOverlayPlane() const {
320   return false;
321 }
322
323 unsigned MailboxOutputSurfaceEfl::GetOverlayTextureId() const {
324   return 0;
325 }
326
327 gfx::BufferFormat MailboxOutputSurfaceEfl::GetOverlayBufferFormat() const {
328   return gfx::BufferFormat::RGBX_8888;
329 }
330
331 bool MailboxOutputSurfaceEfl::SurfaceIsSuspendForRecycle() const {
332   return false;
333 }
334
335 }  // namespace content