blob: 228ad86617f234d40b892fb295a1883a350c7e23 [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:
reed1b55a962015-09-17 20:16:13 -070033 GrDrawPathBatchBase(uint32_t classID, const SkMatrix& viewMatrix, GrColor initialColor)
34 : INHERITED(classID)
35 , fViewMatrix(viewMatrix)
joshualittf2384692015-09-10 11:00:51 -070036 , fColor(initialColor) {}
bsalomon1fcc01c2015-09-09 09:48:06 -070037
bsalomon1fcc01c2015-09-09 09:48:06 -070038 const GrStencilSettings& stencilSettings() const { return fStencilSettings; }
39 const GrPipelineOptimizations& opts() const { return fOpts; }
joshualittf2384692015-09-10 11:00:51 -070040 const SkMatrix& viewMatrix() const { return fViewMatrix; }
41 GrColor color() const { return fColor; }
42
bsalomon1fcc01c2015-09-09 09:48:06 -070043private:
44 void initBatchTracker(const GrPipelineOptimizations& opts) override {
joshualittf2384692015-09-10 11:00:51 -070045 opts.getOverrideColorIfSet(&fColor);
bsalomon1fcc01c2015-09-09 09:48:06 -070046 fOpts = opts;
47 }
48
joshualittf2384692015-09-10 11:00:51 -070049 SkMatrix fViewMatrix;
50 GrColor fColor;
bsalomon1fcc01c2015-09-09 09:48:06 -070051 GrStencilSettings fStencilSettings;
52 GrPipelineOptimizations fOpts;
53
54 typedef GrDrawBatch INHERITED;
55};
56
57class GrDrawPathBatch final : public GrDrawPathBatchBase {
58public:
reed1b55a962015-09-17 20:16:13 -070059 DEFINE_BATCH_CLASS_ID
60
bsalomon1fcc01c2015-09-09 09:48:06 -070061 // This can't return a more abstract type because we install the stencil settings late :(
joshualittf2384692015-09-10 11:00:51 -070062 static GrDrawPathBatchBase* Create(const SkMatrix& viewMatrix, GrColor color,
63 const GrPath* path) {
64 return new GrDrawPathBatch(viewMatrix, color, path);
bsalomonadd79ef2015-08-19 13:26:49 -070065 }
66
67 const char* name() const override { return "DrawPath"; }
68
bsalomon1fcc01c2015-09-09 09:48:06 -070069 SkString dumpInfo() const override;
bsalomonadd79ef2015-08-19 13:26:49 -070070
71private:
joshualittf2384692015-09-10 11:00:51 -070072 GrDrawPathBatch(const SkMatrix& viewMatrix, GrColor color, const GrPath* path)
reed1b55a962015-09-17 20:16:13 -070073 : INHERITED(ClassID(), viewMatrix, color)
bsalomon1fcc01c2015-09-09 09:48:06 -070074 , fPath(path) {
bsalomonadd79ef2015-08-19 13:26:49 -070075 fBounds = path->getBounds();
joshualittf2384692015-09-10 11:00:51 -070076 viewMatrix.mapRect(&fBounds);
bsalomonadd79ef2015-08-19 13:26:49 -070077 }
78
bsalomonadd79ef2015-08-19 13:26:49 -070079 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { return false; }
80
81 void onPrepare(GrBatchFlushState*) override {}
82
bsalomon1fcc01c2015-09-09 09:48:06 -070083 void onDraw(GrBatchFlushState* state) override;
84
85 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
86
87 typedef GrDrawPathBatchBase INHERITED;
88};
89
90/**
91 * This could be nested inside the batch class, but for now it must be declarable in a public
92 * header (GrDrawContext)
93 */
94class GrPathRangeDraw : public GrNonAtomicRef {
95public:
96 typedef GrPathRendering::PathTransformType TransformType;
97
98 static GrPathRangeDraw* Create(GrPathRange* range, TransformType transformType,
99 int reserveCnt) {
100 return SkNEW_ARGS(GrPathRangeDraw, (range, transformType, reserveCnt));
bsalomonadd79ef2015-08-19 13:26:49 -0700101 }
102
bsalomon1fcc01c2015-09-09 09:48:06 -0700103 void append(uint16_t index, float transform[]) {
104 fTransforms.push_back_n(GrPathRendering::PathTransformSize(fTransformType), transform);
105 fIndices.push_back(index);
106 }
107
108 int count() const { return fIndices.count(); }
109
110 TransformType transformType() const { return fTransformType; }
111
112 const float* transforms() const { return fTransforms.begin(); }
113
114 const uint16_t* indices() const { return fIndices.begin(); }
115
116 const GrPathRange* range() const { return fPathRange.get(); }
117
118 static bool CanMerge(const GrPathRangeDraw& a, const GrPathRangeDraw& b) {
119 return a.transformType() == b.transformType() && a.range() == b.range();
120 }
121
122private:
123 GrPathRangeDraw(GrPathRange* range, TransformType transformType, int reserveCnt)
bsalomona82bf952015-09-10 09:31:59 -0700124 : fPathRange(range)
bsalomon1fcc01c2015-09-09 09:48:06 -0700125 , fTransformType(transformType)
126 , fIndices(reserveCnt)
127 , fTransforms(reserveCnt * GrPathRendering::PathTransformSize(transformType)) {
128 SkDEBUGCODE(fUsedInBatch = false;)
129 }
130
131 // Reserve space for 64 paths where indices are 16 bit and transforms are translations.
132 static const int kIndexReserveCnt = 64;
133 static const int kTransformBufferReserveCnt = 2 * 64;
134
135 GrPendingIOResource<const GrPathRange, kRead_GrIOType> fPathRange;
136 GrPathRendering::PathTransformType fTransformType;
137 SkSTArray<kIndexReserveCnt, uint16_t, true> fIndices;
138 SkSTArray<kTransformBufferReserveCnt, float, true> fTransforms;
139
140 // To ensure we don't reuse these across batches.
141#ifdef SK_DEBUG
142 bool fUsedInBatch;
143 friend class GrDrawPathRangeBatch;
144#endif
145
146 typedef GrNonAtomicRef INHERITED;
147};
148
149// Template this if we decide to support index types other than 16bit
150class GrDrawPathRangeBatch final : public GrDrawPathBatchBase {
151public:
reed1b55a962015-09-17 20:16:13 -0700152 DEFINE_BATCH_CLASS_ID
153
154 // This can't return a more abstract type because we install the stencil settings late :(
joshualittf2384692015-09-10 11:00:51 -0700155 static GrDrawPathBatchBase* Create(const SkMatrix& viewMatrix, const SkMatrix& localMatrix,
156 GrColor color, GrPathRangeDraw* pathRangeDraw) {
157 return SkNEW_ARGS(GrDrawPathRangeBatch, (viewMatrix, localMatrix, color, pathRangeDraw));
bsalomon1fcc01c2015-09-09 09:48:06 -0700158 }
159
160 ~GrDrawPathRangeBatch() override;
161
162 const char* name() const override { return "DrawPathRange"; }
163
164 SkString dumpInfo() const override;
165
166private:
167 inline bool isWinding() const;
168
joshualittf2384692015-09-10 11:00:51 -0700169 GrDrawPathRangeBatch(const SkMatrix& viewMatrix, const SkMatrix& localMatrix, GrColor color,
170 GrPathRangeDraw* pathRangeDraw);
bsalomon1fcc01c2015-09-09 09:48:06 -0700171
172 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override;
173
174 void onPrepare(GrBatchFlushState*) override {}
175
176 void onDraw(GrBatchFlushState* state) override;
177
178 typedef SkTLList<GrPathRangeDraw*> DrawList;
179 DrawList fDraws;
180 int fTotalPathCount;
joshualittf2384692015-09-10 11:00:51 -0700181 SkMatrix fLocalMatrix;
bsalomon1fcc01c2015-09-09 09:48:06 -0700182
183 typedef GrDrawPathBatchBase INHERITED;
bsalomonadd79ef2015-08-19 13:26:49 -0700184};
185
186#endif