1 /*
2 * Copyright (C) 2021 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 #include "incremental_pixel_map.h"
17 #include "hilog/log.h"
18 #include "image_source.h"
19 #include "log_tags.h"
20 #include "media_errors.h"
21
22 namespace OHOS {
23 namespace Media {
24 using namespace OHOS::HiviewDFX;
25
26 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_TAG_DOMAIN_ID_IMAGE, "IncrementalPixelMap" };
27
ConvertImageStateToIncrementalState(ImageDecodingState imageState)28 static IncrementalDecodingState ConvertImageStateToIncrementalState(ImageDecodingState imageState)
29 {
30 switch (imageState) {
31 case ImageDecodingState::UNRESOLVED: {
32 return IncrementalDecodingState::UNRESOLVED;
33 }
34 case ImageDecodingState::BASE_INFO_ERROR: {
35 return IncrementalDecodingState::BASE_INFO_ERROR;
36 }
37 case ImageDecodingState::BASE_INFO_PARSED: {
38 return IncrementalDecodingState::BASE_INFO_PARSED;
39 }
40 case ImageDecodingState::IMAGE_DECODING: {
41 return IncrementalDecodingState::IMAGE_DECODING;
42 }
43 case ImageDecodingState::IMAGE_ERROR: {
44 return IncrementalDecodingState::IMAGE_ERROR;
45 }
46 case ImageDecodingState::PARTIAL_IMAGE: {
47 return IncrementalDecodingState::PARTIAL_IMAGE;
48 }
49 case ImageDecodingState::IMAGE_DECODED: {
50 return IncrementalDecodingState::IMAGE_DECODED;
51 }
52 default: {
53 HiLog::Error(LABEL, "unexpected imageState %{public}d.", imageState);
54 return IncrementalDecodingState::UNRESOLVED;
55 }
56 }
57 }
58
~IncrementalPixelMap()59 IncrementalPixelMap::~IncrementalPixelMap()
60 {
61 if (imageSource_ == nullptr) {
62 return;
63 }
64 DetachSource();
65 }
66
IncrementalPixelMap(uint32_t index,const DecodeOptions opts,ImageSource * imageSource)67 IncrementalPixelMap::IncrementalPixelMap(uint32_t index, const DecodeOptions opts, ImageSource *imageSource)
68 : index_(index), opts_(opts), imageSource_(imageSource)
69 {
70 if (imageSource_ != nullptr) {
71 imageSource_->RegisterListener(static_cast<PeerListener *>(this));
72 }
73 }
74
PromoteDecoding(uint8_t & decodeProgress)75 uint32_t IncrementalPixelMap::PromoteDecoding(uint8_t &decodeProgress)
76 {
77 if (imageSource_ == nullptr) {
78 if (decodingStatus_.state == IncrementalDecodingState::BASE_INFO_ERROR ||
79 decodingStatus_.state == IncrementalDecodingState::IMAGE_ERROR) {
80 HiLog::Error(LABEL, "promote decode failed for state %{public}d, errorDetail %{public}u.",
81 decodingStatus_.state, decodingStatus_.errorDetail);
82 return decodingStatus_.errorDetail;
83 }
84 HiLog::Error(LABEL, "promote decode failed or terminated, image source is null.");
85 return ERR_IMAGE_SOURCE_DATA;
86 }
87 ImageDecodingState imageState = ImageDecodingState::UNRESOLVED;
88 uint32_t ret =
89 imageSource_->PromoteDecoding(index_, opts_, *(static_cast<PixelMap *>(this)), imageState, decodeProgress);
90 decodingStatus_.state = ConvertImageStateToIncrementalState(imageState);
91 if (decodeProgress > decodingStatus_.decodingProgress) {
92 decodingStatus_.decodingProgress = decodeProgress;
93 }
94 if (ret != SUCCESS && ret != ERR_IMAGE_SOURCE_DATA_INCOMPLETE) {
95 DetachSource();
96 decodingStatus_.errorDetail = ret;
97 HiLog::Error(LABEL, "promote decode failed, ret=%{public}u.", ret);
98 }
99 if (ret == SUCCESS) {
100 DetachSource();
101 }
102 return ret;
103 }
104
DetachFromDecoding()105 void IncrementalPixelMap::DetachFromDecoding()
106 {
107 if (imageSource_ == nullptr) {
108 return;
109 }
110 DetachSource();
111 }
112
GetDecodingStatus()113 const IncrementalDecodingStatus &IncrementalPixelMap::GetDecodingStatus()
114 {
115 return decodingStatus_;
116 }
117
OnPeerDestory()118 void IncrementalPixelMap::OnPeerDestory()
119 {
120 imageSource_ = nullptr;
121 }
122
DetachSource()123 void IncrementalPixelMap::DetachSource()
124 {
125 imageSource_->DetachIncrementalDecoding(*(static_cast<PixelMap *>(this)));
126 imageSource_->UnRegisterListener(this);
127 imageSource_ = nullptr;
128 }
129 } // namespace Media
130 } // namespace OHOS
131