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