xref: /aosp_15_r20/external/angle/src/image_util/loadimage_astc.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2022 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // loadimage_astc.cpp: Decodes ASTC encoded textures.
8 
9 #include "image_util/AstcDecompressor.h"
10 #include "image_util/loadimage.h"
11 
12 namespace angle
13 {
14 
LoadASTCToRGBA8Inner(const ImageLoadContext & context,size_t width,size_t height,size_t depth,uint32_t blockWidth,uint32_t blockHeight,const uint8_t * input,size_t inputRowPitch,size_t inputDepthPitch,uint8_t * output,size_t outputRowPitch,size_t outputDepthPitch)15 void LoadASTCToRGBA8Inner(const ImageLoadContext &context,
16                           size_t width,
17                           size_t height,
18                           size_t depth,
19                           uint32_t blockWidth,
20                           uint32_t blockHeight,
21                           const uint8_t *input,
22                           size_t inputRowPitch,
23                           size_t inputDepthPitch,
24                           uint8_t *output,
25                           size_t outputRowPitch,
26                           size_t outputDepthPitch)
27 {
28     auto imgWidth  = static_cast<uint32_t>(width);
29     auto imgHeight = static_cast<uint32_t>(height);
30 
31     AstcDecompressor &decompressor = AstcDecompressor::get();
32     if (!decompressor.available())
33     {
34         ERR() << "Trying to decompress ASTC without having ASTC support built.";
35         return;
36     }
37 
38     // Compute the number of ASTC blocks in each dimension
39     uint32_t blockCountX = (imgWidth + blockWidth - 1) / blockWidth;
40     uint32_t blockCountY = (imgHeight + blockHeight - 1) / blockHeight;
41 
42     // Space needed for 16 bytes of output per compressed block
43     size_t blockSize = blockCountX * blockCountY * 16;
44 
45     int32_t result =
46         decompressor.decompress(context.singleThreadPool, context.multiThreadPool, imgWidth,
47                                 imgHeight, blockWidth, blockHeight, input, blockSize, output);
48     if (result != 0)
49     {
50         WARN() << "ASTC decompression failed: " << decompressor.getStatusString(result);
51     }
52 }
53 }  // namespace angle
54