Skia
2D Graphics Library
MutableTextureState.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 Google LLC.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef skgpu_MutableTextureState_DEFINED
9 #define skgpu_MutableTextureState_DEFINED
10 
11 #include "include/core/SkTypes.h"
12 #include "include/gpu/GpuTypes.h"
13 
14 #ifdef SK_VULKAN
15 #include "include/private/gpu/vk/SkiaVulkan.h"
16 #include "include/private/gpu/vk/VulkanTypesPriv.h"
17 
18 #include <cstdint>
19 #endif
20 
21 #include <new>
22 
23 class GrVkGpu;
24 
25 namespace skgpu {
26 
37 class SK_API MutableTextureState {
38 public:
40 
41 #ifdef SK_VULKAN
42  MutableTextureState(VkImageLayout layout, uint32_t queueFamilyIndex)
43  : fVkState(layout, queueFamilyIndex)
44  , fBackend(BackendApi::kVulkan)
45  , fIsValid(true) {}
46 #endif
47 
49  : fBackend(that.fBackend), fIsValid(that.fIsValid) {
50  if (!fIsValid) {
51  return;
52  }
53  switch (fBackend) {
55  #ifdef SK_VULKAN
56  SkASSERT(that.fBackend == BackendApi::kVulkan);
57  fVkState = that.fVkState;
58  #endif
59  break;
60  default:
61  (void)that;
62  SkUNREACHABLE;
63  }
64  }
65 
67  if (this != &that) {
68  this->~MutableTextureState();
69  new (this) MutableTextureState(that);
70  }
71  return *this;
72  }
73 
74 #ifdef SK_VULKAN
75  // If this class is not Vulkan backed it will return value of VK_IMAGE_LAYOUT_UNDEFINED.
76  // Otherwise it will return the VkImageLayout.
77  VkImageLayout getVkImageLayout() const {
78  if (this->isValid() && fBackend != BackendApi::kVulkan) {
79  return VK_IMAGE_LAYOUT_UNDEFINED;
80  }
81  return fVkState.getImageLayout();
82  }
83 
84  // If this class is not Vulkan backed it will return value of VK_QUEUE_FAMILY_IGNORED.
85  // Otherwise it will return the VkImageLayout.
86  uint32_t getQueueFamilyIndex() const {
87  if (this->isValid() && fBackend != BackendApi::kVulkan) {
88  return VK_QUEUE_FAMILY_IGNORED;
89  }
90  return fVkState.getQueueFamilyIndex();
91  }
92 #endif
93 
94  BackendApi backend() const { return fBackend; }
95 
96  // Returns true if the backend mutable state has been initialized.
97  bool isValid() const { return fIsValid; }
98 
99 private:
100  friend class MutableTextureStateRef;
101  friend class ::GrVkGpu;
102 
103 #ifdef SK_VULKAN
104  void setVulkanState(VkImageLayout layout, uint32_t queueFamilyIndex) {
105  SkASSERT(!this->isValid() || fBackend == BackendApi::kVulkan);
106  fVkState.setImageLayout(layout);
107  fVkState.setQueueFamilyIndex(queueFamilyIndex);
108  fBackend = BackendApi::kVulkan;
109  fIsValid = true;
110  }
111 #endif
112 
113  union {
115 #ifdef SK_VULKAN
116  VulkanMutableTextureState fVkState;
117 #endif
118  };
119 
120  BackendApi fBackend = BackendApi::kMock;
121  bool fIsValid = false;
122 };
123 
124 } // namespace skgpu
125 
126 #endif // skgpu_MutableTextureState_DEFINED
Since Skia and clients can both modify gpu textures and their connected state, Skia needs a way for c...
Definition: MutableTextureState.h:37
MutableTextureState & operator=(const MutableTextureState &that)
Definition: MutableTextureState.h:66
bool isValid() const
Definition: MutableTextureState.h:97
MutableTextureState(const MutableTextureState &that)
Definition: MutableTextureState.h:48
char fPlaceholder
Definition: MutableTextureState.h:114
MutableTextureState()
Definition: MutableTextureState.h:39
BackendApi backend() const
Definition: MutableTextureState.h:94
This file includes numerous public types that are used by all of our gpu backends.
Definition: SkCanvas.h:73
BackendApi
Possible 3D APIs that may be used by Graphite.
Definition: GpuTypes.h:22