cdalton | b85a0aa | 2014-07-21 15:32:44 -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 "GrGLPathRange.h" |
| 9 | #include "GrGLPath.h" |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 10 | #include "GrGLPathRendering.h" |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 11 | #include "GrGLGpu.h" |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 12 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 13 | GrGLPathRange::GrGLPathRange(GrGLGpu* gpu, PathGenerator* pathGenerator, const GrStyle& style) |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 14 | : INHERITED(gpu, pathGenerator), |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 15 | fStyle(style), |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 16 | fBasePathID(gpu->glPathRendering()->genPaths(this->getNumPaths())), |
| 17 | fGpuMemorySize(0) { |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 18 | this->init(); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 19 | this->registerWithCache(SkBudgeted::kYes); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 20 | } |
| 21 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 22 | GrGLPathRange::GrGLPathRange(GrGLGpu* gpu, |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 23 | GrGLuint basePathID, |
| 24 | int numPaths, |
| 25 | size_t gpuMemorySize, |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 26 | const GrStyle& style) |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 27 | : INHERITED(gpu, numPaths), |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 28 | fStyle(style), |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 29 | fBasePathID(basePathID), |
| 30 | fGpuMemorySize(gpuMemorySize) { |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 31 | this->init(); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 32 | this->registerWithCache(SkBudgeted::kYes); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 33 | } |
| 34 | |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 35 | void GrGLPathRange::init() { |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 36 | const SkStrokeRec& stroke = fStyle.strokeRec(); |
kkinnunen | 1e2913e | 2015-12-01 04:35:37 -0800 | [diff] [blame] | 37 | // Must force fill: |
| 38 | // * dashing: NVPR stroke dashing is different to Skia. |
| 39 | // * end caps: NVPR stroking degenerate contours with end caps is different to Skia. |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 40 | bool forceFill = fStyle.pathEffect() || |
| 41 | (stroke.needToApply() && stroke.getCap() != SkPaint::kButt_Cap); |
kkinnunen | 1e2913e | 2015-12-01 04:35:37 -0800 | [diff] [blame] | 42 | |
| 43 | if (forceFill) { |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 44 | fShouldStroke = false; |
| 45 | fShouldFill = true; |
| 46 | } else { |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 47 | fShouldStroke = stroke.needToApply(); |
| 48 | fShouldFill = stroke.isFillStyle() || |
| 49 | stroke.getStyle() == SkStrokeRec::kStrokeAndFill_Style; |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | void GrGLPathRange::onInitPath(int index, const SkPath& origSkPath) const { |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 54 | GrGLGpu* gpu = static_cast<GrGLGpu*>(this->getGpu()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 55 | if (nullptr == gpu) { |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 56 | return; |
| 57 | } |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 58 | // Make sure the path at this index hasn't been initted already. |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 59 | SkDEBUGCODE( |
| 60 | GrGLboolean isPath; |
| 61 | GR_GL_CALL_RET(gpu->glInterface(), isPath, IsPath(fBasePathID + index))); |
| 62 | SkASSERT(GR_GL_FALSE == isPath); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 63 | |
kkinnunen | 1e2913e | 2015-12-01 04:35:37 -0800 | [diff] [blame] | 64 | if (origSkPath.isEmpty()) { |
| 65 | GrGLPath::InitPathObjectEmptyPath(gpu, fBasePathID + index); |
| 66 | } else if (fShouldStroke) { |
| 67 | GrGLPath::InitPathObjectPathData(gpu, fBasePathID + index, origSkPath); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 68 | GrGLPath::InitPathObjectStroke(gpu, fBasePathID + index, fStyle.strokeRec()); |
kkinnunen | 1e2913e | 2015-12-01 04:35:37 -0800 | [diff] [blame] | 69 | } else { |
| 70 | const SkPath* skPath = &origSkPath; |
| 71 | SkTLazy<SkPath> tmpPath; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 72 | if (!fStyle.isSimpleFill()) { |
| 73 | SkStrokeRec::InitStyle fill; |
| 74 | // The path effect must be applied to the path. However, if a path effect is present, |
| 75 | // we must convert all the paths to fills. The path effect application may leave |
| 76 | // simple paths as strokes but converts other paths to fills. |
| 77 | // Thus we must stroke the strokes here, so that all paths in the |
| 78 | // path range are using the same style. |
| 79 | if (!fStyle.applyToPath(tmpPath.init(), &fill, *skPath, SK_Scalar1)) { |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 80 | return; |
| 81 | } |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 82 | // We shouldn't have allowed hairlines or arbitrary path effect styles to get here |
| 83 | // so after application we better have a filled path. |
| 84 | SkASSERT(SkStrokeRec::kFill_InitStyle == fill); |
kkinnunen | 1e2913e | 2015-12-01 04:35:37 -0800 | [diff] [blame] | 85 | skPath = tmpPath.get(); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 86 | |
kkinnunen | 1e2913e | 2015-12-01 04:35:37 -0800 | [diff] [blame] | 87 | } |
| 88 | GrGLPath::InitPathObjectPathData(gpu, fBasePathID + index, *skPath); |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 89 | } |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 90 | // TODO: Use a better approximation for the individual path sizes. |
| 91 | fGpuMemorySize += 100; |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void GrGLPathRange::onRelease() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 95 | SkASSERT(this->getGpu()); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 96 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 97 | if (0 != fBasePathID) { |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 98 | static_cast<GrGLGpu*>(this->getGpu())->glPathRendering()->deletePaths(fBasePathID, |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 99 | this->getNumPaths()); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 100 | fBasePathID = 0; |
| 101 | } |
| 102 | |
| 103 | INHERITED::onRelease(); |
| 104 | } |
| 105 | |
| 106 | void GrGLPathRange::onAbandon() { |
| 107 | fBasePathID = 0; |
| 108 | |
| 109 | INHERITED::onAbandon(); |
| 110 | } |