Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 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 | |
jvanverth | e50f3e7 | 2016-03-28 07:03:06 -0700 | [diff] [blame] | 14 | #include "vk/GrVkDefines.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 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; |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 31 | VkFormat fFormat; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 32 | |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 33 | Resource() |
| 34 | : INHERITED() |
| 35 | , fImage(VK_NULL_HANDLE) |
| 36 | , fAlloc(VK_NULL_HANDLE) |
| 37 | , fFlags(kNo_Flags) |
| 38 | , fFormat(VK_FORMAT_UNDEFINED) {} |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 39 | |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 40 | Resource(VkImage image, VkDeviceMemory alloc, Flags flags, VkFormat format) |
| 41 | : fImage(image), fAlloc(alloc), fFlags(flags), fFormat(format) {} |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 42 | |
| 43 | ~Resource() override {} |
| 44 | private: |
| 45 | void freeGPUData(const GrVkGpu* gpu) const override; |
| 46 | |
| 47 | typedef GrVkResource INHERITED; |
| 48 | }; |
| 49 | |
jvanverth | fe170d2 | 2016-03-22 13:15:44 -0700 | [diff] [blame] | 50 | // for wrapped textures |
| 51 | class BorrowedResource : public Resource { |
| 52 | public: |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 53 | BorrowedResource(VkImage image, VkDeviceMemory alloc, Flags flags, VkFormat format) |
| 54 | : Resource(image, alloc, flags, format) {} |
jvanverth | fe170d2 | 2016-03-22 13:15:44 -0700 | [diff] [blame] | 55 | private: |
| 56 | void freeGPUData(const GrVkGpu* gpu) const override; |
| 57 | }; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 58 | |
| 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 | |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 79 | void setImageLayout(const GrVkGpu* gpu, |
| 80 | VkImageLayout newLayout, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 81 | 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 | |
| 112 | protected: |
| 113 | |
| 114 | void releaseImage(const GrVkGpu* gpu); |
| 115 | void abandonImage(); |
| 116 | |
| 117 | const Resource* fResource; |
| 118 | |
| 119 | VkImageLayout fCurrentLayout; |
| 120 | |
| 121 | }; |
| 122 | |
| 123 | #endif |