robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 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 "GrTargetCommands.h" |
| 9 | |
| 10 | #include "GrColor.h" |
| 11 | #include "GrDefaultGeoProcFactory.h" |
| 12 | #include "GrInOrderDrawBuffer.h" |
| 13 | #include "GrTemplates.h" |
| 14 | #include "SkPoint.h" |
| 15 | |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 16 | static bool path_fill_type_is_winding(const GrStencilSettings& pathStencilSettings) { |
| 17 | static const GrStencilSettings::Face pathFace = GrStencilSettings::kFront_Face; |
| 18 | bool isWinding = kInvert_StencilOp != pathStencilSettings.passOp(pathFace); |
| 19 | if (isWinding) { |
| 20 | // Double check that it is in fact winding. |
| 21 | SkASSERT(kIncClamp_StencilOp == pathStencilSettings.passOp(pathFace)); |
| 22 | SkASSERT(kIncClamp_StencilOp == pathStencilSettings.failOp(pathFace)); |
| 23 | SkASSERT(0x1 != pathStencilSettings.writeMask(pathFace)); |
| 24 | SkASSERT(!pathStencilSettings.isTwoSided()); |
| 25 | } |
| 26 | return isWinding; |
| 27 | } |
| 28 | |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 29 | GrTargetCommands::Cmd* GrTargetCommands::recordDrawBatch(State* state, GrBatch* batch) { |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 30 | // Check if there is a Batch Draw we can batch with |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 31 | if (!fCmdBuffer.empty() && Cmd::kDrawBatch_CmdType == fCmdBuffer.back().type()) { |
joshualitt | a30009b | 2015-04-30 09:05:54 -0700 | [diff] [blame] | 32 | DrawBatch* previous = static_cast<DrawBatch*>(&fCmdBuffer.back()); |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 33 | if (previous->fState == state && previous->fBatch->combineIfPossible(batch)) { |
joshualitt | a30009b | 2015-04-30 09:05:54 -0700 | [diff] [blame] | 34 | return NULL; |
| 35 | } |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 36 | } |
| 37 | |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 38 | return GrNEW_APPEND_TO_RECORDER(fCmdBuffer, DrawBatch, (state, batch, &fBatchTarget)); |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | GrTargetCommands::Cmd* GrTargetCommands::recordStencilPath( |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 42 | const GrPipelineBuilder& pipelineBuilder, |
| 43 | const GrPathProcessor* pathProc, |
| 44 | const GrPath* path, |
| 45 | const GrScissorState& scissorState, |
| 46 | const GrStencilSettings& stencilSettings) { |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 47 | StencilPath* sp = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, StencilPath, |
| 48 | (path, pipelineBuilder.getRenderTarget())); |
| 49 | |
| 50 | sp->fScissor = scissorState; |
| 51 | sp->fUseHWAA = pipelineBuilder.isHWAntialias(); |
| 52 | sp->fViewMatrix = pathProc->viewMatrix(); |
| 53 | sp->fStencil = stencilSettings; |
| 54 | return sp; |
| 55 | } |
| 56 | |
| 57 | GrTargetCommands::Cmd* GrTargetCommands::recordDrawPath( |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 58 | State* state, |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 59 | const GrPathProcessor* pathProc, |
| 60 | const GrPath* path, |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 61 | const GrStencilSettings& stencilSettings) { |
| 62 | DrawPath* dp = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, DrawPath, (state, path)); |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 63 | dp->fStencilSettings = stencilSettings; |
| 64 | return dp; |
| 65 | } |
| 66 | |
| 67 | GrTargetCommands::Cmd* GrTargetCommands::recordDrawPaths( |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 68 | State* state, |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 69 | GrInOrderDrawBuffer* iodb, |
| 70 | const GrPathProcessor* pathProc, |
| 71 | const GrPathRange* pathRange, |
| 72 | const void* indexValues, |
| 73 | GrDrawTarget::PathIndexType indexType, |
| 74 | const float transformValues[], |
| 75 | GrDrawTarget::PathTransformType transformType, |
| 76 | int count, |
| 77 | const GrStencilSettings& stencilSettings, |
| 78 | const GrDrawTarget::PipelineInfo& pipelineInfo) { |
| 79 | SkASSERT(pathRange); |
| 80 | SkASSERT(indexValues); |
| 81 | SkASSERT(transformValues); |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 82 | |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 83 | char* savedIndices; |
| 84 | float* savedTransforms; |
| 85 | |
| 86 | iodb->appendIndicesAndTransforms(indexValues, indexType, |
| 87 | transformValues, transformType, |
| 88 | count, &savedIndices, &savedTransforms); |
| 89 | |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 90 | if (!fCmdBuffer.empty() && Cmd::kDrawPaths_CmdType == fCmdBuffer.back().type()) { |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 91 | // The previous command was also DrawPaths. Try to collapse this call into the one |
| 92 | // before. Note that stenciling all the paths at once, then covering, may not be |
| 93 | // equivalent to two separate draw calls if there is overlap. Blending won't work, |
| 94 | // and the combined calls may also cancel each other's winding numbers in some |
| 95 | // places. For now the winding numbers are only an issue if the fill is even/odd, |
| 96 | // because DrawPaths is currently only used for glyphs, and glyphs in the same |
| 97 | // font tend to all wind in the same direction. |
| 98 | DrawPaths* previous = static_cast<DrawPaths*>(&fCmdBuffer.back()); |
| 99 | if (pathRange == previous->pathRange() && |
| 100 | indexType == previous->fIndexType && |
| 101 | transformType == previous->fTransformType && |
| 102 | stencilSettings == previous->fStencilSettings && |
| 103 | path_fill_type_is_winding(stencilSettings) && |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 104 | !pipelineInfo.willBlendWithDst(pathProc) && |
| 105 | previous->fState == state) { |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 106 | const int indexBytes = GrPathRange::PathIndexSizeInBytes(indexType); |
| 107 | const int xformSize = GrPathRendering::PathTransformSize(transformType); |
| 108 | if (&previous->fIndices[previous->fCount*indexBytes] == savedIndices && |
| 109 | (0 == xformSize || |
| 110 | &previous->fTransforms[previous->fCount*xformSize] == savedTransforms)) { |
| 111 | // Fold this DrawPaths call into the one previous. |
| 112 | previous->fCount += count; |
| 113 | return NULL; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 118 | DrawPaths* dp = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, DrawPaths, (state, pathRange)); |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 119 | dp->fIndices = savedIndices; |
| 120 | dp->fIndexType = indexType; |
| 121 | dp->fTransforms = savedTransforms; |
| 122 | dp->fTransformType = transformType; |
| 123 | dp->fCount = count; |
| 124 | dp->fStencilSettings = stencilSettings; |
| 125 | return dp; |
| 126 | } |
| 127 | |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 128 | GrTargetCommands::Cmd* GrTargetCommands::recordClear(const SkIRect* rect, |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 129 | GrColor color, |
| 130 | bool canIgnoreRect, |
| 131 | GrRenderTarget* renderTarget) { |
| 132 | SkASSERT(renderTarget); |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 133 | |
| 134 | SkIRect r; |
| 135 | if (NULL == rect) { |
| 136 | // We could do something smart and remove previous draws and clears to |
| 137 | // the current render target. If we get that smart we have to make sure |
| 138 | // those draws aren't read before this clear (render-to-texture). |
| 139 | r.setLTRB(0, 0, renderTarget->width(), renderTarget->height()); |
| 140 | rect = &r; |
| 141 | } |
| 142 | Clear* clr = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, Clear, (renderTarget)); |
| 143 | GrColorIsPMAssert(color); |
| 144 | clr->fColor = color; |
| 145 | clr->fRect = *rect; |
| 146 | clr->fCanIgnoreRect = canIgnoreRect; |
| 147 | return clr; |
| 148 | } |
| 149 | |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 150 | GrTargetCommands::Cmd* GrTargetCommands::recordClearStencilClip(const SkIRect& rect, |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 151 | bool insideClip, |
| 152 | GrRenderTarget* renderTarget) { |
| 153 | SkASSERT(renderTarget); |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 154 | |
| 155 | ClearStencilClip* clr = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, ClearStencilClip, (renderTarget)); |
| 156 | clr->fRect = rect; |
| 157 | clr->fInsideClip = insideClip; |
| 158 | return clr; |
| 159 | } |
| 160 | |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 161 | GrTargetCommands::Cmd* GrTargetCommands::recordDiscard(GrRenderTarget* renderTarget) { |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 162 | SkASSERT(renderTarget); |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 163 | |
| 164 | Clear* clr = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, Clear, (renderTarget)); |
| 165 | clr->fColor = GrColor_ILLEGAL; |
| 166 | return clr; |
| 167 | } |
| 168 | |
| 169 | void GrTargetCommands::reset() { |
| 170 | fCmdBuffer.reset(); |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void GrTargetCommands::flush(GrInOrderDrawBuffer* iodb) { |
| 174 | if (fCmdBuffer.empty()) { |
| 175 | return; |
| 176 | } |
| 177 | |
joshualitt | 385e26e | 2015-04-27 11:42:30 -0700 | [diff] [blame] | 178 | GrGpu* gpu = iodb->getGpu(); |
| 179 | |
joshualitt | 0dcb8e3 | 2015-04-27 12:03:05 -0700 | [diff] [blame] | 180 | // Loop over all batches and generate geometry |
| 181 | CmdBuffer::Iter genIter(fCmdBuffer); |
| 182 | while (genIter.next()) { |
| 183 | if (Cmd::kDrawBatch_CmdType == genIter->type()) { |
| 184 | DrawBatch* db = reinterpret_cast<DrawBatch*>(genIter.get()); |
| 185 | fBatchTarget.resetNumberOfDraws(); |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 186 | db->fBatch->generateGeometry(&fBatchTarget, db->fState->getPipeline()); |
joshualitt | 0dcb8e3 | 2015-04-27 12:03:05 -0700 | [diff] [blame] | 187 | db->fBatch->setNumberOfDraws(fBatchTarget.numberOfDraws()); |
joshualitt | 0dcb8e3 | 2015-04-27 12:03:05 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
joshualitt | 0dcb8e3 | 2015-04-27 12:03:05 -0700 | [diff] [blame] | 190 | |
| 191 | iodb->getVertexAllocPool()->unmap(); |
| 192 | iodb->getIndexAllocPool()->unmap(); |
| 193 | fBatchTarget.preFlush(); |
| 194 | |
| 195 | CmdBuffer::Iter iter(fCmdBuffer); |
| 196 | |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 197 | while (iter.next()) { |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 198 | GrGpuTraceMarker newMarker("", -1); |
| 199 | SkString traceString; |
| 200 | if (iter->isTraced()) { |
robertphillips | bca3c9f | 2015-03-05 09:17:17 -0800 | [diff] [blame] | 201 | traceString = iodb->getCmdString(iter->markerID()); |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 202 | newMarker.fMarker = traceString.c_str(); |
| 203 | gpu->addGpuTraceMarker(&newMarker); |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 204 | } |
| 205 | |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 206 | iter->execute(gpu); |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 207 | if (iter->isTraced()) { |
| 208 | gpu->removeGpuTraceMarker(&newMarker); |
| 209 | } |
| 210 | } |
| 211 | |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 212 | fBatchTarget.postFlush(); |
| 213 | } |
| 214 | |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 215 | void GrTargetCommands::StencilPath::execute(GrGpu* gpu) { |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 216 | GrGpu::StencilPathState state; |
| 217 | state.fRenderTarget = fRenderTarget.get(); |
| 218 | state.fScissor = &fScissor; |
| 219 | state.fStencil = &fStencil; |
| 220 | state.fUseHWAA = fUseHWAA; |
| 221 | state.fViewMatrix = &fViewMatrix; |
| 222 | |
| 223 | gpu->stencilPath(this->path(), state); |
| 224 | } |
| 225 | |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 226 | void GrTargetCommands::DrawPath::execute(GrGpu* gpu) { |
| 227 | if (!fState->fCompiled) { |
| 228 | gpu->buildProgramDesc(&fState->fDesc, *fState->fPrimitiveProcessor, *fState->getPipeline(), |
| 229 | fState->fBatchTracker); |
| 230 | fState->fCompiled = true; |
| 231 | } |
| 232 | DrawArgs args(fState->fPrimitiveProcessor.get(), fState->getPipeline(), |
| 233 | &fState->fDesc, &fState->fBatchTracker); |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 234 | gpu->drawPath(args, this->path(), fStencilSettings); |
| 235 | } |
| 236 | |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 237 | void GrTargetCommands::DrawPaths::execute(GrGpu* gpu) { |
| 238 | if (!fState->fCompiled) { |
| 239 | gpu->buildProgramDesc(&fState->fDesc, *fState->fPrimitiveProcessor, *fState->getPipeline(), |
| 240 | fState->fBatchTracker); |
| 241 | fState->fCompiled = true; |
| 242 | } |
| 243 | DrawArgs args(fState->fPrimitiveProcessor.get(), fState->getPipeline(), |
| 244 | &fState->fDesc, &fState->fBatchTracker); |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 245 | gpu->drawPaths(args, this->pathRange(), |
| 246 | fIndices, fIndexType, |
| 247 | fTransforms, fTransformType, |
| 248 | fCount, fStencilSettings); |
| 249 | } |
| 250 | |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 251 | void GrTargetCommands::DrawBatch::execute(GrGpu*) { |
| 252 | fBatchTarget->flushNext(fBatch->numberOfDraws()); |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 253 | } |
| 254 | |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 255 | void GrTargetCommands::Clear::execute(GrGpu* gpu) { |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 256 | if (GrColor_ILLEGAL == fColor) { |
| 257 | gpu->discard(this->renderTarget()); |
| 258 | } else { |
| 259 | gpu->clear(&fRect, fColor, fCanIgnoreRect, this->renderTarget()); |
| 260 | } |
| 261 | } |
| 262 | |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 263 | void GrTargetCommands::ClearStencilClip::execute(GrGpu* gpu) { |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 264 | gpu->clearStencilClip(fRect, fInsideClip, this->renderTarget()); |
| 265 | } |
| 266 | |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 267 | void GrTargetCommands::CopySurface::execute(GrGpu* gpu) { |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 268 | gpu->copySurface(this->dst(), this->src(), fSrcRect, fDstPoint); |
| 269 | } |
| 270 | |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 271 | void GrTargetCommands::XferBarrier::execute(GrGpu* gpu) { |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 272 | gpu->xferBarrier(fBarrierType); |
| 273 | } |
| 274 | |
bsalomon | a73239a | 2015-04-28 13:35:17 -0700 | [diff] [blame] | 275 | GrTargetCommands::Cmd* GrTargetCommands::recordCopySurface(GrSurface* dst, |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 276 | GrSurface* src, |
| 277 | const SkIRect& srcRect, |
| 278 | const SkIPoint& dstPoint) { |
bsalomon | a73239a | 2015-04-28 13:35:17 -0700 | [diff] [blame] | 279 | CopySurface* cs = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, CopySurface, (dst, src)); |
| 280 | cs->fSrcRect = srcRect; |
| 281 | cs->fDstPoint = dstPoint; |
| 282 | return cs; |
robertphillips | 193ea93 | 2015-03-03 12:40:49 -0800 | [diff] [blame] | 283 | } |
| 284 | |
joshualitt | 0911430 | 2015-05-04 10:41:57 -0700 | [diff] [blame] | 285 | void GrTargetCommands::recordXferBarrierIfNecessary(const GrPipeline& pipeline, |
| 286 | GrInOrderDrawBuffer* iodb) { |
| 287 | const GrXferProcessor& xp = *pipeline.getXferProcessor(); |
| 288 | GrRenderTarget* rt = pipeline.getRenderTarget(); |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 289 | |
| 290 | GrXferBarrierType barrierType; |
| 291 | if (!xp.willNeedXferBarrier(rt, *iodb->caps(), &barrierType)) { |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | XferBarrier* xb = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, XferBarrier, ()); |
| 296 | xb->fBarrierType = barrierType; |
| 297 | |
| 298 | iodb->recordTraceMarkersIfNecessary(xb); |
| 299 | } |
| 300 | |