begin_frame_source_webview.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2020 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_BEGIN_FRAME_SOURCE_WEBVIEW_H_
  5. #define ANDROID_WEBVIEW_BROWSER_GFX_BEGIN_FRAME_SOURCE_WEBVIEW_H_
  6. #include <memory>
  7. #include "base/android/scoped_java_ref.h"
  8. #include "base/callback.h"
  9. #include "base/callback_helpers.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/no_destructor.h"
  12. #include "components/power_scheduler/power_mode_voter.h"
  13. #include "components/viz/common/frame_sinks/begin_frame_source.h"
  14. #include "components/viz/service/frame_sinks/external_begin_frame_source_android.h"
  15. namespace android_webview {
  16. // The BeginFrameSourceWebView implements ExternalBeginFrameSource by observing
  17. // another begin_frame_source and provides AfterBeginFrame callback that called
  18. // after BeginFrame is sent out to all observers. It supports hierarchy
  19. // BeginFrameSourceWebView to provide AddBeginFrameCompletionCallback which will
  20. // be forwarded to root begin frame source to ensure that callbacks called after
  21. // all BeginFrames are sent.
  22. class BeginFrameSourceWebView : public viz::ExternalBeginFrameSource {
  23. public:
  24. BeginFrameSourceWebView();
  25. ~BeginFrameSourceWebView() override;
  26. // Sets parent of this BeginFrameSource
  27. void SetParentSource(BeginFrameSourceWebView* parent);
  28. bool inside_begin_frame() { return inside_begin_frame_; }
  29. // Schedules BeginFrame completion callback on root begin frame source.
  30. virtual void AddBeginFrameCompletionCallback(base::OnceClosure callback);
  31. // Returns last dispatched begin frame args.
  32. const viz::BeginFrameArgs& LastDispatchedBeginFrameArgs();
  33. protected:
  34. void ObserveBeginFrameSource(viz::BeginFrameSource* begin_frame_source);
  35. virtual void AfterBeginFrame() {}
  36. private:
  37. class BeginFrameObserver;
  38. class BeginFrameSourceClient : public viz::ExternalBeginFrameSourceClient {
  39. public:
  40. BeginFrameSourceClient(BeginFrameSourceWebView* owner);
  41. ~BeginFrameSourceClient();
  42. // ExternalBeginFrameSourceClient implementation.
  43. void OnNeedsBeginFrames(bool needs_begin_frames) override;
  44. private:
  45. const raw_ptr<BeginFrameSourceWebView> owner_;
  46. };
  47. void SendBeginFrame(const viz::BeginFrameArgs& args);
  48. void OnNeedsBeginFrames(bool needs_begin_frames);
  49. BeginFrameSourceClient bfs_client_;
  50. raw_ptr<viz::BeginFrameSource> observed_begin_frame_source_ = nullptr;
  51. raw_ptr<BeginFrameSourceWebView> parent_ = nullptr;
  52. std::unique_ptr<BeginFrameObserver> parent_observer_;
  53. bool inside_begin_frame_ = false;
  54. std::unique_ptr<power_scheduler::PowerModeVoter> animation_power_mode_voter_;
  55. };
  56. // RootBeginFrameSourceWebView is subclass of BeginFrameSourceWebView that
  57. // observes ExternalBeginFrameSourceAndroid to provide actual BeginFrames from
  58. // Android Choreographer and implements the logic of
  59. // AddBeginFrameCompletionCallback.
  60. class RootBeginFrameSourceWebView : public BeginFrameSourceWebView {
  61. public:
  62. static RootBeginFrameSourceWebView* GetInstance();
  63. void OnUpdateRefreshRate(JNIEnv* env,
  64. const base::android::JavaParamRef<jobject>& obj,
  65. float refresh_rate);
  66. // As this is implementation of root BeginFrameSourceWebView this is actual
  67. // implementation of scheduling callbacks.
  68. void AddBeginFrameCompletionCallback(base::OnceClosure callback) override;
  69. private:
  70. friend class base::NoDestructor<RootBeginFrameSourceWebView>;
  71. friend class BeginFrameSourceWebViewTest;
  72. RootBeginFrameSourceWebView();
  73. ~RootBeginFrameSourceWebView() override;
  74. void AfterBeginFrame() override;
  75. viz::ExternalBeginFrameSourceAndroid begin_frame_source_;
  76. std::vector<base::ScopedClosureRunner> after_begin_frame_callbacks_;
  77. base::android::ScopedJavaGlobalRef<jobject> j_object_;
  78. };
  79. } // namespace android_webview
  80. #endif // ANDROID_WEBVIEW_BROWSER_GFX_BEGIN_FRAME_SOURCE_WEBVIEW_H_