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 #ifndef SVG_DECODER_H 16 #define SVG_DECODER_H 17 18 #include "abs_image_decoder.h" 19 #include "nocopyable.h" 20 #include "plugin_class_base.h" 21 #include "include/core/SkStream.h" 22 #include "include/core/SkSize.h" 23 24 #if defined(USE_NEWSVG_IN_NEWSKIA_FLAG) || defined(NEW_SKIA) 25 #include "modules/svg/include/SkSVGDOM.h" 26 #include "modules/svg/include/SkSVGSVG.h" 27 #include "modules/svg/include/SkSVGNode.h" 28 #else 29 #include "experimental/svg/model/SkSVGDOM.h" 30 #endif 31 32 namespace OHOS { 33 namespace ImagePlugin { 34 class SvgDecoder : public AbsImageDecoder, public OHOS::MultimediaPlugin::PluginClassBase { 35 public: 36 SvgDecoder(); 37 ~SvgDecoder(); 38 void SetSource(InputDataStream &sourceStream) override; 39 void Reset() override; 40 uint32_t SetDecodeOptions(uint32_t index, const PixelDecodeOptions &opts, PlImageInfo &info) override; 41 uint32_t Decode(uint32_t index, DecodeContext &context) override; 42 uint32_t PromoteIncrementalDecode(uint32_t index, ProgDecodeContext &context) override; 43 uint32_t GetTopLevelImageNum(uint32_t &num) override; 44 uint32_t GetImageSize(uint32_t index, PlSize &size) override; 45 46 private: 47 DISALLOW_COPY_AND_MOVE(SvgDecoder); 48 bool AllocBuffer(DecodeContext &context); 49 bool BuildStream(); 50 bool BuildDom(); 51 uint32_t DoDecodeHeader(); 52 uint32_t DoSetDecodeOptions(uint32_t index, const PixelDecodeOptions &opts, PlImageInfo &info); 53 uint32_t DoGetImageSize(uint32_t index, PlSize &size); 54 uint32_t DoDecode(uint32_t index, DecodeContext &context); 55 56 private: 57 enum class SvgDecodingState : int32_t { 58 UNDECIDED = 0, 59 SOURCE_INITED = 1, 60 BASE_INFO_PARSING = 2, 61 BASE_INFO_PARSED = 3, 62 IMAGE_DECODING = 4, 63 IMAGE_ERROR = 5, 64 IMAGE_PARTIAL = 6, 65 IMAGE_DECODED = 7 66 }; 67 68 InputDataStream *inputStreamPtr_ {nullptr}; 69 SvgDecodingState state_ {SvgDecodingState::UNDECIDED}; 70 71 std::unique_ptr<SkMemoryStream> svgStream_; 72 sk_sp<SkSVGDOM> svgDom_; 73 SkSize svgSize_ {0, 0}; 74 75 PixelDecodeOptions opts_; 76 }; 77 } // namespace ImagePlugin 78 } // namespace OHOS 79 #endif // SVG_DECODER_H