fake_skia_output_surface.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2019 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. #ifndef COMPONENTS_VIZ_TEST_FAKE_SKIA_OUTPUT_SURFACE_H_
  5. #define COMPONENTS_VIZ_TEST_FAKE_SKIA_OUTPUT_SURFACE_H_
  6. #include <memory>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/containers/flat_map.h"
  10. #include "base/memory/ptr_util.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/threading/thread_checker.h"
  14. #include "build/build_config.h"
  15. #include "components/viz/common/quads/aggregated_render_pass.h"
  16. #include "components/viz/service/display/skia_output_surface.h"
  17. #include "components/viz/test/test_context_provider.h"
  18. #include "gpu/command_buffer/common/sync_token.h"
  19. namespace viz {
  20. class FakeSkiaOutputSurface : public SkiaOutputSurface {
  21. public:
  22. static std::unique_ptr<FakeSkiaOutputSurface> Create3d() {
  23. auto provider = TestContextProvider::Create();
  24. provider->BindToCurrentThread();
  25. return base::WrapUnique(new FakeSkiaOutputSurface(std::move(provider)));
  26. }
  27. static std::unique_ptr<FakeSkiaOutputSurface> Create3d(
  28. scoped_refptr<ContextProvider> provider) {
  29. return base::WrapUnique(new FakeSkiaOutputSurface(std::move(provider)));
  30. }
  31. FakeSkiaOutputSurface(const FakeSkiaOutputSurface&) = delete;
  32. FakeSkiaOutputSurface& operator=(const FakeSkiaOutputSurface&) = delete;
  33. ~FakeSkiaOutputSurface() override;
  34. // OutputSurface implementation:
  35. void BindToClient(OutputSurfaceClient* client) override;
  36. void EnsureBackbuffer() override;
  37. void DiscardBackbuffer() override;
  38. void Reshape(const ReshapeParams& params) override;
  39. void SwapBuffers(OutputSurfaceFrame frame) override;
  40. void ScheduleOutputSurfaceAsOverlay(
  41. OverlayProcessorInterface::OutputSurfaceOverlayPlane output_surface_plane)
  42. override;
  43. bool IsDisplayedAsOverlayPlane() const override;
  44. void SetNeedsSwapSizeNotifications(
  45. bool needs_swap_size_notifications) override;
  46. void SetUpdateVSyncParametersCallback(
  47. UpdateVSyncParametersCallback callback) override;
  48. void SetDisplayTransformHint(gfx::OverlayTransform transform) override {}
  49. gfx::OverlayTransform GetDisplayTransform() override;
  50. // SkiaOutputSurface implementation:
  51. SkCanvas* BeginPaintCurrentFrame() override;
  52. sk_sp<SkImage> MakePromiseSkImageFromYUV(
  53. const std::vector<ImageContext*>& contexts,
  54. sk_sp<SkColorSpace> image_color_space,
  55. SkYUVAInfo::PlaneConfig plane_config,
  56. SkYUVAInfo::Subsampling subsampling) override;
  57. void SwapBuffersSkipped(const gfx::Rect root_pass_damage_rect) override {}
  58. SkCanvas* BeginPaintRenderPass(const AggregatedRenderPassId& id,
  59. const gfx::Size& surface_size,
  60. ResourceFormat format,
  61. bool mipmap,
  62. sk_sp<SkColorSpace> color_space,
  63. bool is_overlay,
  64. const gpu::Mailbox& mailbox) override;
  65. SkCanvas* RecordOverdrawForCurrentPaint() override;
  66. void EndPaint(base::OnceClosure on_finished,
  67. base::OnceCallback<void(gfx::GpuFenceHandle)>
  68. return_release_fence_cb) override;
  69. void MakePromiseSkImage(ImageContext* image_context) override;
  70. sk_sp<SkImage> MakePromiseSkImageFromRenderPass(
  71. const AggregatedRenderPassId& id,
  72. const gfx::Size& size,
  73. ResourceFormat format,
  74. bool mipmap,
  75. sk_sp<SkColorSpace> color_space,
  76. const gpu::Mailbox& mailbox) override;
  77. void RemoveRenderPassResource(
  78. std::vector<AggregatedRenderPassId> ids) override;
  79. void ScheduleOverlays(OverlayList overlays,
  80. std::vector<gpu::SyncToken> sync_tokens) override {}
  81. #if BUILDFLAG(IS_WIN)
  82. void SetEnableDCLayers(bool enable) override {}
  83. #endif
  84. void CopyOutput(AggregatedRenderPassId id,
  85. const copy_output::RenderPassGeometry& geometry,
  86. const gfx::ColorSpace& color_space,
  87. std::unique_ptr<CopyOutputRequest> request,
  88. const gpu::Mailbox& mailbox) override;
  89. void AddContextLostObserver(ContextLostObserver* observer) override;
  90. void RemoveContextLostObserver(ContextLostObserver* observer) override;
  91. gpu::SharedImageInterface* GetSharedImageInterface() override;
  92. gpu::SyncToken Flush() override;
  93. bool EnsureMinNumberOfBuffers(int n) override;
  94. void PreserveChildSurfaceControls() override {}
  95. // ExternalUseClient implementation:
  96. gpu::SyncToken ReleaseImageContexts(
  97. const std::vector<std::unique_ptr<ImageContext>> image_contexts) override;
  98. std::unique_ptr<ImageContext> CreateImageContext(
  99. const gpu::MailboxHolder& holder,
  100. const gfx::Size& size,
  101. ResourceFormat format,
  102. bool concurrent_reads,
  103. const absl::optional<gpu::VulkanYCbCrInfo>& ycbcr_info,
  104. sk_sp<SkColorSpace> color_space,
  105. bool raw_draw_if_possible) override;
  106. // If set true, callbacks triggering will be in a reverse order as SignalQuery
  107. // calls.
  108. void SetOutOfOrderCallbacks(bool out_of_order_callbacks);
  109. void ScheduleGpuTaskForTesting(
  110. base::OnceClosure callback,
  111. std::vector<gpu::SyncToken> sync_tokens) override;
  112. void UsePlatformDelegatedInkForTesting() {
  113. capabilities_.supports_delegated_ink = true;
  114. }
  115. gfx::DelegatedInkMetadata* last_delegated_ink_metadata() const {
  116. return last_delegated_ink_metadata_.get();
  117. }
  118. void InitDelegatedInkPointRendererReceiver(
  119. mojo::PendingReceiver<gfx::mojom::DelegatedInkPointRenderer>
  120. pending_receiver) override;
  121. bool ContainsDelegatedInkPointRendererReceiverForTesting() const {
  122. return delegated_ink_renderer_receiver_arrived_;
  123. }
  124. protected:
  125. explicit FakeSkiaOutputSurface(
  126. scoped_refptr<ContextProvider> context_provider);
  127. private:
  128. ContextProvider* context_provider() { return context_provider_.get(); }
  129. GrDirectContext* gr_context() { return context_provider()->GrContext(); }
  130. gpu::SyncToken GenerateSyncToken();
  131. bool GetGrBackendTexture(const ImageContext& image_context,
  132. GrBackendTexture* backend_texture);
  133. void SwapBuffersAck();
  134. // Provided as a release callback for CopyOutputRequest.
  135. void DestroyCopyOutputTexture(const gpu::Mailbox& mailbox,
  136. const gpu::SyncToken& sync_token,
  137. bool is_lost);
  138. scoped_refptr<ContextProvider> context_provider_;
  139. raw_ptr<OutputSurfaceClient> client_ = nullptr;
  140. uint64_t next_sync_fence_release_ = 1;
  141. // The current render pass id set by BeginPaintRenderPass.
  142. AggregatedRenderPassId current_render_pass_id_;
  143. // SkSurfaces for render passes, sk_surfaces_[0] is the root surface.
  144. base::flat_map<AggregatedRenderPassId, sk_sp<SkSurface>> sk_surfaces_;
  145. // Most recent delegated ink metadata to have arrived via a SwapBuffers call.
  146. std::unique_ptr<gfx::DelegatedInkMetadata> last_delegated_ink_metadata_;
  147. // Flag to mark if a pending delegated ink renderer mojo receiver has arrived
  148. // here or not. Used in testing to confirm that the pending receiver is
  149. // correctly routed towards gpu main when the platform supports delegated ink.
  150. bool delegated_ink_renderer_receiver_arrived_ = false;
  151. THREAD_CHECKER(thread_checker_);
  152. base::WeakPtrFactory<FakeSkiaOutputSurface> weak_ptr_factory_{this};
  153. };
  154. } // namespace viz
  155. #endif // COMPONENTS_VIZ_TEST_FAKE_SKIA_OUTPUT_SURFACE_H_