• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 Huawei Device Co., Ltd.
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  */
15 
16 #ifndef HRP_PIXELMAP_H
17 #define HRP_PIXELMAP_H
18 
19 namespace OHOS::Rosen {
20 
21 using OHOS::sptr;
22 using OHOS::SurfaceBuffer;
23 using OHOS::Media::ImageInfo;
24 using OHOS::Media::PIXEL_MAP_ERR;
25 using OHOS::Media::PixelMap;
26 using OHOS::Media::PixelMemInfo;
27 using OHOS::Media::AllocatorType;
28 
29 using ImageData = std::vector<uint8_t>;
30 
31 struct ImageProperties {
32     explicit ImageProperties(PixelMap& map);
33 
34     Media::PixelFormat format;
35 
36     int32_t width;
37     int32_t height;
38 
39     int32_t stride;
40 };
41 
42 struct TextureHeader {
43     int32_t magicNumber;
44     ImageProperties properties;
45 
46     int32_t totalOriginalSize;
47     int32_t rgbEncodedSize;
48 
49     int32_t alphaOriginalSize;
50     int32_t alphaEncodedSize;
51 };
52 
53 enum class EncodedType : int {
54     NONE = 0,
55     JPEG = 1,
56     XLZ4 = 2,
57 };
58 
59 class PixelMapStorage final {
60 public:
61     static bool Pull(uint64_t id, const ImageInfo& info, PixelMemInfo& memory, size_t& skipBytes);
62     static bool Push(uint64_t id, const ImageInfo& info, const PixelMemInfo& memory, size_t skipBytes);
63 
64     static bool Push(uint64_t id, PixelMap& map);
65 
66 private:
67     static bool Fits(size_t size);
68 
69     static bool PullSharedMemory(uint64_t id, const ImageInfo& info, PixelMemInfo& memory, size_t& skipBytes);
70     static void PushSharedMemory(uint64_t id, const ImageInfo& info, const PixelMemInfo& memory, size_t skipBytes);
71     static void PushSharedMemory(uint64_t id, PixelMap& map);
72 
73     static bool PullDmaMemory(uint64_t id, const ImageInfo& info, PixelMemInfo& memory, size_t& skipBytes);
74     static void PushDmaMemory(uint64_t id, const ImageInfo& info, const PixelMemInfo& memory, size_t skipBytes);
75     static void PushDmaMemory(uint64_t id, PixelMap& map);
76 
77     static void PushImage(uint64_t id, const ImageData& data, size_t skipBytes, BufferHandle* buffer = nullptr,
78                             const ImageProperties* properties = nullptr);
79 
80     static bool IsSharedMemory(const PixelMap& map);
81     static bool IsSharedMemory(const PixelMemInfo& memory);
82     static bool IsSharedMemory(AllocatorType type);
83     static bool IsDmaMemory(const PixelMap& map);
84     static bool IsDmaMemory(const PixelMemInfo& memory);
85     static bool IsDmaMemory(AllocatorType type);
86 
87     static EncodedType TryEncodeTexture(const ImageProperties* properties, const ImageData& data, Image& image);
88 
89     static bool ValidateBufferSize(const PixelMemInfo& memory);
90     static uint32_t GetBytesPerPixel(const ImageInfo& info);
91 
92     static uint8_t* MapImage(int32_t file, size_t size, int32_t flags);
93     static void UnmapImage(void* image, size_t size);
94 
95     static SurfaceBuffer* IncrementSurfaceBufferReference(sptr<SurfaceBuffer>& buffer);
96 
97     static bool IsDataValid(const void* data, size_t size);
98 
99     static int32_t EncodeSeqLZ4(const ImageData& source, ImageData& dst);
100     static int32_t DecodeSeqLZ4(const char* source, ImageData& dst, int32_t sourceSize, int32_t originalSize);
101 
102     static void ExtractAlpha(const ImageData& image, ImageData& alpha, const ImageProperties& properties);
103     static void ReplaceAlpha(ImageData& image, ImageData& alpha, const ImageProperties& properties);
104     static int32_t MakeStride(
105         ImageData& noPadding, ImageData& dst, const ImageProperties& properties, int32_t pixelBytes);
106 
107     static int32_t EncodeJpeg(const ImageData& source, ImageData& dst, const ImageProperties& properties);
108     static int32_t DecodeJpeg(
109         const char* source, ImageData& dst, int32_t sourceSize, const ImageProperties& properties);
110 
111     static bool CopyImageData(const uint8_t* srcImage, size_t srcSize, uint8_t* dstImage, size_t dstSize);
112     static bool CopyImageData(const ImageData& data, uint8_t* dstImage, size_t dstSize);
113     static bool CopyImageData(Image* image, uint8_t* dstImage, size_t dstSize);
114 
115     static ImageData GenerateRawCopy(const uint8_t* data, size_t size);
116     static ImageData GenerateMiniatureAstc(const uint8_t* data, size_t size);
117     static ImageData GenerateMiniature(const uint8_t* data, size_t size, uint32_t pixelBytes);
118     static ImageData GenerateImageData(const uint8_t* data, size_t size, const PixelMap& map);
119     static ImageData GenerateImageData(const ImageInfo& info, const PixelMemInfo& memory);
120     static ImageData GenerateImageData(const uint8_t* data, size_t size, bool isAstc, uint32_t pixelBytes);
121 };
122 
123 } // OHOS::Rosen
124 
125 #endif // HRP_PIXELMAP_H