• 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_HANDLE_H_
6 #define FLUTTER_VULKAN_VULKAN_HANDLE_H_
7 
8 #include <functional>
9 
10 #ifndef RS_ENABLE_VK
11 #include "flutter/fml/logging.h"
12 #include "flutter/fml/macros.h"
13 #endif
14 #include "vulkan_interface.h"
15 
16 namespace vulkan {
17 
18 template <class T>
19 class VulkanHandle {
20  public:
21   using Handle = T;
22   using Disposer = std::function<void(Handle)>;
23 
VulkanHandle()24   VulkanHandle() : handle_(VK_NULL_HANDLE) {}
25 
26   VulkanHandle(Handle handle, Disposer disposer = nullptr)
handle_(handle)27       : handle_(handle), disposer_(disposer) {}
28 
VulkanHandle(VulkanHandle && other)29   VulkanHandle(VulkanHandle&& other)
30       : handle_(other.handle_), disposer_(other.disposer_) {
31     other.handle_ = VK_NULL_HANDLE;
32     other.disposer_ = nullptr;
33   }
34 
~VulkanHandle()35   ~VulkanHandle() { DisposeIfNecessary(); }
36 
37   VulkanHandle& operator=(VulkanHandle&& other) {
38     if (handle_ != other.handle_) {
39       DisposeIfNecessary();
40     }
41 
42     handle_ = other.handle_;
43     disposer_ = other.disposer_;
44 
45     other.handle_ = VK_NULL_HANDLE;
46     other.disposer_ = nullptr;
47 
48     return *this;
49   }
50 
51   operator bool() const { return handle_ != VK_NULL_HANDLE; }
52 
Handle()53   operator Handle() const { return handle_; }
54 
55   /// Relinquish responsibility of collecting the underlying handle when this
56   /// object is collected. It is the responsibility of the caller to ensure that
57   /// the lifetime of the handle extends past the lifetime of this object.
ReleaseOwnership()58   void ReleaseOwnership() { disposer_ = nullptr; }
59 
Reset()60   void Reset() { DisposeIfNecessary(); }
61 
62  private:
63   Handle handle_;
64   Disposer disposer_;
65 
DisposeIfNecessary()66   void DisposeIfNecessary() {
67     if (handle_ == VK_NULL_HANDLE) {
68       return;
69     }
70     if (disposer_) {
71       disposer_(handle_);
72     }
73     handle_ = VK_NULL_HANDLE;
74     disposer_ = nullptr;
75   }
76 
77 #ifndef RS_ENABLE_VK
78   FML_DISALLOW_COPY_AND_ASSIGN(VulkanHandle);
79 #endif
80 };
81 
82 }  // namespace vulkan
83 
84 #endif  // FLUTTER_VULKAN_VULKAN_HANDLE_H_
85