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