blob: d6d071122eaebc478af1fc6703ec74e7d2ab9aaa [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
cdalton8585dd22015-10-08 08:04:09 -070098 static GrPathRangeDraw* Create(TransformType transformType, int reserveCnt) {
99 return new GrPathRangeDraw(transformType, reserveCnt);
bsalomonadd79ef2015-08-19 13:26:49 -0700100 }
101
bsalomon1fcc01c2015-09-09 09:48:06 -0700102 void append(uint16_t index, float transform[]) {
103 fTransforms.push_back_n(GrPathRendering::PathTransformSize(fTransformType), transform);
104 fIndices.push_back(index);
105 }
106
107 int count() const { return fIndices.count(); }
108
109 TransformType transformType() const { return fTransformType; }
110
111 const float* transforms() const { return fTransforms.begin(); }
112
113 const uint16_t* indices() const { return fIndices.begin(); }
114
bsalomon1fcc01c2015-09-09 09:48:06 -0700115 static bool CanMerge(const GrPathRangeDraw& a, const GrPathRangeDraw& b) {
cdalton8585dd22015-10-08 08:04:09 -0700116 return a.transformType() == b.transformType();
bsalomon1fcc01c2015-09-09 09:48:06 -0700117 }
118
119private:
cdalton8585dd22015-10-08 08:04:09 -0700120 GrPathRangeDraw(TransformType transformType, int reserveCnt)
121 : fTransformType(transformType)
bsalomon1fcc01c2015-09-09 09:48:06 -0700122 , fIndices(reserveCnt)
123 , fTransforms(reserveCnt * GrPathRendering::PathTransformSize(transformType)) {
124 SkDEBUGCODE(fUsedInBatch = false;)
125 }
126
127 // Reserve space for 64 paths where indices are 16 bit and transforms are translations.
128 static const int kIndexReserveCnt = 64;
129 static const int kTransformBufferReserveCnt = 2 * 64;
130
bsalomon1fcc01c2015-09-09 09:48:06 -0700131 GrPathRendering::PathTransformType fTransformType;
132 SkSTArray<kIndexReserveCnt, uint16_t, true> fIndices;
133 SkSTArray<kTransformBufferReserveCnt, float, true> fTransforms;
134
135 // To ensure we don't reuse these across batches.
136#ifdef SK_DEBUG
137 bool fUsedInBatch;
138 friend class GrDrawPathRangeBatch;
139#endif
140
141 typedef GrNonAtomicRef INHERITED;
142};
143
144// Template this if we decide to support index types other than 16bit
145class GrDrawPathRangeBatch final : public GrDrawPathBatchBase {
146public:
reed1b55a962015-09-17 20:16:13 -0700147 DEFINE_BATCH_CLASS_ID
148
149 // This can't return a more abstract type because we install the stencil settings late :(
joshualittf2384692015-09-10 11:00:51 -0700150 static GrDrawPathBatchBase* Create(const SkMatrix& viewMatrix, const SkMatrix& localMatrix,
cdalton8585dd22015-10-08 08:04:09 -0700151 GrColor color, GrPathRange* range, GrPathRangeDraw* draw) {
152 return new GrDrawPathRangeBatch(viewMatrix, localMatrix, color, range, draw);
bsalomon1fcc01c2015-09-09 09:48:06 -0700153 }
154
155 ~GrDrawPathRangeBatch() override;
156
157 const char* name() const override { return "DrawPathRange"; }
158
159 SkString dumpInfo() const override;
160
161private:
162 inline bool isWinding() const;
163
joshualittf2384692015-09-10 11:00:51 -0700164 GrDrawPathRangeBatch(const SkMatrix& viewMatrix, const SkMatrix& localMatrix, GrColor color,
cdalton8585dd22015-10-08 08:04:09 -0700165 GrPathRange* range, GrPathRangeDraw* draw);
bsalomon1fcc01c2015-09-09 09:48:06 -0700166
167 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override;
168
169 void onPrepare(GrBatchFlushState*) override {}
170
171 void onDraw(GrBatchFlushState* state) override;
172
cdalton8585dd22015-10-08 08:04:09 -0700173 typedef GrPendingIOResource<const GrPathRange, kRead_GrIOType> PendingPathRange;
bsalomon1fcc01c2015-09-09 09:48:06 -0700174 typedef SkTLList<GrPathRangeDraw*> DrawList;
cdalton8585dd22015-10-08 08:04:09 -0700175 PendingPathRange fPathRange;
176 DrawList fDraws;
177 int fTotalPathCount;
178 SkMatrix fLocalMatrix;
bsalomon1fcc01c2015-09-09 09:48:06 -0700179
180 typedef GrDrawPathBatchBase INHERITED;
bsalomonadd79ef2015-08-19 13:26:49 -0700181};
182
183#endif