Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 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 GrVkImage_DEFINED |
| 9 | #define GrVkImage_DEFINED |
| 10 | |
| 11 | #include "GrVkResource.h" |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 12 | |
| 13 | #include "GrTypesPriv.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 14 | #include "SkTypes.h" |
| 15 | |
jvanverth | e50f3e7 | 2016-03-28 07:03:06 -0700 | [diff] [blame] | 16 | #include "vk/GrVkDefines.h" |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 17 | #include "vk/GrVkTypes.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 18 | |
| 19 | class GrVkGpu; |
| 20 | |
| 21 | class GrVkImage : SkNoncopyable { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 22 | private: |
| 23 | class Resource; |
| 24 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 25 | public: |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 26 | GrVkImage(const GrVkImageInfo& info, GrBackendObjectOwnership ownership) |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 27 | : fInfo(info) |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 28 | , fIsBorrowed(GrBackendObjectOwnership::kBorrowed == ownership) { |
| 29 | if (fIsBorrowed) { |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 30 | fResource = new BorrowedResource(info.fImage, info.fAlloc, info.fImageTiling); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 31 | } else { |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 32 | fResource = new Resource(info.fImage, info.fAlloc, info.fImageTiling); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 33 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 34 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 35 | virtual ~GrVkImage(); |
| 36 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 37 | VkImage image() const { return fInfo.fImage; } |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 38 | const GrVkAlloc& alloc() const { return fInfo.fAlloc; } |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 39 | VkFormat imageFormat() const { return fInfo.fFormat; } |
egdaniel | 7ac5da8 | 2016-07-15 13:41:42 -0700 | [diff] [blame] | 40 | uint32_t mipLevels() const { return fInfo.fLevelCount; } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 41 | const Resource* resource() const { return fResource; } |
| 42 | bool isLinearTiled() const { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 43 | return SkToBool(VK_IMAGE_TILING_LINEAR == fInfo.fImageTiling); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 44 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 45 | bool isBorrowed() const { return fIsBorrowed; } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 46 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 47 | VkImageLayout currentLayout() const { return fInfo.fImageLayout; } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 48 | |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 49 | void setImageLayout(const GrVkGpu* gpu, |
| 50 | VkImageLayout newLayout, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 51 | VkAccessFlags dstAccessMask, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 52 | VkPipelineStageFlags dstStageMask, |
| 53 | bool byRegion); |
| 54 | |
| 55 | struct ImageDesc { |
| 56 | VkImageType fImageType; |
| 57 | VkFormat fFormat; |
| 58 | uint32_t fWidth; |
| 59 | uint32_t fHeight; |
| 60 | uint32_t fLevels; |
| 61 | uint32_t fSamples; |
| 62 | VkImageTiling fImageTiling; |
| 63 | VkImageUsageFlags fUsageFlags; |
| 64 | VkFlags fMemProps; |
| 65 | |
| 66 | ImageDesc() |
| 67 | : fImageType(VK_IMAGE_TYPE_2D) |
| 68 | , fFormat(VK_FORMAT_UNDEFINED) |
| 69 | , fWidth(0) |
| 70 | , fHeight(0) |
| 71 | , fLevels(1) |
| 72 | , fSamples(1) |
| 73 | , fImageTiling(VK_IMAGE_TILING_OPTIMAL) |
| 74 | , fUsageFlags(0) |
| 75 | , fMemProps(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {} |
| 76 | }; |
| 77 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 78 | static bool InitImageInfo(const GrVkGpu* gpu, const ImageDesc& imageDesc, GrVkImageInfo*); |
| 79 | // Destroys the internal VkImage and VkDeviceMemory in the GrVkImageInfo |
| 80 | static void DestroyImageInfo(const GrVkGpu* gpu, GrVkImageInfo*); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 81 | |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 82 | // These match the definitions in SkImage, for whence they came |
| 83 | typedef void* ReleaseCtx; |
| 84 | typedef void (*ReleaseProc)(ReleaseCtx); |
| 85 | |
| 86 | void setResourceRelease(ReleaseProc proc, ReleaseCtx ctx); |
| 87 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 88 | protected: |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 89 | void releaseImage(const GrVkGpu* gpu); |
| 90 | void abandonImage(); |
| 91 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 92 | void setNewResource(VkImage image, const GrVkAlloc& alloc, VkImageTiling tiling); |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 93 | |
| 94 | GrVkImageInfo fInfo; |
| 95 | bool fIsBorrowed; |
| 96 | |
| 97 | private: |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 98 | class Resource : public GrVkResource { |
| 99 | public: |
| 100 | Resource() |
| 101 | : INHERITED() |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 102 | , fReleaseProc(nullptr) |
| 103 | , fReleaseCtx(nullptr) |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 104 | , fImage(VK_NULL_HANDLE) { |
| 105 | fAlloc.fMemory = VK_NULL_HANDLE; |
| 106 | fAlloc.fOffset = 0; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 107 | } |
| 108 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 109 | Resource(VkImage image, const GrVkAlloc& alloc, VkImageTiling tiling) |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 110 | : fReleaseProc(nullptr) |
| 111 | , fReleaseCtx(nullptr) |
| 112 | , fImage(image) |
| 113 | , fAlloc(alloc) |
| 114 | , fImageTiling(tiling) {} |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 115 | |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 116 | ~Resource() override { |
| 117 | SkASSERT(!fReleaseProc); |
| 118 | } |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 119 | |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 120 | #ifdef SK_TRACE_VK_RESOURCES |
| 121 | void dumpInfo() const override { |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 122 | SkDebugf("GrVkImage: %d (%d refs)\n", fImage, this->getRefCnt()); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 123 | } |
| 124 | #endif |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 125 | void setRelease(ReleaseProc proc, ReleaseCtx ctx) const { |
| 126 | fReleaseProc = proc; |
| 127 | fReleaseCtx = ctx; |
| 128 | } |
| 129 | protected: |
| 130 | mutable ReleaseProc fReleaseProc; |
| 131 | mutable ReleaseCtx fReleaseCtx; |
| 132 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 133 | private: |
| 134 | void freeGPUData(const GrVkGpu* gpu) const override; |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 135 | void abandonGPUData() const override { |
| 136 | SkASSERT(!fReleaseProc); |
| 137 | } |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 138 | |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 139 | VkImage fImage; |
| 140 | GrVkAlloc fAlloc; |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 141 | VkImageTiling fImageTiling; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 142 | |
| 143 | typedef GrVkResource INHERITED; |
| 144 | }; |
| 145 | |
| 146 | // for wrapped textures |
| 147 | class BorrowedResource : public Resource { |
| 148 | public: |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 149 | BorrowedResource(VkImage image, const GrVkAlloc& alloc, VkImageTiling tiling) |
| 150 | : Resource(image, alloc, tiling) { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 151 | } |
| 152 | private: |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 153 | void invokeReleaseProc() const { |
| 154 | if (fReleaseProc) { |
| 155 | fReleaseProc(fReleaseCtx); |
| 156 | fReleaseProc = nullptr; |
| 157 | } |
| 158 | } |
| 159 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 160 | void freeGPUData(const GrVkGpu* gpu) const override; |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 161 | void abandonGPUData() const override; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 162 | }; |
| 163 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 164 | const Resource* fResource; |
| 165 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 166 | friend class GrVkRenderTarget; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | #endif |