1 // Copyright 2022 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 
16 #include "compressedTextureFormats/AstcCpuDecompressor.h"
17 #include "vulkan/VkDecoderContext.h"
18 #include "goldfish_vk_dispatch.h"
19 #include "vulkan/vulkan.h"
20 
21 namespace gfxstream {
22 namespace vk {
23 
24 // Holds the resources necessary to perform CPU ASTC decompression of a single texture.
25 class AstcTexture {
26    public:
27     AstcTexture(VulkanDispatch* vk, VkDevice device, VkPhysicalDevice physicalDevice,
28                 VkExtent3D imgSize, uint32_t blockWidth, uint32_t blockHeight,
29                 AstcCpuDecompressor* decompressor);
30 
31     ~AstcTexture();
32 
33     // Whether we're able to decompress ASTC textures on the CPU
34     bool canDecompressOnCpu() const;
35 
36     // Whether this texture was successfully decompressed on the CPU
successfullyDecompressed()37     bool successfullyDecompressed() const { return mSuccess; }
38 
39     void on_vkCmdCopyBufferToImage(VkCommandBuffer commandBuffer, uint8_t* srcAstcData,
40                                    size_t astcDataSize, VkImage dstImage,
41                                    VkImageLayout dstImageLayout, uint32_t regionCount,
42                                    const VkBufferImageCopy* pRegions,
43                                    const VkDecoderContext& context);
44     void on_vkCmdCopyBufferToImage2(VkCommandBuffer commandBuffer, uint8_t* srcAstcData,
45                                    size_t astcDataSize, const VkCopyBufferToImageInfo2* pCopyBufferToImageInfo,
46                                    const VkDecoderContext& context);
47 
48    private:
49 
50     template<typename T>
51     void on_vkCmdCopyBufferToImageImpl(VkCommandBuffer commandBuffer, uint8_t* srcAstcData,
52                                    size_t astcDataSize, VkImage dstImage,
53                                    VkImageLayout dstImageLayout, uint32_t regionCount,
54                                    const T* pRegions,
55                                    const VkDecoderContext& context);
56 
57     uint8_t* createVkBufferAndMapMemory(size_t bufferSize);
58     void destroyVkBuffer();
59 
60     bool mSuccess = false;  // true if the image was successfully decompressed
61     VulkanDispatch* mVk;
62     VkDevice mDevice;
63     VkPhysicalDevice mPhysicalDevice;
64     VkExtent3D mImgSize;
65     uint32_t mBlockWidth;
66     uint32_t mBlockHeight;
67     VkBuffer mDecompBuffer = VK_NULL_HANDLE;              // VkBuffer of the decompressed image
68     VkDeviceMemory mDecompBufferMemory = VK_NULL_HANDLE;  // Memory of the decompressed image
69     uint64_t mBufferSize = 0;                             // Size of the decompressed image
70     AstcCpuDecompressor* mDecompressor;
71 };
72 
73 }  // namespace vk
74 }  // namespace gfxstream
75