blob: e43c2f267220265963f5d5a2c3728e34920873c6 [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
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"
jvanverth900bd4a2016-04-29 13:53:12 -070012
13#include "GrTypesPriv.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050014#include "SkTypes.h"
15
jvanverthe50f3e72016-03-28 07:03:06 -070016#include "vk/GrVkDefines.h"
egdanielb2df0c22016-05-13 11:30:37 -070017#include "vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050018
19class GrVkGpu;
20
21class GrVkImage : SkNoncopyable {
egdanielb2df0c22016-05-13 11:30:37 -070022private:
23 class Resource;
24
Greg Daniel164a9f02016-02-22 09:56:40 -050025public:
Greg Daniel1591c382017-08-17 15:37:20 -040026 GrVkImage(const GrVkImageInfo& info, GrBackendObjectOwnership ownership)
egdanielb2df0c22016-05-13 11:30:37 -070027 : fInfo(info)
Greg Daniel1591c382017-08-17 15:37:20 -040028 , fIsBorrowed(GrBackendObjectOwnership::kBorrowed == ownership) {
29 if (fIsBorrowed) {
jvanverth6b6ffc42016-06-13 14:28:07 -070030 fResource = new BorrowedResource(info.fImage, info.fAlloc, info.fImageTiling);
Greg Daniel164a9f02016-02-22 09:56:40 -050031 } else {
jvanverth6b6ffc42016-06-13 14:28:07 -070032 fResource = new Resource(info.fImage, info.fAlloc, info.fImageTiling);
Greg Daniel164a9f02016-02-22 09:56:40 -050033 }
Greg Daniel164a9f02016-02-22 09:56:40 -050034 }
Greg Daniel164a9f02016-02-22 09:56:40 -050035 virtual ~GrVkImage();
36
egdanielb2df0c22016-05-13 11:30:37 -070037 VkImage image() const { return fInfo.fImage; }
jvanverth1e305ba2016-06-01 09:39:15 -070038 const GrVkAlloc& alloc() const { return fInfo.fAlloc; }
egdanielb2df0c22016-05-13 11:30:37 -070039 VkFormat imageFormat() const { return fInfo.fFormat; }
egdaniel7ac5da82016-07-15 13:41:42 -070040 uint32_t mipLevels() const { return fInfo.fLevelCount; }
Greg Daniel164a9f02016-02-22 09:56:40 -050041 const Resource* resource() const { return fResource; }
42 bool isLinearTiled() const {
egdanielb2df0c22016-05-13 11:30:37 -070043 return SkToBool(VK_IMAGE_TILING_LINEAR == fInfo.fImageTiling);
Greg Daniel164a9f02016-02-22 09:56:40 -050044 }
Brian Osman13dddce2017-05-09 13:19:50 -040045 bool isBorrowed() const { return fIsBorrowed; }
Greg Daniel164a9f02016-02-22 09:56:40 -050046
egdanielb2df0c22016-05-13 11:30:37 -070047 VkImageLayout currentLayout() const { return fInfo.fImageLayout; }
Greg Daniel164a9f02016-02-22 09:56:40 -050048
egdaniel58a8d922016-04-21 08:03:10 -070049 void setImageLayout(const GrVkGpu* gpu,
50 VkImageLayout newLayout,
Greg Daniel164a9f02016-02-22 09:56:40 -050051 VkAccessFlags dstAccessMask,
Greg Daniel164a9f02016-02-22 09:56:40 -050052 VkPipelineStageFlags dstStageMask,
53 bool byRegion);
54
Greg Daniel31cc7312018-03-05 11:41:06 -050055 // This simply updates our tracking of the image layout and does not actually do any gpu work.
56 // This is only used for mip map generation where we are manually changing the layouts as we
57 // blit each layer, and then at the end need to update our tracking.
58 void updateImageLayout(VkImageLayout newLayout) { fInfo.fImageLayout = newLayout; }
59
Greg Daniel164a9f02016-02-22 09:56:40 -050060 struct ImageDesc {
61 VkImageType fImageType;
62 VkFormat fFormat;
63 uint32_t fWidth;
64 uint32_t fHeight;
65 uint32_t fLevels;
66 uint32_t fSamples;
67 VkImageTiling fImageTiling;
68 VkImageUsageFlags fUsageFlags;
69 VkFlags fMemProps;
70
71 ImageDesc()
72 : fImageType(VK_IMAGE_TYPE_2D)
73 , fFormat(VK_FORMAT_UNDEFINED)
74 , fWidth(0)
75 , fHeight(0)
76 , fLevels(1)
77 , fSamples(1)
78 , fImageTiling(VK_IMAGE_TILING_OPTIMAL)
79 , fUsageFlags(0)
80 , fMemProps(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {}
81 };
82
egdanielb2df0c22016-05-13 11:30:37 -070083 static bool InitImageInfo(const GrVkGpu* gpu, const ImageDesc& imageDesc, GrVkImageInfo*);
84 // Destroys the internal VkImage and VkDeviceMemory in the GrVkImageInfo
85 static void DestroyImageInfo(const GrVkGpu* gpu, GrVkImageInfo*);
Greg Daniel164a9f02016-02-22 09:56:40 -050086
Greg Danielcef213c2017-04-21 11:52:27 -040087 // These match the definitions in SkImage, for whence they came
88 typedef void* ReleaseCtx;
89 typedef void (*ReleaseProc)(ReleaseCtx);
90
Greg Daniel6a0176b2018-01-30 09:28:44 -050091 void setResourceRelease(sk_sp<GrReleaseProcHelper> releaseHelper);
Greg Danielcef213c2017-04-21 11:52:27 -040092
Greg Daniel164a9f02016-02-22 09:56:40 -050093protected:
Greg Daniel164a9f02016-02-22 09:56:40 -050094 void releaseImage(const GrVkGpu* gpu);
95 void abandonImage();
96
jvanverth6b6ffc42016-06-13 14:28:07 -070097 void setNewResource(VkImage image, const GrVkAlloc& alloc, VkImageTiling tiling);
egdanielb2df0c22016-05-13 11:30:37 -070098
99 GrVkImageInfo fInfo;
100 bool fIsBorrowed;
101
102private:
egdanielb2df0c22016-05-13 11:30:37 -0700103 class Resource : public GrVkResource {
104 public:
105 Resource()
Greg Daniel6a0176b2018-01-30 09:28:44 -0500106 : fImage(VK_NULL_HANDLE) {
jvanverth1e305ba2016-06-01 09:39:15 -0700107 fAlloc.fMemory = VK_NULL_HANDLE;
108 fAlloc.fOffset = 0;
egdanielb2df0c22016-05-13 11:30:37 -0700109 }
110
jvanverth6b6ffc42016-06-13 14:28:07 -0700111 Resource(VkImage image, const GrVkAlloc& alloc, VkImageTiling tiling)
Greg Daniel6a0176b2018-01-30 09:28:44 -0500112 : fImage(image)
Greg Danielcef213c2017-04-21 11:52:27 -0400113 , fAlloc(alloc)
114 , fImageTiling(tiling) {}
egdanielb2df0c22016-05-13 11:30:37 -0700115
Greg Danielcef213c2017-04-21 11:52:27 -0400116 ~Resource() override {
Greg Daniel6a0176b2018-01-30 09:28:44 -0500117 SkASSERT(!fReleaseHelper);
Greg Danielcef213c2017-04-21 11:52:27 -0400118 }
egdanielb2df0c22016-05-13 11:30:37 -0700119
jvanverth7ec92412016-07-06 09:24:57 -0700120#ifdef SK_TRACE_VK_RESOURCES
121 void dumpInfo() const override {
egdaniela95220d2016-07-21 11:50:37 -0700122 SkDebugf("GrVkImage: %d (%d refs)\n", fImage, this->getRefCnt());
jvanverth7ec92412016-07-06 09:24:57 -0700123 }
124#endif
Greg Daniel6a0176b2018-01-30 09:28:44 -0500125 void setRelease(sk_sp<GrReleaseProcHelper> releaseHelper) {
126 fReleaseHelper = std::move(releaseHelper);
Greg Danielcef213c2017-04-21 11:52:27 -0400127 }
128 protected:
Greg Daniel6a0176b2018-01-30 09:28:44 -0500129 mutable sk_sp<GrReleaseProcHelper> fReleaseHelper;
Greg Danielcef213c2017-04-21 11:52:27 -0400130
egdanielb2df0c22016-05-13 11:30:37 -0700131 private:
132 void freeGPUData(const GrVkGpu* gpu) const override;
Greg Danielcef213c2017-04-21 11:52:27 -0400133 void abandonGPUData() const override {
Greg Daniel6a0176b2018-01-30 09:28:44 -0500134 SkASSERT(!fReleaseHelper);
Greg Danielcef213c2017-04-21 11:52:27 -0400135 }
egdanielb2df0c22016-05-13 11:30:37 -0700136
jvanverth1e305ba2016-06-01 09:39:15 -0700137 VkImage fImage;
138 GrVkAlloc fAlloc;
jvanverth6b6ffc42016-06-13 14:28:07 -0700139 VkImageTiling fImageTiling;
egdanielb2df0c22016-05-13 11:30:37 -0700140
141 typedef GrVkResource INHERITED;
142 };
143
144 // for wrapped textures
145 class BorrowedResource : public Resource {
146 public:
jvanverth6b6ffc42016-06-13 14:28:07 -0700147 BorrowedResource(VkImage image, const GrVkAlloc& alloc, VkImageTiling tiling)
148 : Resource(image, alloc, tiling) {
egdanielb2df0c22016-05-13 11:30:37 -0700149 }
150 private:
Greg Danielcef213c2017-04-21 11:52:27 -0400151 void invokeReleaseProc() const {
Greg Daniel6a0176b2018-01-30 09:28:44 -0500152 if (fReleaseHelper) {
153 // Depending on the ref count of fReleaseHelper this may or may not actually trigger
154 // the ReleaseProc to be called.
155 fReleaseHelper.reset();
Greg Danielcef213c2017-04-21 11:52:27 -0400156 }
157 }
158
egdanielb2df0c22016-05-13 11:30:37 -0700159 void freeGPUData(const GrVkGpu* gpu) const override;
Greg Danielcef213c2017-04-21 11:52:27 -0400160 void abandonGPUData() const override;
egdanielb2df0c22016-05-13 11:30:37 -0700161 };
162
Greg Daniel6a0176b2018-01-30 09:28:44 -0500163 Resource* fResource;
Greg Daniel164a9f02016-02-22 09:56:40 -0500164
egdanielb2df0c22016-05-13 11:30:37 -0700165 friend class GrVkRenderTarget;
Greg Daniel164a9f02016-02-22 09:56:40 -0500166};
167
168#endif