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<ThumbnailData> 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 for (uint32_t i = 0; i < infos.size(); i++) {
49 opts.row = infos[i].id;
50 IThumbnailHelper::AddAsyncTask(IThumbnailHelper::CreateThumbnail, opts, infos[i], false);
51 }
52
53 return E_OK;
54 }
55
CreateLcdBatch(ThumbRdbOpt & opts)56 int32_t ThumbnailGenerateHelper::CreateLcdBatch(ThumbRdbOpt &opts)
57 {
58 if (opts.store == nullptr) {
59 return E_ERR;
60 }
61
62 int lcdCount = 0;
63 int32_t err = GetLcdCount(opts, lcdCount);
64 if (err != E_OK) {
65 MEDIA_ERR_LOG("GetLcdCount err %{private}d , lcdCount %{private}d", err, lcdCount);
66 return err;
67 }
68
69 if (lcdCount >= THUMBNAIL_LCD_GENERATE_THRESHOLD) {
70 MEDIA_INFO_LOG("Not need generate Lcd. lcdCount: %{lcdCount}d", lcdCount);
71 return E_OK;
72 }
73
74 vector<ThumbnailData> infos;
75 err = GetNoLcdData(opts, THUMBNAIL_LCD_GENERATE_THRESHOLD - lcdCount, infos);
76 if ((err != E_OK) || infos.empty()) {
77 MEDIA_ERR_LOG("Failed to GetNoLcdData %{private}d", err);
78 return err;
79 }
80 for (uint32_t i = 0; i < infos.size(); i++) {
81 opts.row = infos[i].id;
82 IThumbnailHelper::AddAsyncTask(IThumbnailHelper::CreateLcd, opts, infos[i], false);
83 }
84 return E_OK;
85 }
86
GetLcdCount(ThumbRdbOpt & opts,int & outLcdCount)87 int32_t ThumbnailGenerateHelper::GetLcdCount(ThumbRdbOpt &opts, int &outLcdCount)
88 {
89 int32_t err = E_ERR;
90 if (!ThumbnailUtils::QueryLcdCount(opts, outLcdCount, err)) {
91 MEDIA_ERR_LOG("Failed to QueryLcdCount %{private}d", err);
92 return err;
93 }
94 return E_OK;
95 }
96
GetNoLcdData(ThumbRdbOpt & opts,int lcdLimit,vector<ThumbnailData> & outDatas)97 int32_t ThumbnailGenerateHelper::GetNoLcdData(ThumbRdbOpt &opts, int lcdLimit, vector<ThumbnailData> &outDatas)
98 {
99 int32_t err = E_ERR;
100 if (!ThumbnailUtils::QueryNoLcdInfos(opts, lcdLimit, outDatas, err)) {
101 MEDIA_ERR_LOG("Failed to QueryNoLcdInfos %{private}d", err);
102 return err;
103 }
104 return E_OK;
105 }
106
GetNoThumbnailData(ThumbRdbOpt & opts,vector<ThumbnailData> & outDatas)107 int32_t ThumbnailGenerateHelper::GetNoThumbnailData(ThumbRdbOpt &opts, vector<ThumbnailData> &outDatas)
108 {
109 int32_t err = E_ERR;
110 if (!ThumbnailUtils::QueryNoThumbnailInfos(opts, outDatas, err)) {
111 MEDIA_ERR_LOG("Failed to QueryNoThumbnailInfos %{private}d", err);
112 return err;
113 }
114 return E_OK;
115 }
116 } // namespace Media
117 } // namespace OHOS
118