blob: 67ab621e8e9a38bd724beae03cf6a233dc134a9b [file] [log] [blame]
dandovecfff212014-08-04 10:02:00 -07001/*
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
dandovb3c9d1c2014-08-12 08:34:29 -070011#include "SkColorPriv.h"
dandovecfff212014-08-04 10:02:00 -070012#include "SkMatrix.h"
13
14class SK_API SkPatchUtils {
halcanary9d524f22016-03-29 09:03:52 -070015
dandovecfff212014-08-04 10:02:00 -070016public:
17 /**
dandovb3c9d1c2014-08-12 08:34:29 -070018 * Structure that holds the vertex data related to the tessellation of a patch. It is passed
19 * as a parameter to the function getVertexData which sets the points, colors and texture
20 * coordinates of the vertices and the indices for them to be drawn as triangles.
21 */
22 struct VertexData {
23 int fVertexCount, fIndexCount;
24 SkPoint* fPoints;
25 SkPoint* fTexCoords;
26 uint32_t* fColors;
27 uint16_t* fIndices;
halcanary9d524f22016-03-29 09:03:52 -070028
dandovb3c9d1c2014-08-12 08:34:29 -070029 VertexData()
30 : fVertexCount(0)
31 , fIndexCount(0)
halcanary96fcdcc2015-08-27 07:41:13 -070032 , fPoints(nullptr)
33 , fTexCoords(nullptr)
34 , fColors(nullptr)
35 , fIndices(nullptr) { }
halcanary9d524f22016-03-29 09:03:52 -070036
dandovb3c9d1c2014-08-12 08:34:29 -070037 ~VertexData() {
halcanary385fe4d2015-08-26 13:07:48 -070038 delete[] fPoints;
39 delete[] fTexCoords;
40 delete[] fColors;
41 delete[] fIndices;
dandovb3c9d1c2014-08-12 08:34:29 -070042 }
43 };
halcanary9d524f22016-03-29 09:03:52 -070044
dandovb3c9d1c2014-08-12 08:34:29 -070045 // Enums for control points based on the order specified in the constructor (clockwise).
46 enum CubicCtrlPts {
47 kTopP0_CubicCtrlPts = 0,
48 kTopP1_CubicCtrlPts = 1,
49 kTopP2_CubicCtrlPts = 2,
50 kTopP3_CubicCtrlPts = 3,
halcanary9d524f22016-03-29 09:03:52 -070051
dandovb3c9d1c2014-08-12 08:34:29 -070052 kRightP0_CubicCtrlPts = 3,
53 kRightP1_CubicCtrlPts = 4,
54 kRightP2_CubicCtrlPts = 5,
55 kRightP3_CubicCtrlPts = 6,
halcanary9d524f22016-03-29 09:03:52 -070056
dandovb3c9d1c2014-08-12 08:34:29 -070057 kBottomP0_CubicCtrlPts = 9,
58 kBottomP1_CubicCtrlPts = 8,
59 kBottomP2_CubicCtrlPts = 7,
60 kBottomP3_CubicCtrlPts = 6,
halcanary9d524f22016-03-29 09:03:52 -070061
dandovb3c9d1c2014-08-12 08:34:29 -070062 kLeftP0_CubicCtrlPts = 0,
63 kLeftP1_CubicCtrlPts = 11,
64 kLeftP2_CubicCtrlPts = 10,
65 kLeftP3_CubicCtrlPts = 9,
66 };
halcanary9d524f22016-03-29 09:03:52 -070067
dandovb3c9d1c2014-08-12 08:34:29 -070068 // Enum for corner also clockwise.
69 enum Corner {
70 kTopLeft_Corner = 0,
71 kTopRight_Corner,
72 kBottomRight_Corner,
73 kBottomLeft_Corner
74 };
halcanary9d524f22016-03-29 09:03:52 -070075
dandovb3c9d1c2014-08-12 08:34:29 -070076 enum {
77 kNumCtrlPts = 12,
78 kNumCorners = 4,
79 kNumPtsCubic = 4
80 };
halcanary9d524f22016-03-29 09:03:52 -070081
dandovb3c9d1c2014-08-12 08:34:29 -070082 /**
halcanary9d524f22016-03-29 09:03:52 -070083 * Method that calculates a level of detail (number of subdivisions) for a patch in both axis.
dandovecfff212014-08-04 10:02:00 -070084 */
dandovb3c9d1c2014-08-12 08:34:29 -070085 static SkISize GetLevelOfDetail(const SkPoint cubics[12], const SkMatrix* matrix);
halcanary9d524f22016-03-29 09:03:52 -070086
dandovb3c9d1c2014-08-12 08:34:29 -070087 /**
88 * Get the points corresponding to the top cubic of cubics.
89 */
90 static void getTopCubic(const SkPoint cubics[12], SkPoint points[4]);
halcanary9d524f22016-03-29 09:03:52 -070091
dandovb3c9d1c2014-08-12 08:34:29 -070092 /**
93 * Get the points corresponding to the bottom cubic of cubics.
94 */
95 static void getBottomCubic(const SkPoint cubics[12], SkPoint points[4]);
halcanary9d524f22016-03-29 09:03:52 -070096
dandovb3c9d1c2014-08-12 08:34:29 -070097 /**
98 * Get the points corresponding to the left cubic of cubics.
99 */
100 static void getLeftCubic(const SkPoint cubics[12], SkPoint points[4]);
halcanary9d524f22016-03-29 09:03:52 -0700101
dandovb3c9d1c2014-08-12 08:34:29 -0700102 /**
103 * Get the points corresponding to the right cubic of cubics.
104 */
105 static void getRightCubic(const SkPoint cubics[12], SkPoint points[4]);
halcanary9d524f22016-03-29 09:03:52 -0700106
dandovb3c9d1c2014-08-12 08:34:29 -0700107 /**
108 * Function that evaluates the coons patch interpolation.
109 * data refers to the pointer of the PatchData struct in which the tessellation data is set.
110 * cubics refers to the points of the cubics.
111 * lod refers the level of detail for each axis.
112 * colors refers to the corner colors that will be bilerp across the patch (optional parameter)
halcanary9d524f22016-03-29 09:03:52 -0700113 * texCoords refers to the corner texture coordinates that will be bilerp across the patch
dandovb3c9d1c2014-08-12 08:34:29 -0700114 (optional parameter)
115 */
116 static bool getVertexData(SkPatchUtils::VertexData* data, const SkPoint cubics[12],
117 const SkColor colors[4], const SkPoint texCoords[4],
118 int lodX, int lodY);
dandovecfff212014-08-04 10:02:00 -0700119};
120
121#endif