blob: 5b3b527202be99236a340485fe93354ec6591baf [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"
12#include "SkTypes.h"
13
jvanverthe50f3e72016-03-28 07:03:06 -070014#include "vk/GrVkDefines.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050015
16class GrVkGpu;
17
18class GrVkImage : SkNoncopyable {
19public:
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;
egdaniel58a8d922016-04-21 08:03:10 -070031 VkFormat fFormat;
Greg Daniel164a9f02016-02-22 09:56:40 -050032
egdaniel58a8d922016-04-21 08:03:10 -070033 Resource()
34 : INHERITED()
35 , fImage(VK_NULL_HANDLE)
36 , fAlloc(VK_NULL_HANDLE)
37 , fFlags(kNo_Flags)
38 , fFormat(VK_FORMAT_UNDEFINED) {}
Greg Daniel164a9f02016-02-22 09:56:40 -050039
egdaniel58a8d922016-04-21 08:03:10 -070040 Resource(VkImage image, VkDeviceMemory alloc, Flags flags, VkFormat format)
41 : fImage(image), fAlloc(alloc), fFlags(flags), fFormat(format) {}
Greg Daniel164a9f02016-02-22 09:56:40 -050042
43 ~Resource() override {}
44 private:
45 void freeGPUData(const GrVkGpu* gpu) const override;
46
47 typedef GrVkResource INHERITED;
48 };
49
jvanverthfe170d22016-03-22 13:15:44 -070050 // for wrapped textures
51 class BorrowedResource : public Resource {
52 public:
egdaniel58a8d922016-04-21 08:03:10 -070053 BorrowedResource(VkImage image, VkDeviceMemory alloc, Flags flags, VkFormat format)
54 : Resource(image, alloc, flags, format) {}
jvanverthfe170d22016-03-22 13:15:44 -070055 private:
56 void freeGPUData(const GrVkGpu* gpu) const override;
57 };
Greg Daniel164a9f02016-02-22 09:56:40 -050058
59 GrVkImage(const Resource* imageResource) : fResource(imageResource) {
60 if (imageResource->fFlags & Resource::kLinearTiling_Flag) {
61 fCurrentLayout = VK_IMAGE_LAYOUT_PREINITIALIZED;
62 } else {
63 fCurrentLayout = VK_IMAGE_LAYOUT_UNDEFINED;
64 }
65 imageResource->ref();
66 }
67
68 virtual ~GrVkImage();
69
70 VkImage textureImage() const { return fResource->fImage; }
71 VkDeviceMemory textureMemory() const { return fResource->fAlloc; }
72 const Resource* resource() const { return fResource; }
73 bool isLinearTiled() const {
74 return SkToBool(fResource->fFlags & Resource::kLinearTiling_Flag);
75 }
76
77 VkImageLayout currentLayout() const { return fCurrentLayout; }
78
egdaniel58a8d922016-04-21 08:03:10 -070079 void setImageLayout(const GrVkGpu* gpu,
80 VkImageLayout newLayout,
Greg Daniel164a9f02016-02-22 09:56:40 -050081 VkAccessFlags srcAccessMask,
82 VkAccessFlags dstAccessMask,
83 VkPipelineStageFlags srcStageMask,
84 VkPipelineStageFlags dstStageMask,
85 bool byRegion);
86
87 struct ImageDesc {
88 VkImageType fImageType;
89 VkFormat fFormat;
90 uint32_t fWidth;
91 uint32_t fHeight;
92 uint32_t fLevels;
93 uint32_t fSamples;
94 VkImageTiling fImageTiling;
95 VkImageUsageFlags fUsageFlags;
96 VkFlags fMemProps;
97
98 ImageDesc()
99 : fImageType(VK_IMAGE_TYPE_2D)
100 , fFormat(VK_FORMAT_UNDEFINED)
101 , fWidth(0)
102 , fHeight(0)
103 , fLevels(1)
104 , fSamples(1)
105 , fImageTiling(VK_IMAGE_TILING_OPTIMAL)
106 , fUsageFlags(0)
107 , fMemProps(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {}
108 };
109
110 static const Resource* CreateResource(const GrVkGpu* gpu, const ImageDesc& imageDesc);
111
112protected:
113
114 void releaseImage(const GrVkGpu* gpu);
115 void abandonImage();
116
117 const Resource* fResource;
118
119 VkImageLayout fCurrentLayout;
120
121};
122
123#endif