• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Flutter 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 #ifndef SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_H_
6 #define SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_H_
7 
8 #include <memory>
9 #include <string>
10 #include <unordered_map>
11 #include <vector>
12 
13 #include "flutter/fml/memory/weak_ptr.h"
14 #include "flutter/fml/platform/android/jni_weak_ref.h"
15 #include "flutter/fml/platform/android/scoped_java_ref.h"
16 #include "flutter/lib/ui/window/platform_message.h"
17 #include "flutter/shell/common/platform_view.h"
18 #include "flutter/shell/platform/android/android_native_window.h"
19 #include "flutter/shell/platform/android/android_surface.h"
20 
21 namespace flutter {
22 
23 class PlatformViewAndroid final : public PlatformView {
24  public:
25   static bool Register(JNIEnv* env);
26   static bool RegisterOnFirstFrame(JNIEnv* env, jint platform);
27 
28   // Creates a PlatformViewAndroid with no rendering surface for use with
29   // background execution.
30   PlatformViewAndroid(PlatformView::Delegate& delegate,
31                       flutter::TaskRunners task_runners,
32                       fml::jni::JavaObjectWeakGlobalRef java_object);
33 
34   // Creates a PlatformViewAndroid with a rendering surface.
35   PlatformViewAndroid(PlatformView::Delegate& delegate,
36                       flutter::TaskRunners task_runners,
37                       fml::jni::JavaObjectWeakGlobalRef java_object,
38                       bool use_software_rendering);
39 
40   ~PlatformViewAndroid() override;
41 
42   void NotifyCreated(fml::RefPtr<AndroidNativeWindow> native_window);
43 
44   void NotifyChanged(const SkISize& size);
45 
46   // |PlatformView|
47   void NotifyDestroyed() override;
48 
49   void DispatchPlatformMessage(JNIEnv* env,
50                                std::string name,
51                                jobject message_data,
52                                jint message_position,
53                                jint response_id);
54 
55   void DispatchEmptyPlatformMessage(JNIEnv* env,
56                                     std::string name,
57                                     jint response_id);
58 
59   void InvokePlatformMessageResponseCallback(JNIEnv* env,
60                                              jint response_id,
61                                              jobject java_response_data,
62                                              jint java_response_position);
63 
64   void InvokePlatformMessageEmptyResponseCallback(JNIEnv* env,
65                                                   jint response_id);
66 
67   void RegisterExternalTexture(
68       int64_t texture_id,
69       const fml::jni::JavaObjectWeakGlobalRef& surface_texture);
70 
71  private:
72   const fml::jni::JavaObjectWeakGlobalRef java_object_;
73   const std::unique_ptr<AndroidSurface> android_surface_;
74   // We use id 0 to mean that no response is expected.
75   int next_response_id_ = 1;
76   std::unordered_map<int, fml::RefPtr<flutter::PlatformMessageResponse>>
77       pending_responses_;
78 
79   // |PlatformView|
80   void HandlePlatformMessage(
81       fml::RefPtr<flutter::PlatformMessage> message) override;
82 
83   // |PlatformView|
84   void OnPreEngineRestart() const override;
85 
86   // |PlatformView|
87   std::unique_ptr<VsyncWaiter> CreateVSyncWaiter(int32_t platform) override;
88 
89   // |PlatformView|
90   std::unique_ptr<Surface> CreateRenderingSurface() override;
91 
92   // |PlatformView|
93   sk_sp<GrContext> CreateResourceContext() const override;
94 
95   // |PlatformView|
96   void ReleaseResourceContext() const override;
97 
98   void InstallFirstFrameCallback();
99 
100   void FireFirstFrameCallback();
101 
102   FML_DISALLOW_COPY_AND_ASSIGN(PlatformViewAndroid);
103 };
104 
105 }  // namespace flutter
106 
107 #endif  // SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_H_
108