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