blob: da1e9fe709da7320d833d0667a405aa073b0cf30 [file] [log] [blame]
cdaltonb85a0aa2014-07-21 15:32:44 -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 "GrGLPathRange.h"
9#include "GrGLPath.h"
cdaltonc7103a12014-08-11 14:05:05 -070010#include "GrGLPathRendering.h"
jvanverth39edf762014-12-22 11:44:19 -080011#include "GrGLGpu.h"
cdaltonb85a0aa2014-07-21 15:32:44 -070012
bsalomon6663acf2016-05-10 09:14:17 -070013GrGLPathRange::GrGLPathRange(GrGLGpu* gpu, PathGenerator* pathGenerator, const GrStyle& style)
kkinnunen50b58e62015-05-18 23:02:07 -070014 : INHERITED(gpu, pathGenerator),
bsalomon6663acf2016-05-10 09:14:17 -070015 fStyle(style),
cdalton855d83f2014-09-18 13:51:53 -070016 fBasePathID(gpu->glPathRendering()->genPaths(this->getNumPaths())),
17 fGpuMemorySize(0) {
kkinnunen50b58e62015-05-18 23:02:07 -070018 this->init();
kkinnunen2e6055b2016-04-22 01:48:29 -070019 this->registerWithCache(SkBudgeted::kYes);
cdalton855d83f2014-09-18 13:51:53 -070020}
21
bsalomon861e1032014-12-16 07:33:49 -080022GrGLPathRange::GrGLPathRange(GrGLGpu* gpu,
cdalton855d83f2014-09-18 13:51:53 -070023 GrGLuint basePathID,
24 int numPaths,
25 size_t gpuMemorySize,
bsalomon6663acf2016-05-10 09:14:17 -070026 const GrStyle& style)
kkinnunen50b58e62015-05-18 23:02:07 -070027 : INHERITED(gpu, numPaths),
bsalomon6663acf2016-05-10 09:14:17 -070028 fStyle(style),
cdalton855d83f2014-09-18 13:51:53 -070029 fBasePathID(basePathID),
30 fGpuMemorySize(gpuMemorySize) {
kkinnunen50b58e62015-05-18 23:02:07 -070031 this->init();
kkinnunen2e6055b2016-04-22 01:48:29 -070032 this->registerWithCache(SkBudgeted::kYes);
cdaltonb85a0aa2014-07-21 15:32:44 -070033}
34
kkinnunen50b58e62015-05-18 23:02:07 -070035void GrGLPathRange::init() {
bsalomon6663acf2016-05-10 09:14:17 -070036 const SkStrokeRec& stroke = fStyle.strokeRec();
kkinnunen1e2913e2015-12-01 04:35:37 -080037 // 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.
bsalomon6663acf2016-05-10 09:14:17 -070040 bool forceFill = fStyle.pathEffect() ||
41 (stroke.needToApply() && stroke.getCap() != SkPaint::kButt_Cap);
kkinnunen1e2913e2015-12-01 04:35:37 -080042
43 if (forceFill) {
kkinnunen50b58e62015-05-18 23:02:07 -070044 fShouldStroke = false;
45 fShouldFill = true;
46 } else {
bsalomon6663acf2016-05-10 09:14:17 -070047 fShouldStroke = stroke.needToApply();
48 fShouldFill = stroke.isFillStyle() ||
49 stroke.getStyle() == SkStrokeRec::kStrokeAndFill_Style;
kkinnunen50b58e62015-05-18 23:02:07 -070050 }
51}
52
53void GrGLPathRange::onInitPath(int index, const SkPath& origSkPath) const {
bsalomon861e1032014-12-16 07:33:49 -080054 GrGLGpu* gpu = static_cast<GrGLGpu*>(this->getGpu());
halcanary96fcdcc2015-08-27 07:41:13 -070055 if (nullptr == gpu) {
cdaltonb85a0aa2014-07-21 15:32:44 -070056 return;
57 }
cdaltonb85a0aa2014-07-21 15:32:44 -070058 // Make sure the path at this index hasn't been initted already.
kkinnunen5b653572014-08-20 04:13:27 -070059 SkDEBUGCODE(
60 GrGLboolean isPath;
61 GR_GL_CALL_RET(gpu->glInterface(), isPath, IsPath(fBasePathID + index)));
62 SkASSERT(GR_GL_FALSE == isPath);
cdaltonb85a0aa2014-07-21 15:32:44 -070063
kkinnunen1e2913e2015-12-01 04:35:37 -080064 if (origSkPath.isEmpty()) {
65 GrGLPath::InitPathObjectEmptyPath(gpu, fBasePathID + index);
66 } else if (fShouldStroke) {
67 GrGLPath::InitPathObjectPathData(gpu, fBasePathID + index, origSkPath);
bsalomon6663acf2016-05-10 09:14:17 -070068 GrGLPath::InitPathObjectStroke(gpu, fBasePathID + index, fStyle.strokeRec());
kkinnunen1e2913e2015-12-01 04:35:37 -080069 } else {
70 const SkPath* skPath = &origSkPath;
71 SkTLazy<SkPath> tmpPath;
bsalomon6663acf2016-05-10 09:14:17 -070072 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)) {
kkinnunen50b58e62015-05-18 23:02:07 -070080 return;
81 }
bsalomon6663acf2016-05-10 09:14:17 -070082 // 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);
kkinnunen1e2913e2015-12-01 04:35:37 -080085 skPath = tmpPath.get();
bsalomon6663acf2016-05-10 09:14:17 -070086
kkinnunen1e2913e2015-12-01 04:35:37 -080087 }
88 GrGLPath::InitPathObjectPathData(gpu, fBasePathID + index, *skPath);
kkinnunen50b58e62015-05-18 23:02:07 -070089 }
cdalton855d83f2014-09-18 13:51:53 -070090 // TODO: Use a better approximation for the individual path sizes.
91 fGpuMemorySize += 100;
cdaltonb85a0aa2014-07-21 15:32:44 -070092}
93
94void GrGLPathRange::onRelease() {
bsalomon49f085d2014-09-05 13:34:00 -070095 SkASSERT(this->getGpu());
cdaltonb85a0aa2014-07-21 15:32:44 -070096
kkinnunen2e6055b2016-04-22 01:48:29 -070097 if (0 != fBasePathID) {
bsalomon861e1032014-12-16 07:33:49 -080098 static_cast<GrGLGpu*>(this->getGpu())->glPathRendering()->deletePaths(fBasePathID,
cdalton855d83f2014-09-18 13:51:53 -070099 this->getNumPaths());
cdaltonb85a0aa2014-07-21 15:32:44 -0700100 fBasePathID = 0;
101 }
102
103 INHERITED::onRelease();
104}
105
106void GrGLPathRange::onAbandon() {
107 fBasePathID = 0;
108
109 INHERITED::onAbandon();
110}