1 /*
2 * Copyright (c) 2021-2024 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 "drawing_brush.h"
17
18 #include "drawing_canvas_utils.h"
19 #include "drawing_helper.h"
20
21 #include "draw/brush.h"
22
23 using namespace OHOS;
24 using namespace Rosen;
25 using namespace Drawing;
26
CastToBrush(OH_Drawing_Brush * cBrush)27 static Brush* CastToBrush(OH_Drawing_Brush* cBrush)
28 {
29 return reinterpret_cast<Brush*>(cBrush);
30 }
31
CastToBrush(const OH_Drawing_Brush & cBrush)32 static const Brush& CastToBrush(const OH_Drawing_Brush& cBrush)
33 {
34 return reinterpret_cast<const Brush&>(cBrush);
35 }
36
CastToShaderEffect(OH_Drawing_ShaderEffect * cShaderEffect)37 static ShaderEffect* CastToShaderEffect(OH_Drawing_ShaderEffect* cShaderEffect)
38 {
39 return reinterpret_cast<ShaderEffect*>(cShaderEffect);
40 }
41
CastToFilter(const OH_Drawing_Filter & cFilter)42 static const Filter& CastToFilter(const OH_Drawing_Filter& cFilter)
43 {
44 return reinterpret_cast<const Filter&>(cFilter);
45 }
46
CastToFilter(const OH_Drawing_Filter * cFilter)47 static const Filter* CastToFilter(const OH_Drawing_Filter* cFilter)
48 {
49 return reinterpret_cast<const Filter*>(cFilter);
50 }
51
OH_Drawing_BrushCreate()52 OH_Drawing_Brush* OH_Drawing_BrushCreate()
53 {
54 return (OH_Drawing_Brush*)new Brush;
55 }
56
OH_Drawing_BrushCopy(OH_Drawing_Brush * cBrush)57 OH_Drawing_Brush* OH_Drawing_BrushCopy(OH_Drawing_Brush* cBrush)
58 {
59 Brush* brush = CastToBrush(cBrush);
60 if (brush == nullptr) {
61 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
62 return nullptr;
63 }
64 return (OH_Drawing_Brush*)new Brush(*brush);
65 }
66
OH_Drawing_BrushDestroy(OH_Drawing_Brush * cBrush)67 void OH_Drawing_BrushDestroy(OH_Drawing_Brush* cBrush)
68 {
69 if (!cBrush) {
70 return;
71 }
72 delete CastToBrush(cBrush);
73 }
74
OH_Drawing_BrushIsAntiAlias(const OH_Drawing_Brush * cBrush)75 bool OH_Drawing_BrushIsAntiAlias(const OH_Drawing_Brush* cBrush)
76 {
77 if (cBrush == nullptr) {
78 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
79 return false;
80 }
81 return CastToBrush(*cBrush).IsAntiAlias();
82 }
83
OH_Drawing_BrushSetAntiAlias(OH_Drawing_Brush * cBrush,bool aa)84 void OH_Drawing_BrushSetAntiAlias(OH_Drawing_Brush* cBrush, bool aa)
85 {
86 Brush* brush = CastToBrush(cBrush);
87 if (brush == nullptr) {
88 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
89 return;
90 }
91 brush->SetAntiAlias(aa);
92 }
93
OH_Drawing_BrushGetColor(const OH_Drawing_Brush * cBrush)94 uint32_t OH_Drawing_BrushGetColor(const OH_Drawing_Brush* cBrush)
95 {
96 if (cBrush == nullptr) {
97 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
98 return 0;
99 }
100 return CastToBrush(*cBrush).GetColor().CastToColorQuad();
101 }
102
OH_Drawing_BrushSetColor(OH_Drawing_Brush * cBrush,uint32_t color)103 void OH_Drawing_BrushSetColor(OH_Drawing_Brush* cBrush, uint32_t color)
104 {
105 Brush* brush = CastToBrush(cBrush);
106 if (brush == nullptr) {
107 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
108 return;
109 }
110 brush->SetColor(color);
111 }
112
OH_Drawing_BrushGetAlpha(const OH_Drawing_Brush * cBrush)113 uint8_t OH_Drawing_BrushGetAlpha(const OH_Drawing_Brush* cBrush)
114 {
115 if (cBrush == nullptr) {
116 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
117 return 0;
118 }
119 return CastToBrush(*cBrush).GetAlpha();
120 }
121
OH_Drawing_BrushSetAlpha(OH_Drawing_Brush * cBrush,uint8_t alpha)122 void OH_Drawing_BrushSetAlpha(OH_Drawing_Brush* cBrush, uint8_t alpha)
123 {
124 Brush* brush = CastToBrush(cBrush);
125 if (brush == nullptr) {
126 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
127 return;
128 }
129 brush->SetAlpha(alpha);
130 }
131
OH_Drawing_BrushSetShaderEffect(OH_Drawing_Brush * cBrush,OH_Drawing_ShaderEffect * cShaderEffect)132 void OH_Drawing_BrushSetShaderEffect(OH_Drawing_Brush* cBrush, OH_Drawing_ShaderEffect* cShaderEffect)
133 {
134 Brush* brush = CastToBrush(cBrush);
135 if (brush == nullptr) {
136 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
137 return;
138 }
139 if (cShaderEffect == nullptr) {
140 brush->SetShaderEffect(nullptr);
141 return;
142 }
143 brush->SetShaderEffect(std::shared_ptr<ShaderEffect>{CastToShaderEffect(cShaderEffect), [](auto p) {}});
144 }
145
OH_Drawing_BrushSetShadowLayer(OH_Drawing_Brush * cBrush,OH_Drawing_ShadowLayer * cShadowLayer)146 void OH_Drawing_BrushSetShadowLayer(OH_Drawing_Brush* cBrush, OH_Drawing_ShadowLayer* cShadowLayer)
147 {
148 Brush* brush = CastToBrush(cBrush);
149 if (brush == nullptr) {
150 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
151 return;
152 }
153 if (cShadowLayer == nullptr) {
154 brush->SetLooper(nullptr);
155 return;
156 }
157 auto blurDrawLooperHandle = Helper::CastTo<OH_Drawing_ShadowLayer*, NativeHandle<BlurDrawLooper>*>(cShadowLayer);
158 brush->SetLooper(blurDrawLooperHandle->value);
159 }
160
OH_Drawing_BrushSetFilter(OH_Drawing_Brush * cBrush,OH_Drawing_Filter * cFilter)161 void OH_Drawing_BrushSetFilter(OH_Drawing_Brush* cBrush, OH_Drawing_Filter* cFilter)
162 {
163 Brush* brush = CastToBrush(cBrush);
164 if (brush == nullptr) {
165 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
166 return;
167 }
168 if (cFilter == nullptr) {
169 Filter filter;
170 brush->SetFilter(filter);
171 return;
172 }
173 brush->SetFilter(CastToFilter(*cFilter));
174 }
175
OH_Drawing_BrushGetFilter(OH_Drawing_Brush * cBrush,OH_Drawing_Filter * cFilter)176 void OH_Drawing_BrushGetFilter(OH_Drawing_Brush* cBrush, OH_Drawing_Filter* cFilter)
177 {
178 Brush* brush = CastToBrush(cBrush);
179 if (brush == nullptr) {
180 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
181 return;
182 }
183 Filter* filter = const_cast<Filter*>(CastToFilter(cFilter));
184 if (filter == nullptr) {
185 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
186 return;
187 }
188 *filter = brush->GetFilter();
189 }
190
OH_Drawing_BrushSetBlendMode(OH_Drawing_Brush * cBrush,OH_Drawing_BlendMode cBlendMode)191 void OH_Drawing_BrushSetBlendMode(OH_Drawing_Brush* cBrush, OH_Drawing_BlendMode cBlendMode)
192 {
193 Brush* brush = CastToBrush(cBrush);
194 if (brush == nullptr) {
195 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
196 return;
197 }
198 if (cBlendMode < BLEND_MODE_CLEAR || cBlendMode > BLEND_MODE_LUMINOSITY) {
199 g_drawingErrorCode = OH_DRAWING_ERROR_PARAMETER_OUT_OF_RANGE;
200 return;
201 }
202 brush->SetBlendMode(static_cast<BlendMode>(cBlendMode));
203 }
204
OH_Drawing_BrushReset(OH_Drawing_Brush * cBrush)205 void OH_Drawing_BrushReset(OH_Drawing_Brush* cBrush)
206 {
207 Brush* brush = CastToBrush(cBrush);
208 if (brush == nullptr) {
209 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
210 return;
211 }
212 brush->Reset();
213 }
214