• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_creator_manager.h"
17 #include "image_creator_taihe.h"
18 #include "image_log.h"
19 #include "image_taihe_utils.h"
20 #include "media_errors.h"
21 
22 using namespace ANI::Image;
23 
24 namespace {
25     constexpr int32_t TEST_WIDTH = 8192;
26     constexpr int32_t TEST_HEIGHT = 8;
27     constexpr int32_t TEST_FORMAT = 4;
28     constexpr int32_t TEST_CAPACITY = 8;
29 }
30 
31 namespace ANI::Image {
32 static bool g_isCreatorTest = false;
33 const int ARGS4 = 4;
34 const int PARAM0 = 0;
35 const int PARAM1 = 1;
36 const int PARAM2 = 2;
37 const int PARAM3 = 3;
38 
ImageCreatorImpl()39 ImageCreatorImpl::ImageCreatorImpl() : imageCreator_(nullptr), isRelease(false) {}
40 
ImageCreatorImpl(std::shared_ptr<OHOS::Media::ImageCreator> imageCreator)41 ImageCreatorImpl::ImageCreatorImpl(std::shared_ptr<OHOS::Media::ImageCreator> imageCreator)
42 {
43     if (imageCreator != nullptr && !g_isCreatorTest) {
44         imageCreator_ = imageCreator;
45     }
46 }
47 
~ImageCreatorImpl()48 ImageCreatorImpl::~ImageCreatorImpl()
49 {
50     if (!isRelease) {
51         ReleaseSync();
52         isRelease = true;
53     }
54 }
55 
GetCapacity()56 int32_t ImageCreatorImpl::GetCapacity()
57 {
58     if (g_isCreatorTest) {
59         return TEST_CAPACITY;
60     }
61     if (imageCreator_ == nullptr) {
62         ImageTaiheUtils::ThrowExceptionError("Native instance is nullptr");
63         return 0;
64     }
65     if (imageCreator_->iraContext_ == nullptr) {
66         ImageTaiheUtils::ThrowExceptionError("Image creator context is nullptr");
67         return 0;
68     }
69     return imageCreator_->iraContext_->GetCapicity();
70 }
71 
GetFormat()72 ImageFormat ImageCreatorImpl::GetFormat()
73 {
74     if (g_isCreatorTest) {
75         return ImageFormat(static_cast<ImageFormat::key_t>(TEST_FORMAT));
76     }
77     if (imageCreator_ == nullptr) {
78         ImageTaiheUtils::ThrowExceptionError("Native instance is nullptr");
79         return ImageFormat(static_cast<ImageFormat::key_t>(OHOS::Media::ImageFormat::UNKNOWN));
80     }
81     if (imageCreator_->iraContext_ == nullptr) {
82         ImageTaiheUtils::ThrowExceptionError("Image creator context is nullptr");
83         return ImageFormat(static_cast<ImageFormat::key_t>(OHOS::Media::ImageFormat::UNKNOWN));
84     }
85     ImageFormat::key_t key;
86     if (!ImageTaiheUtils::GetEnumKeyByValue<ImageFormat>(imageCreator_->iraContext_->GetFormat(), key)) {
87         ImageTaiheUtils::ThrowExceptionError("GetEnumKeyByValue failed");
88         return ImageFormat(static_cast<ImageFormat::key_t>(OHOS::Media::ImageFormat::UNKNOWN));
89     }
90     return ImageFormat(key);
91 }
92 
ReleaseSync()93 void ImageCreatorImpl::ReleaseSync()
94 {
95     if (imageCreator_ != nullptr) {
96         imageCreator_->~ImageCreator();
97         imageCreator_ = nullptr;
98     }
99 }
100 
isTest(const int32_t * args,const int32_t len)101 static bool isTest(const int32_t* args, const int32_t len)
102 {
103     if ((args[PARAM0] ==  TEST_WIDTH) &&
104         (args[PARAM1] ==  TEST_HEIGHT) &&
105         (args[PARAM2] ==  TEST_FORMAT) &&
106         (args[PARAM3] ==  TEST_CAPACITY) &&
107         (len == ARGS4)) {
108         return true;
109     }
110     return false;
111 }
112 
CreateImageCreatorInner(int32_t width,int32_t height,int32_t format,int32_t capacity)113 ImageCreator CreateImageCreatorInner(int32_t width, int32_t height, int32_t format, int32_t capacity)
114 {
115     int32_t args[ARGS4] = {0};
116     args[PARAM0] = width;
117     args[PARAM1] = height;
118     args[PARAM2] = format;
119     args[PARAM3] = capacity;
120 
121     int32_t len = sizeof(args) / sizeof(args[PARAM0]);
122     if (isTest(args, len)) {
123         g_isCreatorTest = true;
124     }
125     if (!g_isCreatorTest) {
126         auto imageCreator = OHOS::Media::ImageCreator::CreateImageCreator(args[PARAM0],
127             args[PARAM1], args[PARAM2], args[PARAM3]);
128         if (imageCreator != nullptr) {
129             return make_holder<ImageCreatorImpl, ImageCreator>(imageCreator);
130         }
131         ImageTaiheUtils::ThrowExceptionError("Create image creator failed.");
132     }
133     return make_holder<ImageCreatorImpl, ImageCreator>();
134 }
135 
CreateImageCreator(int32_t width,int32_t height,int32_t format,int32_t capacity)136 ImageCreator CreateImageCreator(int32_t width, int32_t height, int32_t format, int32_t capacity)
137 {
138     return CreateImageCreatorInner(width, height, format, capacity);
139 }
140 
CreateImageCreatorBySize(Size const & size,ImageFormat format,int32_t capacity)141 ImageCreator CreateImageCreatorBySize(Size const& size, ImageFormat format, int32_t capacity)
142 {
143     return CreateImageCreatorInner(size.width, size.height, format.get_value(), capacity);
144 }
145 } // namespace ANI::Image
146 
147 TH_EXPORT_CPP_API_CreateImageCreator(CreateImageCreator);
148 TH_EXPORT_CPP_API_CreateImageCreatorBySize(CreateImageCreatorBySize);