1 /* 2 * Copyright (C) 2023 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 PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_EXT_DECODER_H 17 #define PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_EXT_DECODER_H 18 19 #include <cstdint> 20 #include <string> 21 22 #include "SkCodec.h" 23 #include "abs_image_decoder.h" 24 #if !defined(IOS_PLATFORM) && !defined(A_PLATFORM) 25 #include "display_type.h" 26 #include "hardware/jpeg_hw_decoder.h" 27 #endif 28 #include "ext_stream.h" 29 #include "exif_info.h" 30 #include "nocopyable.h" 31 #include "plugin_class_base.h" 32 33 namespace OHOS { 34 namespace ImagePlugin { 35 class ExtDecoder : public AbsImageDecoder, public OHOS::MultimediaPlugin::PluginClassBase, NoCopyable { 36 public: 37 ExtDecoder(); 38 ~ExtDecoder() override; 39 bool HasProperty(std::string key) override; 40 uint32_t Decode(uint32_t index, DecodeContext &context) override; 41 #ifdef JPEG_HW_DECODE_ENABLE 42 uint32_t AllocOutputBuffer(DecodeContext &context); 43 void ReleaseOutputBuffer(DecodeContext &context, Media::AllocatorType allocatorType); 44 uint32_t HardWareDecode(DecodeContext &context); 45 uint32_t DoHardWareDecode(DecodeContext &context); 46 #endif 47 uint32_t GifDecode(uint32_t index, DecodeContext &context, const uint64_t rowStride); 48 uint32_t GetImageSize(uint32_t index, PlSize &size) override; 49 uint32_t GetTopLevelImageNum(uint32_t &num) override; 50 uint32_t PromoteIncrementalDecode(uint32_t index, ProgDecodeContext &context) override; 51 uint32_t SetDecodeOptions(uint32_t index, const PixelDecodeOptions &opts, PlImageInfo &info) override; 52 void Reset() override; 53 void SetSource(InputDataStream &sourceStream) override; 54 55 uint32_t GetImagePropertyInt(uint32_t index, const std::string &key, int32_t &value) override; 56 uint32_t GetImagePropertyString(uint32_t index, const std::string &key, std::string &value) override; 57 uint32_t ModifyImageProperty(uint32_t index, const std::string &key, const std::string &value, 58 const std::string &path) override; 59 uint32_t ModifyImageProperty(uint32_t index, const std::string &key, const std::string &value, 60 const int fd) override; 61 uint32_t ModifyImageProperty(uint32_t index, const std::string &key, const std::string &value, 62 uint8_t *data, uint32_t size) override; 63 uint32_t GetFilterArea(const int &privacyType, std::vector<std::pair<uint32_t, uint32_t>> &ranges) override; 64 65 #ifdef IMAGE_COLORSPACE_FLAG 66 OHOS::ColorManager::ColorSpace getGrColorSpace() override; 67 bool IsSupportICCProfile() override; 68 #endif 69 70 private: 71 bool CheckCodec(); 72 bool CheckIndexValied(uint32_t index); 73 bool DecodeHeader(); 74 bool IsSupportScaleOnDecode(); 75 bool GetScaledSize(int &dWidth, int &dHeight, float &scale); 76 bool GetHardwareScaledSize(int &dWidth, int &dHeight, float &scale); 77 bool IsSupportCropOnDecode(); 78 bool IsSupportCropOnDecode(SkIRect &target); 79 bool IsSupportHardwareDecode(); 80 bool ConvertInfoToAlphaType(SkAlphaType &alphaType, PlAlphaType &outputType); 81 bool ConvertInfoToColorType(SkColorType &format, PlPixelFormat &outputFormat); 82 bool GetPropertyCheck(uint32_t index, const std::string &key, uint32_t &res); 83 SkAlphaType ConvertToAlphaType(PlAlphaType desiredType, PlAlphaType &outputType); 84 uint32_t PreDecodeCheck(uint32_t index); 85 bool ResetCodec(); 86 SkColorType ConvertToColorType(PlPixelFormat format, PlPixelFormat &outputFormat); 87 uint32_t SetContextPixelsBuffer(uint64_t byteCount, DecodeContext &context); 88 uint32_t GetMakerImagePropertyString(const std::string &key, std::string &value); 89 uint32_t CheckDecodeOptions(uint32_t index, const PixelDecodeOptions &opts); 90 void ReportImageType(SkEncodedImageFormat skEncodeFormat); 91 ImagePlugin::InputDataStream *stream_ = nullptr; 92 uint32_t streamOff_ = 0; 93 std::unique_ptr<SkCodec> codec_; 94 SkImageInfo info_; 95 SkImageInfo dstInfo_; 96 SkCodec::Options dstOptions_; 97 SkIRect dstSubset_; 98 int32_t frameCount_ = 0; 99 EXIFInfo exifInfo_; 100 uint8_t *gifCache_ = nullptr; 101 int gifCacheIndex_ = 0; 102 #ifdef IMAGE_COLORSPACE_FLAG 103 std::shared_ptr<OHOS::ColorManager::ColorSpace> dstColorSpace_ = nullptr; 104 #endif 105 106 #if !defined(IOS_PLATFORM) && !defined(A_PLATFORM) 107 // hardware 108 OHOS::HDI::Codec::Image::V1_0::CodecImageBuffer outputBuffer_; 109 SkImageInfo hwDstInfo_; 110 PlSize orgImgSize_; 111 PlSize outputBufferSize_; 112 PixelFormat outputColorFmt_ = PIXEL_FMT_RGBA_8888; 113 uint32_t sampleSize_ = 1; 114 static constexpr uint32_t ALIGN_8 = 8; 115 static constexpr uint32_t ALIGN_16 = 16; 116 #endif 117 118 //Yuv 119 PlSize desiredSizeYuv_; 120 }; 121 } // namespace ImagePlugin 122 } // namespace OHOS 123 124 #endif // PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_EXT_DECODER_H 125