Flutter Engine
 
Loading...
Searching...
No Matches
surface_pool.cc
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
6
7#include <utility>
8
9namespace flutter {
10
12 std::unique_ptr<AndroidSurface> android_surface,
13 std::unique_ptr<Surface> surface)
14 : id(id),
15 android_surface(std::move(android_surface)),
16 surface(std::move(surface)) {};
17
19
20SurfacePool::SurfacePool(bool use_new_surface_methods)
21 : use_new_surface_methods_(use_new_surface_methods) {}
22
24
25std::shared_ptr<OverlayLayer> SurfacePool::GetLayer(
26 GrDirectContext* gr_context,
27 const AndroidContext& android_context,
28 const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade,
29 const std::shared_ptr<AndroidSurfaceFactory>& surface_factory) {
30 std::lock_guard lock(mutex_);
31 // Destroy current layers in the pool if the frame size has changed.
32 if (requested_frame_size_ != current_frame_size_) {
33 DestroyLayersLocked(jni_facade);
34 }
35 intptr_t gr_context_key = reinterpret_cast<intptr_t>(gr_context);
36 // Allocate a new surface if there isn't one available.
37 if (available_layer_index_ >= layers_.size()) {
38 std::unique_ptr<AndroidSurface> android_surface =
39 surface_factory->CreateSurface();
40
41 FML_CHECK(android_surface && android_surface->IsValid())
42 << "Could not create an OpenGL, Vulkan or Software surface to set up "
43 "rendering.";
44
45 std::unique_ptr<PlatformViewAndroidJNI::OverlayMetadata> java_metadata =
46 use_new_surface_methods_
47 ? jni_facade->createOverlaySurface2()
48 : jni_facade->FlutterViewCreateOverlaySurface();
49
50 FML_CHECK(java_metadata->window);
51 android_surface->SetNativeWindow(java_metadata->window, jni_facade);
52 android_surface->SetupImpellerSurface();
53
54 std::unique_ptr<Surface> surface =
55 android_surface->CreateGPUSurface(gr_context);
56
57 std::shared_ptr<OverlayLayer> layer =
58 std::make_shared<OverlayLayer>(java_metadata->id, //
59 std::move(android_surface), //
60 std::move(surface) //
61 );
62 layer->gr_context_key = gr_context_key;
63 layers_.push_back(layer);
64 }
65
66 std::shared_ptr<OverlayLayer> layer = layers_[available_layer_index_];
67 // Since the surfaces are recycled, it's possible that the GrContext is
68 // different.
69 if (gr_context_key != layer->gr_context_key) {
70 layer->gr_context_key = gr_context_key;
71 // The overlay already exists, but the GrContext was changed so we need to
72 // recreate the rendering surface with the new GrContext.
73 std::unique_ptr<Surface> surface =
74 layer->android_surface->CreateGPUSurface(gr_context);
75 layer->surface = std::move(surface);
76 }
77 available_layer_index_++;
78 current_frame_size_ = requested_frame_size_;
79 return layer;
80}
81
83 std::lock_guard lock(mutex_);
84 available_layer_index_ = 0;
85}
86
88 std::lock_guard lock(mutex_);
89 return !layers_.empty();
90}
91
93 const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade) {
94 std::lock_guard lock(mutex_);
95 DestroyLayersLocked(jni_facade);
96}
97
98void SurfacePool::DestroyLayersLocked(
99 const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade) {
100 if (layers_.empty()) {
101 return;
102 }
103 if (use_new_surface_methods_) {
104 jni_facade->destroyOverlaySurface2();
105 } else {
106 jni_facade->FlutterViewDestroyOverlaySurfaces();
107 }
108 layers_.clear();
109 available_layer_index_ = 0;
110}
111
112std::vector<std::shared_ptr<OverlayLayer>> SurfacePool::GetUnusedLayers() {
113 std::lock_guard lock(mutex_);
114 std::vector<std::shared_ptr<OverlayLayer>> results;
115 for (size_t i = available_layer_index_; i < layers_.size(); i++) {
116 results.push_back(layers_[i]);
117 }
118 return results;
119}
120
122 std::lock_guard lock(mutex_);
123 requested_frame_size_ = frame_size;
124}
125
127 available_layer_index_ = 0;
128}
129
131 std::lock_guard lock(mutex_);
132 layers_.erase(layers_.begin() + available_layer_index_, layers_.end());
133 available_layer_index_ = 0;
134}
135} // namespace flutter
Holds state that is shared across Android surfaces.
void SetFrameSize(DlISize frame_size)
void DestroyLayers(const std::shared_ptr< PlatformViewAndroidJNI > &jni_facade)
SurfacePool(bool use_new_surface_methods)
std::vector< std::shared_ptr< OverlayLayer > > GetUnusedLayers()
std::shared_ptr< OverlayLayer > GetLayer(GrDirectContext *gr_context, const AndroidContext &android_context, const std::shared_ptr< PlatformViewAndroidJNI > &jni_facade, const std::shared_ptr< AndroidSurfaceFactory > &surface_factory)
VkSurfaceKHR surface
Definition main.cc:65
#define FML_CHECK(condition)
Definition logging.h:104
Definition ref_ptr.h:261
OverlayLayer(int id, std::unique_ptr< AndroidSurface > android_surface, std::unique_ptr< Surface > surface)
const uintptr_t id