1 // Copyright 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 "components/viz/test/fake_output_surface.h"
6 
7 #include "base/bind.h"
8 #include "base/threading/thread_task_runner_handle.h"
9 #include "components/viz/common/resources/returned_resource.h"
10 #include "components/viz/service/display/output_surface_client.h"
11 #include "components/viz/test/begin_frame_args_test.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/gfx/buffer_format_util.h"
14 #include "ui/gfx/presentation_feedback.h"
15 #include "ui/gfx/swap_result.h"
16 
17 namespace viz {
18 
FakeOutputSurface(scoped_refptr<ContextProvider> context_provider)19 FakeOutputSurface::FakeOutputSurface(
20     scoped_refptr<ContextProvider> context_provider)
21     : OutputSurface(std::move(context_provider)) {
22   DCHECK(OutputSurface::context_provider());
23 }
24 
FakeOutputSurface(std::unique_ptr<SoftwareOutputDevice> software_device)25 FakeOutputSurface::FakeOutputSurface(
26     std::unique_ptr<SoftwareOutputDevice> software_device)
27     : OutputSurface(std::move(software_device)) {
28   DCHECK(OutputSurface::software_device());
29 }
30 
31 FakeOutputSurface::~FakeOutputSurface() = default;
32 
Reshape(const gfx::Size & size,float device_scale_factor,const gfx::ColorSpace & color_space,gfx::BufferFormat format,bool use_stencil)33 void FakeOutputSurface::Reshape(const gfx::Size& size,
34                                 float device_scale_factor,
35                                 const gfx::ColorSpace& color_space,
36                                 gfx::BufferFormat format,
37                                 bool use_stencil) {
38   if (context_provider()) {
39     context_provider()->ContextGL()->ResizeCHROMIUM(
40         size.width(), size.height(), device_scale_factor,
41         color_space.AsGLColorSpace(), gfx::AlphaBitsForBufferFormat(format));
42   } else {
43     software_device()->Resize(size, device_scale_factor);
44   }
45   last_reshape_color_space_ = color_space;
46 }
47 
SwapBuffers(OutputSurfaceFrame frame)48 void FakeOutputSurface::SwapBuffers(OutputSurfaceFrame frame) {
49   last_sent_frame_ = std::make_unique<OutputSurfaceFrame>(std::move(frame));
50   ++num_sent_frames_;
51 
52   base::ThreadTaskRunnerHandle::Get()->PostTask(
53       FROM_HERE, base::BindOnce(&FakeOutputSurface::SwapBuffersAck,
54                                 weak_ptr_factory_.GetWeakPtr()));
55 }
56 
SwapBuffersAck()57 void FakeOutputSurface::SwapBuffersAck() {
58   base::TimeTicks now = base::TimeTicks::Now();
59   client_->DidReceiveSwapBuffersAck({now, now});
60   client_->DidReceivePresentationFeedback({now, base::TimeDelta(), 0});
61 }
62 
BindFramebuffer()63 void FakeOutputSurface::BindFramebuffer() {
64   context_provider_->ContextGL()->BindFramebuffer(GL_FRAMEBUFFER, framebuffer_);
65 }
66 
SetDrawRectangle(const gfx::Rect & rect)67 void FakeOutputSurface::SetDrawRectangle(const gfx::Rect& rect) {
68   last_set_draw_rectangle_ = rect;
69 }
70 
SetEnableDCLayers(bool enabled)71 void FakeOutputSurface::SetEnableDCLayers(bool enabled) {
72   context_provider_->ContextGL()->SetEnableDCLayersCHROMIUM(enabled);
73 }
74 
GetFramebufferCopyTextureFormat()75 uint32_t FakeOutputSurface::GetFramebufferCopyTextureFormat() {
76   if (framebuffer_)
77     return framebuffer_format_;
78   else
79     return GL_RGB;
80 }
81 
BindToClient(OutputSurfaceClient * client)82 void FakeOutputSurface::BindToClient(OutputSurfaceClient* client) {
83   DCHECK(client);
84   DCHECK(!client_);
85   client_ = client;
86 }
87 
HasExternalStencilTest() const88 bool FakeOutputSurface::HasExternalStencilTest() const {
89   return has_external_stencil_test_;
90 }
91 
IsDisplayedAsOverlayPlane() const92 bool FakeOutputSurface::IsDisplayedAsOverlayPlane() const {
93   return overlay_texture_id_ != 0;
94 }
95 
GetOverlayTextureId() const96 unsigned FakeOutputSurface::GetOverlayTextureId() const {
97   return overlay_texture_id_;
98 }
99 
UpdateGpuFence()100 unsigned FakeOutputSurface::UpdateGpuFence() {
101   return gpu_fence_id_;
102 }
103 
SetUpdateVSyncParametersCallback(UpdateVSyncParametersCallback callback)104 void FakeOutputSurface::SetUpdateVSyncParametersCallback(
105     UpdateVSyncParametersCallback callback) {}
106 
SetDisplayTransformHint(gfx::OverlayTransform transform)107 void FakeOutputSurface::SetDisplayTransformHint(
108     gfx::OverlayTransform transform) {
109   if (support_display_transform_hint_)
110     display_transform_hint_ = transform;
111 }
112 
GetDisplayTransform()113 gfx::OverlayTransform FakeOutputSurface::GetDisplayTransform() {
114   return support_display_transform_hint_ ? display_transform_hint_
115                                          : gfx::OVERLAY_TRANSFORM_NONE;
116 }
117 
118 #if (defined(OS_LINUX) && !defined(OS_CHROMEOS)) || defined(OS_BSD)
SetNeedsSwapSizeNotifications(bool needs_swap_size_notifications)119 void FakeOutputSurface::SetNeedsSwapSizeNotifications(
120     bool needs_swap_size_notifications) {}
121 #endif
122 
123 }  // namespace viz
124