1 /*
2 * Copyright (C) 2025 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 "image_log.h"
17 #include "image_taihe.h"
18 #include "image_taihe_utils.h"
19 #include "media_errors.h"
20
21 using namespace ANI::Image;
22
23 namespace {
24 constexpr int NUM0 = 0;
25 const std::string MY_NAME = "ImageTaihe";
26 constexpr int32_t TEST_WIDTH = 8192;
27 constexpr int32_t TEST_HEIGHT = 8;
28 constexpr int32_t TEST_FORMAT = 12;
29 }
30
31 namespace ANI::Image {
32 OHOS::Media::ImageHolderManager<OHOS::Media::NativeImage> ImageImpl::sNativeImageHolder_;
33
ImageImpl()34 ImageImpl::ImageImpl() : nativeImage_(nullptr), isTestImage_(false), isRelease(false) {}
35
ImageImpl(std::shared_ptr<OHOS::Media::NativeImage> nativeImage)36 ImageImpl::ImageImpl(std::shared_ptr<OHOS::Media::NativeImage> nativeImage)
37 {
38 if (nativeImage == nullptr) {
39 IMAGE_LOGE("nativeImage is nullptr");
40 ImageTaiheUtils::ThrowExceptionError("nativeImage is nullptr");
41 return;
42 }
43
44 auto id = sNativeImageHolder_.save(nativeImage);
45 nativeImage->SetId(id);
46 nativeImage_ = sNativeImageHolder_.get(id);
47 isTestImage_ = false;
48 if (nativeImage_ == nullptr) {
49 if (MY_NAME.compare(id.c_str()) == 0) {
50 isTestImage_ = true;
51 } else {
52 IMAGE_LOGE("Failed to get native image");
53 ImageTaiheUtils::ThrowExceptionError("Failed to get native image");
54 return;
55 }
56 }
57 }
58
~ImageImpl()59 ImageImpl::~ImageImpl()
60 {
61 ReleaseSync();
62 }
63
Create(std::shared_ptr<OHOS::Media::NativeImage> nativeImage)64 struct Image ImageImpl::Create(std::shared_ptr<OHOS::Media::NativeImage> nativeImage)
65 {
66 return make_holder<ImageImpl, struct Image>(nativeImage);
67 }
68
ReleaseSync()69 void ImageImpl::ReleaseSync()
70 {
71 if (!isRelease) {
72 if (nativeImage_ != nullptr) {
73 nativeImage_ = nullptr;
74 }
75 isRelease = true;
76 }
77 }
78
GetSize()79 Size ImageImpl::GetSize()
80 {
81 Size result {NUM0, NUM0};
82 if (isTestImage_) {
83 result.width = TEST_WIDTH;
84 result.height = TEST_HEIGHT;
85 return result;
86 }
87 if (nativeImage_ == nullptr) {
88 IMAGE_LOGE("Image surface buffer is nullptr");
89 return result;
90 }
91
92 if (nativeImage_->GetSize(result.width, result.height) != OHOS::Media::SUCCESS) {
93 IMAGE_LOGE("Image native get size failed");
94 }
95 return result;
96 }
97
GetFormat()98 int32_t ImageImpl::GetFormat()
99 {
100 int32_t format = NUM0;
101 if (isTestImage_) {
102 return TEST_FORMAT;
103 }
104 if (nativeImage_ == nullptr) {
105 IMAGE_LOGE("Image surface buffer is nullptr");
106 return format;
107 }
108
109 if (nativeImage_->GetFormat(format) != OHOS::Media::SUCCESS) {
110 IMAGE_LOGE("Image native get format failed");
111 }
112 return format;
113 }
114 } // namespace ANI::Image