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