Flutter Engine
surface_pool.h
Go to the documentation of this file.
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 FLUTTER_SHELL_PLATFORM_ANDROID_EXTERNAL_VIEW_EMBEDDER_SURFACE_POOL_H_
6 #define FLUTTER_SHELL_PLATFORM_ANDROID_EXTERNAL_VIEW_EMBEDDER_SURFACE_POOL_H_
7 
8 #include "flutter/flow/surface.h"
9 #include "flutter/shell/platform/android/context/android_context.h"
10 #include "flutter/shell/platform/android/surface/android_surface.h"
11 
12 namespace flutter {
13 
14 //------------------------------------------------------------------------------
15 /// An Overlay layer represents an `android.view.View` in the C side.
16 ///
17 /// The `id` is used to uniquely identify the layer and recycle it between
18 /// frames.
19 ///
20 struct OverlayLayer {
21  OverlayLayer(int id,
22  std::unique_ptr<AndroidSurface> android_surface,
23  std::unique_ptr<Surface> surface);
24 
25  ~OverlayLayer();
26 
27  // A unique id to identify the overlay when it gets recycled.
28  const int id;
29 
30  // A GPU surface.
31  const std::unique_ptr<AndroidSurface> android_surface;
32 
33  // A GPU surface. This may change when the overlay is recycled.
34  std::unique_ptr<Surface> surface;
35 
36  // The `GrContext` that is currently used by the overlay surfaces.
37  // We track this to know when the GrContext for the Flutter app has changed
38  // so we can update the overlay with the new context.
39  //
40  // This may change when the overlay is recycled.
41  intptr_t gr_context_key;
42 };
43 
44 // This class isn't thread safe.
45 class SurfacePool {
46  public:
47  SurfacePool();
48 
49  ~SurfacePool();
50 
51  // Gets a layer from the pool if available, or allocates a new one.
52  // Finally, it marks the layer as used. That is, it increments
53  // `available_layer_index_`.
54  std::shared_ptr<OverlayLayer> GetLayer(
55  GrDirectContext* gr_context,
56  std::shared_ptr<AndroidContext> android_context,
57  std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
58  std::shared_ptr<AndroidSurfaceFactory> surface_factory);
59 
60  // Gets the layers in the pool that aren't currently used.
61  // This method doesn't mark the layers as unused.
62  std::vector<std::shared_ptr<OverlayLayer>> GetUnusedLayers();
63 
64  // Marks the layers in the pool as available for reuse.
65  void RecycleLayers();
66 
67  // Destroys all the layers in the pool.
68  void DestroyLayers(std::shared_ptr<PlatformViewAndroidJNI> jni_facade);
69 
70  // Sets the frame size used by the layers in the pool.
71  // If the current layers in the pool have a different frame size,
72  // then they are deallocated as soon as |GetLayer| is called.
73  void SetFrameSize(SkISize frame_size);
74 
75  private:
76  // The index of the entry in the layers_ vector that determines the beginning
77  // of the unused layers. For example, consider the following vector:
78  // _____
79  // | 0 |
80  // |---|
81  // | 1 | <-- `available_layer_index_`
82  // |---|
83  // | 2 |
84  // |---|
85  //
86  // This indicates that entries starting from 1 can be reused meanwhile the
87  // entry at position 0 cannot be reused.
88  size_t available_layer_index_ = 0;
89 
90  // The layers in the pool.
91  std::vector<std::shared_ptr<OverlayLayer>> layers_;
92 
93  // The frame size of the layers in the pool.
94  SkISize current_frame_size_;
95 
96  // The frame size to be used by future layers.
97  SkISize requested_frame_size_;
98 };
99 
100 } // namespace flutter
101 
102 #endif // FLUTTER_SHELL_PLATFORM_ANDROID_EXTERNAL_VIEW_EMBEDDER_SURFACE_POOL_H_
std::unique_ptr< Surface > surface
Definition: surface_pool.h:34
OverlayLayer(int id, std::unique_ptr< AndroidSurface > android_surface, std::unique_ptr< Surface > surface)
Definition: surface_pool.cc:9
const std::unique_ptr< AndroidSurface > android_surface
Definition: surface_pool.h:31