Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018 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 "GrFillRectOp.h" |
| 9 | |
| 10 | #include "GrGeometryProcessor.h" |
| 11 | #include "GrMeshDrawOp.h" |
| 12 | #include "GrPaint.h" |
| 13 | #include "GrQuad.h" |
| 14 | #include "GrQuadPerEdgeAA.h" |
| 15 | #include "GrSimpleMeshDrawOpHelper.h" |
| 16 | #include "SkMatrix.h" |
| 17 | #include "SkRect.h" |
| 18 | #include "glsl/GrGLSLColorSpaceXformHelper.h" |
| 19 | #include "glsl/GrGLSLGeometryProcessor.h" |
| 20 | #include "glsl/GrGLSLVarying.h" |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | using VertexSpec = GrQuadPerEdgeAA::VertexSpec; |
| 25 | using ColorType = GrQuadPerEdgeAA::ColorType; |
| 26 | |
| 27 | // NOTE: This info structure is intentionally modeled after GrTextureOps' Quad so that they can |
| 28 | // more easily be integrated together in the future. |
| 29 | class TransformedQuad { |
| 30 | public: |
| 31 | TransformedQuad(const GrPerspQuad& deviceQuad, const GrPerspQuad& localQuad, |
| 32 | const SkPMColor4f& color, GrQuadAAFlags aaFlags) |
| 33 | : fDeviceQuad(deviceQuad) |
| 34 | , fLocalQuad(localQuad) |
| 35 | , fColor(color) |
| 36 | , fAAFlags(aaFlags) {} |
| 37 | |
| 38 | const GrPerspQuad& deviceQuad() const { return fDeviceQuad; } |
| 39 | const GrPerspQuad& localQuad() const { return fLocalQuad; } |
| 40 | const SkPMColor4f& color() const { return fColor; } |
| 41 | GrQuadAAFlags aaFlags() const { return fAAFlags; } |
| 42 | |
| 43 | void setColor(const SkPMColor4f& color) { fColor = color; } |
| 44 | |
| 45 | SkString dumpInfo(int index) const { |
| 46 | SkString str; |
| 47 | str.appendf("%d: Color: [%.2f, %.2f, %.2f, %.2f], Edge AA: l%u_t%u_r%u_b%u, \n" |
| 48 | " device quad: [(%.2f, %2.f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), " |
| 49 | "(%.2f, %.2f, %.2f)],\n" |
| 50 | " local quad: [(%.2f, %2.f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), " |
| 51 | "(%.2f, %.2f, %.2f)]\n", |
| 52 | index, fColor.fR, fColor.fG, fColor.fB, fColor.fA, |
| 53 | (uint32_t) (fAAFlags & GrQuadAAFlags::kLeft), |
| 54 | (uint32_t) (fAAFlags & GrQuadAAFlags::kTop), |
| 55 | (uint32_t) (fAAFlags & GrQuadAAFlags::kRight), |
| 56 | (uint32_t) (fAAFlags & GrQuadAAFlags::kBottom), |
| 57 | fDeviceQuad.x(0), fDeviceQuad.y(0), fDeviceQuad.w(0), |
| 58 | fDeviceQuad.x(1), fDeviceQuad.y(1), fDeviceQuad.w(1), |
| 59 | fDeviceQuad.x(2), fDeviceQuad.y(2), fDeviceQuad.w(2), |
| 60 | fDeviceQuad.x(3), fDeviceQuad.y(3), fDeviceQuad.w(3), |
| 61 | fLocalQuad.x(0), fLocalQuad.y(0), fLocalQuad.w(0), |
| 62 | fLocalQuad.x(1), fLocalQuad.y(1), fLocalQuad.w(1), |
| 63 | fLocalQuad.x(2), fLocalQuad.y(2), fLocalQuad.w(2), |
| 64 | fLocalQuad.x(3), fLocalQuad.y(3), fLocalQuad.w(3)); |
| 65 | return str; |
| 66 | } |
| 67 | private: |
| 68 | // NOTE: The TransformedQuad does not store the types for device and local. The owning op tracks |
| 69 | // the most general type for device and local across all of its merged quads. |
| 70 | GrPerspQuad fDeviceQuad; // In device space, allowing rects to be combined across view matrices |
| 71 | GrPerspQuad fLocalQuad; // Original rect transformed by its local matrix |
| 72 | SkPMColor4f fColor; |
| 73 | GrQuadAAFlags fAAFlags; |
| 74 | }; |
| 75 | |
| 76 | // A GeometryProcessor for rendering TransformedQuads using the vertex attributes from |
| 77 | // GrQuadPerEdgeAA. This is similar to the TextureGeometryProcessor of GrTextureOp except that it |
| 78 | // handles full GrPaints. |
| 79 | class QuadPerEdgeAAGeometryProcessor : public GrGeometryProcessor { |
| 80 | public: |
| 81 | |
| 82 | static sk_sp<GrGeometryProcessor> Make(const VertexSpec& spec) { |
| 83 | return sk_sp<QuadPerEdgeAAGeometryProcessor>(new QuadPerEdgeAAGeometryProcessor(spec)); |
| 84 | } |
| 85 | |
| 86 | const char* name() const override { return "QuadPerEdgeAAGeometryProcessor"; } |
| 87 | |
| 88 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
| 89 | // The attributes' key includes the device and local quad types implicitly since those |
| 90 | // types decide the vertex attribute size |
| 91 | b->add32(fAttrs.getKey()); |
| 92 | } |
| 93 | |
| 94 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override { |
| 95 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
| 96 | public: |
| 97 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc, |
| 98 | FPCoordTransformIter&& transformIter) override { |
| 99 | const auto& gp = proc.cast<QuadPerEdgeAAGeometryProcessor>(); |
| 100 | if (gp.fAttrs.hasLocalCoords()) { |
| 101 | this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | private: |
| 106 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 107 | const auto& gp = args.fGP.cast<QuadPerEdgeAAGeometryProcessor>(); |
| 108 | args.fVaryingHandler->emitAttributes(gp); |
| 109 | gpArgs->fPositionVar = gp.fAttrs.positions().asShaderVar(); |
| 110 | |
| 111 | if (gp.fAttrs.hasLocalCoords()) { |
| 112 | this->emitTransforms(args.fVertBuilder, |
| 113 | args.fVaryingHandler, |
| 114 | args.fUniformHandler, |
| 115 | gp.fAttrs.localCoords().asShaderVar(), |
| 116 | args.fFPCoordTransformHandler); |
| 117 | } |
| 118 | |
| 119 | gp.fAttrs.emitColor(args, "paintColor"); |
| 120 | gp.fAttrs.emitCoverage(args, "aaDist"); |
| 121 | } |
| 122 | }; |
| 123 | return new GLSLProcessor; |
| 124 | } |
| 125 | |
| 126 | private: |
| 127 | QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec) |
| 128 | : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID) |
| 129 | , fAttrs(spec) { |
| 130 | SkASSERT(spec.hasVertexColors()); |
| 131 | this->setVertexAttributes(fAttrs.attributes(), fAttrs.attributeCount()); |
| 132 | } |
| 133 | |
| 134 | GrQuadPerEdgeAA::GPAttributes fAttrs; |
| 135 | |
| 136 | typedef GrGeometryProcessor INHERITED; |
| 137 | }; |
| 138 | |
| 139 | class FillRectOp final : public GrMeshDrawOp { |
| 140 | private: |
| 141 | using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 142 | |
| 143 | public: |
| 144 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 145 | GrPaint&& paint, |
| 146 | GrAAType aaType, |
| 147 | GrQuadAAFlags edgeAA, |
| 148 | const GrUserStencilSettings* stencilSettings, |
| 149 | const GrPerspQuad& deviceQuad, |
| 150 | GrQuadType deviceQuadType, |
| 151 | const GrPerspQuad& localQuad, |
| 152 | GrQuadType localQuadType) { |
| 153 | // Clean up deviations between aaType and edgeAA |
| 154 | GrResolveAATypeForQuad(aaType, edgeAA, deviceQuad, deviceQuadType, &aaType, &edgeAA); |
| 155 | |
| 156 | // Analyze the paint to see if it is compatible with scissor-clearing |
| 157 | SkPMColor4f color = paint.getColor4f(); |
| 158 | // Only non-null if the paint can be turned into a clear, it can be a local pointer since |
| 159 | // the op ctor consumes the value right away if it's provided |
| 160 | SkPMColor4f* clearColor = nullptr; |
| 161 | if (paint.isTrivial() || paint.isConstantBlendedColor(&color)) { |
| 162 | clearColor = &color; |
| 163 | } |
| 164 | |
| 165 | return Helper::FactoryHelper<FillRectOp>(context, std::move(paint), clearColor, aaType, |
| 166 | edgeAA, stencilSettings, deviceQuad, deviceQuadType, localQuad, localQuadType); |
| 167 | } |
| 168 | |
| 169 | // Analysis of the GrPaint to determine the const blend color must be done before, passing |
| 170 | // nullptr for constBlendColor disables all scissor-clear optimizations (must keep the |
| 171 | // paintColor argument because it is assumed by the GrSimpleMeshDrawOpHelper). Similarly, aaType |
| 172 | // is passed to Helper in the initializer list, so incongruities between aaType and edgeFlags |
| 173 | // must be resolved prior to calling this constructor. |
| 174 | FillRectOp(Helper::MakeArgs args, SkPMColor4f paintColor, const SkPMColor4f* constBlendColor, |
| 175 | GrAAType aaType, GrQuadAAFlags edgeFlags, const GrUserStencilSettings* stencil, |
| 176 | const GrPerspQuad& deviceQuad, GrQuadType deviceQuadType, |
| 177 | const GrPerspQuad& localQuad, GrQuadType localQuadType) |
| 178 | : INHERITED(ClassID()) |
| 179 | , fHelper(args, aaType, stencil) |
| 180 | , fDeviceQuadType(static_cast<unsigned>(deviceQuadType)) |
| 181 | , fLocalQuadType(static_cast<unsigned>(localQuadType)) { |
| 182 | if (constBlendColor) { |
| 183 | // The GrPaint is compatible with clearing, and the constant blend color overrides the |
| 184 | // paint color (although in most cases they are probably the same) |
| 185 | paintColor = *constBlendColor; |
| 186 | // However, just because the paint is compatible, the device quad must also be a rect |
| 187 | // that is non-AA (AA aligned with pixel bounds should have already been turned into |
| 188 | // non-AA). |
| 189 | fClearCompatible = deviceQuadType == GrQuadType::kRect && aaType == GrAAType::kNone; |
| 190 | } else { |
| 191 | // Paint isn't clear compatible |
| 192 | fClearCompatible = false; |
| 193 | } |
| 194 | |
| 195 | fWideColor = !SkPMColor4fFitsInBytes(paintColor); |
| 196 | |
| 197 | // The color stored with the quad is the clear color if a scissor-clear is decided upon |
| 198 | // when executing the op. |
| 199 | fQuads.emplace_back(deviceQuad, localQuad, paintColor, edgeFlags); |
| 200 | this->setBounds(deviceQuad.bounds(), HasAABloat(aaType == GrAAType::kCoverage), |
| 201 | IsZeroArea::kNo); |
| 202 | } |
| 203 | |
| 204 | const char* name() const override { return "FillRectOp"; } |
| 205 | |
| 206 | void visitProxies(const VisitProxyFunc& func, VisitorType) const override { |
| 207 | return fHelper.visitProxies(func); |
| 208 | } |
| 209 | |
| 210 | #ifdef SK_DEBUG |
| 211 | SkString dumpInfo() const override { |
| 212 | SkString str; |
| 213 | str.appendf("# draws: %d\n", fQuads.count()); |
| 214 | str.appendf("Clear compatible: %u\n", static_cast<bool>(fClearCompatible)); |
| 215 | str.appendf("Device quad type: %u, local quad type: %u\n", |
| 216 | fDeviceQuadType, fLocalQuadType); |
| 217 | str += fHelper.dumpInfo(); |
| 218 | for (int i = 0; i < fQuads.count(); i++) { |
| 219 | str += fQuads[i].dumpInfo(i); |
| 220 | |
| 221 | } |
| 222 | str += INHERITED::dumpInfo(); |
| 223 | return str; |
| 224 | } |
| 225 | #endif |
| 226 | |
| 227 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override { |
| 228 | // Initialize aggregate color analysis with the first quad's color (which always exists) |
| 229 | SkASSERT(fQuads.count() > 0); |
| 230 | GrProcessorAnalysisColor quadColors(fQuads[0].color()); |
| 231 | // Then combine the colors of any additional quads (e.g. from MakeSet) |
| 232 | for (int i = 1; i < fQuads.count(); ++i) { |
| 233 | quadColors = GrProcessorAnalysisColor::Combine(quadColors, fQuads[i].color()); |
| 234 | } |
| 235 | auto result = fHelper.xpRequiresDstTexture( |
| 236 | caps, clip, GrProcessorAnalysisCoverage::kSingleChannel, &quadColors); |
| 237 | // If there is a constant color after analysis, that means all of the quads should be set |
| 238 | // to the same color (even if they started out with different colors). |
| 239 | SkPMColor4f colorOverride; |
| 240 | if (quadColors.isConstant(&colorOverride)) { |
| 241 | for (int i = 0; i < fQuads.count(); ++i) { |
| 242 | fQuads[i].setColor(colorOverride); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | return result; |
| 247 | } |
| 248 | |
| 249 | FixedFunctionFlags fixedFunctionFlags() const override { |
| 250 | // Since the AA type of the whole primitive is kept consistent with the per edge AA flags |
| 251 | // the helper's fixed function flags are appropriate. |
| 252 | return fHelper.fixedFunctionFlags(); |
| 253 | } |
| 254 | |
| 255 | DEFINE_OP_CLASS_ID |
| 256 | |
| 257 | private: |
| 258 | // For GrFillRectOp::MakeSet's use of addQuad |
| 259 | // FIXME(reviewer): better to just make addQuad public? |
| 260 | friend std::unique_ptr<GrDrawOp> GrFillRectOp::MakeSet(GrContext* context, GrPaint&& paint, |
| 261 | GrAAType aaType, const SkMatrix& viewMatrix, |
| 262 | const GrRenderTargetContext::QuadSetEntry quads[], int quadCount, |
| 263 | const GrUserStencilSettings* stencilSettings); |
| 264 | |
| 265 | void onPrepareDraws(Target* target) override { |
| 266 | TRACE_EVENT0("skia", TRACE_FUNC); |
| 267 | |
| 268 | using Domain = GrQuadPerEdgeAA::Domain; |
| 269 | static constexpr SkRect kEmptyDomain = SkRect::MakeEmpty(); |
| 270 | |
| 271 | VertexSpec vertexSpec(this->deviceQuadType(), |
| 272 | fWideColor ? ColorType::kHalf : ColorType::kByte, |
| 273 | this->localQuadType(), fHelper.usesLocalCoords(), Domain::kNo, |
| 274 | fHelper.aaType()); |
| 275 | |
| 276 | sk_sp<GrGeometryProcessor> gp = QuadPerEdgeAAGeometryProcessor::Make(vertexSpec); |
| 277 | size_t vertexSize = gp->vertexStride(); |
| 278 | |
| 279 | const GrBuffer* vbuffer; |
| 280 | int vertexOffsetInBuffer = 0; |
| 281 | |
| 282 | // Fill the allocated vertex data |
| 283 | void* vdata = target->makeVertexSpace(vertexSize, fQuads.count() * 4, &vbuffer, |
| 284 | &vertexOffsetInBuffer); |
| 285 | if (!vdata) { |
| 286 | SkDebugf("Could not allocate vertices\n"); |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | // vertices pointer advances through vdata based on Tessellate's return value |
| 291 | void* vertices = vdata; |
| 292 | for (int i = 0; i < fQuads.count(); ++i) { |
| 293 | const auto& q = fQuads[i]; |
| 294 | vertices = GrQuadPerEdgeAA::Tessellate(vertices, vertexSpec, q.deviceQuad(), q.color(), |
| 295 | q.localQuad(), kEmptyDomain, q.aaFlags()); |
| 296 | } |
| 297 | |
| 298 | // Configure the mesh for the vertex data |
| 299 | GrMesh* mesh; |
| 300 | if (fQuads.count() > 1) { |
| 301 | mesh = target->allocMesh(GrPrimitiveType::kTriangles); |
| 302 | sk_sp<const GrBuffer> ibuffer = target->resourceProvider()->refQuadIndexBuffer(); |
| 303 | if (!ibuffer) { |
| 304 | SkDebugf("Could not allocate quad indices\n"); |
| 305 | return; |
| 306 | } |
| 307 | mesh->setIndexedPatterned(ibuffer.get(), 6, 4, fQuads.count(), |
| 308 | GrResourceProvider::QuadCountOfQuadBuffer()); |
| 309 | } else { |
| 310 | mesh = target->allocMesh(GrPrimitiveType::kTriangleStrip); |
| 311 | mesh->setNonIndexedNonInstanced(4); |
| 312 | } |
| 313 | mesh->setVertexData(vbuffer, vertexOffsetInBuffer); |
| 314 | |
| 315 | auto pipe = fHelper.makePipeline(target); |
| 316 | target->draw(std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState, mesh); |
| 317 | } |
| 318 | |
| 319 | CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
| 320 | TRACE_EVENT0("skia", TRACE_FUNC); |
| 321 | const auto* that = t->cast<FillRectOp>(); |
| 322 | |
| 323 | // Unlike most users of the draw op helper, this op can merge none-aa and coverage-aa |
| 324 | // draw ops together, so pass true as the last argument. |
| 325 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds(), true)) { |
| 326 | return CombineResult::kCannotCombine; |
| 327 | } |
| 328 | |
| 329 | // If the processor sets are compatible, the two ops are always compatible; it just needs |
| 330 | // to adjust the state of the op to be the more general quad and aa types of the two ops. |
| 331 | |
| 332 | // The GrQuadType enum is ordered such that higher values are more general quad types |
| 333 | if (that->fDeviceQuadType > fDeviceQuadType) { |
| 334 | fDeviceQuadType = that->fDeviceQuadType; |
| 335 | } |
| 336 | if (that->fLocalQuadType > fLocalQuadType) { |
| 337 | fLocalQuadType = that->fLocalQuadType; |
| 338 | } |
| 339 | fClearCompatible &= that->fClearCompatible; |
| 340 | fWideColor |= that->fWideColor; |
| 341 | |
| 342 | // The helper stores the aa type, but isCompatible(with true arg) allows the two ops' aa |
| 343 | // types to be none and coverage, in which case this op's aa type must be lifted to coverage |
| 344 | // so that quads with no aa edges can be batched with quads that have some/all edges aa'ed. |
| 345 | if (fHelper.aaType() == GrAAType::kNone && that->fHelper.aaType() == GrAAType::kCoverage) { |
| 346 | fHelper.setAAType(GrAAType::kCoverage); |
| 347 | } |
| 348 | |
| 349 | fQuads.push_back_n(that->fQuads.count(), that->fQuads.begin()); |
| 350 | return CombineResult::kMerged; |
| 351 | } |
| 352 | |
| 353 | // Similar to onCombineIfPossible, but adds a quad assuming its op would have been compatible. |
| 354 | // But since it's avoiding the op list management, it must update the op's bounds. This is only |
| 355 | // used with quad sets, which uses the same view matrix for each quad so this assumes that the |
| 356 | // device quad type of the new quad is the same as the op's. |
| 357 | void addQuad(TransformedQuad&& quad, GrQuadType localQuadType, GrAAType aaType) { |
| 358 | SkASSERT(quad.deviceQuad().quadType() <= this->deviceQuadType()); |
| 359 | |
| 360 | // The new quad's aa type should be the same as the first quad's or none, except when the |
| 361 | // first quad's aa type was already downgraded to none, in which case the stored type must |
| 362 | // be lifted to back to the requested type. |
| 363 | if (aaType != fHelper.aaType()) { |
| 364 | if (aaType != GrAAType::kNone) { |
| 365 | // Original quad was downgraded to non-aa, lift back up to this quad's required type |
| 366 | SkASSERT(fHelper.aaType() == GrAAType::kNone); |
| 367 | fHelper.setAAType(aaType); |
| 368 | } |
| 369 | // else the new quad could have been downgraded but the other quads can't be, so don't |
| 370 | // reset the op's accumulated aa type. |
| 371 | } |
| 372 | |
| 373 | // The new quad's local coordinates could differ |
| 374 | if (localQuadType > this->localQuadType()) { |
| 375 | fLocalQuadType = static_cast<unsigned>(localQuadType); |
| 376 | } |
| 377 | |
| 378 | // clear compatible won't need to be updated, since device quad type and paint is the same, |
| 379 | // but this quad has a new color, so maybe update wide color |
| 380 | fWideColor |= !SkPMColor4fFitsInBytes(quad.color()); |
| 381 | |
| 382 | // Update the bounds and add the quad to this op's storage |
| 383 | SkRect newBounds = this->bounds(); |
| 384 | newBounds.joinPossiblyEmptyRect(quad.deviceQuad().bounds()); |
| 385 | this->setBounds(newBounds, HasAABloat(fHelper.aaType() == GrAAType::kCoverage), |
| 386 | IsZeroArea::kNo); |
| 387 | fQuads.push_back(std::move(quad)); |
| 388 | } |
| 389 | |
| 390 | GrQuadType deviceQuadType() const { return static_cast<GrQuadType>(fDeviceQuadType); } |
| 391 | GrQuadType localQuadType() const { return static_cast<GrQuadType>(fLocalQuadType); } |
| 392 | |
| 393 | Helper fHelper; |
| 394 | SkSTArray<1, TransformedQuad, true> fQuads; |
| 395 | |
| 396 | // While we always store full GrPerspQuads in memory, if the type is known to be simpler we can |
| 397 | // optimize our geometry generation. |
| 398 | unsigned fDeviceQuadType: 2; |
| 399 | unsigned fLocalQuadType: 2; |
| 400 | unsigned fWideColor: 1; |
| 401 | |
| 402 | // True if fQuad produced by a rectangle-preserving view matrix, is pixel aligned or non-AA, |
| 403 | // and its paint is a constant blended color. |
| 404 | unsigned fClearCompatible: 1; |
| 405 | |
| 406 | typedef GrMeshDrawOp INHERITED; |
| 407 | }; |
| 408 | |
| 409 | } // anonymous namespace |
| 410 | |
| 411 | namespace GrFillRectOp { |
| 412 | |
| 413 | std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 414 | GrPaint&& paint, |
| 415 | GrAAType aaType, |
| 416 | GrQuadAAFlags edgeAA, |
| 417 | const SkMatrix& viewMatrix, |
| 418 | const SkRect& rect, |
| 419 | const GrUserStencilSettings* stencilSettings) { |
| 420 | return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings, |
| 421 | GrPerspQuad(rect, viewMatrix), GrQuadTypeForTransformedRect(viewMatrix), |
| 422 | GrPerspQuad(rect, SkMatrix::I()), GrQuadType::kRect); |
| 423 | } |
| 424 | |
| 425 | std::unique_ptr<GrDrawOp> MakeWithLocalMatrix(GrContext* context, |
| 426 | GrPaint&& paint, |
| 427 | GrAAType aaType, |
| 428 | GrQuadAAFlags edgeAA, |
| 429 | const SkMatrix& viewMatrix, |
| 430 | const SkMatrix& localMatrix, |
| 431 | const SkRect& rect, |
| 432 | const GrUserStencilSettings* stencilSettings) { |
| 433 | GrQuadType localQuadType = GrQuadTypeForTransformedRect(localMatrix); |
| 434 | return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings, |
| 435 | GrPerspQuad(rect, viewMatrix), GrQuadTypeForTransformedRect(viewMatrix), |
| 436 | GrPerspQuad(rect, localMatrix), localQuadType); |
| 437 | } |
| 438 | |
| 439 | std::unique_ptr<GrDrawOp> MakeWithLocalRect(GrContext* context, |
| 440 | GrPaint&& paint, |
| 441 | GrAAType aaType, |
| 442 | GrQuadAAFlags edgeAA, |
| 443 | const SkMatrix& viewMatrix, |
| 444 | const SkRect& rect, |
| 445 | const SkRect& localRect, |
| 446 | const GrUserStencilSettings* stencilSettings) { |
| 447 | return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings, |
| 448 | GrPerspQuad(rect, viewMatrix), GrQuadTypeForTransformedRect(viewMatrix), |
| 449 | GrPerspQuad(localRect, SkMatrix::I()), GrQuadType::kRect); |
| 450 | } |
| 451 | |
| 452 | std::unique_ptr<GrDrawOp> MakeSet(GrContext* context, |
| 453 | GrPaint&& paint, |
| 454 | GrAAType aaType, |
| 455 | const SkMatrix& viewMatrix, |
| 456 | const GrRenderTargetContext::QuadSetEntry quads[], |
| 457 | int cnt, |
| 458 | const GrUserStencilSettings* stencilSettings) { |
| 459 | // First make a draw op for the first quad in the set |
| 460 | SkASSERT(cnt > 0); |
| 461 | GrQuadType deviceQuadType = GrQuadTypeForTransformedRect(viewMatrix); |
| 462 | |
| 463 | paint.setColor4f(quads[0].fColor); |
| 464 | std::unique_ptr<GrDrawOp> op = FillRectOp::Make(context, std::move(paint), aaType, |
| 465 | quads[0].fAAFlags, stencilSettings, GrPerspQuad(quads[0].fRect, viewMatrix), |
| 466 | deviceQuadType, GrPerspQuad(quads[0].fRect, quads[0].fLocalMatrix), |
| 467 | GrQuadTypeForTransformedRect(quads[0].fLocalMatrix)); |
| 468 | auto* fillRects = op->cast<FillRectOp>(); |
| 469 | |
| 470 | // Accumulate remaining quads similar to onCombineIfPossible() without creating an op |
| 471 | for (int i = 1; i < cnt; ++i) { |
| 472 | GrPerspQuad deviceQuad(quads[i].fRect, viewMatrix); |
| 473 | |
| 474 | GrAAType resolvedAA; |
| 475 | GrQuadAAFlags resolvedEdgeFlags; |
| 476 | GrResolveAATypeForQuad(aaType, quads[i].fAAFlags, deviceQuad, deviceQuadType, |
| 477 | &resolvedAA, &resolvedEdgeFlags); |
| 478 | |
| 479 | fillRects->addQuad({ deviceQuad, GrPerspQuad(quads[i].fRect, quads[i].fLocalMatrix), |
| 480 | quads[i].fColor, resolvedEdgeFlags }, |
| 481 | GrQuadTypeForTransformedRect(quads[i].fLocalMatrix), resolvedAA); |
| 482 | } |
| 483 | |
| 484 | return op; |
| 485 | } |
| 486 | |
| 487 | } // namespace GrFillRectOp |
| 488 | |
| 489 | #if GR_TEST_UTILS |
| 490 | |
| 491 | #include "GrDrawOpTest.h" |
| 492 | #include "SkGr.h" |
| 493 | |
| 494 | GR_DRAW_OP_TEST_DEFINE(FillRectOp) { |
| 495 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 496 | SkRect rect = GrTest::TestRect(random); |
| 497 | |
| 498 | GrAAType aaType = GrAAType::kNone; |
| 499 | if (random->nextBool()) { |
| 500 | aaType = (fsaaType == GrFSAAType::kUnifiedMSAA) ? GrAAType::kMSAA : GrAAType::kCoverage; |
| 501 | } |
| 502 | const GrUserStencilSettings* stencil = random->nextBool() ? nullptr |
| 503 | : GrGetRandomStencil(random, context); |
| 504 | |
| 505 | GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone; |
| 506 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone; |
| 507 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone; |
| 508 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone; |
| 509 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone; |
| 510 | |
| 511 | if (random->nextBool()) { |
| 512 | if (random->nextBool()) { |
| 513 | if (random->nextBool()) { |
| 514 | // Local matrix with a set op |
| 515 | uint32_t extraQuadCt = random->nextRangeU(1, 4); |
| 516 | SkTArray<GrRenderTargetContext::QuadSetEntry> quads(extraQuadCt + 1); |
| 517 | quads.push_back( |
| 518 | {rect, SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU())), |
| 519 | GrTest::TestMatrixInvertible(random), aaFlags}); |
| 520 | for (uint32_t i = 0; i < extraQuadCt; ++i) { |
| 521 | GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone; |
| 522 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone; |
| 523 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone; |
| 524 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone; |
| 525 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone; |
| 526 | |
| 527 | quads.push_back( |
| 528 | {GrTest::TestRect(random), |
| 529 | SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU())), |
| 530 | GrTest::TestMatrixInvertible(random), aaFlags}); |
| 531 | } |
| 532 | |
| 533 | return GrFillRectOp::MakeSet(context, std::move(paint), aaType, viewMatrix, |
| 534 | quads.begin(), quads.count(), stencil); |
| 535 | } else { |
| 536 | // Single local matrix |
| 537 | SkMatrix localMatrix = GrTest::TestMatrixInvertible(random); |
| 538 | return GrFillRectOp::MakeWithLocalMatrix(context, std::move(paint), aaType, aaFlags, |
| 539 | viewMatrix, localMatrix, rect, stencil); |
| 540 | } |
| 541 | } else { |
| 542 | // Pass local rect directly |
| 543 | SkRect localRect = GrTest::TestRect(random); |
| 544 | return GrFillRectOp::MakeWithLocalRect(context, std::move(paint), aaType, aaFlags, |
| 545 | viewMatrix, rect, localRect, stencil); |
| 546 | } |
| 547 | } else { |
| 548 | // The simplest constructor |
| 549 | return GrFillRectOp::Make(context, std::move(paint), aaType, aaFlags, viewMatrix, rect, |
| 550 | stencil); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | #endif |