Flutter Engine
vulkan_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 #pragma once
6 
7 #include <fuchsia/ui/composition/cpp/fidl.h>
8 
9 #include <unordered_map>
10 #include <vector>
11 
12 #include "flutter/fml/macros.h"
13 #include "vulkan_surface.h"
14 
15 namespace flutter_runner {
16 
17 class VulkanSurfacePool final {
18  public:
19  // Only keep 12 surfaces at a time. This value was based on how many
20  // surfaces got cached in the old, exact-match-only caching logic.
21  static constexpr int kMaxSurfaces = 12;
22  // If a surface doesn't get used for 3 or more generations, we discard it.
23  static constexpr int kMaxSurfaceAge = 3;
24 
26  sk_sp<GrDirectContext> context,
27  scenic::Session* scenic_session);
28 
30 
31  std::unique_ptr<VulkanSurface> CreateSurface(const SkISize& size);
32  std::unique_ptr<VulkanSurface> AcquireSurface(const SkISize& size);
33 
34  void SubmitSurface(std::unique_ptr<SurfaceProducerSurface> surface);
35 
37 
38  // Shrink all oversized |VulkanSurfaces| in |available_surfaces_| to as
39  // small as they can be.
40  void ShrinkToFit();
41 
42  private:
43  vulkan::VulkanProvider& vulkan_provider_;
44  sk_sp<GrDirectContext> context_;
45  scenic::Session* scenic_session_;
46  fuchsia::sysmem::AllocatorSyncPtr sysmem_allocator_;
47  fuchsia::ui::composition::AllocatorPtr flatland_allocator_;
48  std::vector<std::unique_ptr<VulkanSurface>> available_surfaces_;
49  std::unordered_map<uintptr_t, std::unique_ptr<VulkanSurface>>
50  pending_surfaces_;
51  uint32_t buffer_id_ = 1;
52 
53  size_t trace_surfaces_created_ = 0;
54  size_t trace_surfaces_reused_ = 0;
55 
56  std::unique_ptr<VulkanSurface> GetCachedOrCreateSurface(const SkISize& size);
57 
58  void RecycleSurface(std::unique_ptr<VulkanSurface> surface);
59 
60  void RecyclePendingSurface(uintptr_t surface_key);
61 
62  void TraceStats();
63 
64  FML_DISALLOW_COPY_AND_ASSIGN(VulkanSurfacePool);
65 };
66 
67 } // namespace flutter_runner
VulkanSurfacePool(vulkan::VulkanProvider &vulkan_provider, sk_sp< GrDirectContext > context, scenic::Session *scenic_session)
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
std::unique_ptr< VulkanSurface > CreateSurface(const SkISize &size)
void SubmitSurface(std::unique_ptr< SurfaceProducerSurface > surface)
std::unique_ptr< VulkanSurface > AcquireSurface(const SkISize &size)