blob: aafc3455b206a7c38016eb07efe4e0d79e6173f2 [file] [log] [blame]
joshualitte46760e2015-05-05 08:41:50 -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#include "GrCommandBuilder.h"
9
10#include "GrColor.h"
11#include "GrInOrderDrawBuffer.h"
12#include "GrTemplates.h"
13#include "SkPoint.h"
14
15static bool path_fill_type_is_winding(const GrStencilSettings& pathStencilSettings) {
16 static const GrStencilSettings::Face pathFace = GrStencilSettings::kFront_Face;
17 bool isWinding = kInvert_StencilOp != pathStencilSettings.passOp(pathFace);
18 if (isWinding) {
19 // Double check that it is in fact winding.
20 SkASSERT(kIncClamp_StencilOp == pathStencilSettings.passOp(pathFace));
21 SkASSERT(kIncClamp_StencilOp == pathStencilSettings.failOp(pathFace));
22 SkASSERT(0x1 != pathStencilSettings.writeMask(pathFace));
23 SkASSERT(!pathStencilSettings.isTwoSided());
24 }
25 return isWinding;
26}
27
28GrTargetCommands::Cmd* GrCommandBuilder::recordDrawBatch(State* state, GrBatch* batch) {
29 // Check if there is a Batch Draw we can batch with
30 if (!this->cmdBuffer()->empty() &&
31 Cmd::kDrawBatch_CmdType == this->cmdBuffer()->back().type()) {
32 DrawBatch* previous = static_cast<DrawBatch*>(&this->cmdBuffer()->back());
33 if (previous->fState == state && previous->fBatch->combineIfPossible(batch)) {
34 return NULL;
35 }
36 }
37
38 return GrNEW_APPEND_TO_RECORDER(*this->cmdBuffer(), DrawBatch, (state, batch,
39 this->batchTarget()));
40}
41
42GrTargetCommands::Cmd*
43GrCommandBuilder::recordStencilPath(const GrPipelineBuilder& pipelineBuilder,
44 const GrPathProcessor* pathProc,
45 const GrPath* path,
46 const GrScissorState& scissorState,
47 const GrStencilSettings& stencilSettings) {
48 StencilPath* sp = GrNEW_APPEND_TO_RECORDER(*this->cmdBuffer(), StencilPath,
49 (path, pipelineBuilder.getRenderTarget()));
50
51 sp->fScissor = scissorState;
52 sp->fUseHWAA = pipelineBuilder.isHWAntialias();
53 sp->fViewMatrix = pathProc->viewMatrix();
54 sp->fStencil = stencilSettings;
55 return sp;
56}
57
58GrTargetCommands::Cmd*
59GrCommandBuilder::recordDrawPath(State* state,
60 const GrPathProcessor* pathProc,
61 const GrPath* path,
62 const GrStencilSettings& stencilSettings) {
63 DrawPath* dp = GrNEW_APPEND_TO_RECORDER(*this->cmdBuffer(), DrawPath, (state, path));
64 dp->fStencilSettings = stencilSettings;
65 return dp;
66}
67
68GrTargetCommands::Cmd*
69GrCommandBuilder::recordDrawPaths(State* state,
70 GrInOrderDrawBuffer* iodb,
71 const GrPathProcessor* pathProc,
72 const GrPathRange* pathRange,
73 const void* indexValues,
74 GrDrawTarget::PathIndexType indexType,
75 const float transformValues[],
76 GrDrawTarget::PathTransformType transformType,
77 int count,
78 const GrStencilSettings& stencilSettings,
79 const GrDrawTarget::PipelineInfo& pipelineInfo) {
80 SkASSERT(pathRange);
81 SkASSERT(indexValues);
82 SkASSERT(transformValues);
83
84 char* savedIndices;
85 float* savedTransforms;
86
87 iodb->appendIndicesAndTransforms(indexValues, indexType,
88 transformValues, transformType,
89 count, &savedIndices, &savedTransforms);
90
91 if (!this->cmdBuffer()->empty() &&
92 Cmd::kDrawPaths_CmdType == this->cmdBuffer()->back().type()) {
93 // The previous command was also DrawPaths. Try to collapse this call into the one
94 // before. Note that stenciling all the paths at once, then covering, may not be
95 // equivalent to two separate draw calls if there is overlap. Blending won't work,
96 // and the combined calls may also cancel each other's winding numbers in some
97 // places. For now the winding numbers are only an issue if the fill is even/odd,
98 // because DrawPaths is currently only used for glyphs, and glyphs in the same
99 // font tend to all wind in the same direction.
100 DrawPaths* previous = static_cast<DrawPaths*>(&this->cmdBuffer()->back());
101 if (pathRange == previous->pathRange() &&
102 indexType == previous->fIndexType &&
103 transformType == previous->fTransformType &&
104 stencilSettings == previous->fStencilSettings &&
105 path_fill_type_is_winding(stencilSettings) &&
106 !pipelineInfo.willBlendWithDst(pathProc) &&
107 previous->fState == state) {
108 const int indexBytes = GrPathRange::PathIndexSizeInBytes(indexType);
109 const int xformSize = GrPathRendering::PathTransformSize(transformType);
110 if (&previous->fIndices[previous->fCount*indexBytes] == savedIndices &&
111 (0 == xformSize ||
112 &previous->fTransforms[previous->fCount*xformSize] == savedTransforms)) {
113 // Fold this DrawPaths call into the one previous.
114 previous->fCount += count;
115 return NULL;
116 }
117 }
118 }
119
120 DrawPaths* dp = GrNEW_APPEND_TO_RECORDER(*this->cmdBuffer(), DrawPaths, (state, pathRange));
121 dp->fIndices = savedIndices;
122 dp->fIndexType = indexType;
123 dp->fTransforms = savedTransforms;
124 dp->fTransformType = transformType;
125 dp->fCount = count;
126 dp->fStencilSettings = stencilSettings;
127 return dp;
128}
129
130GrTargetCommands::Cmd* GrCommandBuilder::recordClear(const SkIRect* rect,
131 GrColor color,
132 bool canIgnoreRect,
133 GrRenderTarget* renderTarget) {
134 SkASSERT(renderTarget);
135
136 SkIRect r;
137 if (NULL == rect) {
138 // We could do something smart and remove previous draws and clears to
139 // the current render target. If we get that smart we have to make sure
140 // those draws aren't read before this clear (render-to-texture).
141 r.setLTRB(0, 0, renderTarget->width(), renderTarget->height());
142 rect = &r;
143 }
144 Clear* clr = GrNEW_APPEND_TO_RECORDER(*this->cmdBuffer(), Clear, (renderTarget));
145 GrColorIsPMAssert(color);
146 clr->fColor = color;
147 clr->fRect = *rect;
148 clr->fCanIgnoreRect = canIgnoreRect;
149 return clr;
150}
151
152GrTargetCommands::Cmd* GrCommandBuilder::recordClearStencilClip(const SkIRect& rect,
153 bool insideClip,
154 GrRenderTarget* renderTarget) {
155 SkASSERT(renderTarget);
156
157 ClearStencilClip* clr = GrNEW_APPEND_TO_RECORDER(*this->cmdBuffer(),
158 ClearStencilClip,
159 (renderTarget));
160 clr->fRect = rect;
161 clr->fInsideClip = insideClip;
162 return clr;
163}
164
165GrTargetCommands::Cmd* GrCommandBuilder::recordDiscard(GrRenderTarget* renderTarget) {
166 SkASSERT(renderTarget);
167
168 Clear* clr = GrNEW_APPEND_TO_RECORDER(*this->cmdBuffer(), Clear, (renderTarget));
169 clr->fColor = GrColor_ILLEGAL;
170 return clr;
171}
172
173GrTargetCommands::Cmd* GrCommandBuilder::recordCopySurface(GrSurface* dst,
174 GrSurface* src,
175 const SkIRect& srcRect,
176 const SkIPoint& dstPoint) {
177 CopySurface* cs = GrNEW_APPEND_TO_RECORDER(*this->cmdBuffer(), CopySurface, (dst, src));
178 cs->fSrcRect = srcRect;
179 cs->fDstPoint = dstPoint;
180 return cs;
181}
182
183GrTargetCommands::Cmd*
184GrCommandBuilder::recordXferBarrierIfNecessary(const GrPipeline& pipeline,
185 const GrDrawTargetCaps& caps) {
186 const GrXferProcessor& xp = *pipeline.getXferProcessor();
187 GrRenderTarget* rt = pipeline.getRenderTarget();
188
189 GrXferBarrierType barrierType;
190 if (!xp.willNeedXferBarrier(rt, caps, &barrierType)) {
191 return NULL;
192 }
193
194 XferBarrier* xb = GrNEW_APPEND_TO_RECORDER(*this->cmdBuffer(), XferBarrier, ());
195 xb->fBarrierType = barrierType;
196 return xb;
197}