Flutter Engine
The 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
5#include "flutter/shell/platform/android/external_view_embedder/surface_pool.h"
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() = default;
21
23
24std::shared_ptr<OverlayLayer> SurfacePool::GetLayer(
25 GrDirectContext* gr_context,
26 const AndroidContext& android_context,
27 const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade,
28 const std::shared_ptr<AndroidSurfaceFactory>& surface_factory) {
29 std::lock_guard lock(mutex_);
30 // Destroy current layers in the pool if the frame size has changed.
31 if (requested_frame_size_ != current_frame_size_) {
32 DestroyLayersLocked(jni_facade);
33 }
34 intptr_t gr_context_key = reinterpret_cast<intptr_t>(gr_context);
35 // Allocate a new surface if there isn't one available.
36 if (available_layer_index_ >= layers_.size()) {
37 std::unique_ptr<AndroidSurface> android_surface =
38 surface_factory->CreateSurface();
39
40 FML_CHECK(android_surface && android_surface->IsValid())
41 << "Could not create an OpenGL, Vulkan or Software surface to set up "
42 "rendering.";
43
44 std::unique_ptr<PlatformViewAndroidJNI::OverlayMetadata> java_metadata =
45 jni_facade->FlutterViewCreateOverlaySurface();
46
47 FML_CHECK(java_metadata->window);
48 android_surface->SetNativeWindow(java_metadata->window);
49
50 std::unique_ptr<Surface> surface =
51 android_surface->CreateGPUSurface(gr_context);
52
53 std::shared_ptr<OverlayLayer> layer =
54 std::make_shared<OverlayLayer>(java_metadata->id, //
55 std::move(android_surface), //
56 std::move(surface) //
57 );
58 layer->gr_context_key = gr_context_key;
59 layers_.push_back(layer);
60 }
61
62 std::shared_ptr<OverlayLayer> layer = layers_[available_layer_index_];
63 // Since the surfaces are recycled, it's possible that the GrContext is
64 // different.
65 if (gr_context_key != layer->gr_context_key) {
66 layer->gr_context_key = gr_context_key;
67 // The overlay already exists, but the GrContext was changed so we need to
68 // recreate the rendering surface with the new GrContext.
69 std::unique_ptr<Surface> surface =
70 layer->android_surface->CreateGPUSurface(gr_context);
71 layer->surface = std::move(surface);
72 }
73 available_layer_index_++;
74 current_frame_size_ = requested_frame_size_;
75 return layer;
76}
77
79 std::lock_guard lock(mutex_);
80 available_layer_index_ = 0;
81}
82
84 std::lock_guard lock(mutex_);
85 return !layers_.empty();
86}
87
89 const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade) {
90 std::lock_guard lock(mutex_);
91 DestroyLayersLocked(jni_facade);
92}
93
94void SurfacePool::DestroyLayersLocked(
95 const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade) {
96 if (layers_.empty()) {
97 return;
98 }
99 jni_facade->FlutterViewDestroyOverlaySurfaces();
100 layers_.clear();
101 available_layer_index_ = 0;
102}
103
104std::vector<std::shared_ptr<OverlayLayer>> SurfacePool::GetUnusedLayers() {
105 std::lock_guard lock(mutex_);
106 std::vector<std::shared_ptr<OverlayLayer>> results;
107 for (size_t i = available_layer_index_; i < layers_.size(); i++) {
108 results.push_back(layers_[i]);
109 }
110 return results;
111}
112
114 std::lock_guard lock(mutex_);
115 requested_frame_size_ = frame_size;
116}
117
118} // namespace flutter
Holds state that is shared across Android surfaces.
void DestroyLayers(const std::shared_ptr< PlatformViewAndroidJNI > &jni_facade)
void SetFrameSize(SkISize frame_size)
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:49
#define FML_CHECK(condition)
Definition logging.h:85
Definition ref_ptr.h:256
OverlayLayer(int id, std::unique_ptr< AndroidSurface > android_surface, std::unique_ptr< Surface > surface)
const uintptr_t id