1 /*
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 "GrInOrderCommandBuilder.h"
9 
10 #include "GrColor.h"
11 #include "GrInOrderDrawBuffer.h"
12 #include "GrTemplates.h"
13 #include "SkPoint.h"
14 
path_fill_type_is_winding(const GrStencilSettings & pathStencilSettings)15 static 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 
recordDrawBatch(State * state,GrBatch * batch)28 GrTargetCommands::Cmd* GrInOrderCommandBuilder::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 
42 GrTargetCommands::Cmd*
recordStencilPath(const GrPipelineBuilder & pipelineBuilder,const GrPathProcessor * pathProc,const GrPath * path,const GrScissorState & scissorState,const GrStencilSettings & stencilSettings)43 GrInOrderCommandBuilder::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 
58 GrTargetCommands::Cmd*
recordDrawPath(State * state,const GrPathProcessor * pathProc,const GrPath * path,const GrStencilSettings & stencilSettings)59 GrInOrderCommandBuilder::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 
68 GrTargetCommands::Cmd*
recordDrawPaths(State * state,GrInOrderDrawBuffer * iodb,const GrPathProcessor * pathProc,const GrPathRange * pathRange,const void * indexValues,GrDrawTarget::PathIndexType indexType,const float transformValues[],GrDrawTarget::PathTransformType transformType,int count,const GrStencilSettings & stencilSettings,const GrDrawTarget::PipelineInfo & pipelineInfo)69 GrInOrderCommandBuilder::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