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 using namespace Media;
35 
36 class SvgDecoder : public AbsImageDecoder, public OHOS::MultimediaPlugin::PluginClassBase {
37 public:
38     SvgDecoder();
39     ~SvgDecoder();
40     void SetSource(InputDataStream &sourceStream) override;
41     void Reset() override;
42     uint32_t SetDecodeOptions(uint32_t index, const PixelDecodeOptions &opts, PlImageInfo &info) override;
43     uint32_t Decode(uint32_t index, DecodeContext &context) override;
44     uint32_t PromoteIncrementalDecode(uint32_t index, ProgDecodeContext &context) override;
45     uint32_t GetTopLevelImageNum(uint32_t &num) override;
46     uint32_t GetImageSize(uint32_t index, Size &size) override;
47 
48 private:
49     DISALLOW_COPY_AND_MOVE(SvgDecoder);
50     bool AllocBuffer(DecodeContext &context);
51     bool BuildStream();
52     bool BuildDom();
53     uint32_t DoDecodeHeader();
54     uint32_t DoSetDecodeOptions(uint32_t index, const PixelDecodeOptions &opts, PlImageInfo &info);
55     uint32_t DoGetImageSize(uint32_t index, Size &size);
56     uint32_t DoDecode(uint32_t index, DecodeContext &context);
57 
58 private:
59     enum class SvgDecodingState : int32_t {
60         UNDECIDED = 0,
61         SOURCE_INITED = 1,
62         BASE_INFO_PARSING = 2,
63         BASE_INFO_PARSED = 3,
64         IMAGE_DECODING = 4,
65         IMAGE_ERROR = 5,
66         IMAGE_PARTIAL = 6,
67         IMAGE_DECODED = 7
68     };
69 
70     InputDataStream *inputStreamPtr_ {nullptr};
71     SvgDecodingState state_ {SvgDecodingState::UNDECIDED};
72 
73     std::unique_ptr<SkMemoryStream> svgStream_;
74     sk_sp<SkSVGDOM> svgDom_;
75     SkSize svgSize_ {0, 0};
76 
77     PixelDecodeOptions opts_;
78 };
79 } // namespace ImagePlugin
80 } // namespace OHOS
81 #endif // SVG_DECODER_H