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