• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "modules/svg/include/SkSVGDOM.h"
25 #include "modules/svg/include/SkSVGSVG.h"
26 #include "modules/svg/include/SkSVGNode.h"
27 
28 namespace OHOS {
29 namespace ImagePlugin {
30 using namespace Media;
31 
32 class SvgDecoder : public AbsImageDecoder, public OHOS::MultimediaPlugin::PluginClassBase {
33 public:
34     SvgDecoder();
35     ~SvgDecoder();
36     void SetSource(InputDataStream &sourceStream) override;
37     void Reset() override;
38     uint32_t SetDecodeOptions(uint32_t index, const PixelDecodeOptions &opts, PlImageInfo &info) override;
39     uint32_t Decode(uint32_t index, DecodeContext &context) override;
40     uint32_t PromoteIncrementalDecode(uint32_t index, ProgDecodeContext &context) override;
41     uint32_t GetTopLevelImageNum(uint32_t &num) override;
42     uint32_t GetImageSize(uint32_t index, Size &size) override;
GetPluginType()43     std::string GetPluginType() override
44     {
45         return "svg";
46     }
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