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