OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkPatchGrid_DEFINED | 8 #ifndef SkPatchGrid_DEFINED |
9 #define SkPatchGrid_DEFINED | 9 #define SkPatchGrid_DEFINED |
10 | 10 |
11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
12 #include "SkPatchUtils.h" | 12 #include "SkPatchUtils.h" |
13 #include "SkXfermode.h" | 13 #include "SkXfermode.h" |
14 | 14 |
15 /** | 15 /** |
16 * Class that represents a grid of patches. Adjacent patches share their corners
and a color is | 16 * Class that represents a grid of patches. Adjacent patches share their corners
and a color is |
17 * specified at each one of them. The colors are bilinearly interpolated across
the patch. | 17 * specified at each one of them. The colors are bilinearly interpolated across
the patch. |
18 * | 18 * |
19 * This implementation defines a bidimensional array of patches. There are 3 arr
ays to store the | 19 * This implementation defines a bidimensional array of patches. There are 3 arr
ays to store the |
20 * control points of the patches to avoid storing repeated data since there are
several points | 20 * control points of the patches to avoid storing repeated data since there are
several points |
21 * shared between adjacent patches. | 21 * shared between adjacent patches. |
22 * | 22 * |
23 * The array fCornerPts stores the corner control points of the patches. | 23 * The array fCornerPts stores the corner control points of the patches. |
24 * The array fHrzPts holds the intermidiate control points of the top and bottom
curves of a patch. | 24 * The array fHrzPts holds the intermidiate control points of the top and bottom
curves of a patch. |
25 * The array fVrtPts holds the intermidiate control points of the left and right
curves of a patch. | 25 * The array fVrtPts holds the intermidiate control points of the left and right
curves of a patch. |
26 * The array fCornerColors holds the corner colors in the same format as fCorner
Pts. | 26 * The array fCornerColors holds the corner colors in the same format as fCorner
Pts. |
27 * The array fTexCoords holds the texture coordinates in the same format as fCor
nerpts. | 27 * The array fTexCoords holds the texture coordinates in the same format as fCor
nerpts. |
28 * | 28 * |
29 * fCornerPts fHrzPts fVrtPts | 29 * fCornerPts fHrzPts fVrtPts |
30 * -------------- ------------------- -------------- | 30 * -------------- ------------------- -------------- |
31 * | C0 | C1 | C2 | | H0 | H1 | H2 | H3 | | V0 | V1 | V2 | | 31 * | C0 | C1 | C2 | | H0 | H1 | H2 | H3 | | V0 | V1 | V2 | |
(...skipping 15 matching lines...) Expand all Loading... |
47 * \| | |/ | 47 * \| | |/ |
48 * C3-H4-H5-C4-H6-H7-C5 | 48 * C3-H4-H5-C4-H6-H7-C5 |
49 * /| | |\ | 49 * /| | |\ |
50 * v6 | v7 | v8 | 50 * v6 | v7 | v8 |
51 * v9 | v10 | v11 | 51 * v9 | v10 | v11 |
52 * \| | |/ | 52 * \| | |/ |
53 * C6-------C7-------C8 | 53 * C6-------C7-------C8 |
54 * \ / \ / | 54 * \ / \ / |
55 * H8 H9 H10 H11 | 55 * H8 H9 H10 H11 |
56 * | 56 * |
57 * When trying to get a patch at a certain position it justs builds it with the
corresponding | 57 * When trying to get a patch at a certain position it justs builds it with the
corresponding |
58 * points. | 58 * points. |
59 * When adding a patch it tries to add the points at their corresponding positio
n trying to comply | 59 * When adding a patch it tries to add the points at their corresponding positio
n trying to comply |
60 * with the adjacent points or overwriting them. | 60 * with the adjacent points or overwriting them. |
61 * | 61 * |
62 * Based the idea on the SVG2 spec for mesh gradients in which a grid of patches
is build as in the | 62 * Based the idea on the SVG2 spec for mesh gradients in which a grid of patches
is build as in the |
63 * the following example: | 63 * the following example: |
64 * <meshGradient x="100" y="100"> | 64 * <meshGradient x="100" y="100"> |
65 * <meshRow> | 65 * <meshRow> |
66 * <meshPatch> | 66 * <meshPatch> |
67 * <stop .../> | 67 * <stop .../> |
68 * Up to four stops in first patch. See details below. | 68 * Up to four stops in first patch. See details below. |
69 * </meshPatch> | 69 * </meshPatch> |
70 * <meshPatch> | 70 * <meshPatch> |
71 * Any number of meshPatches in row. | 71 * Any number of meshPatches in row. |
72 * </meshPatch> | 72 * </meshPatch> |
73 * </meshRow> | 73 * </meshRow> |
74 * <meshRow> | 74 * <meshRow> |
75 * Any number of meshRows, each with the same number of meshPatches as
in the first row. | 75 * Any number of meshRows, each with the same number of meshPatches as
in the first row. |
76 * </meshRow> | 76 * </meshRow> |
77 * </meshGradient> | 77 * </meshGradient> |
78 */ | 78 */ |
79 class SkPatchGrid { | 79 class SkPatchGrid { |
80 | 80 |
81 public: | 81 public: |
82 | 82 |
83 enum VertexType { | 83 enum VertexType { |
84 kNone_VertexType = 0X00, | 84 kNone_VertexType = 0X00, |
85 kColors_VertexType = 0x01, | 85 kColors_VertexType = 0x01, |
86 kTexs_VertexType = 0x02, | 86 kTexs_VertexType = 0x02, |
87 kColorsAndTexs_VertexType = 0x03 | 87 kColorsAndTexs_VertexType = 0x03 |
88 }; | 88 }; |
89 | 89 |
90 SkPatchGrid(int rows = 0, int cols = 0, VertexType flags = kNone_VertexType, | 90 SkPatchGrid(int rows = 0, int cols = 0, VertexType flags = kNone_VertexType, |
91 SkXfermode* xfer = nullptr); | 91 SkXfermode* xfer = nullptr); |
92 | 92 |
93 ~SkPatchGrid(); | 93 ~SkPatchGrid(); |
94 | 94 |
95 /** | 95 /** |
96 * Add a patch at location (x,y) overwriting the previous patch and shared p
oints so they | 96 * Add a patch at location (x,y) overwriting the previous patch and shared p
oints so they |
97 * mantain C0 connectivity. | 97 * mantain C0 connectivity. |
98 * The control points must be passed in a clockwise order starting at the to
p left corner. | 98 * The control points must be passed in a clockwise order starting at the to
p left corner. |
99 * The colors and texCoords are the values at the corners of the patch which
will be bilerp | 99 * The colors and texCoords are the values at the corners of the patch which
will be bilerp |
100 * across it, they must also be in counterclockwise order starting at the to
p left corner. | 100 * across it, they must also be in counterclockwise order starting at the to
p left corner. |
101 */ | 101 */ |
102 bool setPatch(int x, int y, const SkPoint cubics[12], const SkColor colors[4
], | 102 bool setPatch(int x, int y, const SkPoint cubics[12], const SkColor colors[4
], |
103 const SkPoint texCoords[4]); | 103 const SkPoint texCoords[4]); |
104 | 104 |
105 /** | 105 /** |
106 * Get patch at location (x,y). If cubics, colors or texCoords is not nullpt
r it sets patch's | 106 * Get patch at location (x,y). If cubics, colors or texCoords is not nullpt
r it sets patch's |
107 * array with its corresponding values. | 107 * array with its corresponding values. |
108 * The function returns false if the cubics parameter is nullptr or if the (
x,y) coordinates are | 108 * The function returns false if the cubics parameter is nullptr or if the (
x,y) coordinates are |
109 * not within the range of the grid. | 109 * not within the range of the grid. |
110 */ | 110 */ |
111 bool getPatch(int x, int y, SkPoint cubics[12], SkColor colors[4], SkPoint t
exCoords[4]) const; | 111 bool getPatch(int x, int y, SkPoint cubics[12], SkColor colors[4], SkPoint t
exCoords[4]) const; |
112 | 112 |
113 /** | 113 /** |
114 * Resets the grid of patches to contain rows and cols of patches. | 114 * Resets the grid of patches to contain rows and cols of patches. |
115 */ | 115 */ |
116 void reset(int rows, int cols, VertexType flags, SkXfermode* xMode); | 116 void reset(int rows, int cols, VertexType flags, SkXfermode* xMode); |
117 | 117 |
118 /** | 118 /** |
119 * Draws the grid of patches. The patches are drawn starting at patch (0,0)
drawing columns, so | 119 * Draws the grid of patches. The patches are drawn starting at patch (0,0)
drawing columns, so |
120 * for a 2x2 grid the order would be (0,0)->(0,1)->(1,0)->(1,1). The order f
ollows the order | 120 * for a 2x2 grid the order would be (0,0)->(0,1)->(1,0)->(1,1). The order f
ollows the order |
121 * of the parametric coordinates of the coons patch. | 121 * of the parametric coordinates of the coons patch. |
122 */ | 122 */ |
123 void draw(SkCanvas* canvas, SkPaint& paint); | 123 void draw(SkCanvas* canvas, SkPaint& paint); |
124 | 124 |
125 /** | 125 /** |
126 * Get the dimensions of the grid of patches. | 126 * Get the dimensions of the grid of patches. |
127 */ | 127 */ |
128 SkISize getDimensions() const { | 128 SkISize getDimensions() const { |
129 return SkISize::Make(fCols, fRows); | 129 return SkISize::Make(fCols, fRows); |
130 } | 130 } |
131 | 131 |
132 private: | 132 private: |
133 int fRows, fCols; | 133 int fRows, fCols; |
134 VertexType fModeFlags; | 134 VertexType fModeFlags; |
135 SkPoint* fCornerPts; | 135 SkPoint* fCornerPts; |
136 SkColor* fCornerColors; | 136 SkColor* fCornerColors; |
137 SkPoint* fTexCoords; | 137 SkPoint* fTexCoords; |
138 SkPoint* fHrzCtrlPts; | 138 SkPoint* fHrzCtrlPts; |
139 SkPoint* fVrtCtrlPts; | 139 SkPoint* fVrtCtrlPts; |
140 SkXfermode* fXferMode; | 140 SkXfermode* fXferMode; |
141 }; | 141 }; |
142 | 142 |
143 | 143 |
144 #endif | 144 #endif |
OLD | NEW |