1 /*
2  * Copyright 2014 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkPatchUtils_DEFINED
9 #define SkPatchUtils_DEFINED
10 
11 #include "SkColorPriv.h"
12 #include "SkMatrix.h"
13 #include "SkVertices.h"
14 
15 class SK_API SkPatchUtils {
16 
17 public:
18     // DEPRECATED -- use MakeVertices()
19     /**
20      * Structure that holds the vertex data related to the tessellation of a patch. It is passed
21      * as a parameter to the function getVertexData which sets the points, colors and texture
22      * coordinates of the vertices and the indices for them to be drawn as triangles.
23      */
24     struct VertexData {
25         int fVertexCount, fIndexCount;
26         SkPoint* fPoints;
27         SkPoint* fTexCoords;
28         uint32_t* fColors;
29         uint16_t* fIndices;
30 
VertexDataVertexData31         VertexData()
32         : fVertexCount(0)
33         , fIndexCount(0)
34         , fPoints(nullptr)
35         , fTexCoords(nullptr)
36         , fColors(nullptr)
37         , fIndices(nullptr) { }
38 
~VertexDataVertexData39         ~VertexData() {
40             delete[] fPoints;
41             delete[] fTexCoords;
42             delete[] fColors;
43             delete[] fIndices;
44         }
45     };
46 
47     // Enums for control points based on the order specified in the constructor (clockwise).
48     enum CubicCtrlPts {
49         kTopP0_CubicCtrlPts = 0,
50         kTopP1_CubicCtrlPts = 1,
51         kTopP2_CubicCtrlPts = 2,
52         kTopP3_CubicCtrlPts = 3,
53 
54         kRightP0_CubicCtrlPts = 3,
55         kRightP1_CubicCtrlPts = 4,
56         kRightP2_CubicCtrlPts = 5,
57         kRightP3_CubicCtrlPts = 6,
58 
59         kBottomP0_CubicCtrlPts = 9,
60         kBottomP1_CubicCtrlPts = 8,
61         kBottomP2_CubicCtrlPts = 7,
62         kBottomP3_CubicCtrlPts = 6,
63 
64         kLeftP0_CubicCtrlPts = 0,
65         kLeftP1_CubicCtrlPts = 11,
66         kLeftP2_CubicCtrlPts = 10,
67         kLeftP3_CubicCtrlPts = 9,
68     };
69 
70     // Enum for corner also clockwise.
71     enum Corner {
72         kTopLeft_Corner = 0,
73         kTopRight_Corner,
74         kBottomRight_Corner,
75         kBottomLeft_Corner
76     };
77 
78     enum {
79         kNumCtrlPts = 12,
80         kNumCorners = 4,
81         kNumPtsCubic = 4
82     };
83 
84     /**
85      * Method that calculates a level of detail (number of subdivisions) for a patch in both axis.
86      */
87     static SkISize GetLevelOfDetail(const SkPoint cubics[12], const SkMatrix* matrix);
88 
89     /**
90      * Get the points corresponding to the top cubic of cubics.
91      */
92     static void getTopCubic(const SkPoint cubics[12], SkPoint points[4]);
93 
94     /**
95      * Get the points corresponding to the bottom cubic of cubics.
96      */
97     static void getBottomCubic(const SkPoint cubics[12], SkPoint points[4]);
98 
99     /**
100      * Get the points corresponding to the left cubic of cubics.
101      */
102     static void getLeftCubic(const SkPoint cubics[12], SkPoint points[4]);
103 
104     /**
105      * Get the points corresponding to the right cubic of cubics.
106      */
107     static void getRightCubic(const SkPoint cubics[12], SkPoint points[4]);
108 
109     // DEPRECATED -- use MakeVertices()
110     /**
111      * Function that evaluates the coons patch interpolation.
112      * data refers to the pointer of the PatchData struct in which the tessellation data is set.
113      * cubics refers to the points of the cubics.
114      * lod refers the level of detail for each axis.
115      * colors refers to the corner colors that will be bilerp across the patch (optional parameter)
116      * texCoords refers to the corner texture coordinates that will be bilerp across the patch
117         (optional parameter)
118      */
119     static bool getVertexData(SkPatchUtils::VertexData* data, const SkPoint cubics[12],
120                               const SkColor colors[4], const SkPoint texCoords[4],
121                               int lodX, int lodY);
122 
123     static sk_sp<SkVertices> MakeVertices(const SkPoint cubics[12], const SkColor colors[4],
124                                           const SkPoint texCoords[4], int lodX, int lodY);
125 };
126 
127 #endif
128