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 | |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 13 | GrGLPathRange::GrGLPathRange(GrGLGpu* gpu, PathGenerator* pathGenerator, const GrStrokeInfo& stroke) |
| 14 | : INHERITED(gpu, pathGenerator), |
| 15 | fStroke(stroke), |
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, |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 26 | const GrStrokeInfo& stroke) |
| 27 | : INHERITED(gpu, numPaths), |
| 28 | fStroke(stroke), |
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() { |
kkinnunen | 1e2913e | 2015-12-01 04:35:37 -0800 | [diff] [blame] | 36 | // Must force fill: |
| 37 | // * dashing: NVPR stroke dashing is different to Skia. |
| 38 | // * end caps: NVPR stroking degenerate contours with end caps is different to Skia. |
| 39 | bool forceFill = fStroke.isDashed() || |
| 40 | (fStroke.needToApply() && fStroke.getCap() != SkPaint::kButt_Cap); |
| 41 | |
| 42 | if (forceFill) { |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 43 | fShouldStroke = false; |
| 44 | fShouldFill = true; |
| 45 | } else { |
| 46 | fShouldStroke = fStroke.needToApply(); |
| 47 | fShouldFill = fStroke.isFillStyle() || |
| 48 | fStroke.getStyle() == SkStrokeRec::kStrokeAndFill_Style; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void GrGLPathRange::onInitPath(int index, const SkPath& origSkPath) const { |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 53 | GrGLGpu* gpu = static_cast<GrGLGpu*>(this->getGpu()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 54 | if (nullptr == gpu) { |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 55 | return; |
| 56 | } |
| 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); |
| 68 | GrGLPath::InitPathObjectStroke(gpu, fBasePathID + index, fStroke); |
| 69 | } else { |
| 70 | const SkPath* skPath = &origSkPath; |
| 71 | SkTLazy<SkPath> tmpPath; |
| 72 | const GrStrokeInfo* stroke = &fStroke; |
| 73 | GrStrokeInfo tmpStroke(SkStrokeRec::kFill_InitStyle); |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 74 | |
kkinnunen | 1e2913e | 2015-12-01 04:35:37 -0800 | [diff] [blame] | 75 | // Dashing must be applied to the path. However, if dashing is present, |
| 76 | // we must convert all the paths to fills. The GrStrokeInfo::applyDash leaves |
| 77 | // simple paths as strokes but converts other paths to fills. |
| 78 | // Thus we must stroke the strokes here, so that all paths in the |
| 79 | // path range are using the same style. |
| 80 | if (fStroke.isDashed()) { |
| 81 | if (!stroke->applyDashToPath(tmpPath.init(), &tmpStroke, *skPath)) { |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 82 | return; |
| 83 | } |
kkinnunen | 1e2913e | 2015-12-01 04:35:37 -0800 | [diff] [blame] | 84 | skPath = tmpPath.get(); |
| 85 | stroke = &tmpStroke; |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 86 | } |
kkinnunen | 1e2913e | 2015-12-01 04:35:37 -0800 | [diff] [blame] | 87 | if (stroke->needToApply()) { |
| 88 | if (!tmpPath.isValid()) { |
| 89 | tmpPath.init(); |
| 90 | } |
| 91 | if (!stroke->applyToPath(tmpPath.get(), *tmpPath.get())) { |
| 92 | return; |
| 93 | } |
| 94 | } |
| 95 | GrGLPath::InitPathObjectPathData(gpu, fBasePathID + index, *skPath); |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 96 | } |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 97 | // TODO: Use a better approximation for the individual path sizes. |
| 98 | fGpuMemorySize += 100; |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void GrGLPathRange::onRelease() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 102 | SkASSERT(this->getGpu()); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 103 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame^] | 104 | if (0 != fBasePathID) { |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 105 | static_cast<GrGLGpu*>(this->getGpu())->glPathRendering()->deletePaths(fBasePathID, |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 106 | this->getNumPaths()); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 107 | fBasePathID = 0; |
| 108 | } |
| 109 | |
| 110 | INHERITED::onRelease(); |
| 111 | } |
| 112 | |
| 113 | void GrGLPathRange::onAbandon() { |
| 114 | fBasePathID = 0; |
| 115 | |
| 116 | INHERITED::onAbandon(); |
| 117 | } |