hardware_renderer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #ifndef ANDROID_WEBVIEW_BROWSER_GFX_HARDWARE_RENDERER_H_
  5. #define ANDROID_WEBVIEW_BROWSER_GFX_HARDWARE_RENDERER_H_
  6. #include <memory>
  7. #include "android_webview/browser/gfx/child_frame.h"
  8. #include "android_webview/browser/gfx/output_surface_provider_webview.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "ui/gfx/android/android_surface_control_compat.h"
  11. #include "ui/gfx/color_space.h"
  12. namespace android_webview {
  13. class RenderThreadManager;
  14. struct OverlaysParams {
  15. enum class Mode {
  16. Disabled,
  17. Enabled,
  18. };
  19. typedef ASurfaceControl* (*GetSurfaceControlFn)();
  20. typedef void (*MergeTransactionFn)(ASurfaceTransaction*);
  21. Mode overlays_mode = Mode::Disabled;
  22. GetSurfaceControlFn get_surface_control = nullptr;
  23. MergeTransactionFn merge_transaction = nullptr;
  24. };
  25. struct HardwareRendererDrawParams {
  26. bool operator==(const HardwareRendererDrawParams& other) const;
  27. bool operator!=(const HardwareRendererDrawParams& other) const;
  28. int clip_left;
  29. int clip_top;
  30. int clip_right;
  31. int clip_bottom;
  32. int width;
  33. int height;
  34. float transform[16];
  35. gfx::ColorSpace color_space;
  36. };
  37. class HardwareRenderer {
  38. public:
  39. // Two rules:
  40. // 1) Never wait on |new_frame| on the UI thread, or in kModeSync. Otherwise
  41. // this defeats the purpose of having a future.
  42. // 2) Never replace a non-empty frames with an empty frame.
  43. // The only way to do both is to hold up to two frames. This helper function
  44. // will wait on all frames in the queue, then only keep the last non-empty
  45. // frame and return the rest (non-empty) frames. It takes care to not drop
  46. // other data such as readback requests.
  47. // A common pattern for appending a new frame is:
  48. // * WaitAndPrune the existing frame, after which there is at most one frame
  49. // is left in queue.
  50. // * Append new frame without waiting on it.
  51. static ChildFrameQueue WaitAndPruneFrameQueue(ChildFrameQueue* child_frames);
  52. HardwareRenderer(const HardwareRenderer&) = delete;
  53. HardwareRenderer& operator=(const HardwareRenderer&) = delete;
  54. virtual ~HardwareRenderer();
  55. void Draw(const HardwareRendererDrawParams& params,
  56. const OverlaysParams& overlays_params);
  57. void CommitFrame();
  58. virtual void RemoveOverlays(
  59. OverlaysParams::MergeTransactionFn merge_transaction) = 0;
  60. virtual void AbandonContext() = 0;
  61. void SetChildFrameForTesting(std::unique_ptr<ChildFrame> child_frame);
  62. protected:
  63. explicit HardwareRenderer(RenderThreadManager* state);
  64. void ReturnChildFrame(std::unique_ptr<ChildFrame> child_frame);
  65. void ReturnResourcesToCompositor(std::vector<viz::ReturnedResource> resources,
  66. const viz::FrameSinkId& frame_sink_id,
  67. uint32_t layer_tree_frame_sink_id);
  68. void ReportDrawMetric(const HardwareRendererDrawParams& params);
  69. virtual void DrawAndSwap(const HardwareRendererDrawParams& params,
  70. const OverlaysParams& overlays_params) = 0;
  71. const raw_ptr<RenderThreadManager> render_thread_manager_;
  72. typedef void* EGLContext;
  73. EGLContext last_egl_context_;
  74. ChildFrameQueue child_frame_queue_;
  75. // This holds the last ChildFrame received. Contains the frame info of the
  76. // last frame. The |frame| member is always null since frame has already
  77. // been submitted.
  78. std::unique_ptr<ChildFrame> child_frame_;
  79. // Used in metrics. Indicates if we invalidated/submitted for the ChildFrame
  80. // in |child_frame_|
  81. bool did_invalidate_ = false;
  82. bool did_submit_compositor_frame_ = false;
  83. // Information from UI on last commit.
  84. gfx::Point scroll_offset_;
  85. // HardwareRendererSingleThread guarantees resources are returned in the order
  86. // of layer_tree_frame_sink_id, and resources for old output surfaces are
  87. // dropped.
  88. uint32_t last_committed_layer_tree_frame_sink_id_ = 0u;
  89. // Draw params that was used in previous draw. Used in reporting draw metric.
  90. HardwareRendererDrawParams last_draw_params_ = {};
  91. };
  92. } // namespace android_webview
  93. #endif // ANDROID_WEBVIEW_BROWSER_GFX_HARDWARE_RENDERER_H_