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