blob: dee4d991523150e1740e09feae70da829e188bfa [file] [log] [blame]
bsalomonadd79ef2015-08-19 13:26:49 -07001/*
2 * Copyright 2015 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#ifndef GrDrawPathBatch_DEFINED
9#define GrDrawPathBatch_DEFINED
10
bsalomon1fcc01c2015-09-09 09:48:06 -070011#include "GrBatchFlushState.h"
bsalomonadd79ef2015-08-19 13:26:49 -070012#include "GrDrawBatch.h"
13#include "GrGpu.h"
14#include "GrPath.h"
15#include "GrPathRendering.h"
16#include "GrPathProcessor.h"
17
bsalomon1fcc01c2015-09-09 09:48:06 -070018#include "SkTLList.h"
19
20class GrDrawPathBatchBase : public GrDrawBatch {
bsalomonadd79ef2015-08-19 13:26:49 -070021public:
bsalomon1fcc01c2015-09-09 09:48:06 -070022 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualittf2384692015-09-10 11:00:51 -070023 out->setKnownFourComponents(fColor);
bsalomon1fcc01c2015-09-09 09:48:06 -070024 }
25
26 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualittf2384692015-09-10 11:00:51 -070027 out->setKnownSingleComponent(0xff);
bsalomon1fcc01c2015-09-09 09:48:06 -070028 }
29
30 void setStencilSettings(const GrStencilSettings& stencil) { fStencilSettings = stencil; }
31
32protected:
joshualittf2384692015-09-10 11:00:51 -070033 GrDrawPathBatchBase(const SkMatrix& viewMatrix, GrColor initialColor)
34 : fViewMatrix(viewMatrix)
35 , fColor(initialColor) {}
bsalomon1fcc01c2015-09-09 09:48:06 -070036
bsalomon1fcc01c2015-09-09 09:48:06 -070037 const GrStencilSettings& stencilSettings() const { return fStencilSettings; }
38 const GrPipelineOptimizations& opts() const { return fOpts; }
joshualittf2384692015-09-10 11:00:51 -070039 const SkMatrix& viewMatrix() const { return fViewMatrix; }
40 GrColor color() const { return fColor; }
41
42 // TODO delete
43 const GrBatchTracker* tracker() const { return &fBatchTracker; }
bsalomon1fcc01c2015-09-09 09:48:06 -070044
45private:
46 void initBatchTracker(const GrPipelineOptimizations& opts) override {
joshualittf2384692015-09-10 11:00:51 -070047 opts.getOverrideColorIfSet(&fColor);
bsalomon1fcc01c2015-09-09 09:48:06 -070048 fOpts = opts;
49 }
50
joshualittf2384692015-09-10 11:00:51 -070051 SkMatrix fViewMatrix;
52 GrColor fColor;
bsalomon1fcc01c2015-09-09 09:48:06 -070053 GrStencilSettings fStencilSettings;
54 GrPipelineOptimizations fOpts;
55
joshualittf2384692015-09-10 11:00:51 -070056 // TODO delete
57 GrBatchTracker fBatchTracker;
58
bsalomon1fcc01c2015-09-09 09:48:06 -070059 typedef GrDrawBatch INHERITED;
60};
61
62class GrDrawPathBatch final : public GrDrawPathBatchBase {
63public:
64 // This can't return a more abstract type because we install the stencil settings late :(
joshualittf2384692015-09-10 11:00:51 -070065 static GrDrawPathBatchBase* Create(const SkMatrix& viewMatrix, GrColor color,
66 const GrPath* path) {
67 return new GrDrawPathBatch(viewMatrix, color, path);
bsalomonadd79ef2015-08-19 13:26:49 -070068 }
69
70 const char* name() const override { return "DrawPath"; }
71
bsalomon1fcc01c2015-09-09 09:48:06 -070072 SkString dumpInfo() const override;
bsalomonadd79ef2015-08-19 13:26:49 -070073
74private:
joshualittf2384692015-09-10 11:00:51 -070075 GrDrawPathBatch(const SkMatrix& viewMatrix, GrColor color, const GrPath* path)
76 : INHERITED(viewMatrix, color)
bsalomon1fcc01c2015-09-09 09:48:06 -070077 , fPath(path) {
bsalomonadd79ef2015-08-19 13:26:49 -070078 fBounds = path->getBounds();
joshualittf2384692015-09-10 11:00:51 -070079 viewMatrix.mapRect(&fBounds);
bsalomonadd79ef2015-08-19 13:26:49 -070080 this->initClassID<GrDrawPathBatch>();
81 }
82
bsalomonadd79ef2015-08-19 13:26:49 -070083 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { return false; }
84
85 void onPrepare(GrBatchFlushState*) override {}
86
bsalomon1fcc01c2015-09-09 09:48:06 -070087 void onDraw(GrBatchFlushState* state) override;
88
89 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
90
91 typedef GrDrawPathBatchBase INHERITED;
92};
93
94/**
95 * This could be nested inside the batch class, but for now it must be declarable in a public
96 * header (GrDrawContext)
97 */
98class GrPathRangeDraw : public GrNonAtomicRef {
99public:
100 typedef GrPathRendering::PathTransformType TransformType;
101
102 static GrPathRangeDraw* Create(GrPathRange* range, TransformType transformType,
103 int reserveCnt) {
104 return SkNEW_ARGS(GrPathRangeDraw, (range, transformType, reserveCnt));
bsalomonadd79ef2015-08-19 13:26:49 -0700105 }
106
bsalomon1fcc01c2015-09-09 09:48:06 -0700107 void append(uint16_t index, float transform[]) {
108 fTransforms.push_back_n(GrPathRendering::PathTransformSize(fTransformType), transform);
109 fIndices.push_back(index);
110 }
111
112 int count() const { return fIndices.count(); }
113
114 TransformType transformType() const { return fTransformType; }
115
116 const float* transforms() const { return fTransforms.begin(); }
117
118 const uint16_t* indices() const { return fIndices.begin(); }
119
120 const GrPathRange* range() const { return fPathRange.get(); }
121
122 static bool CanMerge(const GrPathRangeDraw& a, const GrPathRangeDraw& b) {
123 return a.transformType() == b.transformType() && a.range() == b.range();
124 }
125
126private:
127 GrPathRangeDraw(GrPathRange* range, TransformType transformType, int reserveCnt)
bsalomona82bf952015-09-10 09:31:59 -0700128 : fPathRange(range)
bsalomon1fcc01c2015-09-09 09:48:06 -0700129 , fTransformType(transformType)
130 , fIndices(reserveCnt)
131 , fTransforms(reserveCnt * GrPathRendering::PathTransformSize(transformType)) {
132 SkDEBUGCODE(fUsedInBatch = false;)
133 }
134
135 // Reserve space for 64 paths where indices are 16 bit and transforms are translations.
136 static const int kIndexReserveCnt = 64;
137 static const int kTransformBufferReserveCnt = 2 * 64;
138
139 GrPendingIOResource<const GrPathRange, kRead_GrIOType> fPathRange;
140 GrPathRendering::PathTransformType fTransformType;
141 SkSTArray<kIndexReserveCnt, uint16_t, true> fIndices;
142 SkSTArray<kTransformBufferReserveCnt, float, true> fTransforms;
143
144 // To ensure we don't reuse these across batches.
145#ifdef SK_DEBUG
146 bool fUsedInBatch;
147 friend class GrDrawPathRangeBatch;
148#endif
149
150 typedef GrNonAtomicRef INHERITED;
151};
152
153// Template this if we decide to support index types other than 16bit
154class GrDrawPathRangeBatch final : public GrDrawPathBatchBase {
155public:
156 // This can't return a more abstracet type because we install the stencil settings late :(
joshualittf2384692015-09-10 11:00:51 -0700157 static GrDrawPathBatchBase* Create(const SkMatrix& viewMatrix, const SkMatrix& localMatrix,
158 GrColor color, GrPathRangeDraw* pathRangeDraw) {
159 return SkNEW_ARGS(GrDrawPathRangeBatch, (viewMatrix, localMatrix, color, pathRangeDraw));
bsalomon1fcc01c2015-09-09 09:48:06 -0700160 }
161
162 ~GrDrawPathRangeBatch() override;
163
164 const char* name() const override { return "DrawPathRange"; }
165
166 SkString dumpInfo() const override;
167
168private:
169 inline bool isWinding() const;
170
joshualittf2384692015-09-10 11:00:51 -0700171 GrDrawPathRangeBatch(const SkMatrix& viewMatrix, const SkMatrix& localMatrix, GrColor color,
172 GrPathRangeDraw* pathRangeDraw);
bsalomon1fcc01c2015-09-09 09:48:06 -0700173
174 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override;
175
176 void onPrepare(GrBatchFlushState*) override {}
177
178 void onDraw(GrBatchFlushState* state) override;
179
180 typedef SkTLList<GrPathRangeDraw*> DrawList;
181 DrawList fDraws;
182 int fTotalPathCount;
joshualittf2384692015-09-10 11:00:51 -0700183 SkMatrix fLocalMatrix;
bsalomon1fcc01c2015-09-09 09:48:06 -0700184
185 typedef GrDrawPathBatchBase INHERITED;
bsalomonadd79ef2015-08-19 13:26:49 -0700186};
187
188#endif