OLD | NEW |
(Empty) | |
| 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 #include "SkPatchGrid.h" |
| 9 #include "SkPatchUtils.h" |
| 10 |
| 11 SkPatchGrid::SkPatchGrid(int rows, int cols) |
| 12 : fRows(rows) |
| 13 , fCols(cols) |
| 14 , fCornerPts(NULL) |
| 15 , fCornerColors(NULL) |
| 16 , fHrzCtrlPts(NULL) |
| 17 , fVrtCtrlPts(NULL) { |
| 18 this->reset(fRows, fCols); |
| 19 } |
| 20 |
| 21 SkPatchGrid::~SkPatchGrid() { |
| 22 SkDELETE_ARRAY(fCornerPts); |
| 23 SkDELETE_ARRAY(fCornerColors); |
| 24 SkDELETE_ARRAY(fHrzCtrlPts); |
| 25 SkDELETE_ARRAY(fVrtCtrlPts); |
| 26 } |
| 27 |
| 28 bool SkPatchGrid::setPatch(int x, int y, const SkPoint ctrlPts[12], const SkColo
r colors[4]) { |
| 29 // Check for the passed paramaters to be within the range of the mesh gradie
nt dimensions. |
| 30 if (x < 0 || y < 0 || x > fCols - 1 || y > fRows - 1) { |
| 31 return false; |
| 32 } |
| 33 |
| 34 //setup corners and colors |
| 35 int cornerPos = y * (fCols + 1) + x; |
| 36 fCornerPts[cornerPos] = ctrlPts[SkPatch::kTopP0_CubicCtrlPts]; |
| 37 fCornerPts[cornerPos + 1] = ctrlPts[SkPatch::kTopP3_CubicCtrlPts]; |
| 38 fCornerPts[cornerPos + (fCols + 1)] = ctrlPts[SkPatch::kBottomP0_CubicCtrlPt
s]; |
| 39 fCornerPts[cornerPos + (fCols + 1) + 1] = ctrlPts[SkPatch::kBottomP3_CubicCt
rlPts]; |
| 40 |
| 41 fCornerColors[cornerPos] = colors[0]; |
| 42 fCornerColors[cornerPos + 1] = colors[1]; |
| 43 fCornerColors[cornerPos + (fCols + 1)] = colors[3]; |
| 44 fCornerColors[cornerPos + (fCols + 1) + 1] = colors[2]; |
| 45 |
| 46 // set horizontal control points |
| 47 int hrzPos = y * (fCols * 2) + (x * 2); |
| 48 fHrzCtrlPts[hrzPos] = ctrlPts[SkPatch::kTopP1_CubicCtrlPts]; |
| 49 fHrzCtrlPts[hrzPos + 1] = ctrlPts[SkPatch::kTopP2_CubicCtrlPts]; |
| 50 fHrzCtrlPts[hrzPos + (fCols * 2)] = ctrlPts[SkPatch::kBottomP1_CubicCtrlPts]
; |
| 51 fHrzCtrlPts[hrzPos + (fCols * 2) + 1] = ctrlPts[SkPatch::kBottomP2_CubicCtrl
Pts]; |
| 52 |
| 53 int vrtPos = (y*2) * (fCols + 1) + x; |
| 54 fVrtCtrlPts[vrtPos] = ctrlPts[SkPatch::kLeftP1_CubicCtrlPts]; |
| 55 fVrtCtrlPts[vrtPos + 1] = ctrlPts[SkPatch::kRightP1_CubicCtrlPts]; |
| 56 fVrtCtrlPts[vrtPos + (fCols + 1)] = ctrlPts[SkPatch::kLeftP2_CubicCtrlPts]; |
| 57 fVrtCtrlPts[vrtPos + (fCols + 1) + 1] = ctrlPts[SkPatch::kRightP2_CubicCtrlP
ts]; |
| 58 return true; |
| 59 } |
| 60 |
| 61 bool SkPatchGrid::setPatch(int x, int y, const SkPatch& patch) { |
| 62 return this->setPatch(x, y, patch.getControlPoints(), patch.getColors()); |
| 63 } |
| 64 |
| 65 void SkPatchGrid::getPatch(int x, int y, SkPatch* patch) const { |
| 66 |
| 67 if (x < 0 || y < 0 || x > fCols - 1 || y > fRows - 1) { |
| 68 return; |
| 69 } |
| 70 |
| 71 if (NULL != patch) { |
| 72 //set the patch by building the array of points and colors with the corr
esponding values. |
| 73 |
| 74 SkPoint ctrlPts[12]; |
| 75 SkColor colors[4]; |
| 76 |
| 77 int cornerPos = y * (fCols + 1) + x; |
| 78 ctrlPts[SkPatch::kTopP0_CubicCtrlPts] = fCornerPts[cornerPos]; |
| 79 ctrlPts[SkPatch::kTopP3_CubicCtrlPts] = fCornerPts[cornerPos + 1]; |
| 80 ctrlPts[SkPatch::kBottomP0_CubicCtrlPts] = fCornerPts[cornerPos + (fCols
+ 1)]; |
| 81 ctrlPts[SkPatch::kBottomP3_CubicCtrlPts] = fCornerPts[cornerPos + (fCols
+ 1) + 1]; |
| 82 |
| 83 colors[0] = fCornerColors[cornerPos]; |
| 84 colors[1] = fCornerColors[cornerPos + 1]; |
| 85 colors[3] = fCornerColors[cornerPos + (fCols + 1)]; |
| 86 colors[2] = fCornerColors[cornerPos + (fCols + 1) + 1]; |
| 87 |
| 88 int hrzPos = y * (fCols * 2) + (x * 2); |
| 89 ctrlPts[SkPatch::kTopP1_CubicCtrlPts] = fHrzCtrlPts[hrzPos]; |
| 90 ctrlPts[SkPatch::kTopP2_CubicCtrlPts] = fHrzCtrlPts[hrzPos + 1]; |
| 91 ctrlPts[SkPatch::kBottomP1_CubicCtrlPts] = fHrzCtrlPts[hrzPos + (fCols *
2)]; |
| 92 ctrlPts[SkPatch::kBottomP2_CubicCtrlPts] = fHrzCtrlPts[hrzPos + (fCols *
2) + 1]; |
| 93 |
| 94 int vrtPos = (y*2) * (fCols + 1) + x; |
| 95 ctrlPts[SkPatch::kLeftP1_CubicCtrlPts] = fVrtCtrlPts[vrtPos]; |
| 96 ctrlPts[SkPatch::kRightP1_CubicCtrlPts] = fVrtCtrlPts[vrtPos + 1]; |
| 97 ctrlPts[SkPatch::kLeftP2_CubicCtrlPts] = fVrtCtrlPts[vrtPos + (fCols + 1
)]; |
| 98 ctrlPts[SkPatch::kRightP2_CubicCtrlPts] = fVrtCtrlPts[vrtPos + (fCols +
1) + 1]; |
| 99 |
| 100 patch->reset(ctrlPts, colors); |
| 101 } |
| 102 } |
| 103 |
| 104 void SkPatchGrid::reset(int rows, int cols) { |
| 105 SkDELETE_ARRAY(fCornerPts); |
| 106 SkDELETE_ARRAY(fCornerColors); |
| 107 SkDELETE_ARRAY(fHrzCtrlPts); |
| 108 SkDELETE_ARRAY(fVrtCtrlPts); |
| 109 |
| 110 fCols = cols; |
| 111 fRows = rows; |
| 112 |
| 113 fCornerPts = SkNEW_ARRAY(SkPoint, (fRows + 1) * (fCols + 1)); |
| 114 fCornerColors = SkNEW_ARRAY(SkColor, (fRows + 1) * (fCols + 1)); |
| 115 fHrzCtrlPts = SkNEW_ARRAY(SkPoint, (fRows + 1) * fCols * 2); |
| 116 fVrtCtrlPts = SkNEW_ARRAY(SkPoint, fRows * 2 * (fCols + 1)); |
| 117 |
| 118 memset(fCornerPts, 0, (fRows + 1) * (fCols + 1) * sizeof(SkPoint)); |
| 119 memset(fCornerColors, 0, (fRows + 1) * (fCols + 1) * sizeof(SkColor)); |
| 120 memset(fHrzCtrlPts, 0, (fRows + 1) * fCols * 2 * sizeof(SkPoint)); |
| 121 memset(fVrtCtrlPts, 0, fRows * 2 * (fCols + 1) * sizeof(SkPoint)); |
| 122 } |
| 123 |
| 124 void SkPatchGrid::draw(SkCanvas* canvas, SkPaint& paint) { |
| 125 int* maxCols = SkNEW_ARRAY(int, fCols); |
| 126 int* maxRows = SkNEW_ARRAY(int, fRows); |
| 127 memset(maxCols, 0, fCols * sizeof(int)); |
| 128 memset(maxRows, 0, fRows * sizeof(int)); |
| 129 |
| 130 // Get the maximum level of detail per axis for each row and column |
| 131 for (int y = 0; y < fRows; y++) { |
| 132 for (int x = 0; x < fCols; x++) { |
| 133 SkPatch patch; |
| 134 this->getPatch(x, y, &patch); |
| 135 SkMatrix matrix = canvas->getTotalMatrix(); |
| 136 SkISize lod = SkPatchUtils::GetLevelOfDetail(patch, &matrix); |
| 137 maxCols[x] = SkMax32(maxCols[x], lod.width()); |
| 138 maxRows[y] = SkMax32(maxRows[y], lod.height()); |
| 139 } |
| 140 } |
| 141 // Draw the patches by generating their mesh with the maximum level of detai
l per axis. |
| 142 for (int y = 0; y < fRows; y++) { |
| 143 for (int x = 0; x < fCols; x++) { |
| 144 SkPatch patch; |
| 145 this->getPatch(x, y, &patch); |
| 146 SkPatch::VertexData data; |
| 147 patch.getVertexData(&data, maxCols[x], maxRows[y]); |
| 148 canvas->drawVertices(SkCanvas::kTriangles_VertexMode, data.fVertexCo
unt, |
| 149 data.fPoints, data.fTexCoords, data.fColors, NU
LL, |
| 150 data.fIndices, data.fIndexCount, paint); |
| 151 } |
| 152 } |
| 153 SkDELETE_ARRAY(maxCols); |
| 154 SkDELETE_ARRAY(maxRows); |
| 155 } |
OLD | NEW |