-// Copyright (c) 2016 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "content/browser/compositor/mailbox_output_surface_efl.h"
-
-#include "base/threading/thread_task_runner_handle.h"
-#include "build/tizen_version.h"
-//#include "cc/output/output_surface_client.h"
-#include "components/viz/service/display/output_surface_client.h"
-//#include "cc/output/output_surface_frame.h"
-#include "cc/raster/single_thread_task_graph_runner.h"
-#include "components/viz/service/display/output_surface_frame.h"
-#include "content/browser/renderer_host/render_widget_host_impl.h"
-#include "gpu/command_buffer/client/context_support.h"
-#include "gpu/command_buffer/client/gles2_interface.h"
-#include "third_party/khronos/GLES2/gl2.h"
-#include "third_party/khronos/GLES2/gl2ext.h"
-
-using gpu::gles2::GLES2Interface;
-
-namespace content {
-
-MailboxOutputSurfaceEfl::MailboxOutputSurfaceEfl(
- const scoped_refptr<ui::ContextProviderCommandBuffer>& context_provider,
- viz::ResourceFormat format)
- : viz::OutputSurface(context_provider),
- swap_buffers_completion_callback_(
- base::Bind(&MailboxOutputSurfaceEfl::OnSwapBuffersCompleted,
- base::Unretained(this))),
- fbo_(0),
- is_backbuffer_discarded_(false),
- texture_upload_pending_(false),
- format_(format) {
- capabilities_.max_frames_pending = 1;
- capabilities_.uses_default_gl_framebuffer = false;
-}
-
-MailboxOutputSurfaceEfl::~MailboxOutputSurfaceEfl() {}
-
-void MailboxOutputSurfaceEfl::EnsureBackbuffer() {
- is_backbuffer_discarded_ = false;
-
- GLES2Interface* gl = context_provider_->ContextGL();
-
- if (!current_backing_.texture_id) {
- // Find a texture of matching size to recycle.
- while (!returned_textures_.empty()) {
- TransferableFrame& texture = returned_textures_.front();
-#if !defined(EWK_BRINGUP) // FIXME: m67 bringup
- // FIXME: EWK_BRINGUP definition should be removed.
- // surface_size_ variable was removed in
- // cc/output/output_surface.h
- if (texture.size == surface_size_) {
- current_backing_ = texture;
- if (current_backing_.sync_token.HasData())
- gl->WaitSyncTokenCHROMIUM(current_backing_.sync_token.GetConstData());
- returned_textures_.pop();
- break;
- }
-#endif // !defined(EWK_BRINGUP)
-
- gl->DeleteTextures(1, &texture.texture_id);
- returned_textures_.pop();
- }
-
- if (!current_backing_.texture_id) {
- gl->GenTextures(1, ¤t_backing_.texture_id);
-#if !defined(EWK_BRINGUP) // FIXME: m67 bringup
- // FIXME: EWK_BRINGUP definition should be removed.
- // surface_size_ variable was removed in
- // cc/output/output_surface.h
- current_backing_.size = surface_size_;
-#endif // !defined(EWK_BRINGUP)
- gl->BindTexture(GL_TEXTURE_2D, current_backing_.texture_id);
- gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-#if !defined(EWK_BRINGUP) // FIXME: m67 bringup
- // FIXME: EWK_BRINGUP definition should be removed.
- // surface_size_ variable was removed in
- // cc/output/output_surface.h
- gl->TexImage2D(GL_TEXTURE_2D, 0, GLInternalFormat(format_),
- surface_size_.width(), surface_size_.height(), 0,
- GLDataFormat(format_), GLDataType(format_), NULL);
-#endif // !defined(EWK_BRINGUP)
- gl->GenMailboxCHROMIUM(current_backing_.mailbox.name);
- gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, current_backing_.mailbox.name);
- texture_upload_pending_ = true;
- }
- }
-}
-
-/*void MailboxOutputSurfaceEfl::fvDiscardBackbuffer() {
- is_backbuffer_discarded_ = true;
-
- GLES2Interface* gl = context_provider_->ContextGL();
-
- if (current_backing_.texture_id) {
- gl->DeleteTextures(1, ¤t_backing_.texture_id);
- current_backing_ = TransferableFrame();
- }
-
- while (!returned_textures_.empty()) {
- const TransferableFrame& frame = returned_textures_.front();
- gl->DeleteTextures(1, &frame.texture_id);
- returned_textures_.pop();
- }
-
- if (fbo_) {
- gl->BindFramebuffer(GL_FRAMEBUFFER, fbo_);
- gl->DeleteFramebuffers(1, &fbo_);
- fbo_ = 0;
- }
-}*/
-
-void MailboxOutputSurfaceEfl::Reshape(const gfx::Size& size,
- float scale_factor,
- const gfx::ColorSpace& color_space,
- bool has_alpha,
- bool use_stencil) {
-#if !defined(EWK_BRINGUP) // FIXME: m67 bringup
- // FIXME: EWK_BRINGUP definition should be removed.
- // surface_size_ variable was removed in cc/output/output_surface.h
- if (size == surface_size_)
- return;
-
- surface_size_ = size;
- device_scale_factor_ = scale_factor;
-#endif // !defined(EWK_BRINGUP)
- DiscardBackbuffer();
- EnsureBackbuffer();
-}
-
-void MailboxOutputSurfaceEfl::BindFramebuffer() {
- EnsureBackbuffer();
- DCHECK(current_backing_.texture_id);
-
- GLES2Interface* gl = context_provider_->ContextGL();
-
- if (!fbo_)
- gl->GenFramebuffers(1, &fbo_);
- gl->BindFramebuffer(GL_FRAMEBUFFER, fbo_);
- gl->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
- current_backing_.texture_id, 0);
-}
-
-#if !defined(EWK_BRINGUP) // FIXME: m67 bringup
-// FIXME: EWK_BRINGUP definition should be removed.
-// cc::GLFrameData was removed in cc/output/gl_frame_data.h
-void MailboxOutputSurfaceEfl::DrawTexture(
- std::unique_ptr<cc::GLFrameData> gl_frame_data) {
- // Draw texture on RWHV
- if (gl_frame_data) {
- compositor_client_->GetTextureFromMailbox(&gl_frame_data->mailbox,
- surface_size_);
- }
-}
-
-void MailboxOutputSurfaceEfl::OnSwapAck(
- std::unique_ptr<cc::GLFrameData> gl_frame_data) {
- // Ignore message if it's a stale one coming from a different output surface
- // (e.g. after a lost context).
-
- if (!gl_frame_data->mailbox.IsZero()) {
- DCHECK(!gl_frame_data->size.IsEmpty());
- // The browser could be returning the oldest or any other pending texture
- // if it decided to skip a frame.
- std::deque<TransferableFrame>::iterator it;
- for (it = pending_textures_.begin(); it != pending_textures_.end(); it++) {
- DCHECK(!it->mailbox.IsZero());
- if (!memcmp(it->mailbox.name, gl_frame_data->mailbox.name,
- sizeof(it->mailbox.name))) {
- DCHECK(it->size == gl_frame_data->size);
- break;
- }
- }
- DCHECK(it != pending_textures_.end());
- it->sync_token = gl_frame_data->sync_token;
-
- if (!is_backbuffer_discarded_) {
- returned_textures_.push(*it);
- } else {
- context_provider_->ContextGL()->DeleteTextures(1, &it->texture_id);
- }
-
- pending_textures_.erase(it);
- } else {
- DCHECK(!pending_textures_.empty());
- // The browser always keeps one texture as the frontbuffer.
- // If it does not return a mailbox, it discarded the frontbuffer which is
- // the oldest texture we sent.
- uint32_t texture_id = pending_textures_.front().texture_id;
- if (texture_id)
- context_provider_->ContextGL()->DeleteTextures(1, &texture_id);
- pending_textures_.pop_front();
- }
-
- if (gl_frame_data) {
- base::Closure closure =
- base::Bind(&MailboxOutputSurfaceEfl::DrawTexture,
- base::Unretained(this), base::Passed(&gl_frame_data));
- base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, closure);
- }
- client_->DidSwapBuffersComplete();
-}
-#endif // !defined(EWK_BRINGUP)
-
-void MailboxOutputSurfaceEfl::SwapBuffers(viz::OutputSurfaceFrame frame) {
-#if !defined(EWK_BRINGUP) // FIXME: m67 bringup
- // FIXME: EWK_BRINGUP definition should be removed.
- // The type of frame was changed, so gl_frame_data isn't member of
- // frame
- DCHECK(frame.gl_frame_data);
- DCHECK(!surface_size_.IsEmpty());
- DCHECK(surface_size_ == current_backing_.size);
- DCHECK(frame.gl_frame_data->size == current_backing_.size);
- DCHECK(!current_backing_.mailbox.IsZero() ||
- context_provider_->ContextGL()->GetGraphicsResetStatusKHR() !=
- GL_NO_ERROR);
- frame.gl_frame_data->mailbox = current_backing_.mailbox;
-
-// Using glFinish call instead of glFlush, fixes black screen issue with
-// static pages and IE Fish page.
-// Black screen issue is seen on page in Tizen 2.4 product TV.
-#if TIZEN_VERSION_EQ(2, 4, 0) && defined(OS_TIZEN_TV)
- context_provider_->ContextGL()->Finish();
-#else
- if (texture_upload_pending_) {
- context_provider_->ContextGL()->Finish();
- texture_upload_pending_ = false;
- } else {
- context_provider_->ContextGL()->Flush();
- }
-#endif
-
- gpu::gles2::GLES2Interface* gl = context_provider()->ContextGL();
- const uint64_t fence_sync = gl->InsertFenceSyncCHROMIUM();
- gl->Flush();
-
- gpu::SyncToken sync_token;
- gl->GenSyncTokenCHROMIUM(fence_sync, sync_token.GetData());
-
- frame.gl_frame_data->sync_token = sync_token;
-
- base::Closure closure =
- base::Bind(&MailboxOutputSurfaceEfl::OnSwapAck, base::Unretained(this),
- base::Passed(&frame.gl_frame_data));
-
- context_provider()->ContextSupport()->SignalSyncToken(sync_token, closure);
-
- client_->DidSwapBuffers();
-
- pending_textures_.push_back(current_backing_);
- current_backing_ = TransferableFrame();
-#endif // !defined(EWK_BRINGUP)
-}
-
-uint32_t MailboxOutputSurfaceEfl::GetFramebufferCopyTextureFormat() {
- auto* gl =
- static_cast<ui::ContextProviderCommandBuffer*>(context_provider_.get());
- return gl->GetCopyTextureInternalFormat();
-}
-
-void MailboxOutputSurfaceEfl::BindToClient(viz::OutputSurfaceClient* client) {
- OutputSurface::BindToClient(client);
- GetCommandBufferProxy()->SetSwapBuffersCompletionCallback(
- swap_buffers_completion_callback_.callback());
- return;
-}
-
-gpu::CommandBufferProxyImpl* MailboxOutputSurfaceEfl::GetCommandBufferProxy() {
- ui::ContextProviderCommandBuffer* provider_command_buffer =
- static_cast<ui::ContextProviderCommandBuffer*>(context_provider_.get());
- gpu::CommandBufferProxyImpl* command_buffer_proxy =
- provider_command_buffer->GetCommandBufferProxy();
- DCHECK(command_buffer_proxy);
- return command_buffer_proxy;
-}
-
-/*void MailboxOutputSurfaceEfl::OnSwapBuffersCompleted(
- const std::vector<ui::LatencyInfo>& latency_info,
- gfx::SwapResult result,
- const gpu::GpuProcessHostedCALayerTreeParamsMac* params_mac) {
- RenderWidgetHostImpl::CompositorFrameDrawn(latency_info);
-#if !defined(EWK_BRINGUP) // FIXME: m67 bringup
- // FIXME: EWK_BRINGUP definition should be removed.
- // OnSwapBuffersComplete was removed in cc/output/output_surface.h
- OutputSurface::OnSwapBuffersComplete();
-#endif // !defined(EWK_BRINGUP)
-}*/
-
-MailboxOutputSurfaceEfl::TransferableFrame::TransferableFrame()
- : texture_id(0) {}
-
-MailboxOutputSurfaceEfl::TransferableFrame::TransferableFrame(
- uint32_t texture_id,
- const gpu::Mailbox& mailbox,
- const gfx::Size size)
- : texture_id(texture_id), mailbox(mailbox), size(size) {}
-
-void MailboxOutputSurfaceEfl::SetDrawRectangle(const gfx::Rect& rect) {}
-
-bool MailboxOutputSurfaceEfl::HasExternalStencilTest() const {
- NOTIMPLEMENTED();
- return false;
-}
-
-void MailboxOutputSurfaceEfl::ApplyExternalStencil() {
- NOTIMPLEMENTED();
-}
-
-viz::OverlayCandidateValidator*
-MailboxOutputSurfaceEfl::GetOverlayCandidateValidator() const {
- return nullptr;
-}
-
-bool MailboxOutputSurfaceEfl::IsDisplayedAsOverlayPlane() const {
- return false;
-}
-
-unsigned MailboxOutputSurfaceEfl::GetOverlayTextureId() const {
- return 0;
-}
-
-gfx::BufferFormat MailboxOutputSurfaceEfl::GetOverlayBufferFormat() const {
- return gfx::BufferFormat::RGBX_8888;
-}
-
-bool MailboxOutputSurfaceEfl::SurfaceIsSuspendForRecycle() const {
- return false;
-}
-
-} // namespace content