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     }
51 
currentLayout()52     VkImageLayout currentLayout() const { return fInfo.fImageLayout; }
53 
54     void setImageLayout(const GrVkGpu* gpu,
55                         VkImageLayout newLayout,
56                         VkAccessFlags dstAccessMask,
57                         VkPipelineStageFlags dstStageMask,
58                         bool byRegion);
59 
60     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 
ImageDescImageDesc71         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 
83     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*);
86 
87 protected:
88     void releaseImage(const GrVkGpu* gpu);
89     void abandonImage();
90 
91     void setNewResource(VkImage image, const GrVkAlloc& alloc, VkImageTiling tiling);
92 
93     GrVkImageInfo   fInfo;
94     bool            fIsBorrowed;
95 
96 private:
97     class Resource : public GrVkResource {
98     public:
Resource()99         Resource()
100             : INHERITED()
101             , fImage(VK_NULL_HANDLE) {
102             fAlloc.fMemory = VK_NULL_HANDLE;
103             fAlloc.fOffset = 0;
104         }
105 
Resource(VkImage image,const GrVkAlloc & alloc,VkImageTiling tiling)106         Resource(VkImage image, const GrVkAlloc& alloc, VkImageTiling tiling)
107             : fImage(image), fAlloc(alloc), fImageTiling(tiling) {}
108 
~Resource()109         ~Resource() override {}
110 
111 #ifdef SK_TRACE_VK_RESOURCES
dumpInfo()112         void dumpInfo() const override {
113             SkDebugf("GrVkImage: %d (%d refs)\n", fImage, this->getRefCnt());
114         }
115 #endif
116     private:
117         void freeGPUData(const GrVkGpu* gpu) const override;
118 
119         VkImage        fImage;
120         GrVkAlloc      fAlloc;
121         VkImageTiling  fImageTiling;
122 
123         typedef GrVkResource INHERITED;
124     };
125 
126     // for wrapped textures
127     class BorrowedResource : public Resource {
128     public:
BorrowedResource(VkImage image,const GrVkAlloc & alloc,VkImageTiling tiling)129         BorrowedResource(VkImage image, const GrVkAlloc& alloc, VkImageTiling tiling)
130             : Resource(image, alloc, tiling) {
131         }
132     private:
133         void freeGPUData(const GrVkGpu* gpu) const override;
134     };
135 
136     const Resource* fResource;
137 
138     friend class GrVkRenderTarget;
139 };
140 
141 #endif
142