Flutter Engine
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 namespace flutter {
8 
10  std::unique_ptr<AndroidSurface> android_surface,
11  std::unique_ptr<Surface> surface)
12  : id(id),
13  android_surface(std::move(android_surface)),
14  surface(std::move(surface)){};
15 
16 OverlayLayer::~OverlayLayer() = default;
17 
18 SurfacePool::SurfacePool() = default;
19 
20 SurfacePool::~SurfacePool() = default;
21 
22 std::shared_ptr<OverlayLayer> SurfacePool::GetLayer(
23  GrDirectContext* gr_context,
24  std::shared_ptr<AndroidContext> android_context,
25  std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
26  std::shared_ptr<AndroidSurfaceFactory> surface_factory) {
27  // Destroy current layers in the pool if the frame size has changed.
28  if (requested_frame_size_ != current_frame_size_) {
29  DestroyLayers(jni_facade);
30  }
31 
32  intptr_t gr_context_key = reinterpret_cast<intptr_t>(gr_context);
33  // Allocate a new surface if there isn't one available.
34  if (available_layer_index_ >= layers_.size()) {
35  std::unique_ptr<AndroidSurface> android_surface =
36  surface_factory->CreateSurface();
37 
38  FML_CHECK(android_surface && android_surface->IsValid())
39  << "Could not create an OpenGL, Vulkan or Software surface to setup "
40  "rendering.";
41 
42  std::unique_ptr<PlatformViewAndroidJNI::OverlayMetadata> java_metadata =
43  jni_facade->FlutterViewCreateOverlaySurface();
44 
45  FML_CHECK(java_metadata->window);
46  android_surface->SetNativeWindow(java_metadata->window);
47 
48  std::unique_ptr<Surface> surface =
49  android_surface->CreateGPUSurface(gr_context);
50 
51  std::shared_ptr<OverlayLayer> layer =
52  std::make_shared<OverlayLayer>(java_metadata->id, //
53  std::move(android_surface), //
54  std::move(surface) //
55  );
56  layer->gr_context_key = gr_context_key;
57  layers_.push_back(layer);
58  }
59  std::shared_ptr<OverlayLayer> layer = layers_[available_layer_index_];
60  // Since the surfaces are recycled, it's possible that the GrContext is
61  // different.
62  if (gr_context_key != layer->gr_context_key) {
63  layer->gr_context_key = gr_context_key;
64  // The overlay already exists, but the GrContext was changed so we need to
65  // recreate the rendering surface with the new GrContext.
66  std::unique_ptr<Surface> surface =
67  layer->android_surface->CreateGPUSurface(gr_context);
68  layer->surface = std::move(surface);
69  }
70  available_layer_index_++;
71  current_frame_size_ = requested_frame_size_;
72  return layer;
73 }
74 
76  available_layer_index_ = 0;
77 }
78 
80  std::shared_ptr<PlatformViewAndroidJNI> jni_facade) {
81  if (layers_.size() > 0) {
82  jni_facade->FlutterViewDestroyOverlaySurfaces();
83  }
84  layers_.clear();
85  available_layer_index_ = 0;
86 }
87 
88 std::vector<std::shared_ptr<OverlayLayer>> SurfacePool::GetUnusedLayers() {
89  std::vector<std::shared_ptr<OverlayLayer>> results;
90  for (size_t i = available_layer_index_; i < layers_.size(); i++) {
91  results.push_back(layers_[i]);
92  }
93  return results;
94 }
95 
96 void SurfacePool::SetFrameSize(SkISize frame_size) {
97  requested_frame_size_ = frame_size;
98 }
99 
100 } // namespace flutter
Definition: ref_ptr.h:252
std::shared_ptr< OverlayLayer > GetLayer(GrDirectContext *gr_context, std::shared_ptr< AndroidContext > android_context, std::shared_ptr< PlatformViewAndroidJNI > jni_facade, std::shared_ptr< AndroidSurfaceFactory > surface_factory)
Definition: surface_pool.cc:22
std::unique_ptr< Surface > surface
Definition: surface_pool.h:34
void SetFrameSize(SkISize frame_size)
Definition: surface_pool.cc:96
OverlayLayer(int id, std::unique_ptr< AndroidSurface > android_surface, std::unique_ptr< Surface > surface)
Definition: surface_pool.cc:9
#define FML_CHECK(condition)
Definition: logging.h:68
void DestroyLayers(std::shared_ptr< PlatformViewAndroidJNI > jni_facade)
Definition: surface_pool.cc:79
std::vector< std::shared_ptr< OverlayLayer > > GetUnusedLayers()
Definition: surface_pool.cc:88
const std::unique_ptr< AndroidSurface > android_surface
Definition: surface_pool.h:31