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 | 8c0f615 | 2014-12-10 14:12:22 -0800 | [diff] [blame] | 17 | GrColor color, |
| 18 | uint8_t coverage, |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 19 | const GrDrawTargetCaps& caps, |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 20 | const ScissorState& 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) { |
joshualitt | 8c0f615 | 2014-12-10 14:12:22 -0800 | [diff] [blame] | 24 | GrColor coverageColor = GrColorPackRGBA(coverage, coverage, coverage, coverage); |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 25 | fDrawType = drawType; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 26 | |
joshualitt | 8c0f615 | 2014-12-10 14:12:22 -0800 | [diff] [blame] | 27 | const GrProcOptInfo& colorPOI = drawState.colorProcInfo(color); |
| 28 | const GrProcOptInfo& coveragePOI = drawState.coverageProcInfo(coverageColor); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 29 | |
| 30 | fColor = colorPOI.inputColorToEffectiveStage(); |
joshualitt | 8c0f615 | 2014-12-10 14:12:22 -0800 | [diff] [blame] | 31 | fCoverage = coverage; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 32 | |
| 33 | // Create XferProcessor from DS's XPFactory |
| 34 | SkAutoTUnref<GrXferProcessor> xferProcessor( |
| 35 | drawState.getXPFactory()->createXferProcessor(colorPOI, coveragePOI)); |
| 36 | |
| 37 | GrXferProcessor::OptFlags optFlags; |
| 38 | if (xferProcessor) { |
| 39 | fXferProcessor.reset(xferProcessor.get()); |
| 40 | |
| 41 | optFlags = xferProcessor->getOptimizations(colorPOI, |
| 42 | coveragePOI, |
| 43 | drawState.isCoverageDrawing(), |
| 44 | drawState.isColorWriteDisabled(), |
| 45 | drawState.getStencil().doesWrite(), |
| 46 | &fColor, |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame^] | 47 | &fCoverage, |
| 48 | caps); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 49 | } |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 50 | |
| 51 | // When path rendering the stencil settings are not always set on the draw state |
| 52 | // so we must check the draw type. In cases where we will skip drawing we simply return a |
| 53 | // null GrOptDrawState. |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 54 | if (!xferProcessor || ((GrXferProcessor::kSkipDraw_OptFlag & optFlags) && |
| 55 | GrGpu::kStencilPath_DrawType != drawType)) { |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 56 | // Set the fields that don't default init and return. The lack of a render target will |
| 57 | // indicate that this can be skipped. |
| 58 | fFlags = 0; |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 59 | fDrawFace = GrDrawState::kInvalid_DrawFace; |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 60 | fViewMatrix.reset(); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | fRenderTarget.reset(drawState.fRenderTarget.get()); |
| 65 | SkASSERT(fRenderTarget); |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 66 | fScissorState = scissorState; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 67 | fViewMatrix = drawState.getViewMatrix(); |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 68 | fStencilSettings = drawState.getStencil(); |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 69 | fDrawFace = drawState.getDrawFace(); |
joshualitt | 9176e2c | 2014-11-20 07:28:52 -0800 | [diff] [blame] | 70 | // TODO move this out of optDrawState |
| 71 | if (dstCopy) { |
| 72 | fDstCopy = *dstCopy; |
| 73 | } |
| 74 | |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 75 | fFlags = 0; |
| 76 | if (drawState.isHWAntialias()) { |
| 77 | fFlags |= kHWAA_Flag; |
| 78 | } |
| 79 | if (drawState.isColorWriteDisabled()) { |
| 80 | fFlags |= kDisableColorWrite_Flag; |
| 81 | } |
| 82 | if (drawState.isDither()) { |
| 83 | fFlags |= kDither_Flag; |
| 84 | } |
| 85 | |
joshualitt | 8c0f615 | 2014-12-10 14:12:22 -0800 | [diff] [blame] | 86 | fDescInfo.fHasVertexColor = drawState.hasGeometryProcessor() && |
| 87 | drawState.getGeometryProcessor()->hasVertexColor(); |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 88 | |
joshualitt | 8c0f615 | 2014-12-10 14:12:22 -0800 | [diff] [blame] | 89 | fDescInfo.fHasVertexCoverage = drawState.hasGeometryProcessor() && |
| 90 | drawState.getGeometryProcessor()->hasVertexCoverage(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 91 | |
joshualitt | 8c0f615 | 2014-12-10 14:12:22 -0800 | [diff] [blame] | 92 | bool hasLocalCoords = drawState.hasGeometryProcessor() && |
| 93 | drawState.getGeometryProcessor()->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(); |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 96 | fDescInfo.fInputColorIsUsed = colorPOI.inputColorIsUsed(); |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 97 | if (colorPOI.removeVertexAttrib()) { |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 98 | fDescInfo.fHasVertexColor = false; |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 99 | } |
| 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; |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 104 | fDescInfo.fInputCoverageIsUsed = true; |
egdaniel | 912b3d2 | 2014-11-17 07:45:53 -0800 | [diff] [blame] | 105 | |
joshualitt | 8c0f615 | 2014-12-10 14:12:22 -0800 | [diff] [blame] | 106 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 107 | GrXferProcessor::BlendInfo blendInfo; |
| 108 | fXferProcessor->getBlendInfo(&blendInfo); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 109 | |
| 110 | this->adjustProgramFromOptimizations(drawState, optFlags, colorPOI, coveragePOI, |
| 111 | &firstColorStageIdx, &firstCoverageStageIdx); |
| 112 | |
| 113 | fDescInfo.fRequiresLocalCoordAttrib = hasLocalCoords; |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 114 | |
joshualitt | 8c0f615 | 2014-12-10 14:12:22 -0800 | [diff] [blame] | 115 | // Copy GeometryProcesssor from DS or ODS |
| 116 | SkASSERT(GrGpu::IsPathRenderingDrawType(drawType) || |
| 117 | GrGpu::kStencilPath_DrawType || |
| 118 | drawState.hasGeometryProcessor()); |
| 119 | fGeometryProcessor.reset(drawState.getGeometryProcessor()); |
| 120 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 121 | // Copy Stages from DS to ODS |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 122 | for (int i = firstColorStageIdx; i < drawState.numColorStages(); ++i) { |
| 123 | SkNEW_APPEND_TO_TARRAY(&fFragmentStages, |
| 124 | GrPendingFragmentStage, |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 125 | (drawState.fColorStages[i], hasLocalCoords)); |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 126 | } |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 127 | |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 128 | fNumColorStages = fFragmentStages.count(); |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 129 | for (int i = firstCoverageStageIdx; i < drawState.numCoverageStages(); ++i) { |
| 130 | SkNEW_APPEND_TO_TARRAY(&fFragmentStages, |
| 131 | GrPendingFragmentStage, |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 132 | (drawState.fCoverageStages[i], hasLocalCoords)); |
egdaniel | 9cf45bf | 2014-10-08 06:49:10 -0700 | [diff] [blame] | 133 | } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 134 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 135 | // let the GP init the batch tracker |
joshualitt | 8c0f615 | 2014-12-10 14:12:22 -0800 | [diff] [blame] | 136 | if (drawState.hasGeometryProcessor()) { |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 137 | GrGeometryProcessor::InitBT init; |
| 138 | init.fOutputColor = fDescInfo.fInputColorIsUsed; |
| 139 | init.fOutputCoverage = fDescInfo.fInputCoverageIsUsed; |
| 140 | init.fColor = this->getColor(); |
| 141 | init.fCoverage = this->getCoverage(); |
| 142 | fGeometryProcessor->initBatchTracker(&fBatchTracker, init); |
| 143 | } |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 144 | } |
| 145 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 146 | void GrOptDrawState::adjustProgramFromOptimizations(const GrDrawState& ds, |
| 147 | GrXferProcessor::OptFlags flags, |
| 148 | const GrProcOptInfo& colorPOI, |
| 149 | const GrProcOptInfo& coveragePOI, |
| 150 | int* firstColorStageIdx, |
| 151 | int* firstCoverageStageIdx) { |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 152 | fDescInfo.fReadsDst = false; |
| 153 | fDescInfo.fReadsFragPosition = false; |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 154 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 155 | if (flags & GrXferProcessor::kClearColorStages_OptFlag) { |
| 156 | fDescInfo.fInputColorIsUsed = true; |
| 157 | *firstColorStageIdx = ds.numColorStages(); |
| 158 | fDescInfo.fHasVertexColor = false; |
| 159 | } else { |
| 160 | fDescInfo.fReadsDst = colorPOI.readsDst(); |
| 161 | fDescInfo.fReadsFragPosition = colorPOI.readsFragPosition(); |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 162 | } |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 163 | |
| 164 | if (flags & GrXferProcessor::kClearCoverageStages_OptFlag) { |
| 165 | fDescInfo.fInputCoverageIsUsed = true; |
| 166 | *firstCoverageStageIdx = ds.numCoverageStages(); |
| 167 | fDescInfo.fHasVertexCoverage = false; |
| 168 | } else { |
| 169 | if (coveragePOI.readsDst()) { |
| 170 | fDescInfo.fReadsDst = true; |
| 171 | } |
| 172 | if (coveragePOI.readsFragPosition()) { |
| 173 | fDescInfo.fReadsFragPosition = true; |
| 174 | } |
egdaniel | a7dc0a8 | 2014-09-17 08:25:05 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 178 | void GrOptDrawState::finalize(GrGpu* gpu) { |
| 179 | gpu->buildProgramDesc(*this, fDescInfo, fDrawType, &fDesc); |
| 180 | fFinalized = true; |
| 181 | } |
| 182 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 183 | //////////////////////////////////////////////////////////////////////////////// |
| 184 | |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 185 | bool GrOptDrawState::operator== (const GrOptDrawState& that) const { |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 186 | if (fDescInfo != that.fDescInfo) { |
joshualitt | f78c60c | 2014-12-04 06:01:45 -0800 | [diff] [blame] | 187 | return false; |
| 188 | } |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 189 | |
| 190 | if (!fDescInfo.fHasVertexColor && this->fColor != that.fColor) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 191 | return false; |
| 192 | } |
| 193 | |
| 194 | if (this->getRenderTarget() != that.getRenderTarget() || |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 195 | this->fFragmentStages.count() != that.fFragmentStages.count() || |
| 196 | this->fNumColorStages != that.fNumColorStages || |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 197 | this->fScissorState != that.fScissorState || |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 198 | !this->fViewMatrix.cheapEqualTo(that.fViewMatrix) || |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 199 | this->fDrawType != that.fDrawType || |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 200 | this->fFlags != that.fFlags || |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 201 | this->fStencilSettings != that.fStencilSettings || |
joshualitt | 9176e2c | 2014-11-20 07:28:52 -0800 | [diff] [blame] | 202 | this->fDrawFace != that.fDrawFace || |
| 203 | this->fDstCopy.texture() != that.fDstCopy.texture()) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 204 | return false; |
| 205 | } |
| 206 | |
joshualitt | dafa4d0 | 2014-12-04 08:59:10 -0800 | [diff] [blame] | 207 | if (!fDescInfo.fHasVertexCoverage && this->fCoverage != that.fCoverage) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 208 | return false; |
| 209 | } |
| 210 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 211 | if (this->hasGeometryProcessor()) { |
| 212 | if (!that.hasGeometryProcessor()) { |
| 213 | return false; |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 214 | } else if (!this->getGeometryProcessor()->isEqual(*that.getGeometryProcessor())) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 215 | return false; |
| 216 | } |
| 217 | } else if (that.hasGeometryProcessor()) { |
| 218 | return false; |
| 219 | } |
| 220 | |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame^] | 221 | if (!this->getXferProcessor()->isEqual(*that.getXferProcessor())) { |
| 222 | return false; |
| 223 | } |
| 224 | |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 225 | // The program desc comparison should have already assured that the stage counts match. |
| 226 | SkASSERT(this->numFragmentStages() == that.numFragmentStages()); |
egdaniel | d9aa218 | 2014-10-09 13:47:05 -0700 | [diff] [blame] | 227 | for (int i = 0; i < this->numFragmentStages(); i++) { |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 228 | |
| 229 | if (this->getFragmentStage(i) != that.getFragmentStage(i)) { |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 230 | return false; |
| 231 | } |
| 232 | } |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 233 | return true; |
| 234 | } |
| 235 | |