• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 #define MLOG_TAG "Thumbnail"
16 
17 #include "thumbnail_generate_helper.h"
18 
19 #include "ithumbnail_helper.h"
20 #include "medialibrary_errno.h"
21 #include "media_log.h"
22 #include "thumbnail_const.h"
23 
24 using namespace std;
25 using namespace OHOS::DistributedKv;
26 using namespace OHOS::NativeRdb;
27 
28 namespace OHOS {
29 namespace Media {
CreateThumbnailBatch(ThumbRdbOpt & opts)30 int32_t ThumbnailGenerateHelper::CreateThumbnailBatch(ThumbRdbOpt &opts)
31 {
32     if (opts.store == nullptr) {
33         MEDIA_ERR_LOG("rdbStore is not init");
34         return E_ERR;
35     }
36 
37     vector<ThumbnailRdbData> infos;
38     int32_t err = GetNoThumbnailData(opts, infos);
39     if (err != E_OK) {
40         MEDIA_ERR_LOG("Failed to GetNoLcdData %{private}d", err);
41         return err;
42     }
43 
44     if (infos.empty()) {
45         MEDIA_INFO_LOG("No need generate thumbnail.");
46         return E_OK;
47     }
48     ThumbnailData data;
49     for (uint32_t i = 0; i < infos.size(); i++) {
50         opts.row = infos[i].id;
51         ThumbnailUtils::ThumbnailDataCopy(data, infos[i]);
52         IThumbnailHelper::AddAsyncTask(IThumbnailHelper::CreateThumbnail, opts, data, false);
53     }
54 
55     return E_OK;
56 }
57 
CreateLcdBatch(ThumbRdbOpt & opts)58 int32_t ThumbnailGenerateHelper::CreateLcdBatch(ThumbRdbOpt &opts)
59 {
60     if (opts.store == nullptr) {
61         MEDIA_ERR_LOG("rdbStore is not init");
62         return E_ERR;
63     }
64 
65     int lcdCount = 0;
66     int32_t err = GetLcdCount(opts, lcdCount);
67     if (err != E_OK) {
68         MEDIA_ERR_LOG("GetLcdCount err %{private}d , lcdCount %{private}d", err, lcdCount);
69         return err;
70     }
71 
72     if (lcdCount >= THUMBNAIL_LCD_GENERATE_THRESHOLD) {
73         MEDIA_INFO_LOG("Not need generate Lcd. lcdCount: %{lcdCount}d", lcdCount);
74         return E_OK;
75     }
76 
77     vector<ThumbnailRdbData> infos;
78     err = GetNoLcdData(opts, THUMBNAIL_LCD_GENERATE_THRESHOLD - lcdCount, infos);
79     if ((err != E_OK) || infos.empty()) {
80         MEDIA_ERR_LOG("Failed to GetNoLcdData %{private}d", err);
81         return err;
82     }
83     ThumbnailData data;
84     for (uint32_t i = 0; i < infos.size(); i++) {
85         opts.row = infos[i].id;
86         ThumbnailUtils::ThumbnailDataCopy(data, infos[i]);
87         IThumbnailHelper::AddAsyncTask(IThumbnailHelper::CreateLcd, opts, data, false);
88     }
89     return E_OK;
90 }
91 
GetLcdCount(ThumbRdbOpt & opts,int & outLcdCount)92 int32_t ThumbnailGenerateHelper::GetLcdCount(ThumbRdbOpt &opts, int &outLcdCount)
93 {
94     int32_t err = E_ERR;
95     if (!ThumbnailUtils::QueryLcdCount(opts, outLcdCount, err)) {
96         MEDIA_ERR_LOG("Failed to QueryLcdCount %{private}d", err);
97         return err;
98     }
99     return E_OK;
100 }
101 
GetNoLcdData(ThumbRdbOpt & opts,int lcdLimit,vector<ThumbnailRdbData> & outDatas)102 int32_t ThumbnailGenerateHelper::GetNoLcdData(ThumbRdbOpt &opts, int lcdLimit, vector<ThumbnailRdbData> &outDatas)
103 {
104     int32_t err = E_ERR;
105     if (!ThumbnailUtils::QueryNoLcdInfos(opts, lcdLimit, outDatas, err)) {
106         MEDIA_ERR_LOG("Failed to QueryNoLcdInfos %{private}d", err);
107         return err;
108     }
109     return E_OK;
110 }
111 
GetNoThumbnailData(ThumbRdbOpt & opts,vector<ThumbnailRdbData> & outDatas)112 int32_t ThumbnailGenerateHelper::GetNoThumbnailData(ThumbRdbOpt &opts, vector<ThumbnailRdbData> &outDatas)
113 {
114     int32_t err = E_ERR;
115     if (!ThumbnailUtils::QueryNoThumbnailInfos(opts, outDatas, err)) {
116         MEDIA_ERR_LOG("Failed to QueryNoThumbnailInfos %{private}d", err);
117         return err;
118     }
119     return E_OK;
120 }
121 } // namespace Media
122 } // namespace OHOS
123