• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 FRAMEWORKS_SERVICES_THUMBNAIL_SOURCE_LOADING_H
17 #define FRAMEWORKS_SERVICES_THUMBNAIL_SOURCE_LOADING_H
18 
19 #include <unordered_map>
20 
21 #include "image_packer.h"
22 #include "media_log.h"
23 #include "medialibrary_errno.h"
24 #include "thumbnail_const.h"
25 #include "thumbnail_data.h"
26 
27 namespace OHOS {
28 namespace Media {
29 #define EXPORT __attribute__ ((visibility ("default")))
30 
31 EXPORT std::string GetLocalThumbnailPath(const std::string &path, const std::string &key);
32 EXPORT std::string GetLocalKeyFrameThumbnailPath(const std::string &path, const std::string &key,
33     const std::string &timeStamp);
34 EXPORT Size ConvertDecodeSize(ThumbnailData &data, const Size &sourceSize, Size &desiredSize);
35 EXPORT bool GenDecodeOpts(const Size &sourceSize, const Size &targetSize, DecodeOptions &decodeOpts);
36 EXPORT std::unique_ptr<ImageSource> LoadImageSource(const std::string &path, uint32_t &err);
37 EXPORT bool NeedAutoResize(const Size &size);
38 EXPORT int32_t ParseDesiredMinSide(const ThumbnailType &type);
39 
40 const std::unordered_map<SourceState, std::string> STATE_NAME_MAP = {
41     { SourceState::BEGIN, "BEGIN" },
42     { SourceState::LOCAL_THUMB, "LOCAL_THUMB" },
43     { SourceState::LOCAL_LCD, "LOCAL_LCD" },
44     { SourceState::LOCAL_ORIGIN, "LOCAL_ORIGIN" },
45     { SourceState::CLOUD_THUMB, "CLOUD_THUMB" },
46     { SourceState::CLOUD_LCD, "CLOUD_LCD" },
47     { SourceState::CLOUD_ORIGIN, "CLOUD_ORIGIN" },
48     { SourceState::ERROR, "ERROR" },
49     { SourceState::FINISH, "FINISH" },
50 };
51 
52 class BeginSource {
53 public:
GetSourcePath(ThumbnailData & data,int32_t & error)54     EXPORT static std::string GetSourcePath(ThumbnailData &data, int32_t &error)
55         { return ""; }
IsSizeLargeEnough(ThumbnailData & data,int32_t & minSize)56     EXPORT static bool IsSizeLargeEnough(ThumbnailData &data, int32_t &minSize) { return false; }
57 };
58 
59 class LocalThumbSource {
60 public:
61     EXPORT static std::string GetSourcePath(ThumbnailData &data, int32_t &error);
62     EXPORT static bool IsSizeLargeEnough(ThumbnailData &data, int32_t &minSize);
63 };
64 
65 class LocalLcdSource {
66 public:
67     EXPORT static std::string GetSourcePath(ThumbnailData &data, int32_t &error);
68     EXPORT static bool IsSizeLargeEnough(ThumbnailData &data, int32_t &minSize);
69 };
70 
71 class LocalOriginSource {
72 public:
73     EXPORT static std::string GetSourcePath(ThumbnailData &data, int32_t &error);
74     EXPORT static bool IsSizeLargeEnough(ThumbnailData &data, int32_t &minSize);
75 };
76 
77 class CloudThumbSource {
78 public:
79     EXPORT static std::string GetSourcePath(ThumbnailData &data, int32_t &error);
80     EXPORT static bool IsSizeLargeEnough(ThumbnailData &data, int32_t &minSize);
81 };
82 
83 class CloudLcdSource {
84 public:
85     EXPORT static std::string GetSourcePath(ThumbnailData &data, int32_t &error);
86     EXPORT static bool IsSizeLargeEnough(ThumbnailData &data, int32_t &minSize);
87 };
88 
89 class CloudOriginSource {
90 public:
91     EXPORT static std::string GetSourcePath(ThumbnailData &data, int32_t &error);
92     EXPORT static bool IsSizeLargeEnough(ThumbnailData &data, int32_t &minSize);
93 };
94 
95 class ErrorSource {
96 public:
GetSourcePath(ThumbnailData & data,int32_t & error)97     EXPORT static std::string GetSourcePath(ThumbnailData &data, int32_t &error)
98         { return ""; }
IsSizeLargeEnough(ThumbnailData & data,int32_t & minSize)99     EXPORT static bool IsSizeLargeEnough(ThumbnailData &data, int32_t &minSize) { return false; }
100 };
101 
102 class FinishSource {
103 public:
GetSourcePath(ThumbnailData & data,int32_t & error)104     EXPORT static std::string GetSourcePath(ThumbnailData &data, int32_t &error)
105         { return ""; }
IsSizeLargeEnough(ThumbnailData & data,int32_t & minSize)106     EXPORT static bool IsSizeLargeEnough(ThumbnailData &data, int32_t &minSize) { return false; }
107 };
108 
109 struct StateFunc {
110     std::string (*GetSourcePath)(ThumbnailData &data, int32_t &error);
111     bool (*IsSizeLargeEnough)(ThumbnailData &data, int32_t &minSize);
112 };
113 
114 const std::unordered_map<SourceState, StateFunc> STATE_FUNC_MAP = {
115     { SourceState::BEGIN, { BeginSource::GetSourcePath, BeginSource::IsSizeLargeEnough } },
116     { SourceState::LOCAL_THUMB, { LocalThumbSource::GetSourcePath, LocalThumbSource::IsSizeLargeEnough } },
117     { SourceState::LOCAL_LCD, { LocalLcdSource::GetSourcePath, LocalLcdSource::IsSizeLargeEnough } },
118     { SourceState::LOCAL_ORIGIN, { LocalOriginSource::GetSourcePath, LocalOriginSource::IsSizeLargeEnough } },
119     { SourceState::CLOUD_THUMB, { CloudThumbSource::GetSourcePath, CloudThumbSource::IsSizeLargeEnough } },
120     { SourceState::CLOUD_LCD, { CloudLcdSource::GetSourcePath, CloudLcdSource::IsSizeLargeEnough } },
121     { SourceState::CLOUD_ORIGIN, { CloudOriginSource::GetSourcePath, CloudOriginSource::IsSizeLargeEnough } },
122     { SourceState::ERROR, { ErrorSource::GetSourcePath, ErrorSource::IsSizeLargeEnough } },
123     { SourceState::FINISH, { FinishSource::GetSourcePath, FinishSource::IsSizeLargeEnough } },
124 };
125 
126 class SourceLoader {
127 public:
128     /*
129      * Define source loading states sequence for creating thumbnails from local photo.
130      */
131     static const std::unordered_map<SourceState, SourceState> LOCAL_SOURCE_LOADING_STATES;
132 
133     /*
134      * Define source loading states sequence for creating year&month thumbnails from local thumbnails.
135      */
136     static const std::unordered_map<SourceState, SourceState> LOCAL_THUMB_SOURCE_LOADING_STATES;
137 
138     /*
139      * Define source loading states sequence for creating thumbnails from cloud photo.
140      */
141     static const std::unordered_map<SourceState, SourceState> CLOUD_SOURCE_LOADING_STATES;
142 
143     /*
144      * Define source loading states sequence for creating thumbnails from cloud origin photo.
145      */
146     static const std::unordered_map<SourceState, SourceState> CLOUD_ORIGIN_SOURCE_LOADING_STATES;
147 
148     /*
149      * Define source loading states sequence for creating thumbnails on demand.
150      */
151     static const std::unordered_map<SourceState, SourceState> ALL_SOURCE_LOADING_STATES;
152 
153     /*
154      * Define source loading states sequence for creating cloud video thumbnails on demand.
155      */
156     static const std::unordered_map<SourceState, SourceState> ALL_SOURCE_LOADING_CLOUD_VIDEO_STATES;
157 
158     /*
159      * Define source loading states sequence for creating thumbnails resolved cloud LCD.
160      */
161     static const std::unordered_map<SourceState, SourceState> CLOUD_LCD_SOURCE_LOADING_STATES;
162 
163     /*
164      * Define source loading states sequence for creating thumbnails resolved local LCD.
165      */
166     static const std::unordered_map<SourceState, SourceState> LOCAL_LCD_SOURCE_LOADING_STATES;
167 
168     /*
169      * Define source loading states sequence for upgrading thumbnails.
170      */
171     static const std::unordered_map<SourceState, SourceState> UPGRADE_SOURCE_LOADING_STATES;
172 
173     /*
174      * Define source loading states sequence for upgrading video thumbnails.
175      */
176     static const std::unordered_map<SourceState, SourceState> UPGRADE_VIDEO_SOURCE_LOADING_STATES;
177 
SourceLoader(Size & desiredSize,ThumbnailData & data)178     SourceLoader(Size &desiredSize, ThumbnailData &data) : data_(data), desiredSize_(desiredSize)
179     {
180         GetSourcePath = nullptr;
181         IsSizeLargeEnough = nullptr;
182     };
183     ~SourceLoader() = default;
184     EXPORT bool RunLoading();
185 
186 private:
187     EXPORT void SetCurrentStateFunction();
188     EXPORT bool IsSizeAcceptable(std::unique_ptr<ImageSource>& imageSource, ImageInfo& imageInfo);
189     EXPORT bool CreateSourcePixelMap();
190     EXPORT bool CreateImagePixelMap(const std::string &sourcePath);
191     EXPORT bool CreateVideoFramePixelMap();
192     EXPORT bool GeneratePictureSource(std::unique_ptr<ImageSource> &imageSource, const Size &targetSize);
193     EXPORT bool GeneratePixelMapSource(std::unique_ptr<ImageSource> &imageSource, const Size &sourceSize,
194         const Size &targetSize);
195     EXPORT bool CreateSourceFromOriginalPhotoPicture();
196     EXPORT bool CreateSourceWithWholeOriginalPicture();
197     EXPORT bool CreateSourceWithOriginalPictureMainPixel();
198     EXPORT bool IsLoadingOriginalSource();
199 
200     bool IsFinal();
201 
202     int32_t error_ { E_OK };
203     std::string (*GetSourcePath)(ThumbnailData &data, int32_t &error);
204     bool (*IsSizeLargeEnough)(ThumbnailData &data, int32_t &minSize);
205 
206     ThumbnailData &data_;
207     Size &desiredSize_;
208     SourceState state_ { SourceState::BEGIN };
209 };
210 
211 } // namespace Media
212 } // namespace OHOS
213 
214 #endif // FRAMEWORKS_SERVICES_THUMBNAIL_SOURCE_LOADING_H