1 /*
2 * Copyright (c) 2023 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 "c/drawing_shader_effect.h"
17
18 #include "effect/shader_effect.h"
19
20 using namespace OHOS;
21 using namespace Rosen;
22 using namespace Drawing;
23
CastToPoint(const OH_Drawing_Point & cPoint)24 static const Point& CastToPoint(const OH_Drawing_Point& cPoint)
25 {
26 return reinterpret_cast<const Point&>(cPoint);
27 }
28
CastToShaderEffect(OH_Drawing_ShaderEffect * cShaderEffect)29 static ShaderEffect* CastToShaderEffect(OH_Drawing_ShaderEffect* cShaderEffect)
30 {
31 return reinterpret_cast<ShaderEffect*>(cShaderEffect);
32 }
33
OH_Drawing_ShaderEffectCreateLinearGradient(const OH_Drawing_Point * cStartPt,const OH_Drawing_Point * cEndPt,const uint32_t * colors,const float * pos,uint32_t size,OH_Drawing_TileMode cTileMode)34 OH_Drawing_ShaderEffect* OH_Drawing_ShaderEffectCreateLinearGradient(const OH_Drawing_Point* cStartPt,
35 const OH_Drawing_Point* cEndPt, const uint32_t* colors, const float* pos, uint32_t size,
36 OH_Drawing_TileMode cTileMode)
37 {
38 if (cStartPt == nullptr || cEndPt == nullptr || colors == nullptr || pos == nullptr) {
39 return nullptr;
40 }
41 std::vector<ColorQuad> colorsVector;
42 std::vector<scalar> posVector;
43 for (uint32_t i = 0; i < size; i++) {
44 colorsVector.emplace_back(colors[i]);
45 posVector.emplace_back(pos[i]);
46 }
47 return (OH_Drawing_ShaderEffect*)new ShaderEffect(ShaderEffect::ShaderEffectType::LINEAR_GRADIENT,
48 CastToPoint(*cStartPt), CastToPoint(*cEndPt), colorsVector, posVector, static_cast<TileMode>(cTileMode));
49 }
50
OH_Drawing_ShaderEffectCreateRadialGradient(const OH_Drawing_Point * cCenterPt,float radius,const uint32_t * colors,const float * pos,uint32_t size,OH_Drawing_TileMode cTileMode)51 OH_Drawing_ShaderEffect* OH_Drawing_ShaderEffectCreateRadialGradient(const OH_Drawing_Point* cCenterPt, float radius,
52 const uint32_t* colors, const float* pos, uint32_t size, OH_Drawing_TileMode cTileMode)
53 {
54 if (cCenterPt == nullptr || colors == nullptr || pos == nullptr) {
55 return nullptr;
56 }
57 std::vector<ColorQuad> colorsVector;
58 std::vector<scalar> posVector;
59 for (uint32_t i = 0; i < size; i++) {
60 colorsVector.emplace_back(colors[i]);
61 posVector.emplace_back(pos[i]);
62 }
63 return (OH_Drawing_ShaderEffect*)new ShaderEffect(ShaderEffect::ShaderEffectType::RADIAL_GRADIENT,
64 CastToPoint(*cCenterPt), radius, colorsVector, posVector, static_cast<TileMode>(cTileMode));
65 }
66
OH_Drawing_ShaderEffectCreateSweepGradient(const OH_Drawing_Point * cCenterPt,const uint32_t * colors,const float * pos,uint32_t size,OH_Drawing_TileMode cTileMode)67 OH_Drawing_ShaderEffect* OH_Drawing_ShaderEffectCreateSweepGradient(const OH_Drawing_Point* cCenterPt,
68 const uint32_t* colors, const float* pos, uint32_t size, OH_Drawing_TileMode cTileMode)
69 {
70 if (cCenterPt == nullptr || colors == nullptr || pos == nullptr) {
71 return nullptr;
72 }
73 std::vector<ColorQuad> colorsVector;
74 std::vector<scalar> posVector;
75 for (uint32_t i = 0; i < size; i++) {
76 colorsVector.emplace_back(colors[i]);
77 posVector.emplace_back(pos[i]);
78 }
79 return (OH_Drawing_ShaderEffect*)new ShaderEffect(ShaderEffect::ShaderEffectType::SWEEP_GRADIENT,
80 CastToPoint(*cCenterPt), colorsVector, posVector, static_cast<TileMode>(cTileMode), 0,
81 360, nullptr); // 360: endAngle
82 }
83
OH_Drawing_ShaderEffectDestroy(OH_Drawing_ShaderEffect * cShaderEffect)84 void OH_Drawing_ShaderEffectDestroy(OH_Drawing_ShaderEffect* cShaderEffect)
85 {
86 delete CastToShaderEffect(cShaderEffect);
87 }
88