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