cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 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 "gl/GrGLPathRendering.h" |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 9 | #include "gl/GrGLNameAllocator.h" |
| 10 | #include "gl/GrGLUtil.h" |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 11 | #include "gl/GrGpuGL.h" |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 12 | |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 13 | #include "GrGLPath.h" |
| 14 | #include "GrGLPathRange.h" |
| 15 | #include "GrGLPathRendering.h" |
| 16 | |
| 17 | #define GL_CALL(X) GR_GL_CALL(fGpu->glInterface(), X) |
| 18 | #define GL_CALL_RET(RET, X) GR_GL_CALL_RET(fGpu->glInterface(), RET, X) |
| 19 | |
| 20 | |
| 21 | static const GrGLenum gXformType2GLType[] = { |
| 22 | GR_GL_NONE, |
| 23 | GR_GL_TRANSLATE_X, |
| 24 | GR_GL_TRANSLATE_Y, |
| 25 | GR_GL_TRANSLATE_2D, |
| 26 | GR_GL_TRANSPOSE_AFFINE_2D |
| 27 | }; |
| 28 | |
| 29 | GR_STATIC_ASSERT(0 == GrPathRendering::kNone_PathTransformType); |
| 30 | GR_STATIC_ASSERT(1 == GrPathRendering::kTranslateX_PathTransformType); |
| 31 | GR_STATIC_ASSERT(2 == GrPathRendering::kTranslateY_PathTransformType); |
| 32 | GR_STATIC_ASSERT(3 == GrPathRendering::kTranslate_PathTransformType); |
| 33 | GR_STATIC_ASSERT(4 == GrPathRendering::kAffine_PathTransformType); |
| 34 | GR_STATIC_ASSERT(GrPathRendering::kAffine_PathTransformType == GrPathRendering::kLast_PathTransformType); |
| 35 | |
| 36 | static GrGLenum gr_stencil_op_to_gl_path_rendering_fill_mode(GrStencilOp op) { |
| 37 | switch (op) { |
| 38 | default: |
| 39 | SkFAIL("Unexpected path fill."); |
| 40 | /* fallthrough */; |
| 41 | case kIncClamp_StencilOp: |
| 42 | return GR_GL_COUNT_UP; |
| 43 | case kInvert_StencilOp: |
| 44 | return GR_GL_INVERT; |
| 45 | } |
| 46 | } |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 47 | |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 48 | GrGLPathRendering::GrGLPathRendering(GrGpuGL* gpu) |
| 49 | : fGpu(gpu) { |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 50 | const GrGLInterface* glInterface = gpu->glInterface(); |
| 51 | fCaps.stencilThenCoverSupport = |
cdalton | 149b3ec | 2014-09-17 09:19:18 -0700 | [diff] [blame^] | 52 | NULL != glInterface->fFunctions.fStencilThenCoverFillPath && |
| 53 | NULL != glInterface->fFunctions.fStencilThenCoverStrokePath && |
| 54 | NULL != glInterface->fFunctions.fStencilThenCoverFillPathInstanced && |
| 55 | NULL != glInterface->fFunctions.fStencilThenCoverStrokePathInstanced; |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 56 | fCaps.fragmentInputGenSupport = |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 57 | kGLES_GrGLStandard == glInterface->fStandard && |
cdalton | 149b3ec | 2014-09-17 09:19:18 -0700 | [diff] [blame^] | 58 | NULL != glInterface->fFunctions.fProgramPathFragmentInputGen; |
| 59 | fCaps.glyphLoadingSupport = |
| 60 | NULL != glInterface->fFunctions.fPathMemoryGlyphIndexArray; |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 61 | |
| 62 | if (!fCaps.fragmentInputGenSupport) { |
| 63 | fHWPathTexGenSettings.reset(fGpu->glCaps().maxFixedFunctionTextureCoords()); |
| 64 | } |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | GrGLPathRendering::~GrGLPathRendering() { |
| 68 | } |
| 69 | |
| 70 | void GrGLPathRendering::abandonGpuResources() { |
| 71 | fPathNameAllocator.reset(NULL); |
| 72 | } |
| 73 | |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 74 | void GrGLPathRendering::resetContext() { |
| 75 | fHWProjectionMatrixState.invalidate(); |
| 76 | // we don't use the model view matrix. |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 77 | GrGLenum matrixMode = |
| 78 | fGpu->glStandard() == kGLES_GrGLStandard ? GR_GL_PATH_MODELVIEW : GR_GL_MODELVIEW; |
| 79 | GL_CALL(MatrixLoadIdentity(matrixMode)); |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 80 | |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 81 | if (!caps().fragmentInputGenSupport) { |
| 82 | for (int i = 0; i < fGpu->glCaps().maxFixedFunctionTextureCoords(); ++i) { |
| 83 | GL_CALL(PathTexGen(GR_GL_TEXTURE0 + i, GR_GL_NONE, 0, NULL)); |
| 84 | fHWPathTexGenSettings[i].fMode = GR_GL_NONE; |
| 85 | fHWPathTexGenSettings[i].fNumComponents = 0; |
| 86 | } |
| 87 | fHWActivePathTexGenSets = 0; |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 88 | } |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 89 | fHWPathStencilSettings.invalidate(); |
| 90 | } |
| 91 | |
| 92 | GrPath* GrGLPathRendering::createPath(const SkPath& inPath, const SkStrokeRec& stroke) { |
| 93 | return SkNEW_ARGS(GrGLPath, (fGpu, inPath, stroke)); |
| 94 | } |
| 95 | |
| 96 | GrPathRange* GrGLPathRendering::createPathRange(size_t size, const SkStrokeRec& stroke) { |
| 97 | return SkNEW_ARGS(GrGLPathRange, (fGpu, size, stroke)); |
| 98 | } |
| 99 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 100 | void GrGLPathRendering::stencilPath(const GrPath* path, SkPath::FillType fill) { |
| 101 | GrGLuint id = static_cast<const GrGLPath*>(path)->pathID(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 102 | SkASSERT(fGpu->drawState()->getRenderTarget()); |
| 103 | SkASSERT(fGpu->drawState()->getRenderTarget()->getStencilBuffer()); |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 104 | |
| 105 | this->flushPathStencilSettings(fill); |
| 106 | SkASSERT(!fHWPathStencilSettings.isTwoSided()); |
| 107 | |
| 108 | GrGLenum fillMode = |
| 109 | gr_stencil_op_to_gl_path_rendering_fill_mode(fHWPathStencilSettings.passOp(GrStencilSettings::kFront_Face)); |
| 110 | GrGLint writeMask = fHWPathStencilSettings.writeMask(GrStencilSettings::kFront_Face); |
| 111 | GL_CALL(StencilFillPath(id, fillMode, writeMask)); |
| 112 | } |
| 113 | |
| 114 | void GrGLPathRendering::drawPath(const GrPath* path, SkPath::FillType fill) { |
| 115 | GrGLuint id = static_cast<const GrGLPath*>(path)->pathID(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 116 | SkASSERT(fGpu->drawState()->getRenderTarget()); |
| 117 | SkASSERT(fGpu->drawState()->getRenderTarget()->getStencilBuffer()); |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 118 | |
| 119 | this->flushPathStencilSettings(fill); |
| 120 | SkASSERT(!fHWPathStencilSettings.isTwoSided()); |
| 121 | |
| 122 | const SkStrokeRec& stroke = path->getStroke(); |
| 123 | |
| 124 | SkPath::FillType nonInvertedFill = SkPath::ConvertToNonInverseFillType(fill); |
| 125 | |
| 126 | GrGLenum fillMode = |
| 127 | gr_stencil_op_to_gl_path_rendering_fill_mode(fHWPathStencilSettings.passOp(GrStencilSettings::kFront_Face)); |
| 128 | GrGLint writeMask = fHWPathStencilSettings.writeMask(GrStencilSettings::kFront_Face); |
| 129 | |
| 130 | if (nonInvertedFill == fill) { |
| 131 | if (stroke.needToApply()) { |
| 132 | if (SkStrokeRec::kStrokeAndFill_Style == stroke.getStyle()) { |
| 133 | GL_CALL(StencilFillPath(id, fillMode, writeMask)); |
| 134 | } |
| 135 | this->stencilThenCoverStrokePath(id, 0xffff, writeMask, GR_GL_BOUNDING_BOX); |
| 136 | } else { |
| 137 | this->stencilThenCoverFillPath(id, fillMode, writeMask, GR_GL_BOUNDING_BOX); |
| 138 | } |
| 139 | } else { |
| 140 | if (stroke.isFillStyle() || SkStrokeRec::kStrokeAndFill_Style == stroke.getStyle()) { |
| 141 | GL_CALL(StencilFillPath(id, fillMode, writeMask)); |
| 142 | } |
| 143 | if (stroke.needToApply()) { |
| 144 | GL_CALL(StencilStrokePath(id, 0xffff, writeMask)); |
| 145 | } |
| 146 | |
| 147 | GrDrawState* drawState = fGpu->drawState(); |
| 148 | GrDrawState::AutoViewMatrixRestore avmr; |
| 149 | SkRect bounds = SkRect::MakeLTRB(0, 0, |
| 150 | SkIntToScalar(drawState->getRenderTarget()->width()), |
| 151 | SkIntToScalar(drawState->getRenderTarget()->height())); |
| 152 | SkMatrix vmi; |
| 153 | // mapRect through persp matrix may not be correct |
| 154 | if (!drawState->getViewMatrix().hasPerspective() && drawState->getViewInverse(&vmi)) { |
| 155 | vmi.mapRect(&bounds); |
| 156 | // theoretically could set bloat = 0, instead leave it because of matrix inversion |
| 157 | // precision. |
| 158 | SkScalar bloat = drawState->getViewMatrix().getMaxScale() * SK_ScalarHalf; |
| 159 | bounds.outset(bloat, bloat); |
| 160 | } else { |
| 161 | avmr.setIdentity(drawState); |
| 162 | } |
| 163 | |
| 164 | fGpu->drawSimpleRect(bounds); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void GrGLPathRendering::drawPaths(const GrPathRange* pathRange, const uint32_t indices[], int count, |
| 169 | const float transforms[], PathTransformType transformsType, |
| 170 | SkPath::FillType fill) { |
| 171 | SkASSERT(fGpu->caps()->pathRenderingSupport()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 172 | SkASSERT(fGpu->drawState()->getRenderTarget()); |
| 173 | SkASSERT(fGpu->drawState()->getRenderTarget()->getStencilBuffer()); |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 174 | |
| 175 | GrGLuint baseID = static_cast<const GrGLPathRange*>(pathRange)->basePathID(); |
| 176 | |
| 177 | this->flushPathStencilSettings(fill); |
| 178 | SkASSERT(!fHWPathStencilSettings.isTwoSided()); |
| 179 | |
| 180 | const SkStrokeRec& stroke = pathRange->getStroke(); |
| 181 | |
| 182 | SkPath::FillType nonInvertedFill = |
| 183 | SkPath::ConvertToNonInverseFillType(fill); |
| 184 | |
| 185 | GrGLenum fillMode = |
| 186 | gr_stencil_op_to_gl_path_rendering_fill_mode( |
| 187 | fHWPathStencilSettings.passOp(GrStencilSettings::kFront_Face)); |
| 188 | GrGLint writeMask = |
| 189 | fHWPathStencilSettings.writeMask(GrStencilSettings::kFront_Face); |
| 190 | |
| 191 | if (nonInvertedFill == fill) { |
| 192 | if (stroke.needToApply()) { |
| 193 | if (SkStrokeRec::kStrokeAndFill_Style == stroke.getStyle()) { |
| 194 | GL_CALL(StencilFillPathInstanced( |
| 195 | count, GR_GL_UNSIGNED_INT, indices, baseID, fillMode, |
| 196 | writeMask, gXformType2GLType[transformsType], |
| 197 | transforms)); |
| 198 | } |
| 199 | this->stencilThenCoverStrokePathInstanced( |
| 200 | count, GR_GL_UNSIGNED_INT, indices, baseID, 0xffff, writeMask, |
| 201 | GR_GL_BOUNDING_BOX_OF_BOUNDING_BOXES, |
| 202 | gXformType2GLType[transformsType], transforms); |
| 203 | } else { |
| 204 | this->stencilThenCoverFillPathInstanced( |
| 205 | count, GR_GL_UNSIGNED_INT, indices, baseID, fillMode, writeMask, |
| 206 | GR_GL_BOUNDING_BOX_OF_BOUNDING_BOXES, |
| 207 | gXformType2GLType[transformsType], transforms); |
| 208 | } |
| 209 | } else { |
| 210 | if (stroke.isFillStyle() || SkStrokeRec::kStrokeAndFill_Style == stroke.getStyle()) { |
| 211 | GL_CALL(StencilFillPathInstanced( |
| 212 | count, GR_GL_UNSIGNED_INT, indices, baseID, fillMode, |
| 213 | writeMask, gXformType2GLType[transformsType], |
| 214 | transforms)); |
| 215 | } |
| 216 | if (stroke.needToApply()) { |
| 217 | GL_CALL(StencilStrokePathInstanced( |
| 218 | count, GR_GL_UNSIGNED_INT, indices, baseID, 0xffff, |
| 219 | writeMask, gXformType2GLType[transformsType], |
| 220 | transforms)); |
| 221 | } |
| 222 | |
| 223 | GrDrawState* drawState = fGpu->drawState(); |
| 224 | GrDrawState::AutoViewMatrixRestore avmr; |
| 225 | SkRect bounds = SkRect::MakeLTRB(0, 0, |
| 226 | SkIntToScalar(drawState->getRenderTarget()->width()), |
| 227 | SkIntToScalar(drawState->getRenderTarget()->height())); |
| 228 | SkMatrix vmi; |
| 229 | // mapRect through persp matrix may not be correct |
| 230 | if (!drawState->getViewMatrix().hasPerspective() && drawState->getViewInverse(&vmi)) { |
| 231 | vmi.mapRect(&bounds); |
| 232 | // theoretically could set bloat = 0, instead leave it because of matrix inversion |
| 233 | // precision. |
| 234 | SkScalar bloat = drawState->getViewMatrix().getMaxScale() * SK_ScalarHalf; |
| 235 | bounds.outset(bloat, bloat); |
| 236 | } else { |
| 237 | avmr.setIdentity(drawState); |
| 238 | } |
| 239 | |
| 240 | fGpu->drawSimpleRect(bounds); |
| 241 | } |
| 242 | } |
| 243 | |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 244 | void GrGLPathRendering::enablePathTexGen(int unitIdx, PathTexGenComponents components, |
| 245 | const GrGLfloat* coefficients) { |
| 246 | SkASSERT(components >= kS_PathTexGenComponents && |
| 247 | components <= kSTR_PathTexGenComponents); |
| 248 | SkASSERT(fGpu->glCaps().maxFixedFunctionTextureCoords() >= unitIdx); |
| 249 | |
| 250 | if (GR_GL_OBJECT_LINEAR == fHWPathTexGenSettings[unitIdx].fMode && |
| 251 | components == fHWPathTexGenSettings[unitIdx].fNumComponents && |
| 252 | !memcmp(coefficients, fHWPathTexGenSettings[unitIdx].fCoefficients, |
| 253 | 3 * components * sizeof(GrGLfloat))) { |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | fGpu->setTextureUnit(unitIdx); |
| 258 | |
| 259 | fHWPathTexGenSettings[unitIdx].fNumComponents = components; |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 260 | GL_CALL(PathTexGen(GR_GL_TEXTURE0 + unitIdx, GR_GL_OBJECT_LINEAR, components, coefficients)); |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 261 | |
| 262 | memcpy(fHWPathTexGenSettings[unitIdx].fCoefficients, coefficients, |
| 263 | 3 * components * sizeof(GrGLfloat)); |
| 264 | } |
| 265 | |
| 266 | void GrGLPathRendering::enablePathTexGen(int unitIdx, PathTexGenComponents components, |
| 267 | const SkMatrix& matrix) { |
| 268 | GrGLfloat coefficients[3 * 3]; |
| 269 | SkASSERT(components >= kS_PathTexGenComponents && |
| 270 | components <= kSTR_PathTexGenComponents); |
| 271 | |
| 272 | coefficients[0] = SkScalarToFloat(matrix[SkMatrix::kMScaleX]); |
| 273 | coefficients[1] = SkScalarToFloat(matrix[SkMatrix::kMSkewX]); |
| 274 | coefficients[2] = SkScalarToFloat(matrix[SkMatrix::kMTransX]); |
| 275 | |
| 276 | if (components >= kST_PathTexGenComponents) { |
| 277 | coefficients[3] = SkScalarToFloat(matrix[SkMatrix::kMSkewY]); |
| 278 | coefficients[4] = SkScalarToFloat(matrix[SkMatrix::kMScaleY]); |
| 279 | coefficients[5] = SkScalarToFloat(matrix[SkMatrix::kMTransY]); |
| 280 | } |
| 281 | |
| 282 | if (components >= kSTR_PathTexGenComponents) { |
| 283 | coefficients[6] = SkScalarToFloat(matrix[SkMatrix::kMPersp0]); |
| 284 | coefficients[7] = SkScalarToFloat(matrix[SkMatrix::kMPersp1]); |
| 285 | coefficients[8] = SkScalarToFloat(matrix[SkMatrix::kMPersp2]); |
| 286 | } |
| 287 | |
| 288 | this->enablePathTexGen(unitIdx, components, coefficients); |
| 289 | } |
| 290 | |
| 291 | void GrGLPathRendering::flushPathTexGenSettings(int numUsedTexCoordSets) { |
| 292 | SkASSERT(fGpu->glCaps().maxFixedFunctionTextureCoords() >= numUsedTexCoordSets); |
| 293 | |
| 294 | // Only write the inactive path tex gens, since active path tex gens were |
| 295 | // written when they were enabled. |
| 296 | |
| 297 | SkDEBUGCODE( |
| 298 | for (int i = 0; i < numUsedTexCoordSets; i++) { |
| 299 | SkASSERT(0 != fHWPathTexGenSettings[i].fNumComponents); |
| 300 | } |
| 301 | ); |
| 302 | |
| 303 | for (int i = numUsedTexCoordSets; i < fHWActivePathTexGenSets; i++) { |
| 304 | SkASSERT(0 != fHWPathTexGenSettings[i].fNumComponents); |
| 305 | |
| 306 | fGpu->setTextureUnit(i); |
| 307 | GL_CALL(PathTexGen(GR_GL_TEXTURE0 + i, GR_GL_NONE, 0, NULL)); |
| 308 | fHWPathTexGenSettings[i].fNumComponents = 0; |
| 309 | } |
| 310 | |
| 311 | fHWActivePathTexGenSets = numUsedTexCoordSets; |
| 312 | } |
| 313 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 314 | void GrGLPathRendering::setProgramPathFragmentInputTransform(GrGLuint program, GrGLint location, |
| 315 | GrGLenum genMode, GrGLint components, |
| 316 | const SkMatrix& matrix) { |
| 317 | SkASSERT(caps().fragmentInputGenSupport); |
| 318 | GrGLfloat coefficients[3 * 3]; |
| 319 | SkASSERT(components >= 1 && components <= 3); |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 320 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 321 | coefficients[0] = SkScalarToFloat(matrix[SkMatrix::kMScaleX]); |
| 322 | coefficients[1] = SkScalarToFloat(matrix[SkMatrix::kMSkewX]); |
| 323 | coefficients[2] = SkScalarToFloat(matrix[SkMatrix::kMTransX]); |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 324 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 325 | if (components >= 2) { |
| 326 | coefficients[3] = SkScalarToFloat(matrix[SkMatrix::kMSkewY]); |
| 327 | coefficients[4] = SkScalarToFloat(matrix[SkMatrix::kMScaleY]); |
| 328 | coefficients[5] = SkScalarToFloat(matrix[SkMatrix::kMTransY]); |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 329 | } |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 330 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 331 | if (components >= 3) { |
| 332 | coefficients[6] = SkScalarToFloat(matrix[SkMatrix::kMPersp0]); |
| 333 | coefficients[7] = SkScalarToFloat(matrix[SkMatrix::kMPersp1]); |
| 334 | coefficients[8] = SkScalarToFloat(matrix[SkMatrix::kMPersp2]); |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 335 | } |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 336 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 337 | GL_CALL(ProgramPathFragmentInputGen(program, location, genMode, components, coefficients)); |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void GrGLPathRendering::setProjectionMatrix(const SkMatrix& matrix, |
| 341 | const SkISize& renderTargetSize, |
| 342 | GrSurfaceOrigin renderTargetOrigin) { |
| 343 | |
| 344 | SkASSERT(fGpu->glCaps().pathRenderingSupport()); |
| 345 | |
| 346 | if (renderTargetOrigin == fHWProjectionMatrixState.fRenderTargetOrigin && |
| 347 | renderTargetSize == fHWProjectionMatrixState.fRenderTargetSize && |
| 348 | matrix.cheapEqualTo(fHWProjectionMatrixState.fViewMatrix)) { |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | fHWProjectionMatrixState.fViewMatrix = matrix; |
| 353 | fHWProjectionMatrixState.fRenderTargetSize = renderTargetSize; |
| 354 | fHWProjectionMatrixState.fRenderTargetOrigin = renderTargetOrigin; |
| 355 | |
| 356 | GrGLfloat glMatrix[4 * 4]; |
| 357 | fHWProjectionMatrixState.getRTAdjustedGLMatrix<4>(glMatrix); |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 358 | GrGLenum matrixMode = |
| 359 | fGpu->glStandard() == kGLES_GrGLStandard ? GR_GL_PATH_PROJECTION : GR_GL_PROJECTION; |
| 360 | GL_CALL(MatrixLoadf(matrixMode, glMatrix)); |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 361 | } |
| 362 | |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 363 | GrGLuint GrGLPathRendering::genPaths(GrGLsizei range) { |
| 364 | if (range > 1) { |
| 365 | GrGLuint name; |
| 366 | GL_CALL_RET(name, GenPaths(range)); |
| 367 | return name; |
| 368 | } |
| 369 | |
| 370 | if (NULL == fPathNameAllocator.get()) { |
| 371 | static const int range = 65536; |
| 372 | GrGLuint firstName; |
| 373 | GL_CALL_RET(firstName, GenPaths(range)); |
| 374 | fPathNameAllocator.reset(SkNEW_ARGS(GrGLNameAllocator, (firstName, firstName + range))); |
| 375 | } |
| 376 | |
| 377 | // When allocating names one at a time, pull from a client-side pool of |
| 378 | // available names in order to save a round trip to the GL server. |
| 379 | GrGLuint name = fPathNameAllocator->allocateName(); |
| 380 | |
| 381 | if (0 == name) { |
| 382 | // Our reserved path names are all in use. Fall back on GenPaths. |
| 383 | GL_CALL_RET(name, GenPaths(1)); |
| 384 | } |
| 385 | |
| 386 | return name; |
| 387 | } |
| 388 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 389 | void GrGLPathRendering::deletePaths(GrGLuint path, GrGLsizei range) { |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 390 | if (range > 1) { |
| 391 | // It is not supported to delete names in ranges that were allocated |
| 392 | // individually using GrGLPathNameAllocator. |
| 393 | SkASSERT(NULL == fPathNameAllocator.get() || |
| 394 | path + range <= fPathNameAllocator->firstName() || |
| 395 | path >= fPathNameAllocator->endName()); |
| 396 | GL_CALL(DeletePaths(path, range)); |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | if (NULL == fPathNameAllocator.get() || |
| 401 | path < fPathNameAllocator->firstName() || |
| 402 | path >= fPathNameAllocator->endName()) { |
| 403 | // If we aren't inside fPathNameAllocator's range then this name was |
| 404 | // generated by the GenPaths fallback (or else was never allocated). |
| 405 | GL_CALL(DeletePaths(path, 1)); |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | // Make the path empty to save memory, but don't free the name in the driver. |
| 410 | GL_CALL(PathCommands(path, 0, NULL, 0, GR_GL_FLOAT, NULL)); |
| 411 | fPathNameAllocator->free(path); |
| 412 | } |
| 413 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 414 | void GrGLPathRendering::flushPathStencilSettings(SkPath::FillType fill) { |
| 415 | GrStencilSettings pathStencilSettings; |
| 416 | fGpu->getPathStencilSettingsForFillType(fill, &pathStencilSettings); |
| 417 | if (fHWPathStencilSettings != pathStencilSettings) { |
| 418 | // Just the func, ref, and mask is set here. The op and write mask are params to the call |
| 419 | // that draws the path to the SB (glStencilFillPath) |
| 420 | GrGLenum func = |
| 421 | GrToGLStencilFunc(pathStencilSettings.func(GrStencilSettings::kFront_Face)); |
| 422 | GL_CALL(PathStencilFunc(func, pathStencilSettings.funcRef(GrStencilSettings::kFront_Face), |
| 423 | pathStencilSettings.funcMask(GrStencilSettings::kFront_Face))); |
| 424 | |
| 425 | fHWPathStencilSettings = pathStencilSettings; |
| 426 | } |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 427 | } |
| 428 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 429 | inline void GrGLPathRendering::stencilThenCoverFillPath(GrGLuint path, GrGLenum fillMode, |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 430 | GrGLuint mask, GrGLenum coverMode) { |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 431 | if (caps().stencilThenCoverSupport) { |
| 432 | GL_CALL(StencilThenCoverFillPath(path, fillMode, mask, coverMode)); |
| 433 | return; |
| 434 | } |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 435 | GL_CALL(StencilFillPath(path, fillMode, mask)); |
| 436 | GL_CALL(CoverFillPath(path, coverMode)); |
| 437 | } |
| 438 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 439 | inline void GrGLPathRendering::stencilThenCoverStrokePath(GrGLuint path, GrGLint reference, |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 440 | GrGLuint mask, GrGLenum coverMode) { |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 441 | if (caps().stencilThenCoverSupport) { |
| 442 | GL_CALL(StencilThenCoverStrokePath(path, reference, mask, coverMode)); |
| 443 | return; |
| 444 | } |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 445 | GL_CALL(StencilStrokePath(path, reference, mask)); |
| 446 | GL_CALL(CoverStrokePath(path, coverMode)); |
| 447 | } |
| 448 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 449 | inline void GrGLPathRendering::stencilThenCoverFillPathInstanced( |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 450 | GrGLsizei numPaths, GrGLenum pathNameType, const GrGLvoid *paths, |
| 451 | GrGLuint pathBase, GrGLenum fillMode, GrGLuint mask, GrGLenum coverMode, |
| 452 | GrGLenum transformType, const GrGLfloat *transformValues) { |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 453 | if (caps().stencilThenCoverSupport) { |
| 454 | GL_CALL(StencilThenCoverFillPathInstanced(numPaths, pathNameType, paths, pathBase, fillMode, |
| 455 | mask, coverMode, transformType, transformValues)); |
| 456 | return; |
| 457 | } |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 458 | GL_CALL(StencilFillPathInstanced(numPaths, pathNameType, paths, pathBase, |
| 459 | fillMode, mask, transformType, transformValues)); |
| 460 | GL_CALL(CoverFillPathInstanced(numPaths, pathNameType, paths, pathBase, |
| 461 | coverMode, transformType, transformValues)); |
| 462 | } |
| 463 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 464 | inline void GrGLPathRendering::stencilThenCoverStrokePathInstanced( |
| 465 | GrGLsizei numPaths, GrGLenum pathNameType, const GrGLvoid *paths, |
| 466 | GrGLuint pathBase, GrGLint reference, GrGLuint mask, GrGLenum coverMode, |
| 467 | GrGLenum transformType, const GrGLfloat *transformValues) { |
| 468 | if (caps().stencilThenCoverSupport) { |
| 469 | GL_CALL(StencilThenCoverStrokePathInstanced(numPaths, pathNameType, paths, pathBase, |
| 470 | reference, mask, coverMode, transformType, |
| 471 | transformValues)); |
| 472 | return; |
| 473 | } |
| 474 | |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 475 | GL_CALL(StencilStrokePathInstanced(numPaths, pathNameType, paths, pathBase, |
| 476 | reference, mask, transformType, transformValues)); |
| 477 | GL_CALL(CoverStrokePathInstanced(numPaths, pathNameType, paths, pathBase, |
| 478 | coverMode, transformType, transformValues)); |
| 479 | } |