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