blob: 8508cfb99dc1b53316971a3c5650f9b86ea0515f [file] [log] [blame]
cdaltonc7103a12014-08-11 14:05:05 -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#include "gl/GrGLPathRendering.h"
cdaltonc7103a12014-08-11 14:05:05 -07009#include "gl/GrGLUtil.h"
jvanverth39edf762014-12-22 11:44:19 -080010#include "gl/GrGLGpu.h"
cdaltonc7103a12014-08-11 14:05:05 -070011
kkinnunenccdaa042014-08-20 01:36:23 -070012#include "GrGLPath.h"
13#include "GrGLPathRange.h"
14#include "GrGLPathRendering.h"
15
cdalton855d83f2014-09-18 13:51:53 -070016#include "SkStream.h"
17#include "SkTypeface.h"
18
kkinnunencabe20c2015-06-01 01:37:26 -070019#define GL_CALL(X) GR_GL_CALL(this->gpu()->glInterface(), X)
20#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->gpu()->glInterface(), RET, X)
kkinnunenccdaa042014-08-20 01:36:23 -070021
kkinnunen702501d2016-01-13 23:36:45 -080022// Number of paths to allocate per glGenPaths call. The call can be overly slow on command buffer GL
23// implementation. The call has a result value, and thus waiting for the call completion is needed.
24static const GrGLsizei kPathIDPreallocationAmount = 65536;
kkinnunenccdaa042014-08-20 01:36:23 -070025
cdalton55b24af2014-11-25 11:00:56 -080026static const GrGLenum gIndexType2GLType[] = {
27 GR_GL_UNSIGNED_BYTE,
28 GR_GL_UNSIGNED_SHORT,
29 GR_GL_UNSIGNED_INT
30};
31
32GR_STATIC_ASSERT(0 == GrPathRange::kU8_PathIndexType);
33GR_STATIC_ASSERT(1 == GrPathRange::kU16_PathIndexType);
34GR_STATIC_ASSERT(2 == GrPathRange::kU32_PathIndexType);
35GR_STATIC_ASSERT(GrPathRange::kU32_PathIndexType == GrPathRange::kLast_PathIndexType);
36
kkinnunenccdaa042014-08-20 01:36:23 -070037static const GrGLenum gXformType2GLType[] = {
38 GR_GL_NONE,
39 GR_GL_TRANSLATE_X,
40 GR_GL_TRANSLATE_Y,
41 GR_GL_TRANSLATE_2D,
42 GR_GL_TRANSPOSE_AFFINE_2D
43};
44
45GR_STATIC_ASSERT(0 == GrPathRendering::kNone_PathTransformType);
46GR_STATIC_ASSERT(1 == GrPathRendering::kTranslateX_PathTransformType);
47GR_STATIC_ASSERT(2 == GrPathRendering::kTranslateY_PathTransformType);
48GR_STATIC_ASSERT(3 == GrPathRendering::kTranslate_PathTransformType);
49GR_STATIC_ASSERT(4 == GrPathRendering::kAffine_PathTransformType);
50GR_STATIC_ASSERT(GrPathRendering::kAffine_PathTransformType == GrPathRendering::kLast_PathTransformType);
51
kkinnunen68c63b32016-03-04 00:12:33 -080052#ifdef SK_DEBUG
53static const GrGLenum gXformType2ComponentCount[] = {
54 0,
55 1,
56 1,
57 2,
58 6
59};
60
61static void verify_floats(const float* floats, int count) {
62 for (int i = 0; i < count; ++i) {
63 SkASSERT(!SkScalarIsNaN(SkFloatToScalar(floats[i])));
64 }
65}
66#endif
67
kkinnunenccdaa042014-08-20 01:36:23 -070068static GrGLenum gr_stencil_op_to_gl_path_rendering_fill_mode(GrStencilOp op) {
69 switch (op) {
70 default:
71 SkFAIL("Unexpected path fill.");
72 /* fallthrough */;
cdalton193d9cf2016-05-12 11:52:02 -070073 case GrStencilOp::kIncWrap:
kkinnunenccdaa042014-08-20 01:36:23 -070074 return GR_GL_COUNT_UP;
cdalton93a379b2016-05-11 13:58:08 -070075 case GrStencilOp::kInvert:
kkinnunenccdaa042014-08-20 01:36:23 -070076 return GR_GL_INVERT;
77 }
78}
cdaltonc7103a12014-08-11 14:05:05 -070079
bsalomon861e1032014-12-16 07:33:49 -080080GrGLPathRendering::GrGLPathRendering(GrGLGpu* gpu)
kkinnunen702501d2016-01-13 23:36:45 -080081 : GrPathRendering(gpu)
82 , fPreallocatedPathCount(0) {
kkinnunen6bb6d402015-07-14 10:59:23 -070083 const GrGLInterface* glInterface = gpu->glInterface();
84 fCaps.bindFragmentInputSupport =
halcanary96fcdcc2015-08-27 07:41:13 -070085 nullptr != glInterface->fFunctions.fBindFragmentInputLocation;
cdaltonc7103a12014-08-11 14:05:05 -070086}
87
88GrGLPathRendering::~GrGLPathRendering() {
kkinnunen702501d2016-01-13 23:36:45 -080089 if (fPreallocatedPathCount > 0) {
90 this->deletePaths(fFirstPreallocatedPathID, fPreallocatedPathCount);
91 }
cdaltonc7103a12014-08-11 14:05:05 -070092}
93
bsalomon6e2aad42016-04-01 11:54:31 -070094void GrGLPathRendering::disconnect(GrGpu::DisconnectType type) {
95 if (GrGpu::DisconnectType::kCleanup == type) {
96 this->deletePaths(fFirstPreallocatedPathID, fPreallocatedPathCount);
97 };
kkinnunen702501d2016-01-13 23:36:45 -080098 fPreallocatedPathCount = 0;
cdaltonc7103a12014-08-11 14:05:05 -070099}
100
kkinnunenccdaa042014-08-20 01:36:23 -0700101void GrGLPathRendering::resetContext() {
102 fHWProjectionMatrixState.invalidate();
103 // we don't use the model view matrix.
jvanverth50530632015-04-27 10:36:27 -0700104 GL_CALL(MatrixLoadIdentity(GR_GL_PATH_MODELVIEW));
kkinnunenccdaa042014-08-20 01:36:23 -0700105
kkinnunenccdaa042014-08-20 01:36:23 -0700106 fHWPathStencilSettings.invalidate();
107}
108
bsalomon6663acf2016-05-10 09:14:17 -0700109GrPath* GrGLPathRendering::createPath(const SkPath& inPath, const GrStyle& style) {
110 return new GrGLPath(this->gpu(), inPath, style);
kkinnunenccdaa042014-08-20 01:36:23 -0700111}
112
cdalton855d83f2014-09-18 13:51:53 -0700113GrPathRange* GrGLPathRendering::createPathRange(GrPathRange::PathGenerator* pathGenerator,
bsalomon6663acf2016-05-10 09:14:17 -0700114 const GrStyle& style) {
115 return new GrGLPathRange(this->gpu(), pathGenerator, style);
cdalton855d83f2014-09-18 13:51:53 -0700116}
117
kkinnunencabe20c2015-06-01 01:37:26 -0700118void GrGLPathRendering::onStencilPath(const StencilPathArgs& args, const GrPath* path) {
119 GrGLGpu* gpu = this->gpu();
120 SkASSERT(gpu->caps()->shaderCaps()->pathRenderingSupport());
121 gpu->flushColorWrite(false);
robertphillips5fa7f302016-07-21 09:21:04 -0700122 gpu->flushDrawFace(GrDrawFace::kBoth);
kkinnunencabe20c2015-06-01 01:37:26 -0700123
124 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(args.fRenderTarget);
125 SkISize size = SkISize::Make(rt->width(), rt->height());
126 this->setProjectionMatrix(*args.fViewMatrix, size, rt->origin());
127 gpu->flushScissor(*args.fScissor, rt->getViewport(), rt->origin());
cdaltonaf8bc7d2016-02-05 09:35:20 -0800128 gpu->flushHWAAState(rt, args.fUseHWAA, true);
halcanary96fcdcc2015-08-27 07:41:13 -0700129 gpu->flushRenderTarget(rt, nullptr);
kkinnunencabe20c2015-06-01 01:37:26 -0700130
kkinnunen50b58e62015-05-18 23:02:07 -0700131 const GrGLPath* glPath = static_cast<const GrGLPath*>(path);
kkinnunen5b653572014-08-20 04:13:27 -0700132
kkinnunencabe20c2015-06-01 01:37:26 -0700133 this->flushPathStencilSettings(*args.fStencil);
kkinnunen5b653572014-08-20 04:13:27 -0700134 SkASSERT(!fHWPathStencilSettings.isTwoSided());
135
cdalton93a379b2016-05-11 13:58:08 -0700136 GrGLenum fillMode =
137 gr_stencil_op_to_gl_path_rendering_fill_mode(fHWPathStencilSettings.front().fPassOp);
138 GrGLint writeMask = fHWPathStencilSettings.front().fWriteMask;
joshualitt92e496f2014-10-31 13:56:50 -0700139
kkinnunen50b58e62015-05-18 23:02:07 -0700140 if (glPath->shouldFill()) {
141 GL_CALL(StencilFillPath(glPath->pathID(), fillMode, writeMask));
joshualitt92e496f2014-10-31 13:56:50 -0700142 }
kkinnunen50b58e62015-05-18 23:02:07 -0700143 if (glPath->shouldStroke()) {
144 GL_CALL(StencilStrokePath(glPath->pathID(), 0xffff, writeMask));
joshualitt92e496f2014-10-31 13:56:50 -0700145 }
146}
147
stephana1dc17212016-04-25 07:01:22 -0700148void GrGLPathRendering::onDrawPath(const GrPipeline& pipeline,
149 const GrPrimitiveProcessor& primProc,
cdalton193d9cf2016-05-12 11:52:02 -0700150 const GrStencilSettings& stencilPassSettings,
stephana1dc17212016-04-25 07:01:22 -0700151 const GrPath* path) {
152 if (!this->gpu()->flushGLState(pipeline, primProc)) {
153 return;
154 }
155 const GrGLPath* glPath = static_cast<const GrGLPath*>(path);
156
cdalton193d9cf2016-05-12 11:52:02 -0700157 this->flushPathStencilSettings(stencilPassSettings);
stephana1dc17212016-04-25 07:01:22 -0700158 SkASSERT(!fHWPathStencilSettings.isTwoSided());
159
cdalton93a379b2016-05-11 13:58:08 -0700160 GrGLenum fillMode =
161 gr_stencil_op_to_gl_path_rendering_fill_mode(fHWPathStencilSettings.front().fPassOp);
162 GrGLint writeMask = fHWPathStencilSettings.front().fWriteMask;
stephana1dc17212016-04-25 07:01:22 -0700163
164 if (glPath->shouldStroke()) {
165 if (glPath->shouldFill()) {
166 GL_CALL(StencilFillPath(glPath->pathID(), fillMode, writeMask));
167 }
168 GL_CALL(StencilThenCoverStrokePath(glPath->pathID(), 0xffff, writeMask,
169 GR_GL_BOUNDING_BOX));
170 } else {
171 GL_CALL(StencilThenCoverFillPath(glPath->pathID(), fillMode, writeMask,
172 GR_GL_BOUNDING_BOX));
173 }
174}
175
egdaniel0e1853c2016-03-17 11:35:45 -0700176void GrGLPathRendering::onDrawPaths(const GrPipeline& pipeline,
177 const GrPrimitiveProcessor& primProc,
cdalton193d9cf2016-05-12 11:52:02 -0700178 const GrStencilSettings& stencilPassSettings,
179 const GrPathRange* pathRange, const void* indices,
180 PathIndexType indexType, const float transformValues[],
181 PathTransformType transformType, int count) {
kkinnunen68c63b32016-03-04 00:12:33 -0800182 SkDEBUGCODE(verify_floats(transformValues, gXformType2ComponentCount[transformType] * count));
183
egdaniel0e1853c2016-03-17 11:35:45 -0700184 if (!this->gpu()->flushGLState(pipeline, primProc)) {
kkinnunencabe20c2015-06-01 01:37:26 -0700185 return;
186 }
cdalton193d9cf2016-05-12 11:52:02 -0700187 this->flushPathStencilSettings(stencilPassSettings);
kkinnunencabe20c2015-06-01 01:37:26 -0700188 SkASSERT(!fHWPathStencilSettings.isTwoSided());
189
kkinnunen5b653572014-08-20 04:13:27 -0700190
kkinnunen50b58e62015-05-18 23:02:07 -0700191 const GrGLPathRange* glPathRange = static_cast<const GrGLPathRange*>(pathRange);
kkinnunen5b653572014-08-20 04:13:27 -0700192
kkinnunen5b653572014-08-20 04:13:27 -0700193 GrGLenum fillMode =
cdalton93a379b2016-05-11 13:58:08 -0700194 gr_stencil_op_to_gl_path_rendering_fill_mode(fHWPathStencilSettings.front().fPassOp);
195 GrGLint writeMask = fHWPathStencilSettings.front().fWriteMask;
kkinnunen5b653572014-08-20 04:13:27 -0700196
kkinnunen50b58e62015-05-18 23:02:07 -0700197 if (glPathRange->shouldStroke()) {
198 if (glPathRange->shouldFill()) {
kkinnunen5b653572014-08-20 04:13:27 -0700199 GL_CALL(StencilFillPathInstanced(
kkinnunen50b58e62015-05-18 23:02:07 -0700200 count, gIndexType2GLType[indexType], indices, glPathRange->basePathID(),
201 fillMode, writeMask, gXformType2GLType[transformType],
202 transformValues));
kkinnunen5b653572014-08-20 04:13:27 -0700203 }
kkinnunencfe62e32015-07-01 02:58:50 -0700204 GL_CALL(StencilThenCoverStrokePathInstanced(
kkinnunen50b58e62015-05-18 23:02:07 -0700205 count, gIndexType2GLType[indexType], indices, glPathRange->basePathID(),
cdalton55b24af2014-11-25 11:00:56 -0800206 0xffff, writeMask, GR_GL_BOUNDING_BOX_OF_BOUNDING_BOXES,
kkinnunencfe62e32015-07-01 02:58:50 -0700207 gXformType2GLType[transformType], transformValues));
joshualitt92e496f2014-10-31 13:56:50 -0700208 } else {
kkinnunencfe62e32015-07-01 02:58:50 -0700209 GL_CALL(StencilThenCoverFillPathInstanced(
kkinnunen50b58e62015-05-18 23:02:07 -0700210 count, gIndexType2GLType[indexType], indices, glPathRange->basePathID(),
cdalton55b24af2014-11-25 11:00:56 -0800211 fillMode, writeMask, GR_GL_BOUNDING_BOX_OF_BOUNDING_BOXES,
kkinnunencfe62e32015-07-01 02:58:50 -0700212 gXformType2GLType[transformType], transformValues));
kkinnunen5b653572014-08-20 04:13:27 -0700213 }
214}
215
kkinnunen5b653572014-08-20 04:13:27 -0700216void GrGLPathRendering::setProgramPathFragmentInputTransform(GrGLuint program, GrGLint location,
217 GrGLenum genMode, GrGLint components,
218 const SkMatrix& matrix) {
egdaniel018fb622015-10-28 07:26:40 -0700219 float coefficients[3 * 3];
kkinnunen5b653572014-08-20 04:13:27 -0700220 SkASSERT(components >= 1 && components <= 3);
kkinnunenccdaa042014-08-20 01:36:23 -0700221
kkinnunen5b653572014-08-20 04:13:27 -0700222 coefficients[0] = SkScalarToFloat(matrix[SkMatrix::kMScaleX]);
223 coefficients[1] = SkScalarToFloat(matrix[SkMatrix::kMSkewX]);
224 coefficients[2] = SkScalarToFloat(matrix[SkMatrix::kMTransX]);
kkinnunenccdaa042014-08-20 01:36:23 -0700225
kkinnunen5b653572014-08-20 04:13:27 -0700226 if (components >= 2) {
227 coefficients[3] = SkScalarToFloat(matrix[SkMatrix::kMSkewY]);
228 coefficients[4] = SkScalarToFloat(matrix[SkMatrix::kMScaleY]);
229 coefficients[5] = SkScalarToFloat(matrix[SkMatrix::kMTransY]);
kkinnunenccdaa042014-08-20 01:36:23 -0700230 }
kkinnunenccdaa042014-08-20 01:36:23 -0700231
kkinnunen5b653572014-08-20 04:13:27 -0700232 if (components >= 3) {
233 coefficients[6] = SkScalarToFloat(matrix[SkMatrix::kMPersp0]);
234 coefficients[7] = SkScalarToFloat(matrix[SkMatrix::kMPersp1]);
235 coefficients[8] = SkScalarToFloat(matrix[SkMatrix::kMPersp2]);
kkinnunenccdaa042014-08-20 01:36:23 -0700236 }
kkinnunen68c63b32016-03-04 00:12:33 -0800237 SkDEBUGCODE(verify_floats(coefficients, components * 3));
kkinnunenccdaa042014-08-20 01:36:23 -0700238
kkinnunen5b653572014-08-20 04:13:27 -0700239 GL_CALL(ProgramPathFragmentInputGen(program, location, genMode, components, coefficients));
kkinnunenccdaa042014-08-20 01:36:23 -0700240}
241
242void GrGLPathRendering::setProjectionMatrix(const SkMatrix& matrix,
joshualittee2af952014-12-30 09:04:15 -0800243 const SkISize& renderTargetSize,
244 GrSurfaceOrigin renderTargetOrigin) {
kkinnunenccdaa042014-08-20 01:36:23 -0700245
kkinnunencabe20c2015-06-01 01:37:26 -0700246 SkASSERT(this->gpu()->glCaps().shaderCaps()->pathRenderingSupport());
kkinnunenccdaa042014-08-20 01:36:23 -0700247
248 if (renderTargetOrigin == fHWProjectionMatrixState.fRenderTargetOrigin &&
249 renderTargetSize == fHWProjectionMatrixState.fRenderTargetSize &&
250 matrix.cheapEqualTo(fHWProjectionMatrixState.fViewMatrix)) {
251 return;
252 }
253
254 fHWProjectionMatrixState.fViewMatrix = matrix;
255 fHWProjectionMatrixState.fRenderTargetSize = renderTargetSize;
256 fHWProjectionMatrixState.fRenderTargetOrigin = renderTargetOrigin;
257
egdaniel018fb622015-10-28 07:26:40 -0700258 float glMatrix[4 * 4];
kkinnunenccdaa042014-08-20 01:36:23 -0700259 fHWProjectionMatrixState.getRTAdjustedGLMatrix<4>(glMatrix);
kkinnunen68c63b32016-03-04 00:12:33 -0800260 SkDEBUGCODE(verify_floats(glMatrix, SK_ARRAY_COUNT(glMatrix)));
jvanverth50530632015-04-27 10:36:27 -0700261 GL_CALL(MatrixLoadf(GR_GL_PATH_PROJECTION, glMatrix));
kkinnunenccdaa042014-08-20 01:36:23 -0700262}
263
cdaltonc7103a12014-08-11 14:05:05 -0700264GrGLuint GrGLPathRendering::genPaths(GrGLsizei range) {
kkinnunen702501d2016-01-13 23:36:45 -0800265 SkASSERT(range > 0);
266 GrGLuint firstID;
267 if (fPreallocatedPathCount >= range) {
268 firstID = fFirstPreallocatedPathID;
269 fPreallocatedPathCount -= range;
270 fFirstPreallocatedPathID += range;
271 return firstID;
272 }
273 // Allocate range + the amount to fill up preallocation amount. If succeed, either join with
274 // the existing preallocation range or delete the existing and use the new (potentially partial)
275 // preallocation range.
276 GrGLsizei allocAmount = range + (kPathIDPreallocationAmount - fPreallocatedPathCount);
277 if (allocAmount >= range) {
278 GL_CALL_RET(firstID, GenPaths(allocAmount));
279
280 if (firstID != 0) {
281 if (fPreallocatedPathCount > 0 &&
282 firstID == fFirstPreallocatedPathID + fPreallocatedPathCount) {
283 firstID = fFirstPreallocatedPathID;
284 fPreallocatedPathCount += allocAmount - range;
285 fFirstPreallocatedPathID += range;
286 return firstID;
287 }
288
289 if (allocAmount > range) {
290 if (fPreallocatedPathCount > 0) {
291 this->deletePaths(fFirstPreallocatedPathID, fPreallocatedPathCount);
292 }
293 fFirstPreallocatedPathID = firstID + range;
294 fPreallocatedPathCount = allocAmount - range;
295 }
296 // Special case: if allocAmount == range, we have full preallocated range.
297 return firstID;
298 }
299 }
300 // Failed to allocate with preallocation. Remove existing preallocation and try to allocate just
301 // the range.
302 if (fPreallocatedPathCount > 0) {
303 this->deletePaths(fFirstPreallocatedPathID, fPreallocatedPathCount);
304 fPreallocatedPathCount = 0;
cdaltonc7103a12014-08-11 14:05:05 -0700305 }
306
kkinnunen702501d2016-01-13 23:36:45 -0800307 GL_CALL_RET(firstID, GenPaths(range));
308 if (firstID == 0) {
309 SkDebugf("Warning: Failed to allocate path\n");
cdaltonc7103a12014-08-11 14:05:05 -0700310 }
kkinnunen702501d2016-01-13 23:36:45 -0800311 return firstID;
cdaltonc7103a12014-08-11 14:05:05 -0700312}
313
kkinnunen5b653572014-08-20 04:13:27 -0700314void GrGLPathRendering::deletePaths(GrGLuint path, GrGLsizei range) {
kkinnunen702501d2016-01-13 23:36:45 -0800315 GL_CALL(DeletePaths(path, range));
cdaltonc7103a12014-08-11 14:05:05 -0700316}
317
joshualitt92e496f2014-10-31 13:56:50 -0700318void GrGLPathRendering::flushPathStencilSettings(const GrStencilSettings& stencilSettings) {
319 if (fHWPathStencilSettings != stencilSettings) {
kkinnunen1517f932016-01-25 06:07:26 -0800320 SkASSERT(stencilSettings.isValid());
kkinnunen5b653572014-08-20 04:13:27 -0700321 // Just the func, ref, and mask is set here. The op and write mask are params to the call
322 // that draws the path to the SB (glStencilFillPath)
cdalton93a379b2016-05-11 13:58:08 -0700323 uint16_t ref = stencilSettings.front().fRef;
324 GrStencilTest test = stencilSettings.front().fTest;
325 uint16_t testMask = stencilSettings.front().fTestMask;
kkinnunen5b653572014-08-20 04:13:27 -0700326
kkinnunen1517f932016-01-25 06:07:26 -0800327 if (!fHWPathStencilSettings.isValid() ||
cdalton93a379b2016-05-11 13:58:08 -0700328 ref != fHWPathStencilSettings.front().fRef ||
329 test != fHWPathStencilSettings.front().fTest ||
330 testMask != fHWPathStencilSettings.front().fTestMask) {
331 GL_CALL(PathStencilFunc(GrToGLStencilFunc(test), ref, testMask));
kkinnunen1517f932016-01-25 06:07:26 -0800332 }
joshualitt92e496f2014-10-31 13:56:50 -0700333 fHWPathStencilSettings = stencilSettings;
kkinnunen5b653572014-08-20 04:13:27 -0700334 }
cdaltonc7103a12014-08-11 14:05:05 -0700335}
336
kkinnunencabe20c2015-06-01 01:37:26 -0700337inline GrGLGpu* GrGLPathRendering::gpu() {
338 return static_cast<GrGLGpu*>(fGpu);
339}