joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 8 | #include "GrColor.h" |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 9 | #include "GrDefaultGeoProcFactory.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 10 | #include "GrMeshDrawOp.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 11 | #include "GrOpFlushState.h" |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 12 | #include "GrRectOpFactory.h" |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 13 | #include "GrResourceKey.h" |
| 14 | #include "GrResourceProvider.h" |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 15 | #include "GrTypes.h" |
| 16 | #include "SkMatrix.h" |
| 17 | #include "SkRect.h" |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 18 | #include "ops/GrSimpleMeshDrawOpHelper.h" |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 19 | |
| 20 | GR_DECLARE_STATIC_UNIQUE_KEY(gAAFillRectIndexBufferKey); |
| 21 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 22 | static inline bool view_matrix_ok_for_aa_fill_rect(const SkMatrix& viewMatrix) { |
| 23 | return viewMatrix.preservesRightAngles(); |
| 24 | } |
| 25 | |
| 26 | static inline void set_inset_fan(SkPoint* pts, size_t stride, const SkRect& r, SkScalar dx, |
| 27 | SkScalar dy) { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 28 | pts->setRectFan(r.fLeft + dx, r.fTop + dy, r.fRight - dx, r.fBottom - dy, stride); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 29 | } |
| 30 | |
joshualitt | 27801bf | 2015-08-12 12:52:47 -0700 | [diff] [blame] | 31 | static const int kNumAAFillRectsInIndexBuffer = 256; |
| 32 | static const int kVertsPerAAFillRect = 8; |
| 33 | static const int kIndicesPerAAFillRect = 30; |
| 34 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 35 | const GrBuffer* get_index_buffer(GrResourceProvider* resourceProvider) { |
joshualitt | 27801bf | 2015-08-12 12:52:47 -0700 | [diff] [blame] | 36 | GR_DEFINE_STATIC_UNIQUE_KEY(gAAFillRectIndexBufferKey); |
| 37 | |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 38 | // clang-format off |
joshualitt | 27801bf | 2015-08-12 12:52:47 -0700 | [diff] [blame] | 39 | static const uint16_t gFillAARectIdx[] = { |
| 40 | 0, 1, 5, 5, 4, 0, |
| 41 | 1, 2, 6, 6, 5, 1, |
| 42 | 2, 3, 7, 7, 6, 2, |
| 43 | 3, 0, 4, 4, 7, 3, |
| 44 | 4, 5, 6, 6, 7, 4, |
| 45 | }; |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 46 | // clang-format on |
| 47 | |
joshualitt | 27801bf | 2015-08-12 12:52:47 -0700 | [diff] [blame] | 48 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(gFillAARectIdx) == kIndicesPerAAFillRect); |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 49 | return resourceProvider->findOrCreatePatternedIndexBuffer( |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 50 | gFillAARectIdx, kIndicesPerAAFillRect, kNumAAFillRectsInIndexBuffer, |
| 51 | kVertsPerAAFillRect, gAAFillRectIndexBufferKey); |
joshualitt | 27801bf | 2015-08-12 12:52:47 -0700 | [diff] [blame] | 52 | } |
| 53 | |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 54 | static void generate_aa_fill_rect_geometry(intptr_t verts, |
| 55 | size_t vertexStride, |
| 56 | GrColor color, |
| 57 | const SkMatrix& viewMatrix, |
| 58 | const SkRect& rect, |
| 59 | const SkRect& devRect, |
Brian Salomon | ecea86a | 2017-01-04 13:25:17 -0500 | [diff] [blame] | 60 | bool tweakAlphaForCoverage, |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 61 | const SkMatrix* localMatrix) { |
| 62 | SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts); |
| 63 | SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride); |
| 64 | |
robertphillips | 0851d2d | 2016-06-02 05:21:34 -0700 | [diff] [blame] | 65 | SkScalar inset; |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 66 | |
| 67 | if (viewMatrix.rectStaysRect()) { |
robertphillips | 0851d2d | 2016-06-02 05:21:34 -0700 | [diff] [blame] | 68 | inset = SkMinScalar(devRect.width(), SK_Scalar1); |
| 69 | inset = SK_ScalarHalf * SkMinScalar(inset, devRect.height()); |
| 70 | |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 71 | set_inset_fan(fan0Pos, vertexStride, devRect, -SK_ScalarHalf, -SK_ScalarHalf); |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 72 | set_inset_fan(fan1Pos, vertexStride, devRect, inset, inset); |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 73 | } else { |
| 74 | // compute transformed (1, 0) and (0, 1) vectors |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 75 | SkVector vec[2] = {{viewMatrix[SkMatrix::kMScaleX], viewMatrix[SkMatrix::kMSkewY]}, |
| 76 | {viewMatrix[SkMatrix::kMSkewX], viewMatrix[SkMatrix::kMScaleY]}}; |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 77 | |
robertphillips | 0851d2d | 2016-06-02 05:21:34 -0700 | [diff] [blame] | 78 | SkScalar len1 = SkPoint::Normalize(&vec[0]); |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 79 | vec[0].scale(SK_ScalarHalf); |
robertphillips | 0851d2d | 2016-06-02 05:21:34 -0700 | [diff] [blame] | 80 | SkScalar len2 = SkPoint::Normalize(&vec[1]); |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 81 | vec[1].scale(SK_ScalarHalf); |
| 82 | |
robertphillips | 0851d2d | 2016-06-02 05:21:34 -0700 | [diff] [blame] | 83 | inset = SkMinScalar(len1 * rect.width(), SK_Scalar1); |
| 84 | inset = SK_ScalarHalf * SkMinScalar(inset, len2 * rect.height()); |
| 85 | |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 86 | // create the rotated rect |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 87 | fan0Pos->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vertexStride); |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 88 | viewMatrix.mapPointsWithStride(fan0Pos, vertexStride, 4); |
| 89 | |
| 90 | // Now create the inset points and then outset the original |
| 91 | // rotated points |
| 92 | |
| 93 | // TL |
| 94 | *((SkPoint*)((intptr_t)fan1Pos + 0 * vertexStride)) = |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 95 | *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) + vec[0] + vec[1]; |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 96 | *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) -= vec[0] + vec[1]; |
| 97 | // BL |
| 98 | *((SkPoint*)((intptr_t)fan1Pos + 1 * vertexStride)) = |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 99 | *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) + vec[0] - vec[1]; |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 100 | *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) -= vec[0] - vec[1]; |
| 101 | // BR |
| 102 | *((SkPoint*)((intptr_t)fan1Pos + 2 * vertexStride)) = |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 103 | *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) - vec[0] - vec[1]; |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 104 | *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) += vec[0] + vec[1]; |
| 105 | // TR |
| 106 | *((SkPoint*)((intptr_t)fan1Pos + 3 * vertexStride)) = |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 107 | *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) - vec[0] + vec[1]; |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 108 | *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) += vec[0] - vec[1]; |
| 109 | } |
| 110 | |
| 111 | if (localMatrix) { |
| 112 | SkMatrix invViewMatrix; |
| 113 | if (!viewMatrix.invert(&invViewMatrix)) { |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 114 | SkDebugf("View matrix is non-invertible, local coords will be wrong."); |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 115 | invViewMatrix = SkMatrix::I(); |
| 116 | } |
| 117 | SkMatrix localCoordMatrix; |
| 118 | localCoordMatrix.setConcat(*localMatrix, invViewMatrix); |
| 119 | SkPoint* fan0Loc = reinterpret_cast<SkPoint*>(verts + sizeof(SkPoint) + sizeof(GrColor)); |
| 120 | localCoordMatrix.mapPointsWithStride(fan0Loc, fan0Pos, vertexStride, 8); |
| 121 | } |
| 122 | |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 123 | // Make verts point to vertex color and then set all the color and coverage vertex attrs |
| 124 | // values. |
| 125 | verts += sizeof(SkPoint); |
| 126 | |
| 127 | // The coverage offset is always the last vertex attribute |
| 128 | intptr_t coverageOffset = vertexStride - sizeof(GrColor) - sizeof(SkPoint); |
| 129 | for (int i = 0; i < 4; ++i) { |
| 130 | if (tweakAlphaForCoverage) { |
| 131 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0; |
| 132 | } else { |
| 133 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; |
| 134 | *reinterpret_cast<float*>(verts + i * vertexStride + coverageOffset) = 0; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | int scale; |
| 139 | if (inset < SK_ScalarHalf) { |
| 140 | scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf)); |
| 141 | SkASSERT(scale >= 0 && scale <= 255); |
| 142 | } else { |
| 143 | scale = 0xff; |
| 144 | } |
| 145 | |
| 146 | verts += 4 * vertexStride; |
| 147 | |
| 148 | float innerCoverage = GrNormalizeByteToFloat(scale); |
| 149 | GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale); |
| 150 | |
| 151 | for (int i = 0; i < 4; ++i) { |
| 152 | if (tweakAlphaForCoverage) { |
| 153 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor; |
| 154 | } else { |
| 155 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 156 | *reinterpret_cast<float*>(verts + i * vertexStride + coverageOffset) = innerCoverage; |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | } |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 160 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 161 | namespace { |
| 162 | |
| 163 | class AAFillRectOp final : public GrMeshDrawOp { |
| 164 | private: |
| 165 | using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 166 | |
joshualitt | 2ad37be | 2015-08-18 10:16:01 -0700 | [diff] [blame] | 167 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 168 | DEFINE_OP_CLASS_ID |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 169 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 170 | static std::unique_ptr<GrDrawOp> Make(GrPaint&& paint, |
| 171 | const SkMatrix& viewMatrix, |
| 172 | const SkRect& rect, |
| 173 | const SkRect& devRect, |
| 174 | const SkMatrix* localMatrix, |
| 175 | const GrUserStencilSettings* stencil) { |
| 176 | SkASSERT(view_matrix_ok_for_aa_fill_rect(viewMatrix)); |
| 177 | return Helper::FactoryHelper<AAFillRectOp>(std::move(paint), viewMatrix, rect, devRect, |
| 178 | localMatrix, stencil); |
| 179 | } |
| 180 | |
| 181 | AAFillRectOp(const Helper::MakeArgs& helperArgs, |
| 182 | GrColor color, |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 183 | const SkMatrix& viewMatrix, |
| 184 | const SkRect& rect, |
| 185 | const SkRect& devRect, |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 186 | const SkMatrix* localMatrix, |
| 187 | const GrUserStencilSettings* stencil) |
| 188 | : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencil) { |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 189 | if (localMatrix) { |
| 190 | void* mem = fRectData.push_back_n(sizeof(RectWithLocalMatrixInfo)); |
| 191 | new (mem) RectWithLocalMatrixInfo(color, viewMatrix, rect, devRect, *localMatrix); |
| 192 | } else { |
| 193 | void* mem = fRectData.push_back_n(sizeof(RectInfo)); |
| 194 | new (mem) RectInfo(color, viewMatrix, rect, devRect); |
| 195 | } |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 196 | IsZeroArea zeroArea = |
| 197 | (!rect.width() || !rect.height()) ? IsZeroArea::kYes : IsZeroArea::kNo; |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 198 | this->setBounds(devRect, HasAABloat::kYes, zeroArea); |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 199 | fRectCnt = 1; |
bsalomon | 0fdec8a | 2016-07-01 06:31:25 -0700 | [diff] [blame] | 200 | } |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 201 | |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 202 | const char* name() const override { return "AAFillRectOp"; } |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 203 | |
| 204 | SkString dumpInfo() const override { |
| 205 | SkString str; |
Robert Phillips | f5442bb | 2017-04-17 14:18:34 -0400 | [diff] [blame] | 206 | str.append(INHERITED::dumpInfo()); |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 207 | str.appendf("# combined: %d\n", fRectCnt); |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 208 | const RectInfo* info = this->first(); |
| 209 | for (int i = 0; i < fRectCnt; ++i) { |
| 210 | const SkRect& rect = info->rect(); |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 211 | str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i, |
| 212 | info->color(), rect.fLeft, rect.fTop, rect.fRight, rect.fBottom); |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 213 | info = this->next(info); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 214 | } |
Brian Salomon | 82dfd3d | 2017-06-14 12:30:35 -0400 | [diff] [blame] | 215 | str += fHelper.dumpInfo(); |
| 216 | str += INHERITED::dumpInfo(); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 217 | return str; |
| 218 | } |
| 219 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 220 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 221 | |
Brian Salomon | f86d37b | 2017-06-16 10:04:34 -0400 | [diff] [blame] | 222 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override { |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 223 | GrColor color = this->first()->color(); |
Brian Salomon | f86d37b | 2017-06-16 10:04:34 -0400 | [diff] [blame] | 224 | auto result = fHelper.xpRequiresDstTexture( |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 225 | caps, clip, GrProcessorAnalysisCoverage::kSingleChannel, &color); |
| 226 | this->first()->setColor(color); |
| 227 | return result; |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 228 | } |
| 229 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 230 | private: |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame] | 231 | void onPrepareDraws(Target* target) const override { |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 232 | using namespace GrDefaultGeoProcFactory; |
| 233 | |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 234 | Color color(Color::kPremulGrColorAttribute_Type); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 235 | Coverage::Type coverageType = fHelper.compatibleWithAlphaAsCoverage() |
| 236 | ? Coverage::kSolid_Type |
| 237 | : Coverage::kAttribute_Type; |
| 238 | LocalCoords lc = fHelper.usesLocalCoords() ? LocalCoords::kHasExplicit_Type |
| 239 | : LocalCoords::kUnused_Type; |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 240 | sk_sp<GrGeometryProcessor> gp = |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 241 | GrDefaultGeoProcFactory::Make(color, coverageType, lc, SkMatrix::I()); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 242 | if (!gp) { |
| 243 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | size_t vertexStride = gp->getVertexStride(); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 248 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 249 | sk_sp<const GrBuffer> indexBuffer(get_index_buffer(target->resourceProvider())); |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 250 | PatternHelper helper(GrPrimitiveType::kTriangles); |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 251 | void* vertices = |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 252 | helper.init(target, vertexStride, indexBuffer.get(), kVertsPerAAFillRect, |
| 253 | kIndicesPerAAFillRect, fRectCnt); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 254 | if (!vertices || !indexBuffer) { |
| 255 | SkDebugf("Could not allocate vertices\n"); |
| 256 | return; |
| 257 | } |
| 258 | |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 259 | const RectInfo* info = this->first(); |
| 260 | const SkMatrix* localMatrix = nullptr; |
| 261 | for (int i = 0; i < fRectCnt; i++) { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 262 | intptr_t verts = |
| 263 | reinterpret_cast<intptr_t>(vertices) + i * kVertsPerAAFillRect * vertexStride; |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 264 | if (fHelper.usesLocalCoords()) { |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 265 | if (info->hasLocalMatrix()) { |
| 266 | localMatrix = &static_cast<const RectWithLocalMatrixInfo*>(info)->localMatrix(); |
| 267 | } else { |
| 268 | localMatrix = &SkMatrix::I(); |
| 269 | } |
| 270 | } |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 271 | generate_aa_fill_rect_geometry(verts, vertexStride, info->color(), info->viewMatrix(), |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 272 | info->rect(), info->devRect(), |
| 273 | fHelper.compatibleWithAlphaAsCoverage(), localMatrix); |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 274 | info = this->next(info); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 275 | } |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 276 | helper.recordDraw(target, gp.get(), fHelper.makePipeline(target)); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 279 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 280 | AAFillRectOp* that = t->cast<AAFillRectOp>(); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 281 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame] | 282 | return false; |
| 283 | } |
| 284 | |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 285 | fRectData.push_back_n(that->fRectData.count(), that->fRectData.begin()); |
| 286 | fRectCnt += that->fRectCnt; |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 287 | this->joinBounds(*that); |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame] | 288 | return true; |
| 289 | } |
| 290 | |
| 291 | struct RectInfo { |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 292 | public: |
| 293 | RectInfo(GrColor color, const SkMatrix& viewMatrix, const SkRect& rect, |
| 294 | const SkRect& devRect) |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 295 | : RectInfo(color, viewMatrix, rect, devRect, HasLocalMatrix::kNo) {} |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 296 | bool hasLocalMatrix() const { return HasLocalMatrix::kYes == fHasLocalMatrix; } |
| 297 | GrColor color() const { return fColor; } |
| 298 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
| 299 | const SkRect& rect() const { return fRect; } |
| 300 | const SkRect& devRect() const { return fDevRect; } |
| 301 | |
| 302 | void setColor(GrColor color) { fColor = color; } |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 303 | |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 304 | protected: |
| 305 | enum class HasLocalMatrix : uint32_t { kNo, kYes }; |
| 306 | |
| 307 | RectInfo(GrColor color, const SkMatrix& viewMatrix, const SkRect& rect, |
| 308 | const SkRect& devRect, HasLocalMatrix hasLM) |
| 309 | : fHasLocalMatrix(hasLM) |
| 310 | , fColor(color) |
| 311 | , fViewMatrix(viewMatrix) |
| 312 | , fRect(rect) |
| 313 | , fDevRect(devRect) {} |
| 314 | |
| 315 | HasLocalMatrix fHasLocalMatrix; |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame] | 316 | GrColor fColor; |
| 317 | SkMatrix fViewMatrix; |
| 318 | SkRect fRect; |
| 319 | SkRect fDevRect; |
| 320 | }; |
| 321 | |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 322 | struct RectWithLocalMatrixInfo : public RectInfo { |
| 323 | public: |
| 324 | RectWithLocalMatrixInfo(GrColor color, const SkMatrix& viewMatrix, const SkRect& rect, |
| 325 | const SkRect& devRect, const SkMatrix& localMatrix) |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 326 | : RectInfo(color, viewMatrix, rect, devRect, HasLocalMatrix::kYes) |
| 327 | , fLocalMatrix(localMatrix) {} |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 328 | const SkMatrix& localMatrix() const { return fLocalMatrix; } |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 329 | |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 330 | private: |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame] | 331 | SkMatrix fLocalMatrix; |
bsalomon | 0fdec8a | 2016-07-01 06:31:25 -0700 | [diff] [blame] | 332 | }; |
| 333 | |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 334 | RectInfo* first() { return reinterpret_cast<RectInfo*>(fRectData.begin()); } |
| 335 | const RectInfo* first() const { return reinterpret_cast<const RectInfo*>(fRectData.begin()); } |
| 336 | const RectInfo* next(const RectInfo* prev) const { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 337 | intptr_t next = |
| 338 | reinterpret_cast<intptr_t>(prev) + |
| 339 | (prev->hasLocalMatrix() ? sizeof(RectWithLocalMatrixInfo) : sizeof(RectInfo)); |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 340 | return reinterpret_cast<const RectInfo*>(next); |
| 341 | } |
| 342 | |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 343 | SkSTArray<4 * sizeof(RectWithLocalMatrixInfo), uint8_t, true> fRectData; |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 344 | Helper fHelper; |
bsalomon | 4be9a30 | 2016-07-06 07:03:26 -0700 | [diff] [blame] | 345 | int fRectCnt; |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 346 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 347 | typedef GrMeshDrawOp INHERITED; |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 348 | }; |
| 349 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 350 | } // anonymous namespace |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 351 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 352 | namespace GrRectOpFactory { |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 353 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 354 | std::unique_ptr<GrDrawOp> MakeAAFill(GrPaint&& paint, const SkMatrix& viewMatrix, |
| 355 | const SkRect& rect, const GrUserStencilSettings* stencil) { |
| 356 | if (!view_matrix_ok_for_aa_fill_rect(viewMatrix)) { |
| 357 | return nullptr; |
| 358 | } |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 359 | SkRect devRect; |
| 360 | viewMatrix.mapRect(&devRect, rect); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 361 | return AAFillRectOp::Make(std::move(paint), viewMatrix, rect, devRect, nullptr, stencil); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 362 | } |
| 363 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 364 | std::unique_ptr<GrDrawOp> MakeAAFillWithLocalMatrix(GrPaint&& paint, const SkMatrix& viewMatrix, |
| 365 | const SkMatrix& localMatrix, |
| 366 | const SkRect& rect) { |
| 367 | if (!view_matrix_ok_for_aa_fill_rect(viewMatrix)) { |
| 368 | return nullptr; |
| 369 | } |
| 370 | SkRect devRect; |
| 371 | viewMatrix.mapRect(&devRect, rect); |
| 372 | return AAFillRectOp::Make(std::move(paint), viewMatrix, rect, devRect, &localMatrix, nullptr); |
| 373 | } |
| 374 | |
| 375 | std::unique_ptr<GrDrawOp> MakeAAFillWithLocalRect(GrPaint&& paint, const SkMatrix& viewMatrix, |
| 376 | const SkRect& rect, const SkRect& localRect) { |
| 377 | if (!view_matrix_ok_for_aa_fill_rect(viewMatrix)) { |
| 378 | return nullptr; |
| 379 | } |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 380 | SkRect devRect; |
| 381 | viewMatrix.mapRect(&devRect, rect); |
| 382 | SkMatrix localMatrix; |
| 383 | if (!localMatrix.setRectToRect(rect, localRect, SkMatrix::kFill_ScaleToFit)) { |
| 384 | return nullptr; |
| 385 | } |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 386 | return AAFillRectOp::Make(std::move(paint), viewMatrix, rect, devRect, &localMatrix, nullptr); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 387 | } |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 388 | |
| 389 | } // namespace GrRectOpFactory |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 390 | |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 391 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 392 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 393 | #if GR_TEST_UTILS |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 394 | |
Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame] | 395 | #include "GrDrawOpTest.h" |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 396 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 397 | GR_DRAW_OP_TEST_DEFINE(AAFillRectOp) { |
| 398 | SkMatrix viewMatrix; |
| 399 | do { |
| 400 | viewMatrix = GrTest::TestMatrixInvertible(random); |
| 401 | } while (!view_matrix_ok_for_aa_fill_rect(viewMatrix)); |
joshualitt | 090ae8e | 2015-08-14 09:01:21 -0700 | [diff] [blame] | 402 | SkRect rect = GrTest::TestRect(random); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 403 | SkRect devRect; |
| 404 | viewMatrix.mapRect(&devRect, rect); |
| 405 | const SkMatrix* localMatrix = nullptr; |
| 406 | SkMatrix m; |
| 407 | if (random->nextBool()) { |
| 408 | m = GrTest::TestMatrix(random); |
| 409 | } |
| 410 | const GrUserStencilSettings* stencil = |
| 411 | random->nextBool() ? nullptr : GrGetRandomStencil(random, context); |
| 412 | return AAFillRectOp::Make(std::move(paint), viewMatrix, rect, devRect, localMatrix, stencil); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | #endif |