cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2014 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "GrGLPathRange.h" |
| 10 | #include "GrGLPath.h" |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 11 | #include "GrGLPathRendering.h" |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 12 | #include "GrGLGpu.h" |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 13 | |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 14 | GrGLPathRange::GrGLPathRange(GrGLGpu* gpu, PathGenerator* pathGenerator, const GrStrokeInfo& stroke) |
| 15 | : INHERITED(gpu, pathGenerator), |
| 16 | fStroke(stroke), |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 17 | fBasePathID(gpu->glPathRendering()->genPaths(this->getNumPaths())), |
| 18 | fGpuMemorySize(0) { |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 19 | this->init(); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 20 | this->registerWithCache(); |
| 21 | } |
| 22 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 23 | GrGLPathRange::GrGLPathRange(GrGLGpu* gpu, |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 24 | GrGLuint basePathID, |
| 25 | int numPaths, |
| 26 | size_t gpuMemorySize, |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 27 | const GrStrokeInfo& stroke) |
| 28 | : INHERITED(gpu, numPaths), |
| 29 | fStroke(stroke), |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 30 | fBasePathID(basePathID), |
| 31 | fGpuMemorySize(gpuMemorySize) { |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 32 | this->init(); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 33 | this->registerWithCache(); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 34 | } |
| 35 | |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 36 | void GrGLPathRange::init() { |
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. |
| 40 | bool forceFill = fStroke.isDashed() || |
| 41 | (fStroke.needToApply() && fStroke.getCap() != SkPaint::kButt_Cap); |
| 42 | |
| 43 | if (forceFill) { |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 44 | fShouldStroke = false; |
| 45 | fShouldFill = true; |
| 46 | } else { |
| 47 | fShouldStroke = fStroke.needToApply(); |
| 48 | fShouldFill = fStroke.isFillStyle() || |
| 49 | fStroke.getStyle() == SkStrokeRec::kStrokeAndFill_Style; |
| 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 | } |
| 58 | |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 59 | // Make sure the path at this index hasn't been initted already. |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 60 | SkDEBUGCODE( |
| 61 | GrGLboolean isPath; |
| 62 | GR_GL_CALL_RET(gpu->glInterface(), isPath, IsPath(fBasePathID + index))); |
| 63 | SkASSERT(GR_GL_FALSE == isPath); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 64 | |
kkinnunen | 1e2913e | 2015-12-01 04:35:37 -0800 | [diff] [blame] | 65 | if (origSkPath.isEmpty()) { |
| 66 | GrGLPath::InitPathObjectEmptyPath(gpu, fBasePathID + index); |
| 67 | } else if (fShouldStroke) { |
| 68 | GrGLPath::InitPathObjectPathData(gpu, fBasePathID + index, origSkPath); |
| 69 | GrGLPath::InitPathObjectStroke(gpu, fBasePathID + index, fStroke); |
| 70 | } else { |
| 71 | const SkPath* skPath = &origSkPath; |
| 72 | SkTLazy<SkPath> tmpPath; |
| 73 | const GrStrokeInfo* stroke = &fStroke; |
| 74 | GrStrokeInfo tmpStroke(SkStrokeRec::kFill_InitStyle); |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 75 | |
kkinnunen | 1e2913e | 2015-12-01 04:35:37 -0800 | [diff] [blame] | 76 | // Dashing must be applied to the path. However, if dashing is present, |
| 77 | // we must convert all the paths to fills. The GrStrokeInfo::applyDash leaves |
| 78 | // simple paths as strokes but converts other paths to fills. |
| 79 | // Thus we must stroke the strokes here, so that all paths in the |
| 80 | // path range are using the same style. |
| 81 | if (fStroke.isDashed()) { |
| 82 | if (!stroke->applyDashToPath(tmpPath.init(), &tmpStroke, *skPath)) { |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 83 | return; |
| 84 | } |
kkinnunen | 1e2913e | 2015-12-01 04:35:37 -0800 | [diff] [blame] | 85 | skPath = tmpPath.get(); |
| 86 | stroke = &tmpStroke; |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 87 | } |
kkinnunen | 1e2913e | 2015-12-01 04:35:37 -0800 | [diff] [blame] | 88 | if (stroke->needToApply()) { |
| 89 | if (!tmpPath.isValid()) { |
| 90 | tmpPath.init(); |
| 91 | } |
| 92 | if (!stroke->applyToPath(tmpPath.get(), *tmpPath.get())) { |
| 93 | return; |
| 94 | } |
| 95 | } |
| 96 | GrGLPath::InitPathObjectPathData(gpu, fBasePathID + index, *skPath); |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 97 | } |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 98 | // TODO: Use a better approximation for the individual path sizes. |
| 99 | fGpuMemorySize += 100; |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void GrGLPathRange::onRelease() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 103 | SkASSERT(this->getGpu()); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 104 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 105 | if (0 != fBasePathID && this->shouldFreeResources()) { |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 106 | static_cast<GrGLGpu*>(this->getGpu())->glPathRendering()->deletePaths(fBasePathID, |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 107 | this->getNumPaths()); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 108 | fBasePathID = 0; |
| 109 | } |
| 110 | |
| 111 | INHERITED::onRelease(); |
| 112 | } |
| 113 | |
| 114 | void GrGLPathRange::onAbandon() { |
| 115 | fBasePathID = 0; |
| 116 | |
| 117 | INHERITED::onAbandon(); |
| 118 | } |