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 OHOS_PHOTO_PICTURE_MANAGER_THREAD_H
17 #define OHOS_PHOTO_PICTURE_MANAGER_THREAD_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 #include <list>
25 #include <thread>
26 #include "picture.h"
27 #include "picture_data_operations.h"
28 
29 namespace OHOS {
30 namespace Media {
31 #define EXPORT __attribute__ ((visibility ("default")))
32 
33 enum State {
34     STOPPED = 0, // 停止状态,包括从未启动过和启动后被停止
35     RUNNING, // 运行状态
36     PAUSED, // 暂停状态
37 };
38 
39 class PictureManagerThread : public RefBase {
40 public:
41     EXPORT static PictureManagerThread* GetInstance();
42     PictureManagerThread();
43     ~PictureManagerThread();
44 
45     State State() const;
46     EXPORT void Start();
47     void Stop();
48     void Pause();
49     void Resume();
50     EXPORT void InsertPictureData(const std::string& imageId, sptr<PicturePair>& picturePair, PictureType pictureType);
51     EXPORT std::shared_ptr<Media::Picture> GetDataWithImageId(const std::string& imageId,
52         bool &isHighQualityPicture, bool isCleanImmediately = false);
53     EXPORT bool IsExsitDataForPictureType(PictureType pictureType);
54     EXPORT bool IsExsitPictureByImageId(const std::string& imageId);
55     EXPORT void SaveLowQualityPicture(const std::string& imageId = "default");
56     EXPORT void FinishAccessingPicture(const std::string& imageId);
57     EXPORT void SavePictureWithImageId(const std::string& imageId);
58     EXPORT int32_t AddSavePictureTask(sptr<PicturePair>& picturePair);
59     EXPORT int32_t GetPendingTaskSize();
60     EXPORT void DeleteDataWithImageId(const std::string& imageId, PictureType pictureType);
61 private:
62     void Run();
63     std::unique_ptr<std::thread> thread_ = nullptr;
64     std::mutex threadMutex_;
65     std::mutex runningMutex_;
66     std::condition_variable condition_;
67     std::atomic_bool pauseFlag_; // 暂停标识
68     std::atomic_bool stopFlag_; // 停止标识
69     enum State state_;
70     static std::unique_ptr<PictureManagerThread> instance_;
71     static std::mutex mutex_;
72     sptr<PictureDataOperations> pictureDataOperations_;
73     int32_t lastPendingTaskSize_ = 0;
74 }; // class PictureManagerThread
75 } // namespace Media
76 }  // namespace OHOS
77 #endif // OHOS_PHOTO_PICTURE_MANAGER_THREAD_H