blob: c1e8ac1c2f69d83d799d69388f48156e823a51d5 [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"
Mike Reed795c5ea2017-03-17 14:29:05 -040013#include "SkVertices.h"
dandovecfff212014-08-04 10:02:00 -070014
15class SK_API SkPatchUtils {
halcanary9d524f22016-03-29 09:03:52 -070016
dandovecfff212014-08-04 10:02:00 -070017public:
Mike Reed795c5ea2017-03-17 14:29:05 -040018 // DEPRECATED -- use MakeVertices()
dandovecfff212014-08-04 10:02:00 -070019 /**
dandovb3c9d1c2014-08-12 08:34:29 -070020 * 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;
halcanary9d524f22016-03-29 09:03:52 -070030
dandovb3c9d1c2014-08-12 08:34:29 -070031 VertexData()
32 : fVertexCount(0)
33 , fIndexCount(0)
halcanary96fcdcc2015-08-27 07:41:13 -070034 , fPoints(nullptr)
35 , fTexCoords(nullptr)
36 , fColors(nullptr)
37 , fIndices(nullptr) { }
halcanary9d524f22016-03-29 09:03:52 -070038
dandovb3c9d1c2014-08-12 08:34:29 -070039 ~VertexData() {
halcanary385fe4d2015-08-26 13:07:48 -070040 delete[] fPoints;
41 delete[] fTexCoords;
42 delete[] fColors;
43 delete[] fIndices;
dandovb3c9d1c2014-08-12 08:34:29 -070044 }
45 };
halcanary9d524f22016-03-29 09:03:52 -070046
dandovb3c9d1c2014-08-12 08:34:29 -070047 // 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,
halcanary9d524f22016-03-29 09:03:52 -070053
dandovb3c9d1c2014-08-12 08:34:29 -070054 kRightP0_CubicCtrlPts = 3,
55 kRightP1_CubicCtrlPts = 4,
56 kRightP2_CubicCtrlPts = 5,
57 kRightP3_CubicCtrlPts = 6,
halcanary9d524f22016-03-29 09:03:52 -070058
dandovb3c9d1c2014-08-12 08:34:29 -070059 kBottomP0_CubicCtrlPts = 9,
60 kBottomP1_CubicCtrlPts = 8,
61 kBottomP2_CubicCtrlPts = 7,
62 kBottomP3_CubicCtrlPts = 6,
halcanary9d524f22016-03-29 09:03:52 -070063
dandovb3c9d1c2014-08-12 08:34:29 -070064 kLeftP0_CubicCtrlPts = 0,
65 kLeftP1_CubicCtrlPts = 11,
66 kLeftP2_CubicCtrlPts = 10,
67 kLeftP3_CubicCtrlPts = 9,
68 };
halcanary9d524f22016-03-29 09:03:52 -070069
dandovb3c9d1c2014-08-12 08:34:29 -070070 // Enum for corner also clockwise.
71 enum Corner {
72 kTopLeft_Corner = 0,
73 kTopRight_Corner,
74 kBottomRight_Corner,
75 kBottomLeft_Corner
76 };
halcanary9d524f22016-03-29 09:03:52 -070077
dandovb3c9d1c2014-08-12 08:34:29 -070078 enum {
79 kNumCtrlPts = 12,
80 kNumCorners = 4,
81 kNumPtsCubic = 4
82 };
halcanary9d524f22016-03-29 09:03:52 -070083
dandovb3c9d1c2014-08-12 08:34:29 -070084 /**
halcanary9d524f22016-03-29 09:03:52 -070085 * Method that calculates a level of detail (number of subdivisions) for a patch in both axis.
dandovecfff212014-08-04 10:02:00 -070086 */
dandovb3c9d1c2014-08-12 08:34:29 -070087 static SkISize GetLevelOfDetail(const SkPoint cubics[12], const SkMatrix* matrix);
halcanary9d524f22016-03-29 09:03:52 -070088
dandovb3c9d1c2014-08-12 08:34:29 -070089 /**
90 * Get the points corresponding to the top cubic of cubics.
91 */
92 static void getTopCubic(const SkPoint cubics[12], SkPoint points[4]);
halcanary9d524f22016-03-29 09:03:52 -070093
dandovb3c9d1c2014-08-12 08:34:29 -070094 /**
95 * Get the points corresponding to the bottom cubic of cubics.
96 */
97 static void getBottomCubic(const SkPoint cubics[12], SkPoint points[4]);
halcanary9d524f22016-03-29 09:03:52 -070098
dandovb3c9d1c2014-08-12 08:34:29 -070099 /**
100 * Get the points corresponding to the left cubic of cubics.
101 */
102 static void getLeftCubic(const SkPoint cubics[12], SkPoint points[4]);
halcanary9d524f22016-03-29 09:03:52 -0700103
dandovb3c9d1c2014-08-12 08:34:29 -0700104 /**
105 * Get the points corresponding to the right cubic of cubics.
106 */
107 static void getRightCubic(const SkPoint cubics[12], SkPoint points[4]);
halcanary9d524f22016-03-29 09:03:52 -0700108
Mike Reed795c5ea2017-03-17 14:29:05 -0400109 // DEPRECATED -- use MakeVertices()
dandovb3c9d1c2014-08-12 08:34:29 -0700110 /**
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)
halcanary9d524f22016-03-29 09:03:52 -0700116 * texCoords refers to the corner texture coordinates that will be bilerp across the patch
dandovb3c9d1c2014-08-12 08:34:29 -0700117 (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);
Mike Reed795c5ea2017-03-17 14:29:05 -0400122
123 static sk_sp<SkVertices> MakeVertices(const SkPoint cubics[12], const SkColor colors[4],
124 const SkPoint texCoords[4], int lodX, int lodY);
dandovecfff212014-08-04 10:02:00 -0700125};
126
127#endif