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; |
| 31 | |
jvanverth | 9846ef2 | 2016-03-02 12:08:22 -0800 | [diff] [blame] | 32 | Resource() : INHERITED(), fImage(VK_NULL_HANDLE), fAlloc(VK_NULL_HANDLE), fFlags(kNo_Flags) {} |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 33 | |
| 34 | Resource(VkImage image, VkDeviceMemory alloc, Flags flags) |
| 35 | : fImage(image), fAlloc(alloc), fFlags(flags) {} |
| 36 | |
| 37 | ~Resource() override {} |
| 38 | private: |
| 39 | void freeGPUData(const GrVkGpu* gpu) const override; |
| 40 | |
| 41 | typedef GrVkResource INHERITED; |
| 42 | }; |
| 43 | |
jvanverth | fe170d2 | 2016-03-22 13:15:44 -0700 | [diff] [blame] | 44 | // for wrapped textures |
| 45 | class BorrowedResource : public Resource { |
| 46 | public: |
| 47 | BorrowedResource(VkImage image, VkDeviceMemory alloc, Flags flags) |
| 48 | : Resource(image, alloc, flags) {} |
| 49 | private: |
| 50 | void freeGPUData(const GrVkGpu* gpu) const override; |
| 51 | }; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 52 | |
| 53 | GrVkImage(const Resource* imageResource) : fResource(imageResource) { |
| 54 | if (imageResource->fFlags & Resource::kLinearTiling_Flag) { |
| 55 | fCurrentLayout = VK_IMAGE_LAYOUT_PREINITIALIZED; |
| 56 | } else { |
| 57 | fCurrentLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 58 | } |
| 59 | imageResource->ref(); |
| 60 | } |
| 61 | |
| 62 | virtual ~GrVkImage(); |
| 63 | |
| 64 | VkImage textureImage() const { return fResource->fImage; } |
| 65 | VkDeviceMemory textureMemory() const { return fResource->fAlloc; } |
| 66 | const Resource* resource() const { return fResource; } |
| 67 | bool isLinearTiled() const { |
| 68 | return SkToBool(fResource->fFlags & Resource::kLinearTiling_Flag); |
| 69 | } |
| 70 | |
| 71 | VkImageLayout currentLayout() const { return fCurrentLayout; } |
| 72 | |
| 73 | void setImageLayout(const GrVkGpu* gpu, VkImageLayout newLayout, |
| 74 | VkAccessFlags srcAccessMask, |
| 75 | VkAccessFlags dstAccessMask, |
| 76 | VkPipelineStageFlags srcStageMask, |
| 77 | VkPipelineStageFlags dstStageMask, |
| 78 | bool byRegion); |
| 79 | |
| 80 | struct ImageDesc { |
| 81 | VkImageType fImageType; |
| 82 | VkFormat fFormat; |
| 83 | uint32_t fWidth; |
| 84 | uint32_t fHeight; |
| 85 | uint32_t fLevels; |
| 86 | uint32_t fSamples; |
| 87 | VkImageTiling fImageTiling; |
| 88 | VkImageUsageFlags fUsageFlags; |
| 89 | VkFlags fMemProps; |
| 90 | |
| 91 | ImageDesc() |
| 92 | : fImageType(VK_IMAGE_TYPE_2D) |
| 93 | , fFormat(VK_FORMAT_UNDEFINED) |
| 94 | , fWidth(0) |
| 95 | , fHeight(0) |
| 96 | , fLevels(1) |
| 97 | , fSamples(1) |
| 98 | , fImageTiling(VK_IMAGE_TILING_OPTIMAL) |
| 99 | , fUsageFlags(0) |
| 100 | , fMemProps(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {} |
| 101 | }; |
| 102 | |
| 103 | static const Resource* CreateResource(const GrVkGpu* gpu, const ImageDesc& imageDesc); |
| 104 | |
| 105 | protected: |
| 106 | |
| 107 | void releaseImage(const GrVkGpu* gpu); |
| 108 | void abandonImage(); |
| 109 | |
| 110 | const Resource* fResource; |
| 111 | |
| 112 | VkImageLayout fCurrentLayout; |
| 113 | |
| 114 | }; |
| 115 | |
| 116 | #endif |