• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef FLUTTER_VULKAN_VULKAN_SWAPCHAIN_H_
6 #define FLUTTER_VULKAN_VULKAN_SWAPCHAIN_H_
7 
8 #include <memory>
9 #include <utility>
10 #include <vector>
11 
12 #include "flutter/fml/compiler_specific.h"
13 #include "flutter/fml/macros.h"
14 #include "flutter/vulkan/vulkan_handle.h"
15 #include "third_party/skia/include/core/SkSize.h"
16 #include "third_party/skia/include/core/SkSurface.h"
17 
18 namespace vulkan {
19 
20 class VulkanProcTable;
21 class VulkanDevice;
22 class VulkanSurface;
23 class VulkanBackbuffer;
24 class VulkanImage;
25 
26 class VulkanSwapchain {
27  public:
28   VulkanSwapchain(const VulkanProcTable& vk,
29                   const VulkanDevice& device,
30                   const VulkanSurface& surface,
31                   GrContext* skia_context,
32                   std::unique_ptr<VulkanSwapchain> old_swapchain,
33                   uint32_t queue_family_index);
34 
35   ~VulkanSwapchain();
36 
37   bool IsValid() const;
38 
39   enum class AcquireStatus {
40     /// A valid SkSurface was acquired successfully from the swapchain.
41     Success,
42     /// The underlying surface of the swapchain was permanently lost. This is an
43     /// unrecoverable error. The entire surface must be recreated along with the
44     /// swapchain.
45     ErrorSurfaceLost,
46     /// The swapchain surface is out-of-date with the underlying surface. The
47     /// swapchain must be recreated.
48     ErrorSurfaceOutOfDate,
49   };
50   using AcquireResult = std::pair<AcquireStatus, sk_sp<SkSurface>>;
51 
52   /// Acquire an SkSurface from the swapchain for the caller to render into for
53   /// later submission via |Submit|. There must not be consecutive calls to
54   /// |AcquireFrame| without and interleaving |Submit|.
55   AcquireResult AcquireSurface();
56 
57   /// Submit a previously acquired. There must not be consecutive calls to
58   /// |Submit| without and interleaving |AcquireFrame|.
59   FML_WARN_UNUSED_RESULT
60   bool Submit();
61 
62   SkISize GetSize() const;
63 
64 #if OS_ANDROID
65  private:
66   const VulkanProcTable& vk;
67   const VulkanDevice& device_;
68   VkSurfaceCapabilitiesKHR capabilities_;
69   VkSurfaceFormatKHR surface_format_;
70   VulkanHandle<VkSwapchainKHR> swapchain_;
71   std::vector<std::unique_ptr<VulkanBackbuffer>> backbuffers_;
72   std::vector<std::unique_ptr<VulkanImage>> images_;
73   std::vector<sk_sp<SkSurface>> surfaces_;
74   VkPipelineStageFlagBits current_pipeline_stage_;
75   size_t current_backbuffer_index_;
76   size_t current_image_index_;
77   bool valid_;
78 
79   std::vector<VkImage> GetImages() const;
80 
81   bool CreateSwapchainImages(GrContext* skia_context,
82                              SkColorType color_type,
83                              sk_sp<SkColorSpace> color_space);
84 
85   sk_sp<SkSurface> CreateSkiaSurface(GrContext* skia_context,
86                                      VkImage image,
87                                      const SkISize& size,
88                                      SkColorType color_type,
89                                      sk_sp<SkColorSpace> color_space) const;
90 
91   VulkanBackbuffer* GetNextBackbuffer();
92 #endif  // OS_ANDROID
93 
94   FML_DISALLOW_COPY_AND_ASSIGN(VulkanSwapchain);
95 };
96 
97 }  // namespace vulkan
98 
99 #endif  // FLUTTER_VULKAN_VULKAN_SWAPCHAIN_H_
100