CanvasRenderer: Add Gradient, LinearGradient, RadialGradient classes
[platform/core/uifw/dali-adaptor.git] / dali / internal / canvas-renderer / tizen / gradient-impl-tizen.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/canvas-renderer/tizen/gradient-impl-tizen.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/common/stage.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/object/type-registry.h>
25
26 namespace Dali
27 {
28 namespace Internal
29 {
30 namespace Adaptor
31 {
32 namespace // unnamed namespace
33 {
34 // Type Registration
35 Dali::BaseHandle Create()
36 {
37   return Dali::BaseHandle();
38 }
39
40 Dali::TypeRegistration type(typeid(Dali::CanvasRenderer::Gradient), typeid(Dali::BaseHandle), Create);
41
42 } // unnamed namespace
43
44 GradientTizen* GradientTizen::New()
45 {
46   return new GradientTizen();
47 }
48
49 GradientTizen::GradientTizen()
50 : mChanged(false)
51 #ifdef THORVG_SUPPORT
52   ,
53   mTvgFill(nullptr)
54 #endif
55 {
56 }
57
58 GradientTizen::~GradientTizen()
59 {
60 #ifdef THORVG_SUPPORT
61   if(mTvgFill)
62   {
63     delete mTvgFill;
64   }
65 #endif
66 }
67
68 bool GradientTizen::SetColorStops(Dali::CanvasRenderer::Gradient::ColorStops& colorStops)
69 {
70 #ifdef THORVG_SUPPORT
71   if(!mTvgFill)
72   {
73     DALI_LOG_ERROR("Fill(Gradient) is null [%p]\n", this);
74     return false;
75   }
76   SetChanged(true);
77
78   tvg::Fill::ColorStop* tvgColorStops = (tvg::Fill::ColorStop*)alloca(sizeof(tvg::Fill::ColorStop) * colorStops.Count());
79
80   for(unsigned int i = 0u; i < colorStops.Count(); ++i)
81   {
82     tvgColorStops[i].offset = colorStops[i].offset;
83     tvgColorStops[i].r      = colorStops[i].color.r * 255.0f;
84     tvgColorStops[i].g      = colorStops[i].color.g * 255.0f;
85     tvgColorStops[i].b      = colorStops[i].color.b * 255.0f;
86     tvgColorStops[i].a      = colorStops[i].color.a * 255.0f;
87   }
88
89   if(mTvgFill->colorStops(tvgColorStops, colorStops.Count()) != tvg::Result::Success)
90   {
91     DALI_LOG_ERROR("SetColorStops() fail.\n");
92     return false;
93   }
94
95   return true;
96 #else
97   return false;
98 #endif
99 }
100
101 Dali::CanvasRenderer::Gradient::ColorStops GradientTizen::GetColorStops() const
102 {
103 #ifdef THORVG_SUPPORT
104   if(!mTvgFill)
105   {
106     DALI_LOG_ERROR("Fill(Gradient) is null [%p]\n", this);
107     return Dali::CanvasRenderer::Gradient::ColorStops();
108   }
109
110   const tvg::Fill::ColorStop* tvgColorStops = nullptr;
111   uint32_t                    count         = 0;
112
113   count = mTvgFill->colorStops(&tvgColorStops);
114   if(!tvgColorStops || count <= 0)
115   {
116     DALI_LOG_ERROR("GetColorStops() fail.\n");
117     return Dali::CanvasRenderer::Gradient::ColorStops();
118   }
119
120   Dali::CanvasRenderer::Gradient::ColorStops colorStops;
121
122   colorStops.Reserve(count);
123
124   for(unsigned int i = 0u; i < count; ++i)
125   {
126     Dali::CanvasRenderer::Gradient::ColorStop stop = {tvgColorStops[i].offset, Vector4(tvgColorStops[i].r / 255.0f, tvgColorStops[i].g / 255.0f, tvgColorStops[i].b / 255.0f, tvgColorStops[i].a / 255.0f)};
127
128     colorStops.PushBack(stop);
129   }
130   return colorStops;
131 #else
132   return Dali::CanvasRenderer::Gradient::ColorStops();
133 #endif
134 }
135
136 bool GradientTizen::SetSpread(Dali::CanvasRenderer::Gradient::Spread spread)
137 {
138 #ifdef THORVG_SUPPORT
139   if(!mTvgFill)
140   {
141     DALI_LOG_ERROR("Fill(Gradient) is null [%p]\n", this);
142     return false;
143   }
144   if(mTvgFill->spread(static_cast<tvg::FillSpread>(spread)) != tvg::Result::Success)
145   {
146     DALI_LOG_ERROR("SetSpread() fail.\n");
147     return false;
148   }
149   SetChanged(true);
150
151   return true;
152 #else
153   return false;
154 #endif
155 }
156
157 Dali::CanvasRenderer::Gradient::Spread GradientTizen::GetSpread() const
158 {
159 #ifdef THORVG_SUPPORT
160   if(!mTvgFill)
161   {
162     DALI_LOG_ERROR("Fill(Gradient) is null [%p]\n", this);
163     return Dali::CanvasRenderer::Gradient::Spread::PAD;
164   }
165
166   tvg::FillSpread spread = mTvgFill->spread();
167
168   return static_cast<Dali::CanvasRenderer::Gradient::Spread>(spread);
169 #else
170   return Dali::CanvasRenderer::Gradient::Spread::PAD;
171 #endif
172 }
173
174 void GradientTizen::SetObject(const void* object)
175 {
176 #ifdef THORVG_SUPPORT
177   if(object)
178   {
179     mTvgFill = static_cast<tvg::Fill*>((void*)object);
180   }
181 #endif
182 }
183
184 void* GradientTizen::GetObject() const
185 {
186 #ifdef THORVG_SUPPORT
187   return static_cast<void*>(mTvgFill);
188 #else
189   return nullptr;
190 #endif
191 }
192
193 void GradientTizen::SetChanged(bool changed)
194 {
195   if(!mChanged && changed) Dali::Stage::GetCurrent().KeepRendering(0.0f);
196   mChanged = !!changed;
197 }
198
199 bool GradientTizen::GetChanged() const
200 {
201   return mChanged;
202 }
203 } // namespace Adaptor
204
205 } // namespace Internal
206
207 } // namespace Dali