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 "adapter/ohos/osal/drawable_descriptor_ohos.h"
17
18 #include "base/log/log.h"
19 #include "core/components/common/layout/constants.h"
20
21 namespace OHOS::Ace {
CreateDrawable(void * sptrAddr)22 RefPtr<DrawableDescriptor> DrawableDescriptor::CreateDrawable(void* sptrAddr)
23 {
24 auto* drawable = reinterpret_cast<Drawable::DrawableDescriptor*>(sptrAddr);
25 if (drawable == nullptr) {
26 TAG_LOGW(AceLogTag::ACE_DRAWABLE_DESCRIPTOR, "drawable is nullptr when create drawable");
27 return nullptr;
28 }
29 return AceType::MakeRefPtr<DrawableDescriptorOhos>(drawable);
30 }
31
DrawableDescriptorOhos(Drawable::DrawableDescriptor * drawable)32 DrawableDescriptorOhos::DrawableDescriptorOhos(Drawable::DrawableDescriptor* drawable) : drawable_(drawable)
33 {
34 drawable_->AddDestructCallback(this, [mutex = mutex_, isValid = isValid_] {
35 std::lock_guard lg(*mutex);
36 *isValid = false;
37 });
38 }
39
~DrawableDescriptorOhos()40 DrawableDescriptorOhos::~DrawableDescriptorOhos()
41 {
42 std::lock_guard lg(*mutex_);
43 if (*isValid_) {
44 drawable_->RemoveDestructCallback(this);
45 }
46 }
47
GetDrawableSrcType()48 int32_t DrawableDescriptorOhos::GetDrawableSrcType()
49 {
50 if (!(*isValid_)) {
51 TAG_LOGE(AceLogTag::ACE_DRAWABLE_DESCRIPTOR, "drawable is suspended when get src type");
52 return -1;
53 }
54 return static_cast<int32_t>(drawable_->GetSrcType());
55 }
56
RegisterRedrawCallback(RedrawCallback && callback)57 void DrawableDescriptorOhos::RegisterRedrawCallback(RedrawCallback&& callback)
58 {
59 if (!(*isValid_)) {
60 TAG_LOGE(AceLogTag::ACE_DRAWABLE_DESCRIPTOR, "drawable is suspended when component register redraw callback");
61 return;
62 }
63 drawable_->RegisterRedrawCallback(std::move(callback));
64 }
65
ConverConfig(const NG::ImagePaintConfig & config)66 Drawable::PaintConfig DrawableDescriptorOhos::ConverConfig(const NG::ImagePaintConfig& config)
67 {
68 Drawable::PaintConfig drawableConfig;
69 drawableConfig.imageFit = static_cast<Drawable::ImageFit>(config.imageFit_);
70 return drawableConfig;
71 }
72
Draw(RSCanvas & canvas,const NG::ImagePaintConfig & config)73 void DrawableDescriptorOhos::Draw(RSCanvas& canvas, const NG::ImagePaintConfig& config)
74 {
75 if (!(*isValid_)) {
76 TAG_LOGE(AceLogTag::ACE_DRAWABLE_DESCRIPTOR, "drawable is suspended when drawable draw to canvas");
77 return;
78 }
79 drawable_->Draw(canvas, ConverConfig(config));
80 }
81 } // namespace OHOS::Ace
82