• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 INTERFACES_INNERKITS_INCLUDE_IMAGE_SOURCE_H_
17 #define INTERFACES_INNERKITS_INCLUDE_IMAGE_SOURCE_H_
18 
19 #include <cstdint>
20 #include <iostream>
21 #include <map>
22 #include <memory>
23 #include <mutex>
24 #include <set>
25 
26 #include "decode_listener.h"
27 #include "image_type.h"
28 #include "incremental_pixel_map.h"
29 #include "peer_listener.h"
30 #include "pixel_map.h"
31 
32 namespace OHOS {
33 namespace MultimediaPlugin {
34 constexpr float EPSILON = 1e-6;
35 
36 class PluginServer;
37 } // namespace MultimediaPlugin
38 } // namespace OHOS
39 
40 namespace OHOS {
41 namespace ImagePlugin {
42 class AbsImageFormatAgent;
43 class AbsImageDecoder;
44 struct PixelDecodeOptions;
45 struct PlImageInfo;
46 } // namespace ImagePlugin
47 } // namespace OHOS
48 
49 namespace OHOS {
50 namespace Media {
51 struct SourceOptions {
52     std::string formatHint;
53     int32_t baseDensity = 0;
54     PixelFormat pixelFormat = PixelFormat::UNKNOWN;
55     Size size;
56 };
57 
58 struct IncrementalSourceOptions {
59     SourceOptions sourceOptions;
60     IncrementalMode incrementalMode = IncrementalMode::FULL_DATA;
61 };
62 
63 struct NinePatchInfo {
64     void *ninePatch = nullptr;
65     size_t patchSize = 0;
66 };
67 
68 enum class DecodeEvent : int32_t {
69     EVENT_COMPLETE_DECODE = 0,
70     EVENT_PARTIAL_DECODE = 1,
71     EVENT_HEADER_DECODE = 2,
72     EVENT_LAST = 3
73 };
74 
75 enum class ImageDecodingState : int32_t {
76     UNRESOLVED = 0,
77     BASE_INFO_ERROR = 1,
78     BASE_INFO_PARSED = 2,
79     IMAGE_DECODING = 3,
80     IMAGE_ERROR = 4,
81     PARTIAL_IMAGE = 5,
82     IMAGE_DECODED = 6
83 };
84 
85 enum class SourceDecodingState : int32_t {
86     UNRESOLVED = 0,
87     SOURCE_ERROR = 1,
88     UNKNOWN_FORMAT = 2,
89     FORMAT_RECOGNIZED = 3,
90     UNSUPPORTED_FORMAT = 4,
91     FILE_INFO_ERROR = 5,
92     FILE_INFO_DECODED = 6,
93     IMAGE_DECODING = 7,
94     ALL_IMAGES_ERROR = 8
95 };
96 
97 enum class SourceInfoState : int32_t {
98     SOURCE_ERROR = 0,
99     SOURCE_INCOMPLETE = 1,
100     UNKNOWN_FORMAT = 2,
101     UNSUPPORTED_FORMAT = 3,
102     FILE_INFO_ERROR = 4,
103     FILE_INFO_PARSED = 5
104 };
105 
106 struct ImageDecodingStatus {
107     ImageInfo imageInfo;
108     ImageDecodingState imageState = ImageDecodingState::UNRESOLVED;
109 };
110 
111 struct SourceInfo {
112     int32_t baseDensity = 0;
113     uint32_t topLevelImageNum = 0;
114     std::string encodedFormat;
115     SourceInfoState state = SourceInfoState::SOURCE_ERROR;
116 };
117 
118 struct IncrementalDecodingContext {
119     std::unique_ptr<ImagePlugin::AbsImageDecoder> decoder;
120     ImageDecodingState IncrementalState = ImageDecodingState::UNRESOLVED;
121     uint8_t decodingProgress = 0;
122 };
123 
124 class SourceStream;
125 
126 class ImageSource {
127 public:
128     ~ImageSource();
129     NATIVEEXPORT static uint32_t GetSupportedFormats(std::set<std::string> &formats);
130     NATIVEEXPORT static std::unique_ptr<ImageSource> CreateImageSource(std::unique_ptr<std::istream> is,
131                                                                        const SourceOptions &opts, uint32_t &errorCode);
132     NATIVEEXPORT static std::unique_ptr<ImageSource> CreateImageSource(const uint8_t *data, uint32_t size,
133                                                                        const SourceOptions &opts, uint32_t &errorCode);
134     NATIVEEXPORT static std::unique_ptr<ImageSource> CreateImageSource(const std::string &pathName,
135                                                                        const SourceOptions &opts, uint32_t &errorCode);
136     NATIVEEXPORT static std::unique_ptr<ImageSource> CreateImageSource(const int fd, const SourceOptions &opts,
137                                                        uint32_t &errorCode);
138     NATIVEEXPORT static std::unique_ptr<ImageSource> CreateIncrementalImageSource(const IncrementalSourceOptions &opts,
139                                                                                   uint32_t &errorCode);
140 
CreatePixelMap(const DecodeOptions & opts,uint32_t & errorCode)141     NATIVEEXPORT std::unique_ptr<PixelMap> CreatePixelMap(const DecodeOptions &opts, uint32_t &errorCode)
142     {
143         return CreatePixelMapEx(0, opts, errorCode);
144     }
145     NATIVEEXPORT std::unique_ptr<PixelMap> CreatePixelMapEx(uint32_t index, const DecodeOptions &opts,
146                                                             uint32_t &errorCode);
147     NATIVEEXPORT std::unique_ptr<PixelMap> CreatePixelMap(uint32_t index, const DecodeOptions &opts,
148                                                           uint32_t &errorCode);
149     NATIVEEXPORT std::unique_ptr<IncrementalPixelMap> CreateIncrementalPixelMap(uint32_t index,
150                                                                                 const DecodeOptions &opts,
151                                                                                 uint32_t &errorCode);
152     // for incremental source.
153     NATIVEEXPORT uint32_t UpdateData(const uint8_t *data, uint32_t size, bool isCompleted);
154     // for obtaining basic image information without decoding image data.
GetImageInfo(ImageInfo & imageInfo)155     NATIVEEXPORT uint32_t GetImageInfo(ImageInfo &imageInfo)
156     {
157         return GetImageInfo(0, imageInfo);
158     }
159     NATIVEEXPORT uint32_t GetImageInfo(uint32_t index, ImageInfo &imageInfo);
160     NATIVEEXPORT const SourceInfo &GetSourceInfo(uint32_t &errorCode);
161     NATIVEEXPORT void RegisterListener(PeerListener *listener);
162     NATIVEEXPORT void UnRegisterListener(PeerListener *listener);
163     NATIVEEXPORT DecodeEvent GetDecodeEvent();
164     NATIVEEXPORT void AddDecodeListener(DecodeListener *listener);
165     NATIVEEXPORT void RemoveDecodeListener(DecodeListener *listener);
166     NATIVEEXPORT bool IsIncrementalSource();
167     NATIVEEXPORT uint32_t GetImagePropertyInt(uint32_t index, const std::string &key, int32_t &value);
168     NATIVEEXPORT uint32_t GetImagePropertyString(uint32_t index, const std::string &key, std::string &value);
169     NATIVEEXPORT uint32_t ModifyImageProperty(uint32_t index, const std::string &key, const std::string &value,
170         const std::string &path);
171     NATIVEEXPORT uint32_t ModifyImageProperty(uint32_t index, const std::string &key, const std::string &value,
172         const int fd);
173     NATIVEEXPORT uint32_t ModifyImageProperty(uint32_t index, const std::string &key, const std::string &value,
174         uint8_t *data, uint32_t size);
175     NATIVEEXPORT const NinePatchInfo &GetNinePatchInfo() const;
176     NATIVEEXPORT void SetMemoryUsagePreference(const MemoryUsagePreference preference);
177     NATIVEEXPORT MemoryUsagePreference GetMemoryUsagePreference();
178     NATIVEEXPORT uint32_t GetFilterArea(const int &privacyType, std::vector<std::pair<uint32_t, uint32_t>> &ranges);
179 
180 private:
181     DISALLOW_COPY_AND_MOVE(ImageSource);
182     using FormatAgentMap = std::map<std::string, ImagePlugin::AbsImageFormatAgent *>;
183     using ImageStatusMap = std::map<uint32_t, ImageDecodingStatus>;
184     using IncrementalRecordMap = std::map<PixelMap *, IncrementalDecodingContext>;
185     ImageSource(std::unique_ptr<SourceStream> &&stream, const SourceOptions &opts);
186     uint32_t CheckEncodedFormat(ImagePlugin::AbsImageFormatAgent &agent);
187     static FormatAgentMap InitClass();
188     uint32_t GetEncodedFormat(const std::string &formatHint, std::string &format);
189     uint32_t DecodeImageInfo(uint32_t index, ImageStatusMap::iterator &iter);
190     uint32_t DecodeSourceInfo(bool isAcquiredImageNum);
191     uint32_t InitMainDecoder();
192     ImagePlugin::AbsImageDecoder *CreateDecoder(uint32_t &errorCode);
193     void CopyOptionsToPlugin(const DecodeOptions &opts, ImagePlugin::PixelDecodeOptions &plOpts);
194     void CopyOptionsToProcOpts(const DecodeOptions &opts, DecodeOptions &procOpts, PixelMap &pixelMap);
195     uint32_t CheckFormatHint(const std::string &formatHint, FormatAgentMap::iterator &formatIter);
196     uint32_t GetSourceInfo();
197     uint32_t OnSourceRecognized(bool isAcquiredImageNum);
198     uint32_t OnSourceUnresolved();
199     uint32_t SetDecodeOptions(std::unique_ptr<ImagePlugin::AbsImageDecoder> &decoder, uint32_t index,
200                               const DecodeOptions &opts, ImagePlugin::PlImageInfo &plInfo);
201     uint32_t UpdatePixelMapInfo(const DecodeOptions &opts, ImagePlugin::PlImageInfo &plInfo, PixelMap &pixelMap);
202     // declare friend class, only IncrementalPixelMap can call PromoteDecoding function.
203     friend class IncrementalPixelMap;
204     uint32_t PromoteDecoding(uint32_t index, const DecodeOptions &opts, PixelMap &pixelMap, ImageDecodingState &state,
205                              uint8_t &decodeProgress);
206     void DetachIncrementalDecoding(PixelMap &pixelMap);
207     ImageStatusMap::iterator GetValidImageStatus(uint32_t index, uint32_t &errorCode);
208     uint32_t AddIncrementalContext(PixelMap &pixelMap, IncrementalRecordMap::iterator &iterator);
209     uint32_t DoIncrementalDecoding(uint32_t index, const DecodeOptions &opts, PixelMap &pixelMap,
210                                    IncrementalDecodingContext &recordContext);
211     void SetIncrementalSource(const bool isIncrementalSource);
212     bool IsStreamCompleted();
213     FinalOutputStep GetFinalOutputStep(const DecodeOptions &opts, PixelMap &pixelMap, bool hasNinePatch);
214     bool HasDensityChange(const DecodeOptions &opts, ImageInfo &srcImageInfo, bool hasNinePatch);
215     bool ImageSizeChange(int32_t width, int32_t height, int32_t desiredWidth, int32_t desiredHeight);
216     bool ImageConverChange(const Rect &cropRect, ImageInfo &dstImageInfo, ImageInfo &srcImageInfo);
217     void Reset();
218     static std::unique_ptr<SourceStream> DecodeBase64(const uint8_t *data, uint32_t size);
219     static std::unique_ptr<SourceStream> DecodeBase64(const std::string &data);
220     bool IsSpecialYUV();
221     bool ConvertYUV420ToRGBA(uint8_t *data, uint32_t size, bool isSupportOdd, bool isAddUV, uint32_t &errorCode);
222     std::unique_ptr<PixelMap> CreatePixelMapForYUV(uint32_t &errorCode);
223 
224     const std::string NINE_PATCH = "ninepatch";
225     const std::string SKIA_DECODER = "SKIA_DECODER";
226     static MultimediaPlugin::PluginServer &pluginServer_;
227     static FormatAgentMap formatAgentMap_;
228     std::unique_ptr<SourceStream> sourceStreamPtr_;
229     SourceDecodingState decodeState_ = SourceDecodingState::UNRESOLVED;
230     SourceInfo sourceInfo_;
231     SourceOptions sourceOptions_;
232     NinePatchInfo ninePatchInfo_;
233     ImageStatusMap imageStatusMap_;
234     IncrementalRecordMap incDecodingMap_;
235     // The main decoder is responsible for ordinary decoding (non-Incremental decoding),
236     // as well as decoding SourceInfo and ImageInfo.
237     std::unique_ptr<ImagePlugin::AbsImageDecoder> mainDecoder_;
238     DecodeOptions opts_;
239     std::set<PeerListener *> listeners_;
240     DecodeEvent decodeEvent_ = DecodeEvent::EVENT_COMPLETE_DECODE;
241     std::map<int32_t, int32_t> decodeEventMap_;
242     std::set<DecodeListener *> decodeListeners_;
243     std::mutex listenerMutex_;
244     std::mutex decodingMutex_;
245     bool isIncrementalSource_ = false;
246     bool isIncrementalCompleted_ = false;
247     MemoryUsagePreference preference_ = MemoryUsagePreference::DEFAULT;
248 };
249 } // namespace Media
250 } // namespace OHOS
251 
252 #endif // INTERFACES_INNERKITS_INCLUDE_IMAGE_SOURCE_H_
253