joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 1 | /* |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 2 | * Copyright 2017 Google Inc. |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 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 | |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 8 | #include "GrAppliedClip.h" |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 9 | #include "GrColor.h" |
| 10 | #include "GrDefaultGeoProcFactory.h" |
Brian Salomon | 1772663 | 2017-05-12 14:09:46 -0400 | [diff] [blame] | 11 | #include "GrDrawOpTest.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 12 | #include "GrMeshDrawOp.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 13 | #include "GrOpFlushState.h" |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 14 | #include "GrPrimitiveProcessor.h" |
joshualitt | ae5b2c6 | 2015-08-19 08:48:41 -0700 | [diff] [blame] | 15 | #include "GrQuad.h" |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 16 | #include "GrRectOpFactory.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 17 | #include "GrResourceProvider.h" |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 18 | #include "GrSimpleMeshDrawOpHelper.h" |
reed | a39667c | 2016-08-22 06:39:49 -0700 | [diff] [blame] | 19 | #include "SkMatrixPriv.h" |
| 20 | |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 21 | static const int kVertsPerRect = 4; |
| 22 | static const int kIndicesPerRect = 6; |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 23 | |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 24 | /** We always use per-vertex colors so that rects can be combined across color changes. Sometimes |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 25 | we have explicit local coords and sometimes not. We *could* always provide explicit local |
| 26 | coords and just duplicate the positions when the caller hasn't provided a local coord rect, |
| 27 | but we haven't seen a use case which frequently switches between local rect and no local |
| 28 | rect draws. |
| 29 | |
| 30 | The vertex attrib order is always pos, color, [local coords]. |
| 31 | */ |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 32 | static sk_sp<GrGeometryProcessor> make_gp() { |
robertphillips | 6abd1d1 | 2016-07-01 09:06:56 -0700 | [diff] [blame] | 33 | using namespace GrDefaultGeoProcFactory; |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 34 | return GrDefaultGeoProcFactory::Make(Color::kPremulGrColorAttribute_Type, Coverage::kSolid_Type, |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 35 | LocalCoords::kHasExplicit_Type, SkMatrix::I()); |
robertphillips | 6abd1d1 | 2016-07-01 09:06:56 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 38 | static sk_sp<GrGeometryProcessor> make_perspective_gp(const SkMatrix& viewMatrix, |
| 39 | bool hasExplicitLocalCoords, |
| 40 | const SkMatrix* localMatrix) { |
| 41 | SkASSERT(viewMatrix.hasPerspective() || (localMatrix && localMatrix->hasPerspective())); |
| 42 | |
| 43 | using namespace GrDefaultGeoProcFactory; |
| 44 | |
| 45 | // If we have perspective on the viewMatrix then we won't map on the CPU, nor will we map |
| 46 | // the local rect on the cpu (in case the localMatrix also has perspective). |
| 47 | // Otherwise, if we have a local rect, then we apply the localMatrix directly to the localRect |
| 48 | // to generate vertex local coords |
| 49 | if (viewMatrix.hasPerspective()) { |
| 50 | LocalCoords localCoords(hasExplicitLocalCoords ? LocalCoords::kHasExplicit_Type |
| 51 | : LocalCoords::kUsePosition_Type, |
| 52 | localMatrix); |
| 53 | return GrDefaultGeoProcFactory::Make(Color::kPremulGrColorAttribute_Type, |
| 54 | Coverage::kSolid_Type, localCoords, viewMatrix); |
| 55 | } else if (hasExplicitLocalCoords) { |
| 56 | LocalCoords localCoords(LocalCoords::kHasExplicit_Type, localMatrix); |
| 57 | return GrDefaultGeoProcFactory::Make(Color::kPremulGrColorAttribute_Type, |
| 58 | Coverage::kSolid_Type, localCoords, SkMatrix::I()); |
| 59 | } else { |
| 60 | LocalCoords localCoords(LocalCoords::kUsePosition_Type, localMatrix); |
| 61 | return GrDefaultGeoProcFactory::MakeForDeviceSpace(Color::kPremulGrColorAttribute_Type, |
| 62 | Coverage::kSolid_Type, localCoords, |
| 63 | viewMatrix); |
| 64 | } |
| 65 | } |
| 66 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 67 | static void tesselate(intptr_t vertices, |
| 68 | size_t vertexStride, |
| 69 | GrColor color, |
robertphillips | 6abd1d1 | 2016-07-01 09:06:56 -0700 | [diff] [blame] | 70 | const SkMatrix* viewMatrix, |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 71 | const SkRect& rect, |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 72 | const GrQuad* localQuad) { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 73 | SkPoint* positions = reinterpret_cast<SkPoint*>(vertices); |
| 74 | |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 75 | SkPointPriv::SetRectTriStrip(positions, rect, vertexStride); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 76 | |
robertphillips | 6abd1d1 | 2016-07-01 09:06:56 -0700 | [diff] [blame] | 77 | if (viewMatrix) { |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 78 | SkMatrixPriv::MapPointsWithStride(*viewMatrix, positions, vertexStride, kVertsPerRect); |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // Setup local coords |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 82 | // TODO we should only do this if local coords are being read |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 83 | if (localQuad) { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 84 | static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 85 | for (int i = 0; i < kVertsPerRect; i++) { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 86 | SkPoint* coords = |
| 87 | reinterpret_cast<SkPoint*>(vertices + kLocalOffset + i * vertexStride); |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 88 | *coords = localQuad->point(i); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | static const int kColorOffset = sizeof(SkPoint); |
| 93 | GrColor* vertColor = reinterpret_cast<GrColor*>(vertices + kColorOffset); |
| 94 | for (int j = 0; j < 4; ++j) { |
| 95 | *vertColor = color; |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 96 | vertColor = (GrColor*)((intptr_t)vertColor + vertexStride); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 100 | namespace { |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 101 | |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 102 | class NonAAFillRectOp final : public GrMeshDrawOp { |
| 103 | private: |
| 104 | using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 105 | |
| 106 | public: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 107 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 108 | GrPaint&& paint, |
| 109 | const SkMatrix& viewMatrix, |
| 110 | const SkRect& rect, |
| 111 | const SkRect* localRect, |
| 112 | const SkMatrix* localMatrix, |
| 113 | GrAAType aaType, |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 114 | const GrUserStencilSettings* stencilSettings) { |
| 115 | SkASSERT(GrAAType::kCoverage != aaType); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 116 | return Helper::FactoryHelper<NonAAFillRectOp>(context, std::move(paint), viewMatrix, rect, |
| 117 | localRect, localMatrix, aaType, |
| 118 | stencilSettings); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | NonAAFillRectOp() = delete; |
| 122 | |
| 123 | NonAAFillRectOp(const Helper::MakeArgs& args, GrColor color, const SkMatrix& viewMatrix, |
| 124 | const SkRect& rect, const SkRect* localRect, const SkMatrix* localMatrix, |
| 125 | GrAAType aaType, const GrUserStencilSettings* stencilSettings) |
| 126 | : INHERITED(ClassID()), fHelper(args, aaType, stencilSettings) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 127 | |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 128 | SkASSERT(!viewMatrix.hasPerspective() && (!localMatrix || !localMatrix->hasPerspective())); |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 129 | RectInfo& info = fRects.push_back(); |
| 130 | info.fColor = color; |
| 131 | info.fViewMatrix = viewMatrix; |
| 132 | info.fRect = rect; |
| 133 | if (localRect && localMatrix) { |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 134 | info.fLocalQuad = GrQuad(*localRect, *localMatrix); |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 135 | } else if (localRect) { |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 136 | info.fLocalQuad = GrQuad(*localRect); |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 137 | } else if (localMatrix) { |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 138 | info.fLocalQuad = GrQuad(rect, *localMatrix); |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 139 | } else { |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 140 | info.fLocalQuad = GrQuad(rect); |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 141 | } |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 142 | this->setTransformedBounds(fRects[0].fRect, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo); |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 143 | } |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 144 | |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 145 | const char* name() const override { return "NonAAFillRectOp"; } |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 146 | |
Robert Phillips | f1748f5 | 2017-09-14 14:11:24 -0400 | [diff] [blame] | 147 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 148 | fHelper.visitProxies(func); |
| 149 | } |
| 150 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 151 | SkString dumpInfo() const override { |
| 152 | SkString str; |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 153 | str.append(GrMeshDrawOp::dumpInfo()); |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 154 | str.appendf("# combined: %d\n", fRects.count()); |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 155 | for (int i = 0; i < fRects.count(); ++i) { |
| 156 | const RectInfo& info = fRects[i]; |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 157 | str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i, |
| 158 | info.fColor, info.fRect.fLeft, info.fRect.fTop, info.fRect.fRight, |
| 159 | info.fRect.fBottom); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 160 | } |
Brian Salomon | 82dfd3d | 2017-06-14 12:30:35 -0400 | [diff] [blame] | 161 | str += fHelper.dumpInfo(); |
| 162 | str += INHERITED::dumpInfo(); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 163 | return str; |
| 164 | } |
| 165 | |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 166 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override { |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 167 | GrColor* color = &fRects.front().fColor; |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 168 | return fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kNone, color); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 172 | |
| 173 | DEFINE_OP_CLASS_ID |
| 174 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 175 | private: |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 176 | void onPrepareDraws(Target* target) override { |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 177 | sk_sp<GrGeometryProcessor> gp = make_gp(); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 178 | if (!gp) { |
| 179 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 180 | return; |
| 181 | } |
| 182 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 183 | static constexpr size_t kVertexStride = |
| 184 | sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr); |
| 185 | SkASSERT(kVertexStride == gp->debugOnly_vertexStride()); |
| 186 | |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 187 | int rectCount = fRects.count(); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 188 | |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 189 | sk_sp<const GrBuffer> indexBuffer = target->resourceProvider()->refQuadIndexBuffer(); |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 190 | PatternHelper helper(GrPrimitiveType::kTriangles); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 191 | void* vertices = helper.init(target, kVertexStride, indexBuffer.get(), kVertsPerRect, |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 192 | kIndicesPerRect, rectCount); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 193 | if (!vertices || !indexBuffer) { |
| 194 | SkDebugf("Could not allocate vertices\n"); |
| 195 | return; |
| 196 | } |
| 197 | |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 198 | for (int i = 0; i < rectCount; i++) { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 199 | intptr_t verts = |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 200 | reinterpret_cast<intptr_t>(vertices) + i * kVertsPerRect * kVertexStride; |
| 201 | tesselate(verts, kVertexStride, fRects[i].fColor, &fRects[i].fViewMatrix, |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 202 | fRects[i].fRect, &fRects[i].fLocalQuad); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 203 | } |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 204 | auto pipe = fHelper.makePipeline(target); |
| 205 | helper.recordDraw(target, gp.get(), pipe.fPipeline, pipe.fFixedDynamicState); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 208 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 209 | NonAAFillRectOp* that = t->cast<NonAAFillRectOp>(); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 210 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 211 | return false; |
| 212 | } |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 213 | fRects.push_back_n(that->fRects.count(), that->fRects.begin()); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 214 | this->joinBounds(*that); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 215 | return true; |
| 216 | } |
| 217 | |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 218 | struct RectInfo { |
| 219 | GrColor fColor; |
| 220 | SkMatrix fViewMatrix; |
| 221 | SkRect fRect; |
| 222 | GrQuad fLocalQuad; |
| 223 | }; |
| 224 | |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 225 | Helper fHelper; |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 226 | SkSTArray<1, RectInfo, true> fRects; |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 227 | typedef GrMeshDrawOp INHERITED; |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 228 | }; |
| 229 | |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 230 | // We handle perspective in the local matrix or viewmatrix with special ops. |
| 231 | class NonAAFillRectPerspectiveOp final : public GrMeshDrawOp { |
| 232 | private: |
| 233 | using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 234 | |
| 235 | public: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 236 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 237 | GrPaint&& paint, |
| 238 | const SkMatrix& viewMatrix, |
| 239 | const SkRect& rect, |
| 240 | const SkRect* localRect, |
| 241 | const SkMatrix* localMatrix, |
| 242 | GrAAType aaType, |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 243 | const GrUserStencilSettings* stencilSettings) { |
| 244 | SkASSERT(GrAAType::kCoverage != aaType); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 245 | return Helper::FactoryHelper<NonAAFillRectPerspectiveOp>(context, std::move(paint), |
| 246 | viewMatrix, rect, |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 247 | localRect, localMatrix, aaType, |
| 248 | stencilSettings); |
| 249 | } |
| 250 | |
| 251 | NonAAFillRectPerspectiveOp() = delete; |
| 252 | |
| 253 | NonAAFillRectPerspectiveOp(const Helper::MakeArgs& args, GrColor color, |
| 254 | const SkMatrix& viewMatrix, const SkRect& rect, |
| 255 | const SkRect* localRect, const SkMatrix* localMatrix, |
| 256 | GrAAType aaType, const GrUserStencilSettings* stencilSettings) |
| 257 | : INHERITED(ClassID()) |
| 258 | , fHelper(args, aaType, stencilSettings) |
| 259 | , fViewMatrix(viewMatrix) { |
| 260 | SkASSERT(viewMatrix.hasPerspective() || (localMatrix && localMatrix->hasPerspective())); |
| 261 | RectInfo& info = fRects.push_back(); |
| 262 | info.fColor = color; |
| 263 | info.fRect = rect; |
| 264 | fHasLocalRect = SkToBool(localRect); |
| 265 | fHasLocalMatrix = SkToBool(localMatrix); |
| 266 | if (fHasLocalMatrix) { |
| 267 | fLocalMatrix = *localMatrix; |
| 268 | } |
| 269 | if (fHasLocalRect) { |
| 270 | info.fLocalRect = *localRect; |
| 271 | } |
| 272 | this->setTransformedBounds(rect, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo); |
| 273 | } |
| 274 | |
| 275 | const char* name() const override { return "NonAAFillRectPerspectiveOp"; } |
| 276 | |
Robert Phillips | f1748f5 | 2017-09-14 14:11:24 -0400 | [diff] [blame] | 277 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 278 | fHelper.visitProxies(func); |
| 279 | } |
| 280 | |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 281 | SkString dumpInfo() const override { |
| 282 | SkString str; |
| 283 | str.appendf("# combined: %d\n", fRects.count()); |
| 284 | for (int i = 0; i < fRects.count(); ++i) { |
Mike Klein | 1d74620 | 2018-01-25 17:32:51 -0500 | [diff] [blame] | 285 | const RectInfo& geo = fRects[i]; |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 286 | str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i, |
| 287 | geo.fColor, geo.fRect.fLeft, geo.fRect.fTop, geo.fRect.fRight, |
| 288 | geo.fRect.fBottom); |
| 289 | } |
Brian Salomon | 82dfd3d | 2017-06-14 12:30:35 -0400 | [diff] [blame] | 290 | str += fHelper.dumpInfo(); |
| 291 | str += INHERITED::dumpInfo(); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 292 | return str; |
| 293 | } |
| 294 | |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 295 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override { |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 296 | GrColor* color = &fRects.front().fColor; |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 297 | return fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kNone, color); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 301 | |
| 302 | DEFINE_OP_CLASS_ID |
| 303 | |
| 304 | private: |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 305 | void onPrepareDraws(Target* target) override { |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 306 | sk_sp<GrGeometryProcessor> gp = make_perspective_gp( |
| 307 | fViewMatrix, fHasLocalRect, fHasLocalMatrix ? &fLocalMatrix : nullptr); |
| 308 | if (!gp) { |
| 309 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 310 | return; |
| 311 | } |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 312 | size_t vertexStride = fHasLocalRect |
| 313 | ? sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr) |
| 314 | : sizeof(GrDefaultGeoProcFactory::PositionColorAttr); |
| 315 | SkASSERT(vertexStride == gp->debugOnly_vertexStride()); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 316 | |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 317 | int rectCount = fRects.count(); |
| 318 | |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 319 | sk_sp<const GrBuffer> indexBuffer = target->resourceProvider()->refQuadIndexBuffer(); |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 320 | PatternHelper helper(GrPrimitiveType::kTriangles); |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 321 | void* vertices = helper.init(target, vertexStride, indexBuffer.get(), kVertsPerRect, |
| 322 | kIndicesPerRect, rectCount); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 323 | if (!vertices || !indexBuffer) { |
| 324 | SkDebugf("Could not allocate vertices\n"); |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | for (int i = 0; i < rectCount; i++) { |
| 329 | const RectInfo& info = fRects[i]; |
| 330 | intptr_t verts = |
| 331 | reinterpret_cast<intptr_t>(vertices) + i * kVertsPerRect * vertexStride; |
| 332 | if (fHasLocalRect) { |
| 333 | GrQuad quad(info.fLocalRect); |
| 334 | tesselate(verts, vertexStride, info.fColor, nullptr, info.fRect, &quad); |
| 335 | } else { |
| 336 | tesselate(verts, vertexStride, info.fColor, nullptr, info.fRect, nullptr); |
| 337 | } |
| 338 | } |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 339 | auto pipe = fHelper.makePipeline(target); |
| 340 | helper.recordDraw(target, gp.get(), pipe.fPipeline, pipe.fFixedDynamicState); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
| 344 | NonAAFillRectPerspectiveOp* that = t->cast<NonAAFillRectPerspectiveOp>(); |
| 345 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | // We could combine across perspective vm changes if we really wanted to. |
| 350 | if (!fViewMatrix.cheapEqualTo(that->fViewMatrix)) { |
| 351 | return false; |
| 352 | } |
| 353 | if (fHasLocalRect != that->fHasLocalRect) { |
| 354 | return false; |
| 355 | } |
| 356 | if (fHasLocalMatrix && !fLocalMatrix.cheapEqualTo(that->fLocalMatrix)) { |
| 357 | return false; |
| 358 | } |
| 359 | |
| 360 | fRects.push_back_n(that->fRects.count(), that->fRects.begin()); |
| 361 | this->joinBounds(*that); |
| 362 | return true; |
| 363 | } |
| 364 | |
| 365 | struct RectInfo { |
| 366 | SkRect fRect; |
| 367 | GrColor fColor; |
| 368 | SkRect fLocalRect; |
| 369 | }; |
| 370 | |
| 371 | SkSTArray<1, RectInfo, true> fRects; |
| 372 | Helper fHelper; |
| 373 | bool fHasLocalMatrix; |
| 374 | bool fHasLocalRect; |
| 375 | SkMatrix fLocalMatrix; |
| 376 | SkMatrix fViewMatrix; |
| 377 | |
| 378 | typedef GrMeshDrawOp INHERITED; |
| 379 | }; |
| 380 | |
| 381 | } // anonymous namespace |
| 382 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 383 | namespace GrRectOpFactory { |
bsalomon | b8cbd20 | 2016-06-30 13:09:48 -0700 | [diff] [blame] | 384 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 385 | std::unique_ptr<GrDrawOp> MakeNonAAFill(GrContext* context, |
| 386 | GrPaint&& paint, |
| 387 | const SkMatrix& viewMatrix, |
| 388 | const SkRect& rect, |
| 389 | GrAAType aaType, |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 390 | const GrUserStencilSettings* stencilSettings) { |
| 391 | if (viewMatrix.hasPerspective()) { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 392 | return NonAAFillRectPerspectiveOp::Make(context, std::move(paint), viewMatrix, rect, |
| 393 | nullptr, nullptr, aaType, stencilSettings); |
Brian Salomon | 1ec03f3 | 2017-06-14 09:49:26 -0400 | [diff] [blame] | 394 | } else { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 395 | return NonAAFillRectOp::Make(context, std::move(paint), viewMatrix, rect, nullptr, nullptr, |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 396 | aaType, stencilSettings); |
| 397 | } |
| 398 | } |
| 399 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 400 | std::unique_ptr<GrDrawOp> MakeNonAAFillWithLocalMatrix( |
| 401 | GrContext* context, |
| 402 | GrPaint&& paint, |
| 403 | const SkMatrix& viewMatrix, |
| 404 | const SkMatrix& localMatrix, |
| 405 | const SkRect& rect, |
| 406 | GrAAType aaType, |
| 407 | const GrUserStencilSettings* stencilSettings) { |
| 408 | if (viewMatrix.hasPerspective() || localMatrix.hasPerspective()) { |
| 409 | return NonAAFillRectPerspectiveOp::Make(context, std::move(paint), viewMatrix, rect, |
| 410 | nullptr, &localMatrix, aaType, stencilSettings); |
| 411 | } else { |
| 412 | return NonAAFillRectOp::Make(context, std::move(paint), viewMatrix, rect, nullptr, |
| 413 | &localMatrix, aaType, stencilSettings); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | std::unique_ptr<GrDrawOp> MakeNonAAFillWithLocalRect(GrContext* context, |
| 418 | GrPaint&& paint, |
| 419 | const SkMatrix& viewMatrix, |
| 420 | const SkRect& rect, |
| 421 | const SkRect& localRect, |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 422 | GrAAType aaType) { |
| 423 | if (viewMatrix.hasPerspective()) { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 424 | return NonAAFillRectPerspectiveOp::Make(context, std::move(paint), viewMatrix, rect, |
| 425 | &localRect, nullptr, aaType, nullptr); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 426 | } else { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 427 | return NonAAFillRectOp::Make(context, std::move(paint), viewMatrix, rect, &localRect, |
| 428 | nullptr, aaType, nullptr); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
| 432 | } // namespace GrRectOpFactory |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 433 | |
| 434 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Brian Salomon | 1772663 | 2017-05-12 14:09:46 -0400 | [diff] [blame] | 435 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 436 | #if GR_TEST_UTILS |
| 437 | |
Brian Salomon | 1772663 | 2017-05-12 14:09:46 -0400 | [diff] [blame] | 438 | GR_DRAW_OP_TEST_DEFINE(NonAAFillRectOp) { |
| 439 | SkRect rect = GrTest::TestRect(random); |
| 440 | SkRect localRect = GrTest::TestRect(random); |
| 441 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 442 | SkMatrix localMatrix = GrTest::TestMatrix(random); |
Brian Salomon | 1772663 | 2017-05-12 14:09:46 -0400 | [diff] [blame] | 443 | const GrUserStencilSettings* stencil = GrGetRandomStencil(random, context); |
| 444 | GrAAType aaType = GrAAType::kNone; |
| 445 | if (fsaaType == GrFSAAType::kUnifiedMSAA) { |
| 446 | aaType = random->nextBool() ? GrAAType::kMSAA : GrAAType::kNone; |
| 447 | } |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 448 | const SkRect* lr = random->nextBool() ? &localRect : nullptr; |
| 449 | const SkMatrix* lm = random->nextBool() ? &localMatrix : nullptr; |
| 450 | if (viewMatrix.hasPerspective() || (lm && lm->hasPerspective())) { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 451 | return NonAAFillRectPerspectiveOp::Make(context, std::move(paint), viewMatrix, rect, |
| 452 | lr, lm, aaType, stencil); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 453 | } else { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 454 | return NonAAFillRectOp::Make(context, std::move(paint), viewMatrix, rect, |
| 455 | lr, lm, aaType, stencil); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 456 | } |
Brian Salomon | 1772663 | 2017-05-12 14:09:46 -0400 | [diff] [blame] | 457 | } |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 458 | |
| 459 | #endif |