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.
5 #include "content/browser/compositor/mailbox_output_surface_efl.h"
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"
20 using gpu::gles2::GLES2Interface;
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))),
32 is_backbuffer_discarded_(false),
33 texture_upload_pending_(false),
35 capabilities_.max_frames_pending = 1;
36 capabilities_.uses_default_gl_framebuffer = false;
39 MailboxOutputSurfaceEfl::~MailboxOutputSurfaceEfl() {}
41 void MailboxOutputSurfaceEfl::EnsureBackbuffer() {
42 is_backbuffer_discarded_ = false;
44 GLES2Interface* gl = context_provider_->ContextGL();
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();
61 #endif // !defined(EWK_BRINGUP)
63 gl->DeleteTextures(1, &texture.texture_id);
64 returned_textures_.pop();
67 if (!current_backing_.texture_id) {
68 gl->GenTextures(1, ¤t_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;
95 /*void MailboxOutputSurfaceEfl::fvDiscardBackbuffer() {
96 is_backbuffer_discarded_ = true;
98 GLES2Interface* gl = context_provider_->ContextGL();
100 if (current_backing_.texture_id) {
101 gl->DeleteTextures(1, ¤t_backing_.texture_id);
102 current_backing_ = TransferableFrame();
105 while (!returned_textures_.empty()) {
106 const TransferableFrame& frame = returned_textures_.front();
107 gl->DeleteTextures(1, &frame.texture_id);
108 returned_textures_.pop();
112 gl->BindFramebuffer(GL_FRAMEBUFFER, fbo_);
113 gl->DeleteFramebuffers(1, &fbo_);
118 void MailboxOutputSurfaceEfl::Reshape(const gfx::Size& size,
120 const gfx::ColorSpace& color_space,
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_)
129 surface_size_ = size;
130 device_scale_factor_ = scale_factor;
131 #endif // !defined(EWK_BRINGUP)
136 void MailboxOutputSurfaceEfl::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, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
146 current_backing_.texture_id, 0);
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
156 compositor_client_->GetTextureFromMailbox(&gl_frame_data->mailbox,
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).
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);
179 DCHECK(it != pending_textures_.end());
180 it->sync_token = gl_frame_data->sync_token;
182 if (!is_backbuffer_discarded_) {
183 returned_textures_.push(*it);
185 context_provider_->ContextGL()->DeleteTextures(1, &it->texture_id);
188 pending_textures_.erase(it);
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;
196 context_provider_->ContextGL()->DeleteTextures(1, &texture_id);
197 pending_textures_.pop_front();
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);
206 client_->DidSwapBuffersComplete();
208 #endif // !defined(EWK_BRINGUP)
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
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() !=
222 frame.gl_frame_data->mailbox = current_backing_.mailbox;
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();
230 if (texture_upload_pending_) {
231 context_provider_->ContextGL()->Finish();
232 texture_upload_pending_ = false;
234 context_provider_->ContextGL()->Flush();
238 gpu::gles2::GLES2Interface* gl = context_provider()->ContextGL();
239 const uint64_t fence_sync = gl->InsertFenceSyncCHROMIUM();
242 gpu::SyncToken sync_token;
243 gl->GenSyncTokenCHROMIUM(fence_sync, sync_token.GetData());
245 frame.gl_frame_data->sync_token = sync_token;
247 base::Closure closure =
248 base::Bind(&MailboxOutputSurfaceEfl::OnSwapAck, base::Unretained(this),
249 base::Passed(&frame.gl_frame_data));
251 context_provider()->ContextSupport()->SignalSyncToken(sync_token, closure);
253 client_->DidSwapBuffers();
255 pending_textures_.push_back(current_backing_);
256 current_backing_ = TransferableFrame();
257 #endif // !defined(EWK_BRINGUP)
260 uint32_t MailboxOutputSurfaceEfl::GetFramebufferCopyTextureFormat() {
262 static_cast<ui::ContextProviderCommandBuffer*>(context_provider_.get());
263 return gl->GetCopyTextureInternalFormat();
266 void MailboxOutputSurfaceEfl::BindToClient(viz::OutputSurfaceClient* client) {
267 OutputSurface::BindToClient(client);
268 GetCommandBufferProxy()->SetSwapBuffersCompletionCallback(
269 swap_buffers_completion_callback_.callback());
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;
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)
294 MailboxOutputSurfaceEfl::TransferableFrame::TransferableFrame()
297 MailboxOutputSurfaceEfl::TransferableFrame::TransferableFrame(
299 const gpu::Mailbox& mailbox,
300 const gfx::Size size)
301 : texture_id(texture_id), mailbox(mailbox), size(size) {}
303 void MailboxOutputSurfaceEfl::SetDrawRectangle(const gfx::Rect& rect) {}
305 bool MailboxOutputSurfaceEfl::HasExternalStencilTest() const {
310 void MailboxOutputSurfaceEfl::ApplyExternalStencil() {
314 viz::OverlayCandidateValidator*
315 MailboxOutputSurfaceEfl::GetOverlayCandidateValidator() const {
319 bool MailboxOutputSurfaceEfl::IsDisplayedAsOverlayPlane() const {
323 unsigned MailboxOutputSurfaceEfl::GetOverlayTextureId() const {
327 gfx::BufferFormat MailboxOutputSurfaceEfl::GetOverlayBufferFormat() const {
328 return gfx::BufferFormat::RGBX_8888;
331 bool MailboxOutputSurfaceEfl::SurfaceIsSuspendForRecycle() const {
335 } // namespace content