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_DATA_OPERATIONS_H 17 #define OHOS_PHOTO_PICTURE_DATA_OPERATIONS_H 18 19 #include <iostream> 20 #include <sstream> 21 #include <cstdint> 22 #include <functional> 23 #include <memory> 24 #include <mutex> 25 #include <string> 26 #include <list> 27 #include <thread> 28 #include "picture.h" 29 #include "media_log.h" 30 #include "medialibrary_async_worker.h" 31 32 namespace OHOS { 33 namespace Media { 34 #define EXPORT __attribute__ ((visibility ("default"))) 35 enum PictureType { 36 LOW_QUALITY_PICTURE = 0, 37 HIGH_QUALITY_PICTURE 38 }; 39 40 class PicturePair : public RefBase { 41 public: 42 std::shared_ptr<Media::Picture> picture_; 43 std::string photoId_ = "default"; 44 time_t expireTime_; 45 bool isCleanImmediately_ = true; 46 bool isEdited_ = false; 47 bool isTakeEffect_ = false; PicturePair(std::shared_ptr<Media::Picture> picture,std::string photoId,time_t expireTime,bool isCleanImmediately,bool isEdited)48 explicit PicturePair(std::shared_ptr<Media::Picture> picture, std::string photoId, 49 time_t expireTime, bool isCleanImmediately, bool isEdited) 50 { 51 picture_ = std::move(picture); 52 photoId_ = std::move(photoId); 53 expireTime_ = expireTime; 54 isCleanImmediately_ = isCleanImmediately; 55 isEdited_ = isEdited; 56 MEDIA_INFO_LOG("Constructor picturePair: %{public}s.", this->ToString().c_str()); 57 } 58 PicturePair(const PicturePair & other)59 PicturePair(const PicturePair& other) 60 { 61 picture_ = std::move(other.picture_); 62 photoId_ = std::move(other.photoId_); 63 expireTime_ = other.expireTime_; 64 isCleanImmediately_ = other.isCleanImmediately_; 65 isEdited_ = other.isEdited_; 66 MEDIA_INFO_LOG("Copy constructor picturePair: %{public}s.", this->ToString().c_str()); 67 } 68 69 PicturePair& operator=(const PicturePair& other) 70 { 71 if (this != &other) { 72 picture_ = std::move(other.picture_); 73 photoId_ = std::move(other.photoId_); 74 expireTime_ = other.expireTime_; 75 isCleanImmediately_ = other.isCleanImmediately_; 76 isEdited_ = other.isEdited_; 77 } 78 MEDIA_INFO_LOG("Assignment constructor picturePair: %{public}s.", this->ToString().c_str()); 79 return *this; 80 } 81 ~PicturePair()82 ~PicturePair() 83 { 84 MEDIA_INFO_LOG("~PicturePair: %{public}s.", this->ToString().c_str()); 85 if (picture_) { 86 picture_ = nullptr; 87 } 88 } 89 ToString()90 std::string ToString() 91 { 92 std::stringstream ss; 93 ss << "{" 94 << "\"photoId\": " << this->photoId_ 95 << "; \"picture use\": " << static_cast<int32_t>(this->picture_.use_count()) 96 << "; \"picture point to addr\": " << std::to_string(reinterpret_cast<long long>(this->picture_.get())) 97 << "}"; 98 return ss.str(); 99 } 100 SetTakeEffect(bool isTakeEffect)101 void SetTakeEffect(bool isTakeEffect) 102 { 103 isTakeEffect_ = isTakeEffect; 104 } 105 }; 106 107 class SavePictureData : public AsyncTaskData { 108 public: SavePictureData(sptr<PicturePair> picturePair)109 SavePictureData(sptr<PicturePair> picturePair) : picturePair_(picturePair){}; 110 ~SavePictureData() override = default; 111 112 sptr<PicturePair> picturePair_; 113 }; 114 class PictureDataOperations : public RefBase { 115 public: 116 EXPORT explicit PictureDataOperations(); 117 ~PictureDataOperations(); 118 EXPORT void CleanDateForPeriodical(); 119 void CleanPictureMapData(std::map<std::string, sptr<PicturePair>>& pictureMap, PictureType pictureType); 120 EXPORT void InsertPictureData(const std::string& imageId, sptr<PicturePair>& picturePair, PictureType pictureType); 121 std::shared_ptr<Media::Picture> GetDataWithImageId(const std::string& imageId, 122 bool &isHighQualityPicture, bool &isTakeEffect, bool isCleanImmediately = true); 123 std::shared_ptr<Media::Picture> GetDataWithImageIdAndPictureType(const std::string& imageId, 124 PictureType pictureType, bool &isTakeEffect, bool isCleanImmediately = true); 125 void DeleteDataWithImageId(const std::string& imageId, PictureType pictureType); 126 bool IsExsitDataForPictureType(PictureType pictureType); 127 bool IsExsitDataForPictureType(const std::string& imageId, PictureType pictureType); 128 void SaveLowQualityPicture(const std::string& imageId = "default"); 129 void FinishAccessingPicture(const std::string& imageId, PictureType pictureType); 130 void SavePictureWithImageId(const std::string& imageId); 131 static void SavePictureExecutor(AsyncTaskData *data); 132 int32_t AddSavePictureTask(sptr<PicturePair>& picturePair); 133 int32_t GetPendingTaskSize(); 134 private: 135 bool SavePicture(const std::string& imageId, std::map<std::string, sptr<PicturePair>>& pictureMap, 136 bool isLowQualityPicture); 137 void CleanHighQualityPictureDataInternal(const std::string& imageId, sptr<PicturePair>& picturePair, 138 std::list<std::string>& pictureImageIdList); 139 140 const int MAX_PICTURE_CAPBILITY = 3; 141 int max_capibilty = MAX_PICTURE_CAPBILITY; 142 std::mutex pictureMapMutex_; 143 std::map<std::string, sptr<PicturePair>> lowQualityPictureMap_; 144 std::map<std::string, sptr<PicturePair>> highQualityPictureMap_; 145 std::list<std::string> highQualityPictureImageId; 146 std::list<std::string> pendingSavedPicturelist_; 147 static int32_t taskSize; 148 }; // class PictureDataOperation 149 } // namespace Media 150 } // namespace OHOS 151 #endif // OHOS_PHOTO_PICTURE_DATA_OPERATION_H