1 // Copyright 2014 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 "android_webview/browser/gfx/parent_output_surface.h"
6 
7 #include <utility>
8 
9 #include "android_webview/browser/gfx/aw_render_thread_context_provider.h"
10 #include "android_webview/browser/gfx/scoped_app_gl_state_restore.h"
11 #include "components/viz/service/display/output_surface_client.h"
12 #include "components/viz/service/display/output_surface_frame.h"
13 #include "gpu/command_buffer/client/gles2_interface.h"
14 #include "gpu/command_buffer/common/capabilities.h"
15 
16 namespace android_webview {
17 
18 namespace {
19 
AddLatencyInfoSwapTimes(std::vector<ui::LatencyInfo> * latency_info,base::TimeTicks swap_start,base::TimeTicks swap_end)20 void AddLatencyInfoSwapTimes(std::vector<ui::LatencyInfo>* latency_info,
21                              base::TimeTicks swap_start,
22                              base::TimeTicks swap_end) {
23   for (auto& latency : *latency_info) {
24     latency.AddLatencyNumberWithTimestamp(
25         ui::INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT, swap_start);
26     latency.AddLatencyNumberWithTimestamp(
27         ui::INPUT_EVENT_LATENCY_FRAME_SWAP_COMPONENT, swap_end);
28   }
29 }
30 
31 }  // namespace
32 
ParentOutputSurface(scoped_refptr<AwGLSurface> gl_surface,scoped_refptr<AwRenderThreadContextProvider> context_provider)33 ParentOutputSurface::ParentOutputSurface(
34     scoped_refptr<AwGLSurface> gl_surface,
35     scoped_refptr<AwRenderThreadContextProvider> context_provider)
36     : viz::OutputSurface(std::move(context_provider)),
37       gl_surface_(std::move(gl_surface)) {
38   const auto& context_capabilities = context_provider_->ContextCapabilities();
39   capabilities_.max_render_target_size = context_capabilities.max_texture_size;
40 }
41 
42 ParentOutputSurface::~ParentOutputSurface() = default;
43 
BindToClient(viz::OutputSurfaceClient * client)44 void ParentOutputSurface::BindToClient(viz::OutputSurfaceClient* client) {
45   DCHECK(client);
46   DCHECK(!client_);
47   client_ = client;
48 }
49 
EnsureBackbuffer()50 void ParentOutputSurface::EnsureBackbuffer() {}
51 
DiscardBackbuffer()52 void ParentOutputSurface::DiscardBackbuffer() {
53   context_provider()->ContextGL()->DiscardBackbufferCHROMIUM();
54 }
55 
BindFramebuffer()56 void ParentOutputSurface::BindFramebuffer() {
57   context_provider()->ContextGL()->BindFramebuffer(GL_FRAMEBUFFER, 0);
58 }
59 
Reshape(const gfx::Size & size,float scale_factor,const gfx::ColorSpace & color_space,gfx::BufferFormat format,bool use_stencil)60 void ParentOutputSurface::Reshape(const gfx::Size& size,
61                                   float scale_factor,
62                                   const gfx::ColorSpace& color_space,
63                                   gfx::BufferFormat format,
64                                   bool use_stencil) {}
65 
SwapBuffers(viz::OutputSurfaceFrame frame)66 void ParentOutputSurface::SwapBuffers(viz::OutputSurfaceFrame frame) {
67   context_provider_->ContextGL()->ShallowFlushCHROMIUM();
68   gl_surface_->SwapBuffers(base::BindOnce(
69       &ParentOutputSurface::OnPresentation, weak_ptr_factory_.GetWeakPtr(),
70       /*swap_start=*/base::TimeTicks::Now(), std::move(frame.latency_info)));
71 }
72 
OnPresentation(base::TimeTicks swap_start,std::vector<ui::LatencyInfo> latency_info,const gfx::PresentationFeedback & feedback)73 void ParentOutputSurface::OnPresentation(
74     base::TimeTicks swap_start,
75     std::vector<ui::LatencyInfo> latency_info,
76     const gfx::PresentationFeedback& feedback) {
77   DCHECK(client_);
78   // The start/end swap times are only best-effort. It is not possible to get
79   // the real swap times for WebView.
80   AddLatencyInfoSwapTimes(&latency_info, swap_start, base::TimeTicks::Now());
81   latency_tracker_.OnGpuSwapBuffersCompleted(latency_info);
82   client_->DidReceivePresentationFeedback(feedback);
83 }
84 
HasExternalStencilTest() const85 bool ParentOutputSurface::HasExternalStencilTest() const {
86   return ScopedAppGLStateRestore::Current()
87       ->stencil_state()
88       .stencil_test_enabled;
89 }
90 
ApplyExternalStencil()91 void ParentOutputSurface::ApplyExternalStencil() {
92   StencilState stencil_state =
93       ScopedAppGLStateRestore::Current()->stencil_state();
94   DCHECK(stencil_state.stencil_test_enabled);
95   gpu::gles2::GLES2Interface* gl = context_provider()->ContextGL();
96   gl->StencilFuncSeparate(GL_FRONT, stencil_state.stencil_front_func,
97                           stencil_state.stencil_front_mask,
98                           stencil_state.stencil_front_ref);
99   gl->StencilFuncSeparate(GL_BACK, stencil_state.stencil_back_func,
100                           stencil_state.stencil_back_mask,
101                           stencil_state.stencil_back_ref);
102   gl->StencilMaskSeparate(GL_FRONT, stencil_state.stencil_front_writemask);
103   gl->StencilMaskSeparate(GL_BACK, stencil_state.stencil_back_writemask);
104   gl->StencilOpSeparate(GL_FRONT, stencil_state.stencil_front_fail_op,
105                         stencil_state.stencil_front_z_fail_op,
106                         stencil_state.stencil_front_z_pass_op);
107   gl->StencilOpSeparate(GL_BACK, stencil_state.stencil_back_fail_op,
108                         stencil_state.stencil_back_z_fail_op,
109                         stencil_state.stencil_back_z_pass_op);
110 }
111 
GetFramebufferCopyTextureFormat()112 uint32_t ParentOutputSurface::GetFramebufferCopyTextureFormat() {
113   auto* gl = static_cast<AwRenderThreadContextProvider*>(context_provider());
114   return gl->GetCopyTextureInternalFormat();
115 }
116 
IsDisplayedAsOverlayPlane() const117 bool ParentOutputSurface::IsDisplayedAsOverlayPlane() const {
118   return false;
119 }
120 
GetOverlayTextureId() const121 unsigned ParentOutputSurface::GetOverlayTextureId() const {
122   return 0;
123 }
124 
UpdateGpuFence()125 unsigned ParentOutputSurface::UpdateGpuFence() {
126   return 0;
127 }
128 
SetUpdateVSyncParametersCallback(viz::UpdateVSyncParametersCallback callback)129 void ParentOutputSurface::SetUpdateVSyncParametersCallback(
130     viz::UpdateVSyncParametersCallback callback) {}
131 
GetDisplayTransform()132 gfx::OverlayTransform ParentOutputSurface::GetDisplayTransform() {
133   return gfx::OVERLAY_TRANSFORM_NONE;
134 }
135 
136 }  // namespace android_webview
137