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