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" |
| 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" |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 18 | #include "GrVertexBatch.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 | |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 48 | static sk_sp<GrGeometryProcessor> create_fill_rect_gp( |
| 49 | const SkMatrix& viewMatrix, |
| 50 | const GrXPOverridesForBatch& overrides, |
| 51 | GrDefaultGeoProcFactory::LocalCoords::Type localCoordsType) { |
| 52 | using namespace GrDefaultGeoProcFactory; |
| 53 | |
| 54 | Color color(Color::kAttribute_Type); |
| 55 | Coverage::Type coverageType; |
| 56 | // TODO remove coverage if coverage is ignored |
| 57 | /*if (coverageIgnored) { |
| 58 | coverageType = Coverage::kNone_Type; |
| 59 | } else*/ if (overrides.canTweakAlphaForCoverage()) { |
| 60 | coverageType = Coverage::kSolid_Type; |
| 61 | } else { |
| 62 | coverageType = Coverage::kAttribute_Type; |
| 63 | } |
| 64 | Coverage coverage(coverageType); |
| 65 | |
| 66 | // We assume the caller has inverted the viewmatrix |
| 67 | if (LocalCoords::kHasExplicit_Type == localCoordsType) { |
| 68 | LocalCoords localCoords(localCoordsType); |
| 69 | return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMatrix::I()); |
| 70 | } else { |
| 71 | LocalCoords localCoords(overrides.readsLocalCoords() ? localCoordsType : |
| 72 | LocalCoords::kUnused_Type); |
| 73 | return MakeForDeviceSpace(color, coverage, localCoords, viewMatrix); |
| 74 | } |
| 75 | } |
| 76 | |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 77 | static void generate_aa_fill_rect_geometry(intptr_t verts, |
| 78 | size_t vertexStride, |
| 79 | GrColor color, |
| 80 | const SkMatrix& viewMatrix, |
| 81 | const SkRect& rect, |
| 82 | const SkRect& devRect, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 83 | const GrXPOverridesForBatch& overrides, |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 84 | const SkMatrix* localMatrix) { |
| 85 | SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts); |
| 86 | SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride); |
| 87 | |
robertphillips | 0851d2d | 2016-06-02 05:21:34 -0700 | [diff] [blame] | 88 | SkScalar inset; |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 89 | |
| 90 | if (viewMatrix.rectStaysRect()) { |
robertphillips | 0851d2d | 2016-06-02 05:21:34 -0700 | [diff] [blame] | 91 | inset = SkMinScalar(devRect.width(), SK_Scalar1); |
| 92 | inset = SK_ScalarHalf * SkMinScalar(inset, devRect.height()); |
| 93 | |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 94 | set_inset_fan(fan0Pos, vertexStride, devRect, -SK_ScalarHalf, -SK_ScalarHalf); |
| 95 | set_inset_fan(fan1Pos, vertexStride, devRect, inset, inset); |
| 96 | } else { |
| 97 | // compute transformed (1, 0) and (0, 1) vectors |
| 98 | SkVector vec[2] = { |
| 99 | { viewMatrix[SkMatrix::kMScaleX], viewMatrix[SkMatrix::kMSkewY] }, |
| 100 | { viewMatrix[SkMatrix::kMSkewX], viewMatrix[SkMatrix::kMScaleY] } |
| 101 | }; |
| 102 | |
robertphillips | 0851d2d | 2016-06-02 05:21:34 -0700 | [diff] [blame] | 103 | SkScalar len1 = SkPoint::Normalize(&vec[0]); |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 104 | vec[0].scale(SK_ScalarHalf); |
robertphillips | 0851d2d | 2016-06-02 05:21:34 -0700 | [diff] [blame] | 105 | SkScalar len2 = SkPoint::Normalize(&vec[1]); |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 106 | vec[1].scale(SK_ScalarHalf); |
| 107 | |
robertphillips | 0851d2d | 2016-06-02 05:21:34 -0700 | [diff] [blame] | 108 | inset = SkMinScalar(len1 * rect.width(), SK_Scalar1); |
| 109 | inset = SK_ScalarHalf * SkMinScalar(inset, len2 * rect.height()); |
| 110 | |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 111 | // create the rotated rect |
| 112 | fan0Pos->setRectFan(rect.fLeft, rect.fTop, |
| 113 | rect.fRight, rect.fBottom, vertexStride); |
| 114 | viewMatrix.mapPointsWithStride(fan0Pos, vertexStride, 4); |
| 115 | |
| 116 | // Now create the inset points and then outset the original |
| 117 | // rotated points |
| 118 | |
| 119 | // TL |
| 120 | *((SkPoint*)((intptr_t)fan1Pos + 0 * vertexStride)) = |
| 121 | *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) + vec[0] + vec[1]; |
| 122 | *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) -= vec[0] + vec[1]; |
| 123 | // BL |
| 124 | *((SkPoint*)((intptr_t)fan1Pos + 1 * vertexStride)) = |
| 125 | *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) + vec[0] - vec[1]; |
| 126 | *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) -= vec[0] - vec[1]; |
| 127 | // BR |
| 128 | *((SkPoint*)((intptr_t)fan1Pos + 2 * vertexStride)) = |
| 129 | *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) - vec[0] - vec[1]; |
| 130 | *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) += vec[0] + vec[1]; |
| 131 | // TR |
| 132 | *((SkPoint*)((intptr_t)fan1Pos + 3 * vertexStride)) = |
| 133 | *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) - vec[0] + vec[1]; |
| 134 | *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) += vec[0] - vec[1]; |
| 135 | } |
| 136 | |
| 137 | if (localMatrix) { |
| 138 | SkMatrix invViewMatrix; |
| 139 | if (!viewMatrix.invert(&invViewMatrix)) { |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 140 | SkASSERT(false); |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 141 | invViewMatrix = SkMatrix::I(); |
| 142 | } |
| 143 | SkMatrix localCoordMatrix; |
| 144 | localCoordMatrix.setConcat(*localMatrix, invViewMatrix); |
| 145 | SkPoint* fan0Loc = reinterpret_cast<SkPoint*>(verts + sizeof(SkPoint) + sizeof(GrColor)); |
| 146 | localCoordMatrix.mapPointsWithStride(fan0Loc, fan0Pos, vertexStride, 8); |
| 147 | } |
| 148 | |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 149 | bool tweakAlphaForCoverage = overrides.canTweakAlphaForCoverage(); |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 150 | |
| 151 | // Make verts point to vertex color and then set all the color and coverage vertex attrs |
| 152 | // values. |
| 153 | verts += sizeof(SkPoint); |
| 154 | |
| 155 | // The coverage offset is always the last vertex attribute |
| 156 | intptr_t coverageOffset = vertexStride - sizeof(GrColor) - sizeof(SkPoint); |
| 157 | for (int i = 0; i < 4; ++i) { |
| 158 | if (tweakAlphaForCoverage) { |
| 159 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0; |
| 160 | } else { |
| 161 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; |
| 162 | *reinterpret_cast<float*>(verts + i * vertexStride + coverageOffset) = 0; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | int scale; |
| 167 | if (inset < SK_ScalarHalf) { |
| 168 | scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf)); |
| 169 | SkASSERT(scale >= 0 && scale <= 255); |
| 170 | } else { |
| 171 | scale = 0xff; |
| 172 | } |
| 173 | |
| 174 | verts += 4 * vertexStride; |
| 175 | |
| 176 | float innerCoverage = GrNormalizeByteToFloat(scale); |
| 177 | GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale); |
| 178 | |
| 179 | for (int i = 0; i < 4; ++i) { |
| 180 | if (tweakAlphaForCoverage) { |
| 181 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor; |
| 182 | } else { |
| 183 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; |
| 184 | *reinterpret_cast<float*>(verts + i * vertexStride + |
| 185 | coverageOffset) = innerCoverage; |
| 186 | } |
| 187 | } |
| 188 | } |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 189 | |
| 190 | class AAFillRectNoLocalMatrixBatch : public GrVertexBatch { |
joshualitt | 2ad37be | 2015-08-18 10:16:01 -0700 | [diff] [blame] | 191 | public: |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 192 | DEFINE_BATCH_CLASS_ID |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 193 | AAFillRectNoLocalMatrixBatch(GrColor color, |
| 194 | const SkMatrix& viewMatrix, |
| 195 | const SkRect& rect, |
| 196 | const SkRect& devRect) : INHERITED(ClassID()) { |
| 197 | fRects.emplace_back(RectInfo{color, viewMatrix, rect, devRect}); |
bsalomon | 0fdec8a | 2016-07-01 06:31:25 -0700 | [diff] [blame] | 198 | fBounds = devRect; |
| 199 | } |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 200 | |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 201 | const char* name() const override { return "AAFillRectBatchNoLocalMatrix"; } |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 202 | |
| 203 | SkString dumpInfo() const override { |
| 204 | SkString str; |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 205 | str.appendf("# batched: %d\n", fRects.count()); |
| 206 | for (int i = 0; i < fRects.count(); ++i) { |
| 207 | const RectInfo& info = fRects[i]; |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 208 | str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 209 | i, info.fColor, |
| 210 | info.fRect.fLeft, info.fRect.fTop, info.fRect.fRight, info.fRect.fBottom); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 211 | } |
| 212 | str.append(INHERITED::dumpInfo()); |
| 213 | return str; |
| 214 | } |
| 215 | |
| 216 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
| 217 | GrInitInvariantOutput* coverage, |
| 218 | GrBatchToXPOverrides* overrides) const override { |
bsalomon | 0fdec8a | 2016-07-01 06:31:25 -0700 | [diff] [blame] | 219 | // When this is called on a batch, there is only one rect |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 220 | color->setKnownFourComponents(fRects[0].fColor); |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 221 | coverage->setUnknownSingleComponent(); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 225 | overrides.getOverrideColorIfSet(&fRects[0].fColor); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 226 | fOverrides = overrides; |
| 227 | } |
| 228 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 229 | private: |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 230 | AAFillRectNoLocalMatrixBatch() : INHERITED(ClassID()) {} |
bsalomon | e525eca | 2016-07-01 11:02:39 -0700 | [diff] [blame] | 231 | |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 232 | void onPrepareDraws(Target* target) const override { |
| 233 | sk_sp<GrGeometryProcessor> gp = |
| 234 | create_fill_rect_gp(fRects[0].fViewMatrix, fOverrides, |
| 235 | GrDefaultGeoProcFactory::LocalCoords::kUsePosition_Type); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 236 | if (!gp) { |
| 237 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 238 | return; |
| 239 | } |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 240 | SkASSERT(fOverrides.canTweakAlphaForCoverage() ? |
| 241 | gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorAttr) : |
| 242 | gp->getVertexStride() == |
| 243 | sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr)); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 244 | |
| 245 | size_t vertexStride = gp->getVertexStride(); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 246 | |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 247 | SkAutoTUnref<const GrBuffer> indexBuffer(get_index_buffer(target->resourceProvider())); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 248 | InstancedHelper helper; |
| 249 | void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride, |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 250 | indexBuffer, kVertsPerAAFillRect, |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 251 | kIndicesPerAAFillRect, fRects.count()); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 252 | if (!vertices || !indexBuffer) { |
| 253 | SkDebugf("Could not allocate vertices\n"); |
| 254 | return; |
| 255 | } |
| 256 | |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 257 | for (int i = 0; i < fRects.count(); i++) { |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 258 | intptr_t verts = reinterpret_cast<intptr_t>(vertices) + |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 259 | i * kVertsPerAAFillRect * vertexStride; |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 260 | generate_aa_fill_rect_geometry(verts, vertexStride, |
| 261 | fRects[i].fColor, fRects[i].fViewMatrix, |
| 262 | fRects[i].fRect, fRects[i].fDevRect, fOverrides, |
| 263 | nullptr); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 264 | } |
| 265 | helper.recordDraw(target, gp.get()); |
| 266 | } |
| 267 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 268 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 269 | AAFillRectNoLocalMatrixBatch* that = t->cast<AAFillRectNoLocalMatrixBatch>(); |
| 270 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 271 | that->bounds(), caps)) { |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses |
| 276 | // local coords then we won't be able to batch. We could actually upload the viewmatrix |
| 277 | // using vertex attributes in these cases, but haven't investigated that |
| 278 | if (fOverrides.readsLocalCoords() && |
| 279 | !fRects[0].fViewMatrix.cheapEqualTo(that->fRects[0].fViewMatrix)) { |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | // In the event of two batches, one who can tweak, one who cannot, we just fall back to |
| 284 | // not tweaking |
| 285 | if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakAlphaForCoverage()) { |
| 286 | fOverrides = that->fOverrides; |
| 287 | } |
| 288 | |
| 289 | fRects.push_back_n(that->fRects.count(), that->fRects.begin()); |
| 290 | this->joinBounds(that->bounds()); |
| 291 | return true; |
| 292 | } |
| 293 | |
| 294 | struct RectInfo { |
| 295 | GrColor fColor; |
| 296 | SkMatrix fViewMatrix; |
| 297 | SkRect fRect; |
| 298 | SkRect fDevRect; |
| 299 | }; |
| 300 | |
| 301 | GrXPOverridesForBatch fOverrides; |
| 302 | SkSTArray<1, RectInfo, true> fRects; |
| 303 | |
| 304 | typedef GrVertexBatch INHERITED; |
| 305 | }; |
| 306 | |
| 307 | class AAFillRectLocalMatrixBatch : public GrVertexBatch { |
| 308 | public: |
| 309 | DEFINE_BATCH_CLASS_ID |
| 310 | |
| 311 | AAFillRectLocalMatrixBatch(GrColor color, |
| 312 | const SkMatrix& viewMatrix, |
| 313 | const SkMatrix& localMatrix, |
| 314 | const SkRect& rect, |
| 315 | const SkRect& devRect) : INHERITED(ClassID()) { |
| 316 | fRects.emplace_back(RectInfo{color, viewMatrix, localMatrix, rect, devRect}); |
| 317 | fBounds = devRect; |
| 318 | } |
| 319 | |
| 320 | const char* name() const override { return "AAFillRectBatchLocalMatrix"; } |
| 321 | |
| 322 | SkString dumpInfo() const override { |
| 323 | SkString str; |
| 324 | str.appendf("# batched: %d\n", fRects.count()); |
| 325 | for (int i = 0; i < fRects.count(); ++i) { |
| 326 | const RectInfo& info = fRects[i]; |
| 327 | str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", |
| 328 | i, info.fColor, |
| 329 | info.fRect.fLeft, info.fRect.fTop, info.fRect.fRight, info.fRect.fBottom); |
| 330 | } |
| 331 | str.append(INHERITED::dumpInfo()); |
| 332 | return str; |
| 333 | } |
| 334 | |
| 335 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
| 336 | GrInitInvariantOutput* coverage, |
| 337 | GrBatchToXPOverrides* overrides) const override { |
| 338 | // When this is called on a batch, there is only one rect |
| 339 | color->setKnownFourComponents(fRects[0].fColor); |
| 340 | coverage->setUnknownSingleComponent(); |
| 341 | } |
| 342 | |
| 343 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
| 344 | overrides.getOverrideColorIfSet(&fRects[0].fColor); |
| 345 | fOverrides = overrides; |
| 346 | } |
| 347 | |
| 348 | private: |
| 349 | AAFillRectLocalMatrixBatch() : INHERITED(ClassID()) {} |
| 350 | |
| 351 | void onPrepareDraws(Target* target) const override { |
| 352 | sk_sp<GrGeometryProcessor> gp = |
| 353 | create_fill_rect_gp(fRects[0].fViewMatrix, fOverrides, |
| 354 | GrDefaultGeoProcFactory::LocalCoords::kHasExplicit_Type); |
| 355 | if (!gp) { |
| 356 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 357 | return; |
| 358 | } |
| 359 | SkASSERT(fOverrides.canTweakAlphaForCoverage() ? |
| 360 | gp->getVertexStride() == |
| 361 | sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr) : |
| 362 | gp->getVertexStride() == |
| 363 | sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordCoverage)); |
| 364 | |
| 365 | size_t vertexStride = gp->getVertexStride(); |
| 366 | |
| 367 | SkAutoTUnref<const GrBuffer> indexBuffer(get_index_buffer(target->resourceProvider())); |
| 368 | InstancedHelper helper; |
| 369 | void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride, |
| 370 | indexBuffer, kVertsPerAAFillRect, |
| 371 | kIndicesPerAAFillRect, fRects.count()); |
| 372 | if (!vertices || !indexBuffer) { |
| 373 | SkDebugf("Could not allocate vertices\n"); |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | for (int i = 0; i < fRects.count(); i++) { |
| 378 | intptr_t verts = reinterpret_cast<intptr_t>(vertices) + |
| 379 | i * kVertsPerAAFillRect * vertexStride; |
| 380 | generate_aa_fill_rect_geometry(verts, vertexStride, fRects[i].fColor, |
| 381 | fRects[i].fViewMatrix, fRects[i].fRect, |
| 382 | fRects[i].fDevRect, fOverrides, |
| 383 | &fRects[i].fLocalMatrix); |
| 384 | } |
| 385 | helper.recordDraw(target, gp.get()); |
| 386 | } |
| 387 | |
| 388 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
| 389 | AAFillRectLocalMatrixBatch* that = t->cast<AAFillRectLocalMatrixBatch>(); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 390 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 391 | that->bounds(), caps)) { |
| 392 | return false; |
| 393 | } |
| 394 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 395 | // In the event of two batches, one who can tweak, one who cannot, we just fall back to |
| 396 | // not tweaking |
| 397 | if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakAlphaForCoverage()) { |
| 398 | fOverrides = that->fOverrides; |
| 399 | } |
| 400 | |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 401 | fRects.push_back_n(that->fRects.count(), that->fRects.begin()); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 402 | this->joinBounds(that->bounds()); |
| 403 | return true; |
| 404 | } |
| 405 | |
bsalomon | 0fdec8a | 2016-07-01 06:31:25 -0700 | [diff] [blame] | 406 | struct RectInfo { |
| 407 | GrColor fColor; |
| 408 | SkMatrix fViewMatrix; |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 409 | SkMatrix fLocalMatrix; |
bsalomon | 0fdec8a | 2016-07-01 06:31:25 -0700 | [diff] [blame] | 410 | SkRect fRect; |
| 411 | SkRect fDevRect; |
| 412 | }; |
| 413 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 414 | GrXPOverridesForBatch fOverrides; |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 415 | SkSTArray<1, RectInfo, true> fRects; |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 416 | |
| 417 | typedef GrVertexBatch INHERITED; |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 418 | }; |
| 419 | |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 420 | namespace GrAAFillRectBatch { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 421 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 422 | GrDrawBatch* Create(GrColor color, |
| 423 | const SkMatrix& viewMatrix, |
| 424 | const SkRect& rect, |
| 425 | const SkRect& devRect) { |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 426 | return new AAFillRectNoLocalMatrixBatch(color, viewMatrix, rect, devRect); |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 427 | } |
| 428 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 429 | GrDrawBatch* Create(GrColor color, |
| 430 | const SkMatrix& viewMatrix, |
| 431 | const SkMatrix& localMatrix, |
| 432 | const SkRect& rect, |
| 433 | const SkRect& devRect) { |
benjaminwagner | d87a6b2 | 2016-07-04 11:30:01 -0700 | [diff] [blame^] | 434 | return new AAFillRectLocalMatrixBatch(color, viewMatrix, localMatrix, rect, devRect); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 435 | } |
| 436 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 437 | GrDrawBatch* Create(GrColor color, |
| 438 | const SkMatrix& viewMatrix, |
| 439 | const SkMatrix& localMatrix, |
| 440 | const SkRect& rect) { |
| 441 | SkRect devRect; |
| 442 | viewMatrix.mapRect(&devRect, rect); |
| 443 | return Create(color, viewMatrix, localMatrix, rect, devRect); |
| 444 | } |
| 445 | |
| 446 | GrDrawBatch* CreateWithLocalRect(GrColor color, |
| 447 | const SkMatrix& viewMatrix, |
| 448 | const SkRect& rect, |
| 449 | const SkRect& localRect) { |
| 450 | SkRect devRect; |
| 451 | viewMatrix.mapRect(&devRect, rect); |
| 452 | SkMatrix localMatrix; |
| 453 | if (!localMatrix.setRectToRect(rect, localRect, SkMatrix::kFill_ScaleToFit)) { |
| 454 | return nullptr; |
| 455 | } |
| 456 | return Create(color, viewMatrix, localMatrix, rect, devRect); |
| 457 | } |
| 458 | |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 459 | }; |
| 460 | |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 461 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 462 | |
| 463 | #ifdef GR_TEST_UTILS |
| 464 | |
| 465 | #include "GrBatchTest.h" |
| 466 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 467 | DRAW_BATCH_TEST_DEFINE(AAFillRectBatch) { |
joshualitt | 090ae8e | 2015-08-14 09:01:21 -0700 | [diff] [blame] | 468 | GrColor color = GrRandomColor(random); |
| 469 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 470 | SkRect rect = GrTest::TestRect(random); |
| 471 | SkRect devRect = GrTest::TestRect(random); |
| 472 | return GrAAFillRectBatch::Create(color, viewMatrix, rect, devRect); |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 473 | } |
| 474 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 475 | DRAW_BATCH_TEST_DEFINE(AAFillRectBatchLocalMatrix) { |
joshualitt | 090ae8e | 2015-08-14 09:01:21 -0700 | [diff] [blame] | 476 | GrColor color = GrRandomColor(random); |
| 477 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 478 | SkMatrix localMatrix = GrTest::TestMatrix(random); |
| 479 | SkRect rect = GrTest::TestRect(random); |
| 480 | SkRect devRect = GrTest::TestRect(random); |
| 481 | return GrAAFillRectBatch::Create(color, viewMatrix, localMatrix, rect, devRect); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | #endif |