Moved Core Rendering API from devel-api to public-api
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / renderers / gradient / gradient.h
1 #ifndef DALI_TOOLKIT_INTERNAL_GRADIENT_H
2 #define DALI_TOOLKIT_INTERNAL_GRADIENT_H
3
4 /*
5  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 //EXTERNAL INCLUDES
22 #include <dali/public-api/common/dali-vector.h>
23 #include <dali/public-api/images/buffer-image.h>
24 #include <dali/public-api/math/matrix3.h>
25 #include <dali/public-api/object/ref-object.h>
26 #include <dali/public-api/rendering/texture.h>
27
28 namespace Dali
29 {
30
31 class Vector4;
32
33 namespace Toolkit
34 {
35
36 namespace Internal
37 {
38
39 /**
40  * Gradients consist of continuously smooth color transitions along a vector from one color to another,
41  * possibly followed by additional transitions along the same vector to other colors.
42  */
43 class Gradient : public RefObject
44 {
45 public:
46   /**
47    * Defines the coordinate system of the attributes
48    *     (start and end position for linear gradient, circle center and radius for radial gradient)
49    */
50   enum GradientUnits
51   {
52     USER_SPACE_ON_USE,
53     OBJECT_BOUNDING_BOX
54   };
55
56   /**
57    * Indicates what happens if the gradient starts or ends inside the bounds of the object being painted by the gradient.
58    */
59   enum SpreadMethod
60   {
61     PAD,      // use the terminal colors of the gradient to fill the remainder of the target region
62     REPEAT,   // reflect the gradient pattern start-to-end, end-to-start, start-to-end, etc. continuously until the target rectangle is filled
63     REFLECT   // repeat the gradient pattern start-to-end, start-to-end, start-to-end, etc. continuously until the target region is filled
64   };
65
66   /**
67    * The stop node tells the gradient what color it should be at certain position.
68    */
69   struct GradientStop
70   {
71     GradientStop( float offset, const Vector4& color )
72     : mOffset( offset ), mStopColor( color )
73     {}
74
75     bool operator<(const GradientStop& rhs) const
76     {
77       return mOffset < rhs.mOffset;
78     }
79
80     float   mOffset;     // A value ranging from 0 to 1 to indicate where the gradient stop is placed.
81     Vector4 mStopColor;  // The color to use at this gradient stop
82   };
83
84 public:
85
86   /**
87    * Add a gradient stop.
88    *
89    * @param[in] offset The position to place the stop.
90    * @param[in] color  The color to use at this stop.
91    */
92   void AddStop(float offset, const Vector4& color);
93
94   /**
95    * Get the gradient stops.
96    * @return The vector of gradient stops.
97    */
98   const Vector<GradientStop>& GetStops();
99
100   /**
101    * Set the coordinate system used by the gradient attributes.
102    * @param[in] gradientUnits The the attributes are defined using the current user coordinate system or the bounding box of the shape.
103    */
104   void SetGradientUnits( GradientUnits gradientUnits );
105
106   /**
107    * Get the coordinate system used by the gradient attributes.
108    * @return USER_SPACE_ON_USE or OBJECT_BOUNDING_BOX
109    */
110   GradientUnits GetGradientUnits() const;
111
112   /**
113    * Indicates what happens if the gradient starts or ends inside the bounds of the target rectangle.
114    * If not specified, the effect is as if a value of 'pad' were specified
115    *
116    * @param[in] spread The method to fill the remainder of target region which is outside the gradient bounds
117    */
118   void SetSpreadMethod( SpreadMethod spread );
119
120   /**
121    * Get the filling method for the the remainder of target region which is outside the gradient boun.
122    * @return PAD, REFLECT or REPEAT
123    */
124   SpreadMethod GetSpreadMethod() const;
125
126   /**
127    * Get the transformation matrix to align the vertices with the gradient line/circle
128    * @ return the aligning transformation matrix
129    */
130   const Matrix3& GetAlignmentTransform() const;
131
132   /**
133    * Generate the lookup texture with the gradient stops.
134    * @return The lookup texture which transit smoothly between stops.
135    */
136   Dali::Texture GenerateLookupTexture();
137
138 private:
139
140   /**
141    * Estimate the resolution of the lookup texture.
142    * Note: Only call this function after the gradient stops are sorted in order.
143    */
144   unsigned int EstimateTextureResolution();
145
146 protected:
147
148   /**
149    * Construct a new Gradient object
150    * Called in the constructor of subclasses
151    */
152   Gradient();
153
154   /**
155    * @brief A reference counted object may only be deleted by calling Unreference().
156    */
157   virtual ~Gradient();
158
159   // Undefined
160   Gradient( const Gradient& gradient );
161
162   // Undefined
163   Gradient& operator=( const Gradient& handle );
164
165 protected:
166
167   Vector<GradientStop>      mGradientStops;
168   Matrix3                   mAlignmentTransform;
169   GradientUnits             mGradientUnits;
170   SpreadMethod              mSpreadMethod;
171
172 };
173
174 } // namespace Internal
175
176 } // namespace Toolkit
177
178 } // namespace Dali
179
180 #endif // DALI_TOOLKIT_INTERNAL_GRADIENT_RENDERER_H