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