• 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 struct PixelMapAddrInfos {
125     uint8_t *addr;
126     uint8_t *context;
127     uint32_t size;
128     AllocatorType type;
129     CustomFreePixelMap func;
130 };
131 
132 class SourceStream;
133 
134 class ImageSource {
135 public:
136     ~ImageSource();
137     NATIVEEXPORT static uint32_t GetSupportedFormats(std::set<std::string> &formats);
138     NATIVEEXPORT static std::unique_ptr<ImageSource> CreateImageSource(std::unique_ptr<std::istream> is,
139                                                                        const SourceOptions &opts, uint32_t &errorCode);
140     NATIVEEXPORT static std::unique_ptr<ImageSource> CreateImageSource(const uint8_t *data, uint32_t size,
141                                                                        const SourceOptions &opts, uint32_t &errorCode);
142     NATIVEEXPORT static std::unique_ptr<ImageSource> CreateImageSource(const std::string &pathName,
143                                                                        const SourceOptions &opts, uint32_t &errorCode);
144     NATIVEEXPORT static std::unique_ptr<ImageSource> CreateImageSource(const int fd, const SourceOptions &opts,
145                                                        uint32_t &errorCode);
146     NATIVEEXPORT static std::unique_ptr<ImageSource> CreateIncrementalImageSource(const IncrementalSourceOptions &opts,
147                                                                                   uint32_t &errorCode);
148 
CreatePixelMap(const DecodeOptions & opts,uint32_t & errorCode)149     NATIVEEXPORT std::unique_ptr<PixelMap> CreatePixelMap(const DecodeOptions &opts, uint32_t &errorCode)
150     {
151         return CreatePixelMapEx(0, opts, errorCode);
152     }
153     NATIVEEXPORT std::unique_ptr<PixelMap> CreatePixelMapEx(uint32_t index, const DecodeOptions &opts,
154                                                             uint32_t &errorCode);
155     NATIVEEXPORT std::unique_ptr<PixelMap> CreatePixelMap(uint32_t index, const DecodeOptions &opts,
156                                                           uint32_t &errorCode);
157     NATIVEEXPORT std::unique_ptr<IncrementalPixelMap> CreateIncrementalPixelMap(uint32_t index,
158                                                                                 const DecodeOptions &opts,
159                                                                                 uint32_t &errorCode);
160     // for incremental source.
161     NATIVEEXPORT uint32_t UpdateData(const uint8_t *data, uint32_t size, bool isCompleted);
162     // for obtaining basic image information without decoding image data.
GetImageInfo(ImageInfo & imageInfo)163     NATIVEEXPORT uint32_t GetImageInfo(ImageInfo &imageInfo)
164     {
165         return GetImageInfo(0, imageInfo);
166     }
167     NATIVEEXPORT uint32_t GetImageInfo(uint32_t index, ImageInfo &imageInfo);
168     NATIVEEXPORT const SourceInfo &GetSourceInfo(uint32_t &errorCode);
169     NATIVEEXPORT void RegisterListener(PeerListener *listener);
170     NATIVEEXPORT void UnRegisterListener(PeerListener *listener);
171     NATIVEEXPORT DecodeEvent GetDecodeEvent();
172     NATIVEEXPORT void AddDecodeListener(DecodeListener *listener);
173     NATIVEEXPORT void RemoveDecodeListener(DecodeListener *listener);
174     NATIVEEXPORT bool IsIncrementalSource();
175     NATIVEEXPORT uint32_t GetImagePropertyInt(uint32_t index, const std::string &key, int32_t &value);
176     NATIVEEXPORT uint32_t GetImagePropertyString(uint32_t index, const std::string &key, std::string &value);
177     NATIVEEXPORT uint32_t ModifyImageProperty(uint32_t index, const std::string &key, const std::string &value,
178         const std::string &path);
179     NATIVEEXPORT uint32_t ModifyImageProperty(uint32_t index, const std::string &key, const std::string &value,
180         const int fd);
181     NATIVEEXPORT uint32_t ModifyImageProperty(uint32_t index, const std::string &key, const std::string &value,
182         uint8_t *data, uint32_t size);
183     NATIVEEXPORT const NinePatchInfo &GetNinePatchInfo() const;
184     NATIVEEXPORT void SetMemoryUsagePreference(const MemoryUsagePreference preference);
185     NATIVEEXPORT MemoryUsagePreference GetMemoryUsagePreference();
186     NATIVEEXPORT uint32_t GetFilterArea(const int &privacyType, std::vector<std::pair<uint32_t, uint32_t>> &ranges);
187     NATIVEEXPORT std::unique_ptr<std::vector<std::unique_ptr<PixelMap>>> CreatePixelMapList(const DecodeOptions &opts,
188         uint32_t &errorCode);
189     NATIVEEXPORT std::unique_ptr<std::vector<int32_t>> GetDelayTime(uint32_t &errorCode);
190     NATIVEEXPORT uint32_t GetFrameCount(uint32_t &errorCode);
191 #ifdef IMAGE_PURGEABLE_PIXELMAP
192     NATIVEEXPORT size_t GetSourceSize() const;
193 #endif
194 
195 private:
196     DISALLOW_COPY_AND_MOVE(ImageSource);
197     using FormatAgentMap = std::map<std::string, ImagePlugin::AbsImageFormatAgent *>;
198     using ImageStatusMap = std::map<uint32_t, ImageDecodingStatus>;
199     using IncrementalRecordMap = std::map<PixelMap *, IncrementalDecodingContext>;
200     ImageSource(std::unique_ptr<SourceStream> &&stream, const SourceOptions &opts);
201     uint32_t CheckEncodedFormat(ImagePlugin::AbsImageFormatAgent &agent);
202     static FormatAgentMap InitClass();
203     uint32_t GetEncodedFormat(const std::string &formatHint, std::string &format);
204     uint32_t DecodeImageInfo(uint32_t index, ImageStatusMap::iterator &iter);
205     uint32_t DecodeSourceInfo(bool isAcquiredImageNum);
206     uint32_t InitMainDecoder();
207     ImagePlugin::AbsImageDecoder *CreateDecoder(uint32_t &errorCode);
208     void CopyOptionsToPlugin(const DecodeOptions &opts, ImagePlugin::PixelDecodeOptions &plOpts);
209     void CopyOptionsToProcOpts(const DecodeOptions &opts, DecodeOptions &procOpts, PixelMap &pixelMap);
210     uint32_t CheckFormatHint(const std::string &formatHint, FormatAgentMap::iterator &formatIter);
211     uint32_t GetSourceInfo();
212     uint32_t OnSourceRecognized(bool isAcquiredImageNum);
213     uint32_t OnSourceUnresolved();
214     uint32_t SetDecodeOptions(std::unique_ptr<ImagePlugin::AbsImageDecoder> &decoder, uint32_t index,
215                               const DecodeOptions &opts, ImagePlugin::PlImageInfo &plInfo);
216     uint32_t UpdatePixelMapInfo(const DecodeOptions &opts, ImagePlugin::PlImageInfo &plInfo, PixelMap &pixelMap);
217     uint32_t UpdatePixelMapInfo(const DecodeOptions &opts, ImagePlugin::PlImageInfo &plInfo,
218                                 PixelMap &pixelMap, int32_t fitDensity, bool isReUsed = false);
219     // declare friend class, only IncrementalPixelMap can call PromoteDecoding function.
220     friend class IncrementalPixelMap;
221     uint32_t PromoteDecoding(uint32_t index, const DecodeOptions &opts, PixelMap &pixelMap, ImageDecodingState &state,
222                              uint8_t &decodeProgress);
223     void DetachIncrementalDecoding(PixelMap &pixelMap);
224     ImageStatusMap::iterator GetValidImageStatus(uint32_t index, uint32_t &errorCode);
225     uint32_t AddIncrementalContext(PixelMap &pixelMap, IncrementalRecordMap::iterator &iterator);
226     uint32_t DoIncrementalDecoding(uint32_t index, const DecodeOptions &opts, PixelMap &pixelMap,
227                                    IncrementalDecodingContext &recordContext);
228     void SetIncrementalSource(const bool isIncrementalSource);
229     bool IsStreamCompleted();
230     FinalOutputStep GetFinalOutputStep(const DecodeOptions &opts, PixelMap &pixelMap, bool hasNinePatch);
231     bool HasDensityChange(const DecodeOptions &opts, ImageInfo &srcImageInfo, bool hasNinePatch);
232     bool ImageSizeChange(int32_t width, int32_t height, int32_t desiredWidth, int32_t desiredHeight);
233     bool ImageConverChange(const Rect &cropRect, ImageInfo &dstImageInfo, ImageInfo &srcImageInfo);
234     void Reset();
235     static std::unique_ptr<SourceStream> DecodeBase64(const uint8_t *data, uint32_t size);
236     static std::unique_ptr<SourceStream> DecodeBase64(const std::string &data);
237     bool IsSpecialYUV();
238     bool ConvertYUV420ToRGBA(uint8_t *data, uint32_t size, bool isSupportOdd, bool isAddUV, uint32_t &errorCode);
239     std::unique_ptr<PixelMap> CreatePixelMapForYUV(uint32_t &errorCode);
240     uint32_t GetFormatExtended(std::string &format);
241     static std::unique_ptr<ImageSource> DoImageSourceCreate(
242         std::function<std::unique_ptr<SourceStream>(void)> stream,
243         const SourceOptions &opts, uint32_t &errorCode, const std::string traceName = "");
244     std::unique_ptr<PixelMap> CreatePixelMapExtended(uint32_t index, const DecodeOptions &opts,
245                                                      uint32_t &errorCode);
246     std::unique_ptr<PixelMap> CreatePixelMapByInfos(ImagePlugin::PlImageInfo &plInfo,
247                                                     PixelMapAddrInfos &addrInfos, uint32_t &errorCode);
248     const std::string NINE_PATCH = "ninepatch";
249     const std::string SKIA_DECODER = "SKIA_DECODER";
250     static MultimediaPlugin::PluginServer &pluginServer_;
251     static FormatAgentMap formatAgentMap_;
252     std::unique_ptr<SourceStream> sourceStreamPtr_;
253     SourceDecodingState decodeState_ = SourceDecodingState::UNRESOLVED;
254     SourceInfo sourceInfo_;
255     SourceOptions sourceOptions_;
256     NinePatchInfo ninePatchInfo_;
257     ImageStatusMap imageStatusMap_;
258     IncrementalRecordMap incDecodingMap_;
259     // The main decoder is responsible for ordinary decoding (non-Incremental decoding),
260     // as well as decoding SourceInfo and ImageInfo.
261     std::unique_ptr<ImagePlugin::AbsImageDecoder> mainDecoder_;
262     DecodeOptions opts_;
263     std::set<PeerListener *> listeners_;
264     DecodeEvent decodeEvent_ = DecodeEvent::EVENT_COMPLETE_DECODE;
265     std::map<int32_t, int32_t> decodeEventMap_;
266     std::set<DecodeListener *> decodeListeners_;
267     std::mutex listenerMutex_;
268     std::mutex decodingMutex_;
269     bool isIncrementalSource_ = false;
270     bool isIncrementalCompleted_ = false;
271     bool hasDesiredSizeOptions = false;
272     MemoryUsagePreference preference_ = MemoryUsagePreference::DEFAULT;
273 };
274 } // namespace Media
275 } // namespace OHOS
276 
277 #endif // INTERFACES_INNERKITS_INCLUDE_IMAGE_SOURCE_H_
278