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 #include "common/rs_macros.h" 20 namespace OHOS::Rosen { 21 22 using OHOS::sptr; 23 using OHOS::SurfaceBuffer; 24 using OHOS::Media::ImageInfo; 25 using OHOS::Media::PIXEL_MAP_ERR; 26 using OHOS::Media::PixelMap; 27 using OHOS::Media::PixelMemInfo; 28 using OHOS::Media::AllocatorType; 29 30 using ImageData = std::vector<uint8_t>; 31 32 struct RSB_EXPORT ImageProperties { 33 explicit ImageProperties(PixelMap& map); 34 explicit ImageProperties(const ImageInfo& info, AllocatorType type); 35 36 int16_t format; 37 int8_t allocType; 38 39 int32_t width; 40 int32_t height; 41 42 int32_t stride; 43 GetAllocTypeImageProperties44 AllocatorType GetAllocType() const 45 { 46 return static_cast<AllocatorType>(allocType); 47 } 48 SetFormatImageProperties49 void SetFormat(Media::PixelFormat pf) 50 { 51 format = static_cast<int16_t>(pf); 52 } 53 GetFormatImageProperties54 Media::PixelFormat GetFormat() const 55 { 56 return static_cast<Media::PixelFormat>(format); 57 } 58 }; 59 60 struct TextureHeader { 61 int32_t magicNumber; 62 ImageProperties properties; 63 64 int32_t totalOriginalSize; 65 int32_t rgbEncodedSize; 66 67 int32_t alphaOriginalSize; 68 int32_t alphaEncodedSize; 69 }; 70 71 enum class EncodedType : int { 72 NONE = 0, 73 JPEG = 1, 74 XLZ4 = 2, 75 }; 76 77 class RSB_EXPORT PixelMapStorage final { 78 public: 79 static bool Pull(uint64_t id, const ImageInfo& info, PixelMemInfo& memory, size_t& skipBytes); 80 static bool Push(uint64_t id, const ImageInfo& info, const PixelMemInfo& memory, size_t skipBytes); 81 82 static bool Push(uint64_t id, PixelMap& map); 83 84 private: 85 static bool Fits(size_t size); 86 87 static bool PullSharedMemory(uint64_t id, const ImageInfo& info, PixelMemInfo& memory, size_t& skipBytes); 88 static bool PushSharedMemory(uint64_t id, const ImageInfo& info, const PixelMemInfo& memory, size_t skipBytes); 89 static bool PushSharedMemory(uint64_t id, PixelMap& map); 90 91 static bool PullDmaMemory(uint64_t id, const ImageInfo& info, PixelMemInfo& memory, size_t& skipBytes); 92 static bool PushDmaMemory(uint64_t id, const ImageInfo& info, const PixelMemInfo& memory, size_t skipBytes); 93 static bool PushDmaMemory(uint64_t id, PixelMap& map); 94 95 static bool PushImage(uint64_t id, const ImageData& data, size_t skipBytes, BufferHandle* buffer = nullptr, 96 const ImageProperties* properties = nullptr); 97 98 static bool IsSharedMemory(const PixelMap& map); 99 static bool IsSharedMemory(const PixelMemInfo& memory); 100 static bool IsSharedMemory(AllocatorType type); 101 static bool IsDmaMemory(const PixelMap& map); 102 static bool IsDmaMemory(const PixelMemInfo& memory); 103 static bool IsDmaMemory(AllocatorType type); 104 105 static bool PullHeapMemory(uint64_t id, const ImageInfo& info, PixelMemInfo& memory, size_t& skipBytes); 106 static bool PushHeapMemory(uint64_t id, const ImageInfo& info, const PixelMemInfo& memory, size_t skipBytes); 107 static bool PushHeapMemory(uint64_t id, PixelMap& map); 108 109 static bool DefaultHeapMemory(uint64_t id, const ImageInfo& info, PixelMemInfo& memory, size_t& skipBytes); 110 111 static EncodedType TryEncodeTexture(const ImageProperties* properties, const ImageData& data, Image& image); 112 113 static bool ValidateBufferSize(const PixelMemInfo& memory); 114 static uint32_t GetBytesPerPixel(const ImageInfo& info); 115 116 static uint8_t* MapImage(int32_t file, size_t size, int32_t flags); 117 static void UnmapImage(void* image, size_t size); 118 119 static SurfaceBuffer* IncrementSurfaceBufferReference(sptr<SurfaceBuffer>& buffer); 120 121 static bool IsDataValid(const void* data, size_t size); 122 123 static int32_t EncodeSeqLZ4(const ImageData& source, ImageData& dst); 124 static int32_t DecodeSeqLZ4(const char* source, ImageData& dst, int32_t sourceSize, int32_t originalSize); 125 126 static void ExtractAlpha(const ImageData& image, ImageData& alpha, const ImageProperties& properties); 127 static void ReplaceAlpha(ImageData& image, ImageData& alpha, const ImageProperties& properties); 128 static int32_t MakeStride( 129 ImageData& noPadding, ImageData& dst, const ImageProperties& properties, int32_t pixelBytes); 130 131 static int32_t EncodeJpeg(const ImageData& source, ImageData& dst, const ImageProperties& properties); 132 static int32_t DecodeJpeg( 133 const char* source, ImageData& dst, int32_t sourceSize, const ImageProperties& properties); 134 135 static bool CopyImageData(const uint8_t* srcImage, size_t srcSize, uint8_t* dstImage, size_t dstSize); 136 static bool CopyImageData(const ImageData& data, uint8_t* dstImage, size_t dstSize); 137 static bool CopyImageData(Image* image, uint8_t* dstImage, size_t dstSize); 138 139 static ImageData GenerateRawCopy(const uint8_t* data, size_t size); 140 static ImageData GenerateMiniatureAstc(const uint8_t* data, size_t size); 141 static ImageData GenerateMiniature(uint64_t uniqueId, const uint8_t* data, size_t size, uint32_t pixelBytes); 142 static ImageData GenerateImageData(uint64_t uniqueId, const uint8_t* data, size_t size, const PixelMap& map); 143 static ImageData GenerateImageData(uint64_t uniqueId, const ImageInfo& info, const PixelMemInfo& memory); 144 static ImageData GenerateImageData( 145 uint64_t uniqueId, const uint8_t* data, size_t size, bool isAstc, uint32_t pixelBytes); 146 }; 147 148 } // OHOS::Rosen 149 150 #endif // HRP_PIXELMAP_H