Tizen 2.1 base
[framework/osp/uifw.git] / src / graphics / FGrp_CanvasGpPrimitive.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /*
19  * @file        FGrp_CanvasGpPrimitive.h
20  * @brief       This is the header file for 2D Primitive classes.
21  *
22  */
23
24 #ifndef _FGRP_INTERNAL_CANVAS_GP_PRIMITIVE_H_
25 #define _FGRP_INTERNAL_CANVAS_GP_PRIMITIVE_H_
26
27
28 #include <FBaseErrors.h>
29 #include <FGrpCanvasCommon.h>
30
31
32 namespace Tizen { namespace Graphics
33 {
34
35 namespace // unnamed
36 {
37
38 const int _MAX_SLL_BLOCK = 25;
39 const int _MAX_INT = 0x7fffffff;
40 const int _MIN_INT = (-_MAX_INT - 1);
41 const float _PI = (3.14159265358979323846f);
42
43 template <typename T>
44 inline T GP_ABS(T value)
45 {
46         return (value < 0) ? -value : value;
47 }
48
49 template <typename T>
50 inline void GP_SWAP(T& a, T& b)
51 {
52         T tmp = a;
53         a = b;
54         b = tmp;
55 }
56
57 template <typename T>
58 inline T GP_MIN(T a, T b)
59 {
60         return (a < b) ? a : b;
61 }
62
63 template <typename T>
64 inline T GP_MAX(T a, T b)
65 {
66         return (a < b) ? b : a;
67 }
68
69 inline int _GP_RADIAN2DEGREE(float radian)
70 {
71         return int(radian * 180.0f / _PI + 0.5f);
72 }
73
74 inline float _GP_DEGREE2RADIAN(float degree)
75 {
76         return degree * _PI / 180.0f;
77 }
78
79 }
80
81 struct _GpPoint
82 {
83         int x;
84         int y;
85 };
86
87 struct _GpRect
88 {
89         int x;
90         int y;
91         int width;
92         int height;
93
94         _GpRect()
95                 : x(0)
96                 , y(0)
97                 , width(0)
98                 , height(0)
99         {
100         }
101
102         ~_GpRect()
103         {
104         }
105
106         bool SetBounds(int rcX, int rcY, int rcW, int rcH)
107         {
108                 x = rcX;
109                 y = rcY;
110                 width = rcW;
111                 height = rcH;
112
113                 return true;
114         }
115 };
116
117 struct _GpVertex
118 {
119         _GpPoint point;
120         _GpVertex* pNext;
121
122         _GpVertex()
123         {
124                 point.x = 0;
125                 point.y = 0;
126                 pNext = null;
127         }
128
129         ~_GpVertex()
130         {
131                 //      if (pNext)
132                 //              delete next;
133         }
134
135 };
136
137 struct _GpPolygon
138 {
139         int n;
140         _GpVertex* pFirst;
141         _GpVertex* pLast;
142
143         _GpPolygon()
144                 : n(0)
145                 , pFirst(null)
146                 , pLast(null)
147         {
148         }
149
150         ~_GpPolygon()
151         {
152         }
153 };
154
155 struct _GpEdgeTableEntry
156 {
157         int maxY;
158         int minor;
159         int direction;
160         int gradient;
161         int gradient1;
162         int inc1;
163         int inc2;
164         _GpEdgeTableEntry* pNext;
165         _GpEdgeTableEntry* pBack;
166         _GpEdgeTableEntry* pWNext;
167         bool clockWise;
168 };
169
170 struct _GpScanLineList
171 {
172         int scanLine;
173         _GpEdgeTableEntry* pEdgeList;
174         _GpScanLineList* pNext;
175 };
176
177 struct _GpEdgeTable
178 {
179         int maxY;
180         int minY;
181         _GpScanLineList scanLines;
182 };
183
184 struct _GpScanLineListBlock
185 {
186         _GpScanLineList scanLineList[_MAX_SLL_BLOCK];
187         _GpScanLineListBlock* pNext;
188 };
189
190 struct _GpFloatPoint
191 {
192         float fx;
193         float fy;
194 };
195
196 struct _GpBufferInfo
197 {
198         int width;
199         int height;
200         int bitsPerPixel;
201         int pixelPerLine;
202         void* pPixels;
203         unsigned long color32;
204         unsigned short color16;
205         bool isClipBoundsSet;
206         _GpRect clipBounds;
207
208         _GpBufferInfo()
209                 : width(0)
210                 , height(0)
211                 , bitsPerPixel(0)
212                 , pixelPerLine(0)
213                 , pPixels(null)
214                 , color32(0)
215                 , color16(0)
216                 , isClipBoundsSet(false)
217         {
218         }
219
220 };
221
222 enum _GpResult
223 {
224         GP_RESULT_SUCCESS = 0x00000001,
225         GP_RESULT_OUTOFBOUNDS = 0x00000002,
226         GP_RESULT_FAIL = 0x00000004
227 };
228
229 inline bool
230 _IsEqual(float f1, float f2)
231 {
232         return (((f1 > f2) ? f1 - f2 : f2 - f1) < 0.00001f); // epsilon is assumed to be 0.0001, not 1.192092896e-07f
233 }
234
235 class _CanvasLine
236 {
237 public:
238         // A Constructor
239         _CanvasLine(void)
240                 : pFuncSetPixel(null)
241                 , pFuncHorizontalLine(null)
242                 , pFuncVerticalLine(null)
243                 , __lineWidth(1)
244         {
245                 _SetClipFunction(true);
246         }
247         // A Destructor
248         virtual ~_CanvasLine(void) { }
249
250         // Sets the line width.
251         bool SetLineWidth(int lineWidth);
252         // Gets the line width.
253         int GetLineWidth(void) const;
254         // draws a line.
255         result DrawLine(const _GpPoint& startPt, const _GpPoint& endPt, const _GpBufferInfo& bufInfo);
256         // draws a PolyLine.
257         result DrawPolyLine(int pointCount, _GpPoint* pPoints, const _GpBufferInfo& bufInfo);
258         // draws a Polygon.
259         result DrawPolygon(int pointCount, _GpPoint* pPoints, const _GpBufferInfo& bufInfo);
260
261 protected:
262         _GpResult _DrawUnitLineVertical(int x, int startY, int endY, const _GpBufferInfo& bufInfo);
263         _GpResult _DrawUnitLineHorizontal(int startX, int endX, int y, const _GpBufferInfo& bufInfo);
264         _GpResult _DrawWideLineVertical(int x, int startY, int endY, const _GpBufferInfo& bufInfo);
265         _GpResult _DrawWideLineHorizontal(int startX, int endX, int y, const _GpBufferInfo& bufInfo);
266         _GpResult _DrawUnitLineSlant(const _GpPoint& pt1, const _GpPoint& pt2, const _GpBufferInfo& bufInfo);
267         _GpResult _DrawPixel(int x, int y, const _GpBufferInfo& bufInfo);
268
269         _GpResult _DrawUnitLineVerticalWithClipping(int x, int startY, int endY, const _GpBufferInfo& bufInfo);
270         _GpResult _DrawUnitLineHorizontalWithClipping(int startX, int endX, int y, const _GpBufferInfo& bufInfo);
271         _GpResult _DrawWideLineVerticalWithClipping(int x, int startY, int endY, const _GpBufferInfo& bufInfo);
272         _GpResult _DrawWideLineHorizontalWithClipping(int startX, int endX, int y, const _GpBufferInfo& bufInfo);
273         _GpResult _DrawPixelWithClipping(int x, int y, const _GpBufferInfo& bufInfo);
274
275         bool _DrawPolyLine(int pointCount, _GpPoint* pPoints, bool isConnected, const _GpBufferInfo& bufInfo);
276         void _FreePolygon(_GpPolygon* pPolygon) const;
277         bool _FillPolygon(int pointCount, _GpPoint* pPoints, bool isEvenOdd, const _GpBufferInfo& bufInfo);
278         bool _GetOrientationUnitVector(_GpPoint& startPt, _GpPoint& endPt, float* pOx, float* pOy) const;
279         bool _PatchThickContinuousLine(_GpPolygon* pPolygon, _GpPoint& pt1, _GpPoint& pt2, float ox1, float oy1, float ox2, float oy2) const;
280         bool _AddPolygonVertex(_GpPolygon* pPolygon, int x, int y, int pos) const;
281
282         bool _CreateEtAndAet(int pointCount, _GpPoint* pPoints, _GpEdgeTable* pEdgeTable, _GpEdgeTableEntry* pEdgeTableEntry, _GpEdgeTableEntry* pEteBuffer, _GpScanLineListBlock* pSllb) const;
283         void _FreeSllb(_GpScanLineListBlock* pScanLineListBlock) const;
284         void _LoadAet(_GpEdgeTableEntry* pAet, _GpEdgeTableEntry* pEte) const;
285         bool _InsertionSort(_GpEdgeTableEntry* pAet) const;
286         void _ComputeWaet(_GpEdgeTableEntry* pAet) const;
287
288         bool _PatchJoint(_GpPolygon* pPolygon, int x, int y, float ox1, float oy1, float ox2, float oy2) const;
289         bool _PatchRoundCap(_GpPolygon* pPolygon, int x, int y, float ox, float oy, bool isStart) const;
290
291         int _MakeEllipseArcPointArray(_GpFloatPoint** pPoint, _GpFloatPoint** pNorm, float x, float y, float w, float h, int a1, int a2, int rot) const;
292
293         void _Rotate2DPoint(float x, float y, long rot, float* pRx, float* pRy) const;
294
295         bool _IsInClipRect(int rectX, int rectY, int rectW, int rectH, const _GpBufferInfo& bufInfo) const;
296         bool _SetNotClipFunction(bool isWidelineSet);
297         bool _SetClipFunction(bool isWidelineSet) const;
298
299 private:
300         bool __DrawWideLine(const _GpPoint& startPt, const _GpPoint& endPt, const _GpBufferInfo& bufInfo);
301         bool __DrawWideLineWithOpacity32(const _GpPoint& startPt, const _GpPoint& endPt, const _GpBufferInfo& bufInfo);
302         bool __DrawThickContinuousPolyline(int listCount, _GpPoint* pPoints, bool isConnected, const _GpBufferInfo& bufInfo);
303         _GpResult __DrawHorizontalLineWithOpacity32(int startX, int endX, int y, const _GpBufferInfo& bufInfo);
304
305         int __CheckDupVertexForPolygon(int pointCount, _GpPoint* pPoints, bool isConnected) const;
306         void __GetBrushEndPoint(float ox, float oy, int w, int* plx, int* ply, int* prx, int* pry) const;
307         bool __InsertEdge(_GpEdgeTable* pEdgeTable, _GpEdgeTableEntry* pEdgeTableEntry, int scanLine, _GpScanLineListBlock** ppSllb, int* pIsLineListBlock) const;
308
309         bool __PatchArc(_GpPolygon* pPolygon, float x, float y, float w, float h, int d0, int d1, int pos) const;
310
311 public:
312         typedef _GpResult (_CanvasLine::* DrawPixelFunc)(int x, int y, const _GpBufferInfo& bufInfo);
313         typedef _GpResult (_CanvasLine::* DrawHorizontalLine)(int startX, int endX, int y, const _GpBufferInfo& bufInfo);
314         typedef _GpResult (_CanvasLine::* DrawVerticalLine)(int x, int startY, int endY, const _GpBufferInfo& bufInfo);
315
316         mutable DrawPixelFunc pFuncSetPixel;
317         mutable DrawHorizontalLine pFuncHorizontalLine;
318         mutable DrawVerticalLine pFuncVerticalLine;
319
320 private:
321         int __lineWidth;
322
323         friend class _CanvasTriangle;
324         friend class _CanvasArc;
325         friend class _CanvasEllipse;
326         friend class _CanvasFillPolygon;
327         friend class _CanvasRoundRect;
328 };
329
330
331 class _CanvasFillPolygon
332 {
333 public:
334         // A Constructor
335         _CanvasFillPolygon(void)
336         {
337                 __lineWidth = 1;
338         }
339
340         // A Destructor
341         virtual ~_CanvasFillPolygon(void) { }
342
343         // Sets the line width
344         bool SetLineWidth(int lineWidth);
345         // Gets the line width.
346         int GetLineWidth(void) const;
347         // draws the filled polygon.
348         result FillPolygon(int pointCount, _GpPoint* pPoints, const _GpBufferInfo& bufInfo);
349
350 private:
351         int __lineWidth;
352
353 };
354
355
356 class _CanvasEllipse
357 {
358 public:
359         // A Constructor
360         _CanvasEllipse(void)
361         {
362                 __lineWidth = 1;
363         }
364         // A Destructor
365         virtual ~_CanvasEllipse(void) { }
366         // Sets the line width
367         bool SetLineWidth(int lineWidth);
368         // Gets the line width.
369         int GetLineWidth(void) const;
370         // Draws a ellipse.
371         result DrawEllipse(int rectX, int rectY, int rectW, int rectH, const _GpBufferInfo& bufInfo) const;
372 //      bool __DrawEllipseWithWidth(int rectX, int rectY, int rectW, int rectH, _GpBufferInfo& bufInfo);
373         // Draws the filled ellipse.
374         result FillElliepse(int rectX, int rectY, int rectW, int rectH, const _GpBufferInfo& bufInfo) const;
375
376 private:
377         bool __DrawEllipseWithWidth(int rectX, int rectY, int rectW, int rectH, const _GpBufferInfo& bufInfo) const;
378
379 private:
380         int __lineWidth;
381
382 };
383
384
385 class _CanvasArc
386 {
387 public:
388         enum GpArcDiffResult
389         {
390                 DIFF_RESULT_EQUAL = 0,
391                 DIFF_RESULT_GREAT,
392                 DIFF_RESULT_LITTLE,
393         };
394
395         struct GpArcStack
396         {
397                 int left;
398                 int right;
399         };
400
401 public:
402         // A Constructor
403         _CanvasArc(void)
404         {
405                 __lineWidth = 1;
406         }
407         // A Destructor
408         virtual ~_CanvasArc(void) { }
409         // Sets the line width
410         bool SetLineWidth(int lineWidth);
411         // Gets the line width.
412         int GetLineWidth(void) const;
413         // Draws a Arc.
414         result DrawArc(int rectX, int rectY, int rectW, int rectH, int startAngle, int endAngle, Tizen::Graphics::ArcStyle arcType, const _GpBufferInfo& bufInfo);
415
416         void _CalcLeftRightWidth(float* pWidthLeft, float* pWidthRight) const;
417
418 private:
419         bool __GetArcVertices(int radiusX, int radiusY, int theta, int* pX, int* pY) const;
420         float __Sine(int degree) const;
421         float __Cosine(int degree) const;
422         bool __SaveLine(int x1, int y1, int x2, int y2, _GpPoint* pPionts, int* pPixelCount) const;
423         bool __FillArcWithEvenOdd(_GpPoint* pPionts, int pixelCount, bool clip, const _GpBufferInfo& bufInfo) const;
424         void __QuickSort(_GpPoint* pPionts, int left, int right) const;
425
426         bool __Draw32BitArc(int rectX, int rectY, int rectW, int rectH, int d0, int d1, Tizen::Graphics::ArcStyle arcType, const _GpBufferInfo& bufInfo);
427         bool __DrawThickContinuousArc(int cx, int cy, int halfW, int halfH, int d0, int d1, const _GpBufferInfo& bufInfo) const;
428         bool __DrawThickContinuousPie(int cx, int cy, int halfW, int halfH, int d0, int d1, const _GpBufferInfo& bufInfo) const;
429         bool __DrawThickContinuousChord(int cx, int cy, int halfW, int halfH, int d0, int d1, const _GpBufferInfo& bufInfo) const;
430
431 private:
432         int __lineWidth;
433 };
434
435
436 class _CanvasTriangle
437 {
438 public:
439         // A Constructor
440         _CanvasTriangle(void)
441         {
442                 __lineWidth = 1;
443         }
444         // A Destructor
445         virtual ~_CanvasTriangle(void) { }
446         // Sets the line width
447         bool SetLineWidth(int lineWidth);
448         // Gets the line width.
449         int GetLineWidth(void) const;
450         // Draws a triangle.
451         result DrawTriangle(const _GpPoint& pt1, const _GpPoint& pt2, const _GpPoint& pt3, const _GpBufferInfo& bufInfo);
452         // Draws the filled triangle.
453         result FillTriangle(int vertexX1, int vertexY1, int vertexX2, int vertexY2, int vertexX3, int vertexY3, const _GpBufferInfo& bufInfo) const;
454
455 private:
456         bool __FillTriangleEx(int x1, int y1, int x2, int y2, int x3, int y3, const _GpBufferInfo& bufInfo) const;
457
458 private:
459         int __lineWidth;
460 };
461
462
463 class _CanvasRoundRect
464 {
465 public:
466         // A Constructor
467         _CanvasRoundRect(void)
468         {
469                 __lineWidth = 1;
470         }
471         // A Destructor
472         virtual ~_CanvasRoundRect(void) { }
473         // Sets the line width
474         bool SetLineWidth(int lineWidth);
475         // Gets the line width.
476         int GetLineWidth(void) const;
477         // Draws a roundRect.
478         result DrawRoundRect(int rectX, int rectY, int rectW, int rectH, int radiusX, int radiusY, const _GpBufferInfo& bufInfo);
479         // Draws the roundRect.
480         result FillRoundRectangle(int rectX, int rectY, int rectW, int rectH, int radiusX, int radiusY, const _GpBufferInfo& bufInfo) const;
481
482 private:
483         bool __Draw32BitsRoundRect(int rectX, int rectY, int rectW, int rectH, int radiusX, int radiusY, const _GpBufferInfo& bufInfo);
484         int __MakeEllipseArcPointArrayForRoundRectN(_GpFloatPoint** pPoint, _GpFloatPoint** pNorm, float x, float y, float w, float h, int a1, int a2, int rot) const;
485
486 private:
487         int __lineWidth;
488 };
489
490 }} // Tizen::Graphics
491
492 #endif //_FGRP_INTERNAL_CANVAS_GP_PRIMITIVE_H_