egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 "GrOptDrawState.h" |
| 9 | |
| 10 | #include "GrDrawState.h" |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 11 | #include "GrDrawTargetCaps.h" |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 12 | #include "GrGpu.h" |
egdaniel | b6cbc38 | 2014-11-13 11:00:34 -0800 | [diff] [blame] | 13 | #include "GrProcOptInfo.h" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 14 | #include "GrXferProcessor.h" |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 15 | |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 16 | GrOptDrawState::GrOptDrawState(const GrDrawState& drawState, |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 17 | const GrGeometryProcessor* gp, |
| 18 | const GrPathProcessor* pathProc, |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 19 | const GrDrawTargetCaps& caps, |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 20 | const GrScissorState& scissorState, |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 21 | const GrDeviceCoordTexture* dstCopy, |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 22 | GrGpu::DrawType drawType) |
| 23 | : fFinalized(false) { |
| 24 | fDrawType = drawType; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 25 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 26 | // Copy GeometryProcesssor from DS or ODS |
| 27 | if (gp) { |
| 28 | SkASSERT(!pathProc); |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 29 | SkASSERT(!GrGpu::IsPathRenderingDrawType(drawType)); |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 30 | fGeometryProcessor.reset(gp); |
| 31 | fPrimitiveProcessor.reset(gp); |
| 32 | } else { |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 33 | SkASSERT(!gp && pathProc && GrGpu::IsPathRenderingDrawType(drawType)); |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 34 | fPrimitiveProcessor.reset(pathProc); |
| 35 | } |
| 36 | |
| 37 | |
| 38 | const GrProcOptInfo& colorPOI = drawState.colorProcInfo(fPrimitiveProcessor); |
| 39 | const GrProcOptInfo& coveragePOI = drawState.coverageProcInfo(fPrimitiveProcessor); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 40 | |
| 41 | // Create XferProcessor from DS's XPFactory |
| 42 | SkAutoTUnref<GrXferProcessor> xferProcessor( |
| 43 | drawState.getXPFactory()->createXferProcessor(colorPOI, coveragePOI)); |
| 44 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 45 | GrColor overrideColor = GrColor_ILLEGAL; |
| 46 | if (colorPOI.firstEffectiveStageIndex() != 0) { |
| 47 | overrideColor = colorPOI.inputColorToEffectiveStage(); |
| 48 | } |
| 49 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 50 | GrXferProcessor::OptFlags optFlags; |
| 51 | if (xferProcessor) { |
| 52 | fXferProcessor.reset(xferProcessor.get()); |
| 53 | |
| 54 | optFlags = xferProcessor->getOptimizations(colorPOI, |
| 55 | coveragePOI, |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 56 | drawState.getStencil().doesWrite(), |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 57 | &overrideColor, |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 58 | caps); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 59 | } |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 60 | |
| 61 | // When path rendering the stencil settings are not always set on the draw state |
| 62 | // so we must check the draw type. In cases where we will skip drawing we simply return a |
| 63 | // null GrOptDrawState. |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 64 | if (!xferProcessor || (GrXferProcessor::kSkipDraw_OptFlag & optFlags)) { |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 65 | // Set the fields that don't default init and return. The lack of a render target will |
| 66 | // indicate that this can be skipped. |
| 67 | fFlags = 0; |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 68 | fDrawFace = GrDrawState::kInvalid_DrawFace; |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 69 | fViewMatrix.reset(); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | fRenderTarget.reset(drawState.fRenderTarget.get()); |
| 74 | SkASSERT(fRenderTarget); |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 75 | fScissorState = scissorState; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 76 | fViewMatrix = drawState.getViewMatrix(); |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 77 | fStencilSettings = drawState.getStencil(); |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 78 | fDrawFace = drawState.getDrawFace(); |
joshualitt | 9176e2c | 2014-11-20 07:28:52 -0800 | [diff] [blame] | 79 | // TODO move this out of optDrawState |
| 80 | if (dstCopy) { |
| 81 | fDstCopy = *dstCopy; |
| 82 | } |
| 83 | |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 84 | fFlags = 0; |
| 85 | if (drawState.isHWAntialias()) { |
| 86 | fFlags |= kHWAA_Flag; |
| 87 | } |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 88 | if (drawState.isDither()) { |
| 89 | fFlags |= kDither_Flag; |
| 90 | } |
| 91 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 92 | // TODO move local coords completely into GP |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 93 | bool hasLocalCoords = gp && gp->hasLocalCoords(); |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 94 | |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 95 | int firstColorStageIdx = colorPOI.firstEffectiveStageIndex(); |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 96 | |
| 97 | // TODO: Once we can handle single or four channel input into coverage stages then we can use |
| 98 | // drawState's coverageProcInfo (like color above) to set this initial information. |
| 99 | int firstCoverageStageIdx = 0; |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 100 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 101 | GrXferProcessor::BlendInfo blendInfo; |
| 102 | fXferProcessor->getBlendInfo(&blendInfo); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 103 | |
| 104 | this->adjustProgramFromOptimizations(drawState, optFlags, colorPOI, coveragePOI, |
| 105 | &firstColorStageIdx, &firstCoverageStageIdx); |
| 106 | |
| 107 | fDescInfo.fRequiresLocalCoordAttrib = hasLocalCoords; |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 108 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 109 | bool usesLocalCoords = false; |
| 110 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 111 | // Copy Stages from DS to ODS |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 112 | for (int i = firstColorStageIdx; i < drawState.numColorStages(); ++i) { |
| 113 | SkNEW_APPEND_TO_TARRAY(&fFragmentStages, |
| 114 | GrPendingFragmentStage, |
joshualitt | 40d4bd8 | 2014-12-29 09:04:40 -0800 | [diff] [blame^] | 115 | (drawState.fColorStages[i])); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 116 | usesLocalCoords = usesLocalCoords || |
joshualitt | 40d4bd8 | 2014-12-29 09:04:40 -0800 | [diff] [blame^] | 117 | drawState.fColorStages[i].processor()->usesLocalCoords(); |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 118 | } |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 119 | |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 120 | fNumColorStages = fFragmentStages.count(); |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 121 | for (int i = firstCoverageStageIdx; i < drawState.numCoverageStages(); ++i) { |
| 122 | SkNEW_APPEND_TO_TARRAY(&fFragmentStages, |
| 123 | GrPendingFragmentStage, |
joshualitt | 40d4bd8 | 2014-12-29 09:04:40 -0800 | [diff] [blame^] | 124 | (drawState.fCoverageStages[i])); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 125 | usesLocalCoords = usesLocalCoords || |
joshualitt | 40d4bd8 | 2014-12-29 09:04:40 -0800 | [diff] [blame^] | 126 | drawState.fCoverageStages[i].processor()->usesLocalCoords(); |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 127 | } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 128 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 129 | // let the GP init the batch tracker |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 130 | GrGeometryProcessor::InitBT init; |
| 131 | init.fColorIgnored = SkToBool(optFlags & GrXferProcessor::kIgnoreColor_OptFlag); |
| 132 | init.fOverrideColor = init.fColorIgnored ? GrColor_ILLEGAL : overrideColor; |
| 133 | init.fCoverageIgnored = SkToBool(optFlags & GrXferProcessor::kIgnoreCoverage_OptFlag); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 134 | init.fUsesLocalCoords = usesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 135 | fPrimitiveProcessor->initBatchTracker(&fBatchTracker, init); |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 136 | } |
| 137 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 138 | void GrOptDrawState::adjustProgramFromOptimizations(const GrDrawState& ds, |
| 139 | GrXferProcessor::OptFlags flags, |
| 140 | const GrProcOptInfo& colorPOI, |
| 141 | const GrProcOptInfo& coveragePOI, |
| 142 | int* firstColorStageIdx, |
| 143 | int* firstCoverageStageIdx) { |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 144 | fDescInfo.fReadsDst = false; |
| 145 | fDescInfo.fReadsFragPosition = false; |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 146 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 147 | if ((flags & GrXferProcessor::kIgnoreColor_OptFlag) || |
| 148 | (flags & GrXferProcessor::kOverrideColor_OptFlag)) { |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 149 | *firstColorStageIdx = ds.numColorStages(); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 150 | } else { |
| 151 | fDescInfo.fReadsDst = colorPOI.readsDst(); |
| 152 | fDescInfo.fReadsFragPosition = colorPOI.readsFragPosition(); |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 153 | } |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 154 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 155 | if (flags & GrXferProcessor::kIgnoreCoverage_OptFlag) { |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 156 | *firstCoverageStageIdx = ds.numCoverageStages(); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 157 | } else { |
| 158 | if (coveragePOI.readsDst()) { |
| 159 | fDescInfo.fReadsDst = true; |
| 160 | } |
| 161 | if (coveragePOI.readsFragPosition()) { |
| 162 | fDescInfo.fReadsFragPosition = true; |
| 163 | } |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 167 | void GrOptDrawState::finalize(GrGpu* gpu) { |
| 168 | gpu->buildProgramDesc(*this, fDescInfo, fDrawType, &fDesc); |
| 169 | fFinalized = true; |
| 170 | } |
| 171 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 172 | //////////////////////////////////////////////////////////////////////////////// |
| 173 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 174 | bool GrOptDrawState::combineIfPossible(const GrOptDrawState& that) { |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 175 | if (fDescInfo != that.fDescInfo) { |
joshualitt | f78c60c | 2014-12-04 06:01:45 -0800 | [diff] [blame] | 176 | return false; |
| 177 | } |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 178 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 179 | if (this->getRenderTarget() != that.getRenderTarget() || |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 180 | this->fFragmentStages.count() != that.fFragmentStages.count() || |
| 181 | this->fNumColorStages != that.fNumColorStages || |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 182 | this->fScissorState != that.fScissorState || |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 183 | !this->fViewMatrix.cheapEqualTo(that.fViewMatrix) || |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 184 | this->fDrawType != that.fDrawType || |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 185 | this->fFlags != that.fFlags || |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 186 | this->fStencilSettings != that.fStencilSettings || |
joshualitt | 9176e2c | 2014-11-20 07:28:52 -0800 | [diff] [blame] | 187 | this->fDrawFace != that.fDrawFace || |
| 188 | this->fDstCopy.texture() != that.fDstCopy.texture()) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 189 | return false; |
| 190 | } |
| 191 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 192 | if (!this->getPrimitiveProcessor()->canMakeEqual(fBatchTracker, |
| 193 | *that.getPrimitiveProcessor(), |
| 194 | that.getBatchTracker())) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 195 | return false; |
| 196 | } |
| 197 | |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 198 | if (!this->getXferProcessor()->isEqual(*that.getXferProcessor())) { |
| 199 | return false; |
| 200 | } |
| 201 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 202 | // The program desc comparison should have already assured that the stage counts match. |
| 203 | SkASSERT(this->numFragmentStages() == that.numFragmentStages()); |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 204 | for (int i = 0; i < this->numFragmentStages(); i++) { |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 205 | |
| 206 | if (this->getFragmentStage(i) != that.getFragmentStage(i)) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 207 | return false; |
| 208 | } |
| 209 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 210 | |
| 211 | // Now update the GrPrimitiveProcessor's batch tracker |
| 212 | fPrimitiveProcessor->makeEqual(&fBatchTracker, that.getBatchTracker()); |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 213 | return true; |
| 214 | } |
| 215 | |