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 #include "SkTypes.h"
13 
14 #include "vulkan/vulkan.h"
15 
16 class GrVkGpu;
17 
18 class GrVkImage : SkNoncopyable {
19 public:
20     // unlike GrVkBuffer, this needs to be public so GrVkStencilAttachment can use it
21     class Resource : public GrVkResource {
22     public:
23         enum Flags {
24             kNo_Flags = 0,
25             kLinearTiling_Flag = 0x01
26         };
27 
28         VkImage        fImage;
29         VkDeviceMemory fAlloc;
30         Flags          fFlags;
31 
Resource()32         Resource() : INHERITED(), fImage(nullptr), fAlloc(nullptr), fFlags(kNo_Flags) {}
33 
Resource(VkImage image,VkDeviceMemory alloc,Flags flags)34         Resource(VkImage image, VkDeviceMemory alloc, Flags flags)
35             : fImage(image), fAlloc(alloc), fFlags(flags) {}
36 
~Resource()37         ~Resource() override {}
38     private:
39         void freeGPUData(const GrVkGpu* gpu) const override;
40 
41         typedef GrVkResource INHERITED;
42     };
43 
44 
GrVkImage(const Resource * imageResource)45     GrVkImage(const Resource* imageResource) : fResource(imageResource) {
46         if (imageResource->fFlags & Resource::kLinearTiling_Flag) {
47             fCurrentLayout = VK_IMAGE_LAYOUT_PREINITIALIZED;
48         } else {
49             fCurrentLayout = VK_IMAGE_LAYOUT_UNDEFINED;
50         }
51         imageResource->ref();
52     }
53 
54     virtual ~GrVkImage();
55 
textureImage()56     VkImage textureImage() const { return fResource->fImage; }
textureMemory()57     VkDeviceMemory textureMemory() const { return fResource->fAlloc; }
resource()58     const Resource* resource() const { return fResource; }
isLinearTiled()59     bool isLinearTiled() const {
60         return SkToBool(fResource->fFlags & Resource::kLinearTiling_Flag);
61     }
62 
currentLayout()63     VkImageLayout currentLayout() const { return fCurrentLayout; }
64 
65     void setImageLayout(const GrVkGpu* gpu, VkImageLayout newLayout,
66                         VkAccessFlags srcAccessMask,
67                         VkAccessFlags dstAccessMask,
68                         VkPipelineStageFlags srcStageMask,
69                         VkPipelineStageFlags dstStageMask,
70                         bool byRegion);
71 
72     struct ImageDesc {
73         VkImageType         fImageType;
74         VkFormat            fFormat;
75         uint32_t            fWidth;
76         uint32_t            fHeight;
77         uint32_t            fLevels;
78         uint32_t            fSamples;
79         VkImageTiling       fImageTiling;
80         VkImageUsageFlags   fUsageFlags;
81         VkFlags             fMemProps;
82 
ImageDescImageDesc83         ImageDesc()
84             : fImageType(VK_IMAGE_TYPE_2D)
85             , fFormat(VK_FORMAT_UNDEFINED)
86             , fWidth(0)
87             , fHeight(0)
88             , fLevels(1)
89             , fSamples(1)
90             , fImageTiling(VK_IMAGE_TILING_OPTIMAL)
91             , fUsageFlags(0)
92             , fMemProps(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {}
93     };
94 
95     static const Resource* CreateResource(const GrVkGpu* gpu, const ImageDesc& imageDesc);
96 
97 protected:
98 
99     void releaseImage(const GrVkGpu* gpu);
100     void abandonImage();
101 
102     const Resource* fResource;
103 
104     VkImageLayout   fCurrentLayout;
105 
106 };
107 
108 #endif
109