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/GrGLUtil.h" |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 10 | #include "gl/GrGLGpu.h" |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 11 | |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 12 | #include "GrGLPath.h" |
| 13 | #include "GrGLPathRange.h" |
| 14 | #include "GrGLPathRendering.h" |
| 15 | |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 16 | #include "SkStream.h" |
| 17 | #include "SkTypeface.h" |
| 18 | |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 19 | #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) |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 21 | |
kkinnunen | 702501d | 2016-01-13 23:36:45 -0800 | [diff] [blame] | 22 | // 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. |
| 24 | static const GrGLsizei kPathIDPreallocationAmount = 65536; |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 25 | |
cdalton | 55b24af | 2014-11-25 11:00:56 -0800 | [diff] [blame] | 26 | static const GrGLenum gIndexType2GLType[] = { |
| 27 | GR_GL_UNSIGNED_BYTE, |
| 28 | GR_GL_UNSIGNED_SHORT, |
| 29 | GR_GL_UNSIGNED_INT |
| 30 | }; |
| 31 | |
| 32 | GR_STATIC_ASSERT(0 == GrPathRange::kU8_PathIndexType); |
| 33 | GR_STATIC_ASSERT(1 == GrPathRange::kU16_PathIndexType); |
| 34 | GR_STATIC_ASSERT(2 == GrPathRange::kU32_PathIndexType); |
| 35 | GR_STATIC_ASSERT(GrPathRange::kU32_PathIndexType == GrPathRange::kLast_PathIndexType); |
| 36 | |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 37 | static 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 | |
| 45 | GR_STATIC_ASSERT(0 == GrPathRendering::kNone_PathTransformType); |
| 46 | GR_STATIC_ASSERT(1 == GrPathRendering::kTranslateX_PathTransformType); |
| 47 | GR_STATIC_ASSERT(2 == GrPathRendering::kTranslateY_PathTransformType); |
| 48 | GR_STATIC_ASSERT(3 == GrPathRendering::kTranslate_PathTransformType); |
| 49 | GR_STATIC_ASSERT(4 == GrPathRendering::kAffine_PathTransformType); |
| 50 | GR_STATIC_ASSERT(GrPathRendering::kAffine_PathTransformType == GrPathRendering::kLast_PathTransformType); |
| 51 | |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 52 | #ifdef SK_DEBUG |
| 53 | static const GrGLenum gXformType2ComponentCount[] = { |
| 54 | 0, |
| 55 | 1, |
| 56 | 1, |
| 57 | 2, |
| 58 | 6 |
| 59 | }; |
| 60 | |
| 61 | static 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 | |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 68 | static GrGLenum gr_stencil_op_to_gl_path_rendering_fill_mode(GrStencilOp op) { |
| 69 | switch (op) { |
| 70 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 71 | SK_ABORT("Unexpected path fill."); |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 72 | /* fallthrough */; |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 73 | case GrStencilOp::kIncWrap: |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 74 | return GR_GL_COUNT_UP; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 75 | case GrStencilOp::kInvert: |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 76 | return GR_GL_INVERT; |
| 77 | } |
| 78 | } |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 79 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 80 | GrGLPathRendering::GrGLPathRendering(GrGLGpu* gpu) |
kkinnunen | 702501d | 2016-01-13 23:36:45 -0800 | [diff] [blame] | 81 | : GrPathRendering(gpu) |
| 82 | , fPreallocatedPathCount(0) { |
kkinnunen | 6bb6d40 | 2015-07-14 10:59:23 -0700 | [diff] [blame] | 83 | const GrGLInterface* glInterface = gpu->glInterface(); |
Mike Klein | 7634330 | 2017-06-22 13:15:13 -0700 | [diff] [blame] | 84 | fCaps.bindFragmentInputSupport = (bool)glInterface->fFunctions.fBindFragmentInputLocation; |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | GrGLPathRendering::~GrGLPathRendering() { |
kkinnunen | 702501d | 2016-01-13 23:36:45 -0800 | [diff] [blame] | 88 | if (fPreallocatedPathCount > 0) { |
| 89 | this->deletePaths(fFirstPreallocatedPathID, fPreallocatedPathCount); |
| 90 | } |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 91 | } |
| 92 | |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 93 | void GrGLPathRendering::disconnect(GrGpu::DisconnectType type) { |
| 94 | if (GrGpu::DisconnectType::kCleanup == type) { |
| 95 | this->deletePaths(fFirstPreallocatedPathID, fPreallocatedPathCount); |
| 96 | }; |
kkinnunen | 702501d | 2016-01-13 23:36:45 -0800 | [diff] [blame] | 97 | fPreallocatedPathCount = 0; |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 98 | } |
| 99 | |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 100 | void GrGLPathRendering::resetContext() { |
| 101 | fHWProjectionMatrixState.invalidate(); |
| 102 | // we don't use the model view matrix. |
jvanverth | 5053063 | 2015-04-27 10:36:27 -0700 | [diff] [blame] | 103 | GL_CALL(MatrixLoadIdentity(GR_GL_PATH_MODELVIEW)); |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 104 | |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 105 | fHWPathStencilSettings.invalidate(); |
| 106 | } |
| 107 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 108 | sk_sp<GrPath> GrGLPathRendering::createPath(const SkPath& inPath, const GrStyle& style) { |
| 109 | return sk_make_sp<GrGLPath>(this->gpu(), inPath, style); |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 112 | sk_sp<GrPathRange> GrGLPathRendering::createPathRange(GrPathRange::PathGenerator* pathGenerator, |
| 113 | const GrStyle& style) { |
| 114 | return sk_make_sp<GrGLPathRange>(this->gpu(), pathGenerator, style); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 115 | } |
| 116 | |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 117 | void GrGLPathRendering::onStencilPath(const StencilPathArgs& args, const GrPath* path) { |
| 118 | GrGLGpu* gpu = this->gpu(); |
| 119 | SkASSERT(gpu->caps()->shaderCaps()->pathRenderingSupport()); |
| 120 | gpu->flushColorWrite(false); |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 121 | |
Robert Phillips | 7311b40 | 2017-07-27 12:01:46 -0400 | [diff] [blame] | 122 | GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(args.fProxy->priv().peekRenderTarget()); |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 123 | SkISize size = SkISize::Make(rt->width(), rt->height()); |
Robert Phillips | 7311b40 | 2017-07-27 12:01:46 -0400 | [diff] [blame] | 124 | this->setProjectionMatrix(*args.fViewMatrix, size, args.fProxy->origin()); |
| 125 | gpu->flushScissor(*args.fScissor, rt->getViewport(), args.fProxy->origin()); |
cdalton | af8bc7d | 2016-02-05 09:35:20 -0800 | [diff] [blame] | 126 | gpu->flushHWAAState(rt, args.fUseHWAA, true); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 127 | gpu->flushRenderTarget(rt, nullptr); |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 128 | |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 129 | const GrGLPath* glPath = static_cast<const GrGLPath*>(path); |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 130 | |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 131 | this->flushPathStencilSettings(*args.fStencil); |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 132 | SkASSERT(!fHWPathStencilSettings.isTwoSided()); |
| 133 | |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 134 | GrGLenum fillMode = |
| 135 | gr_stencil_op_to_gl_path_rendering_fill_mode(fHWPathStencilSettings.front().fPassOp); |
| 136 | GrGLint writeMask = fHWPathStencilSettings.front().fWriteMask; |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 137 | |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 138 | if (glPath->shouldFill()) { |
| 139 | GL_CALL(StencilFillPath(glPath->pathID(), fillMode, writeMask)); |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 140 | } |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 141 | if (glPath->shouldStroke()) { |
| 142 | GL_CALL(StencilStrokePath(glPath->pathID(), 0xffff, writeMask)); |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
stephana | 1dc1721 | 2016-04-25 07:01:22 -0700 | [diff] [blame] | 146 | void GrGLPathRendering::onDrawPath(const GrPipeline& pipeline, |
| 147 | const GrPrimitiveProcessor& primProc, |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 148 | const GrStencilSettings& stencilPassSettings, |
stephana | 1dc1721 | 2016-04-25 07:01:22 -0700 | [diff] [blame] | 149 | const GrPath* path) { |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 150 | if (!this->gpu()->flushGLState(pipeline, primProc, false)) { |
stephana | 1dc1721 | 2016-04-25 07:01:22 -0700 | [diff] [blame] | 151 | return; |
| 152 | } |
| 153 | const GrGLPath* glPath = static_cast<const GrGLPath*>(path); |
| 154 | |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 155 | this->flushPathStencilSettings(stencilPassSettings); |
stephana | 1dc1721 | 2016-04-25 07:01:22 -0700 | [diff] [blame] | 156 | SkASSERT(!fHWPathStencilSettings.isTwoSided()); |
| 157 | |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 158 | GrGLenum fillMode = |
| 159 | gr_stencil_op_to_gl_path_rendering_fill_mode(fHWPathStencilSettings.front().fPassOp); |
| 160 | GrGLint writeMask = fHWPathStencilSettings.front().fWriteMask; |
stephana | 1dc1721 | 2016-04-25 07:01:22 -0700 | [diff] [blame] | 161 | |
| 162 | if (glPath->shouldStroke()) { |
| 163 | if (glPath->shouldFill()) { |
| 164 | GL_CALL(StencilFillPath(glPath->pathID(), fillMode, writeMask)); |
| 165 | } |
| 166 | GL_CALL(StencilThenCoverStrokePath(glPath->pathID(), 0xffff, writeMask, |
| 167 | GR_GL_BOUNDING_BOX)); |
| 168 | } else { |
| 169 | GL_CALL(StencilThenCoverFillPath(glPath->pathID(), fillMode, writeMask, |
| 170 | GR_GL_BOUNDING_BOX)); |
| 171 | } |
| 172 | } |
| 173 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 174 | void GrGLPathRendering::onDrawPaths(const GrPipeline& pipeline, |
| 175 | const GrPrimitiveProcessor& primProc, |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 176 | const GrStencilSettings& stencilPassSettings, |
| 177 | const GrPathRange* pathRange, const void* indices, |
| 178 | PathIndexType indexType, const float transformValues[], |
| 179 | PathTransformType transformType, int count) { |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 180 | SkDEBUGCODE(verify_floats(transformValues, gXformType2ComponentCount[transformType] * count)); |
| 181 | |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 182 | if (!this->gpu()->flushGLState(pipeline, primProc, false)) { |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 183 | return; |
| 184 | } |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 185 | this->flushPathStencilSettings(stencilPassSettings); |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 186 | SkASSERT(!fHWPathStencilSettings.isTwoSided()); |
| 187 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 188 | |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 189 | const GrGLPathRange* glPathRange = static_cast<const GrGLPathRange*>(pathRange); |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 190 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 191 | GrGLenum fillMode = |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 192 | gr_stencil_op_to_gl_path_rendering_fill_mode(fHWPathStencilSettings.front().fPassOp); |
| 193 | GrGLint writeMask = fHWPathStencilSettings.front().fWriteMask; |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 194 | |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 195 | if (glPathRange->shouldStroke()) { |
| 196 | if (glPathRange->shouldFill()) { |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 197 | GL_CALL(StencilFillPathInstanced( |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 198 | count, gIndexType2GLType[indexType], indices, glPathRange->basePathID(), |
| 199 | fillMode, writeMask, gXformType2GLType[transformType], |
| 200 | transformValues)); |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 201 | } |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 202 | GL_CALL(StencilThenCoverStrokePathInstanced( |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 203 | count, gIndexType2GLType[indexType], indices, glPathRange->basePathID(), |
cdalton | 55b24af | 2014-11-25 11:00:56 -0800 | [diff] [blame] | 204 | 0xffff, writeMask, GR_GL_BOUNDING_BOX_OF_BOUNDING_BOXES, |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 205 | gXformType2GLType[transformType], transformValues)); |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 206 | } else { |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 207 | GL_CALL(StencilThenCoverFillPathInstanced( |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 208 | count, gIndexType2GLType[indexType], indices, glPathRange->basePathID(), |
cdalton | 55b24af | 2014-11-25 11:00:56 -0800 | [diff] [blame] | 209 | fillMode, writeMask, GR_GL_BOUNDING_BOX_OF_BOUNDING_BOXES, |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 210 | gXformType2GLType[transformType], transformValues)); |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 214 | void GrGLPathRendering::setProgramPathFragmentInputTransform(GrGLuint program, GrGLint location, |
| 215 | GrGLenum genMode, GrGLint components, |
| 216 | const SkMatrix& matrix) { |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 217 | float coefficients[3 * 3]; |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 218 | SkASSERT(components >= 1 && components <= 3); |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 219 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 220 | coefficients[0] = SkScalarToFloat(matrix[SkMatrix::kMScaleX]); |
| 221 | coefficients[1] = SkScalarToFloat(matrix[SkMatrix::kMSkewX]); |
| 222 | coefficients[2] = SkScalarToFloat(matrix[SkMatrix::kMTransX]); |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 223 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 224 | if (components >= 2) { |
| 225 | coefficients[3] = SkScalarToFloat(matrix[SkMatrix::kMSkewY]); |
| 226 | coefficients[4] = SkScalarToFloat(matrix[SkMatrix::kMScaleY]); |
| 227 | coefficients[5] = SkScalarToFloat(matrix[SkMatrix::kMTransY]); |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 228 | } |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 229 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 230 | if (components >= 3) { |
| 231 | coefficients[6] = SkScalarToFloat(matrix[SkMatrix::kMPersp0]); |
| 232 | coefficients[7] = SkScalarToFloat(matrix[SkMatrix::kMPersp1]); |
| 233 | coefficients[8] = SkScalarToFloat(matrix[SkMatrix::kMPersp2]); |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 234 | } |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 235 | SkDEBUGCODE(verify_floats(coefficients, components * 3)); |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 236 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 237 | GL_CALL(ProgramPathFragmentInputGen(program, location, genMode, components, coefficients)); |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void GrGLPathRendering::setProjectionMatrix(const SkMatrix& matrix, |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 241 | const SkISize& renderTargetSize, |
| 242 | GrSurfaceOrigin renderTargetOrigin) { |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 243 | |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 244 | SkASSERT(this->gpu()->glCaps().shaderCaps()->pathRenderingSupport()); |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 245 | |
| 246 | if (renderTargetOrigin == fHWProjectionMatrixState.fRenderTargetOrigin && |
| 247 | renderTargetSize == fHWProjectionMatrixState.fRenderTargetSize && |
| 248 | matrix.cheapEqualTo(fHWProjectionMatrixState.fViewMatrix)) { |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | fHWProjectionMatrixState.fViewMatrix = matrix; |
| 253 | fHWProjectionMatrixState.fRenderTargetSize = renderTargetSize; |
| 254 | fHWProjectionMatrixState.fRenderTargetOrigin = renderTargetOrigin; |
| 255 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 256 | float glMatrix[4 * 4]; |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 257 | fHWProjectionMatrixState.getRTAdjustedGLMatrix<4>(glMatrix); |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 258 | SkDEBUGCODE(verify_floats(glMatrix, SK_ARRAY_COUNT(glMatrix))); |
jvanverth | 5053063 | 2015-04-27 10:36:27 -0700 | [diff] [blame] | 259 | GL_CALL(MatrixLoadf(GR_GL_PATH_PROJECTION, glMatrix)); |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 260 | } |
| 261 | |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 262 | GrGLuint GrGLPathRendering::genPaths(GrGLsizei range) { |
kkinnunen | 702501d | 2016-01-13 23:36:45 -0800 | [diff] [blame] | 263 | SkASSERT(range > 0); |
| 264 | GrGLuint firstID; |
| 265 | if (fPreallocatedPathCount >= range) { |
| 266 | firstID = fFirstPreallocatedPathID; |
| 267 | fPreallocatedPathCount -= range; |
| 268 | fFirstPreallocatedPathID += range; |
| 269 | return firstID; |
| 270 | } |
| 271 | // Allocate range + the amount to fill up preallocation amount. If succeed, either join with |
| 272 | // the existing preallocation range or delete the existing and use the new (potentially partial) |
| 273 | // preallocation range. |
| 274 | GrGLsizei allocAmount = range + (kPathIDPreallocationAmount - fPreallocatedPathCount); |
| 275 | if (allocAmount >= range) { |
| 276 | GL_CALL_RET(firstID, GenPaths(allocAmount)); |
| 277 | |
| 278 | if (firstID != 0) { |
| 279 | if (fPreallocatedPathCount > 0 && |
| 280 | firstID == fFirstPreallocatedPathID + fPreallocatedPathCount) { |
| 281 | firstID = fFirstPreallocatedPathID; |
| 282 | fPreallocatedPathCount += allocAmount - range; |
| 283 | fFirstPreallocatedPathID += range; |
| 284 | return firstID; |
| 285 | } |
| 286 | |
| 287 | if (allocAmount > range) { |
| 288 | if (fPreallocatedPathCount > 0) { |
| 289 | this->deletePaths(fFirstPreallocatedPathID, fPreallocatedPathCount); |
| 290 | } |
| 291 | fFirstPreallocatedPathID = firstID + range; |
| 292 | fPreallocatedPathCount = allocAmount - range; |
| 293 | } |
| 294 | // Special case: if allocAmount == range, we have full preallocated range. |
| 295 | return firstID; |
| 296 | } |
| 297 | } |
| 298 | // Failed to allocate with preallocation. Remove existing preallocation and try to allocate just |
| 299 | // the range. |
| 300 | if (fPreallocatedPathCount > 0) { |
| 301 | this->deletePaths(fFirstPreallocatedPathID, fPreallocatedPathCount); |
| 302 | fPreallocatedPathCount = 0; |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 303 | } |
| 304 | |
kkinnunen | 702501d | 2016-01-13 23:36:45 -0800 | [diff] [blame] | 305 | GL_CALL_RET(firstID, GenPaths(range)); |
| 306 | if (firstID == 0) { |
| 307 | SkDebugf("Warning: Failed to allocate path\n"); |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 308 | } |
kkinnunen | 702501d | 2016-01-13 23:36:45 -0800 | [diff] [blame] | 309 | return firstID; |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 310 | } |
| 311 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 312 | void GrGLPathRendering::deletePaths(GrGLuint path, GrGLsizei range) { |
kkinnunen | 702501d | 2016-01-13 23:36:45 -0800 | [diff] [blame] | 313 | GL_CALL(DeletePaths(path, range)); |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 314 | } |
| 315 | |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 316 | void GrGLPathRendering::flushPathStencilSettings(const GrStencilSettings& stencilSettings) { |
| 317 | if (fHWPathStencilSettings != stencilSettings) { |
kkinnunen | 1517f93 | 2016-01-25 06:07:26 -0800 | [diff] [blame] | 318 | SkASSERT(stencilSettings.isValid()); |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 319 | // Just the func, ref, and mask is set here. The op and write mask are params to the call |
| 320 | // that draws the path to the SB (glStencilFillPath) |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 321 | uint16_t ref = stencilSettings.front().fRef; |
| 322 | GrStencilTest test = stencilSettings.front().fTest; |
| 323 | uint16_t testMask = stencilSettings.front().fTestMask; |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 324 | |
kkinnunen | 1517f93 | 2016-01-25 06:07:26 -0800 | [diff] [blame] | 325 | if (!fHWPathStencilSettings.isValid() || |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 326 | ref != fHWPathStencilSettings.front().fRef || |
| 327 | test != fHWPathStencilSettings.front().fTest || |
| 328 | testMask != fHWPathStencilSettings.front().fTestMask) { |
| 329 | GL_CALL(PathStencilFunc(GrToGLStencilFunc(test), ref, testMask)); |
kkinnunen | 1517f93 | 2016-01-25 06:07:26 -0800 | [diff] [blame] | 330 | } |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 331 | fHWPathStencilSettings = stencilSettings; |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 332 | } |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 333 | } |
| 334 | |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 335 | inline GrGLGpu* GrGLPathRendering::gpu() { |
| 336 | return static_cast<GrGLGpu*>(fGpu); |
| 337 | } |