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