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 | |
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" |
| 12 | #include "GrResourceKey.h" |
| 13 | #include "GrResourceProvider.h" |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 14 | #include "GrTypes.h" |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 15 | #include "GrVertexBatch.h" |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 16 | #include "SkMatrix.h" |
| 17 | #include "SkRect.h" |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 18 | |
| 19 | GR_DECLARE_STATIC_UNIQUE_KEY(gAAFillRectIndexBufferKey); |
| 20 | |
| 21 | static void set_inset_fan(SkPoint* pts, size_t stride, |
| 22 | const SkRect& r, SkScalar dx, SkScalar dy) { |
| 23 | pts->setRectFan(r.fLeft + dx, r.fTop + dy, |
| 24 | r.fRight - dx, r.fBottom - dy, stride); |
| 25 | } |
| 26 | |
joshualitt | 27801bf | 2015-08-12 12:52:47 -0700 | [diff] [blame] | 27 | static const int kNumAAFillRectsInIndexBuffer = 256; |
| 28 | static const int kVertsPerAAFillRect = 8; |
| 29 | static const int kIndicesPerAAFillRect = 30; |
| 30 | |
| 31 | const GrIndexBuffer* get_index_buffer(GrResourceProvider* resourceProvider) { |
| 32 | GR_DEFINE_STATIC_UNIQUE_KEY(gAAFillRectIndexBufferKey); |
| 33 | |
| 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 | }; |
| 41 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(gFillAARectIdx) == kIndicesPerAAFillRect); |
| 42 | return resourceProvider->findOrCreateInstancedIndexBuffer(gFillAARectIdx, |
| 43 | kIndicesPerAAFillRect, kNumAAFillRectsInIndexBuffer, kVertsPerAAFillRect, |
| 44 | gAAFillRectIndexBufferKey); |
| 45 | } |
| 46 | |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 47 | /* |
| 48 | * AAFillRectBatch is templated to optionally allow the insertion of an additional |
| 49 | * attribute for explicit local coordinates. |
| 50 | * To use this template, an implementation must define the following static functions: |
| 51 | * A Geometry struct |
| 52 | * |
| 53 | * bool CanCombineLocalCoords(const SkMatrix& mine, const SkMatrix& theirs, |
| 54 | * bool usesLocalCoords) |
| 55 | * |
| 56 | * GrDefaultGeoProcFactory::LocalCoords::Type LocalCoordsType() |
| 57 | * |
| 58 | * bool StrideCheck(size_t vertexStride, bool canTweakAlphaForCoverage, |
| 59 | * bool usesLocalCoords) |
| 60 | * |
| 61 | * void FillInAttributes(intptr_t startVertex, size_t vertexStride, |
| 62 | * SkPoint* fan0Position, const Geometry&) |
| 63 | */ |
| 64 | template <typename Base> |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 65 | class AAFillRectBatch : public GrVertexBatch { |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 66 | public: |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 67 | typedef typename Base::Geometry Geometry; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 68 | |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 69 | static AAFillRectBatch* Create() { |
| 70 | return SkNEW(AAFillRectBatch); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 71 | } |
| 72 | |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 73 | const char* name() const override { return "AAFillRectBatch"; } |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 74 | |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 75 | void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
| 76 | // When this is called on a batch, there is only one geometry bundle |
| 77 | out->setKnownFourComponents(fGeoData[0].fColor); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 78 | } |
| 79 | |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 80 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
| 81 | out->setUnknownSingleComponent(); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 82 | } |
| 83 | |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 84 | void initBatchTracker(const GrPipelineOptimizations& opt) override { |
| 85 | // Handle any color overrides |
| 86 | if (!opt.readsColor()) { |
| 87 | fGeoData[0].fColor = GrColor_ILLEGAL; |
| 88 | } |
| 89 | opt.getOverrideColorIfSet(&fGeoData[0].fColor); |
| 90 | |
| 91 | // setup batch properties |
| 92 | fBatch.fColorIgnored = !opt.readsColor(); |
| 93 | fBatch.fColor = fGeoData[0].fColor; |
| 94 | fBatch.fUsesLocalCoords = opt.readsLocalCoords(); |
| 95 | fBatch.fCoverageIgnored = !opt.readsCoverage(); |
| 96 | fBatch.fCanTweakAlphaForCoverage = opt.canTweakAlphaForCoverage(); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 97 | } |
| 98 | |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 99 | void generateGeometry(GrBatchTarget* batchTarget) override { |
| 100 | bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage(); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 101 | |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 102 | SkAutoTUnref<const GrGeometryProcessor> gp(CreateFillRectGP(canTweakAlphaForCoverage, |
| 103 | this->viewMatrix(), |
| 104 | this->usesLocalCoords(), |
| 105 | Base::LocalCoordsType(), |
| 106 | this->coverageIgnored())); |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 107 | if (!gp) { |
| 108 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 109 | return; |
| 110 | } |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 111 | |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 112 | batchTarget->initDraw(gp, this->pipeline()); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 113 | |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 114 | size_t vertexStride = gp->getVertexStride(); |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 115 | SkASSERT(Base::StrideCheck(vertexStride, canTweakAlphaForCoverage, |
| 116 | this->usesLocalCoords())); |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 117 | int instanceCount = fGeoData.count(); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 118 | |
joshualitt | 27801bf | 2015-08-12 12:52:47 -0700 | [diff] [blame] | 119 | SkAutoTUnref<const GrIndexBuffer> indexBuffer(get_index_buffer( |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 120 | batchTarget->resourceProvider())); |
| 121 | InstancedHelper helper; |
| 122 | void* vertices = helper.init(batchTarget, kTriangles_GrPrimitiveType, vertexStride, |
| 123 | indexBuffer, kVertsPerAAFillRect, kIndicesPerAAFillRect, |
| 124 | instanceCount); |
| 125 | if (!vertices || !indexBuffer) { |
| 126 | SkDebugf("Could not allocate vertices\n"); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | for (int i = 0; i < instanceCount; i++) { |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 131 | this->generateAAFillRectGeometry(vertices, |
| 132 | i * kVertsPerAAFillRect * vertexStride, |
| 133 | vertexStride, |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 134 | fGeoData[i], |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 135 | canTweakAlphaForCoverage); |
| 136 | } |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 137 | helper.issueDraw(batchTarget); |
| 138 | } |
| 139 | |
| 140 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 141 | |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 142 | // to avoid even the initial copy of the struct, we have a getter for the first item which |
| 143 | // is used to seed the batch with its initial geometry. After seeding, the client should call |
| 144 | // init() so the Batch can initialize itself |
| 145 | Geometry* geometry() { return &fGeoData[0]; } |
| 146 | void init() { |
| 147 | const Geometry& geo = fGeoData[0]; |
| 148 | this->setBounds(geo.fDevRect); |
| 149 | } |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 150 | |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 151 | |
| 152 | private: |
| 153 | AAFillRectBatch() { |
| 154 | this->initClassID<AAFillRectBatch<Base>>(); |
| 155 | |
| 156 | // Push back an initial geometry |
| 157 | fGeoData.push_back(); |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 158 | } |
| 159 | |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 160 | GrColor color() const { return fBatch.fColor; } |
| 161 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 162 | bool canTweakAlphaForCoverage() const { return fBatch.fCanTweakAlphaForCoverage; } |
| 163 | bool colorIgnored() const { return fBatch.fColorIgnored; } |
| 164 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 165 | bool coverageIgnored() const { return fBatch.fCoverageIgnored; } |
| 166 | |
bsalomon | c3021ed | 2015-08-12 11:28:11 -0700 | [diff] [blame] | 167 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 168 | AAFillRectBatch* that = t->cast<AAFillRectBatch>(); |
| 169 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 170 | that->bounds(), caps)) { |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 171 | return false; |
| 172 | } |
| 173 | |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 174 | if (!Base::CanCombineLocalCoords(this->viewMatrix(), that->viewMatrix(), |
| 175 | this->usesLocalCoords())) { |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 176 | return false; |
| 177 | } |
| 178 | |
| 179 | if (this->color() != that->color()) { |
| 180 | fBatch.fColor = GrColor_ILLEGAL; |
| 181 | } |
| 182 | |
| 183 | // In the event of two batches, one who can tweak, one who cannot, we just fall back to |
| 184 | // not tweaking |
| 185 | if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) { |
| 186 | fBatch.fCanTweakAlphaForCoverage = false; |
| 187 | } |
| 188 | |
| 189 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
| 190 | this->joinBounds(that->bounds()); |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | void generateAAFillRectGeometry(void* vertices, |
| 195 | size_t offset, |
| 196 | size_t vertexStride, |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 197 | const Geometry& args, |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 198 | bool tweakAlphaForCoverage) const { |
| 199 | intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset; |
| 200 | |
| 201 | SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts); |
| 202 | SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride); |
| 203 | |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 204 | SkScalar inset = SkMinScalar(args.fDevRect.width(), SK_Scalar1); |
| 205 | inset = SK_ScalarHalf * SkMinScalar(inset, args.fDevRect.height()); |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 206 | |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 207 | if (args.fViewMatrix.rectStaysRect()) { |
| 208 | set_inset_fan(fan0Pos, vertexStride, args.fDevRect, -SK_ScalarHalf, -SK_ScalarHalf); |
| 209 | set_inset_fan(fan1Pos, vertexStride, args.fDevRect, inset, inset); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 210 | } else { |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 211 | // compute transformed (1, 0) and (0, 1) vectors |
| 212 | SkVector vec[2] = { |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 213 | { args.fViewMatrix[SkMatrix::kMScaleX], args.fViewMatrix[SkMatrix::kMSkewY] }, |
| 214 | { args.fViewMatrix[SkMatrix::kMSkewX], args.fViewMatrix[SkMatrix::kMScaleY] } |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | vec[0].normalize(); |
| 218 | vec[0].scale(SK_ScalarHalf); |
| 219 | vec[1].normalize(); |
| 220 | vec[1].scale(SK_ScalarHalf); |
| 221 | |
| 222 | // create the rotated rect |
joshualitt | 40ac15a | 2015-08-14 08:45:39 -0700 | [diff] [blame^] | 223 | fan0Pos->setRectFan(args.fRect.fLeft, args.fRect.fTop, |
| 224 | args.fRect.fRight, args.fRect.fBottom, vertexStride); |
| 225 | args.fViewMatrix.mapPointsWithStride(fan0Pos, vertexStride, 4); |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 226 | |
| 227 | // Now create the inset points and then outset the original |
| 228 | // rotated points |
| 229 | |
| 230 | // TL |
| 231 | *((SkPoint*)((intptr_t)fan1Pos + 0 * vertexStride)) = |
| 232 | *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) + vec[0] + vec[1]; |
| 233 | *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) -= vec[0] + vec[1]; |
| 234 | // BL |
| 235 | *((SkPoint*)((intptr_t)fan1Pos + 1 * vertexStride)) = |
| 236 | *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) + vec[0] - vec[1]; |
| 237 | *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) -= vec[0] - vec[1]; |
| 238 | // BR |
| 239 | *((SkPoint*)((intptr_t)fan1Pos + 2 * vertexStride)) = |
| 240 | *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) - vec[0] - vec[1]; |
| 241 | *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) += vec[0] + vec[1]; |
| 242 | // TR |
| 243 | *((SkPoint*)((intptr_t)fan1Pos + 3 * vertexStride)) = |
| 244 | *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) - vec[0] + vec[1]; |
| 245 | *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) += vec[0] - vec[1]; |
| 246 | } |
| 247 | |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 248 | Base::FillInAttributes(verts, vertexStride, fan0Pos, args); |
| 249 | |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 250 | // Make verts point to vertex color and then set all the color and coverage vertex attrs |
| 251 | // values. |
| 252 | verts += sizeof(SkPoint); |
| 253 | for (int i = 0; i < 4; ++i) { |
| 254 | if (tweakAlphaForCoverage) { |
| 255 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0; |
| 256 | } else { |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 257 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = args.fColor; |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 258 | *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = 0; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | int scale; |
| 263 | if (inset < SK_ScalarHalf) { |
| 264 | scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf)); |
| 265 | SkASSERT(scale >= 0 && scale <= 255); |
| 266 | } else { |
| 267 | scale = 0xff; |
| 268 | } |
| 269 | |
| 270 | verts += 4 * vertexStride; |
| 271 | |
| 272 | float innerCoverage = GrNormalizeByteToFloat(scale); |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 273 | GrColor scaledColor = (0xff == scale) ? args.fColor : SkAlphaMulQ(args.fColor, scale); |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 274 | |
| 275 | for (int i = 0; i < 4; ++i) { |
| 276 | if (tweakAlphaForCoverage) { |
| 277 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor; |
| 278 | } else { |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 279 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = args.fColor; |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 280 | *reinterpret_cast<float*>(verts + i * vertexStride + |
| 281 | sizeof(GrColor)) = innerCoverage; |
| 282 | } |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 286 | static const GrGeometryProcessor* CreateFillRectGP( |
| 287 | bool tweakAlphaForCoverage, |
| 288 | const SkMatrix& viewMatrix, |
| 289 | bool usesLocalCoords, |
| 290 | GrDefaultGeoProcFactory::LocalCoords::Type localCoordsType, |
| 291 | bool coverageIgnored) { |
| 292 | using namespace GrDefaultGeoProcFactory; |
| 293 | |
| 294 | Color color(Color::kAttribute_Type); |
| 295 | Coverage::Type coverageType; |
| 296 | // TODO remove coverage if coverage is ignored |
| 297 | /*if (coverageIgnored) { |
| 298 | coverageType = Coverage::kNone_Type; |
| 299 | } else*/ if (tweakAlphaForCoverage) { |
| 300 | coverageType = Coverage::kSolid_Type; |
| 301 | } else { |
| 302 | coverageType = Coverage::kAttribute_Type; |
| 303 | } |
| 304 | Coverage coverage(coverageType); |
| 305 | |
| 306 | // We assume the caller has inverted the viewmatrix |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 307 | if (LocalCoords::kHasExplicit_Type == localCoordsType) { |
joshualitt | 27801bf | 2015-08-12 12:52:47 -0700 | [diff] [blame] | 308 | LocalCoords localCoords(localCoordsType); |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 309 | return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, SkMatrix::I()); |
| 310 | } else { |
joshualitt | 27801bf | 2015-08-12 12:52:47 -0700 | [diff] [blame] | 311 | LocalCoords localCoords(usesLocalCoords ? localCoordsType : LocalCoords::kUnused_Type); |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 312 | return CreateForDeviceSpace(color, coverage, localCoords, viewMatrix); |
| 313 | } |
| 314 | } |
| 315 | |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 316 | struct BatchTracker { |
| 317 | GrColor fColor; |
| 318 | bool fUsesLocalCoords; |
| 319 | bool fColorIgnored; |
| 320 | bool fCoverageIgnored; |
| 321 | bool fCanTweakAlphaForCoverage; |
| 322 | }; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 323 | |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 324 | BatchTracker fBatch; |
| 325 | SkSTArray<1, Geometry, true> fGeoData; |
| 326 | }; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 327 | |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 328 | class AAFillRectBatchNoLocalMatrixImp { |
| 329 | public: |
| 330 | struct Geometry { |
| 331 | SkMatrix fViewMatrix; |
joshualitt | 40ac15a | 2015-08-14 08:45:39 -0700 | [diff] [blame^] | 332 | SkRect fRect; |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 333 | SkRect fDevRect; |
| 334 | GrColor fColor; |
| 335 | }; |
| 336 | |
| 337 | inline static bool CanCombineLocalCoords(const SkMatrix& mine, const SkMatrix& theirs, |
| 338 | bool usesLocalCoords) { |
| 339 | // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses |
| 340 | // local coords then we won't be able to batch. We could actually upload the viewmatrix |
| 341 | // using vertex attributes in these cases, but haven't investigated that |
| 342 | return !usesLocalCoords || mine.cheapEqualTo(theirs); |
| 343 | } |
| 344 | |
| 345 | inline static GrDefaultGeoProcFactory::LocalCoords::Type LocalCoordsType() { |
| 346 | return GrDefaultGeoProcFactory::LocalCoords::kUsePosition_Type; |
| 347 | } |
| 348 | |
| 349 | inline static bool StrideCheck(size_t vertexStride, bool canTweakAlphaForCoverage, |
| 350 | bool usesLocalCoords) { |
| 351 | return canTweakAlphaForCoverage ? |
| 352 | vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr) : |
| 353 | vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr); |
| 354 | } |
| 355 | |
| 356 | inline static void FillInAttributes(intptr_t, size_t, SkPoint*, const Geometry&) {} |
| 357 | }; |
| 358 | |
| 359 | class AAFillRectBatchLocalMatrixImp { |
| 360 | public: |
| 361 | struct Geometry { |
| 362 | SkMatrix fViewMatrix; |
| 363 | SkMatrix fLocalMatrix; |
joshualitt | 40ac15a | 2015-08-14 08:45:39 -0700 | [diff] [blame^] | 364 | SkRect fRect; |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 365 | SkRect fDevRect; |
| 366 | GrColor fColor; |
| 367 | }; |
| 368 | |
| 369 | inline static bool CanCombineLocalCoords(const SkMatrix& mine, const SkMatrix& theirs, |
| 370 | bool usesLocalCoords) { |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | inline static GrDefaultGeoProcFactory::LocalCoords::Type LocalCoordsType() { |
| 375 | return GrDefaultGeoProcFactory::LocalCoords::kHasExplicit_Type; |
| 376 | } |
| 377 | |
| 378 | inline static bool StrideCheck(size_t vertexStride, bool canTweakAlphaForCoverage, |
| 379 | bool usesLocalCoords) { |
| 380 | // Whomever created us should not have done so if there are no local coords |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 381 | return canTweakAlphaForCoverage ? |
| 382 | vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr) : |
| 383 | vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordCoverage); |
| 384 | } |
| 385 | |
| 386 | inline static void FillInAttributes(intptr_t vertices, size_t vertexStride, |
| 387 | SkPoint* fan0Pos, const Geometry& args) { |
| 388 | SkMatrix invViewMatrix; |
| 389 | if (!args.fViewMatrix.invert(&invViewMatrix)) { |
| 390 | SkASSERT(false); |
| 391 | invViewMatrix = SkMatrix::I(); |
| 392 | } |
| 393 | SkMatrix localCoordMatrix; |
| 394 | localCoordMatrix.setConcat(args.fLocalMatrix, invViewMatrix); |
| 395 | SkPoint* fan0Loc = reinterpret_cast<SkPoint*>(vertices + vertexStride - sizeof(SkPoint)); |
| 396 | localCoordMatrix.mapPointsWithStride(fan0Loc, fan0Pos, vertexStride, 8); |
| 397 | } |
| 398 | }; |
| 399 | |
| 400 | typedef AAFillRectBatch<AAFillRectBatchNoLocalMatrixImp> AAFillRectBatchNoLocalMatrix; |
| 401 | typedef AAFillRectBatch<AAFillRectBatchLocalMatrixImp> AAFillRectBatchLocalMatrix; |
| 402 | |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 403 | namespace GrAAFillRectBatch { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 404 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 405 | GrDrawBatch* Create(GrColor color, |
| 406 | const SkMatrix& viewMatrix, |
| 407 | const SkRect& rect, |
| 408 | const SkRect& devRect) { |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 409 | AAFillRectBatchNoLocalMatrix* batch = AAFillRectBatchNoLocalMatrix::Create(); |
| 410 | AAFillRectBatchNoLocalMatrix::Geometry& geo = *batch->geometry(); |
| 411 | geo.fColor = color; |
| 412 | geo.fViewMatrix = viewMatrix; |
joshualitt | 40ac15a | 2015-08-14 08:45:39 -0700 | [diff] [blame^] | 413 | geo.fRect = rect; |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 414 | geo.fDevRect = devRect; |
| 415 | batch->init(); |
| 416 | return batch; |
| 417 | } |
| 418 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 419 | GrDrawBatch* Create(GrColor color, |
| 420 | const SkMatrix& viewMatrix, |
| 421 | const SkMatrix& localMatrix, |
| 422 | const SkRect& rect, |
| 423 | const SkRect& devRect) { |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 424 | AAFillRectBatchLocalMatrix* batch = AAFillRectBatchLocalMatrix::Create(); |
| 425 | AAFillRectBatchLocalMatrix::Geometry& geo = *batch->geometry(); |
| 426 | geo.fColor = color; |
| 427 | geo.fViewMatrix = viewMatrix; |
| 428 | geo.fLocalMatrix = localMatrix; |
joshualitt | 40ac15a | 2015-08-14 08:45:39 -0700 | [diff] [blame^] | 429 | geo.fRect = rect; |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 430 | geo.fDevRect = devRect; |
| 431 | batch->init(); |
| 432 | return batch; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 433 | } |
| 434 | |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 435 | }; |
| 436 | |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 437 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 438 | |
| 439 | #ifdef GR_TEST_UTILS |
| 440 | |
| 441 | #include "GrBatchTest.h" |
| 442 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 443 | DRAW_BATCH_TEST_DEFINE(AAFillRectBatch) { |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 444 | AAFillRectBatchNoLocalMatrix* batch = AAFillRectBatchNoLocalMatrix::Create(); |
| 445 | AAFillRectBatchNoLocalMatrix::Geometry& geo = *batch->geometry(); |
| 446 | geo.fColor = GrRandomColor(random); |
| 447 | geo.fViewMatrix = GrTest::TestMatrix(random); |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 448 | geo.fDevRect = GrTest::TestRect(random); |
| 449 | batch->init(); |
| 450 | return batch; |
| 451 | } |
| 452 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 453 | DRAW_BATCH_TEST_DEFINE(AAFillRectBatchLocalMatrix) { |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 454 | AAFillRectBatchLocalMatrix* batch = AAFillRectBatchLocalMatrix::Create(); |
| 455 | AAFillRectBatchLocalMatrix::Geometry& geo = *batch->geometry(); |
| 456 | geo.fColor = GrRandomColor(random); |
| 457 | geo.fViewMatrix = GrTest::TestMatrix(random); |
| 458 | geo.fLocalMatrix = GrTest::TestMatrix(random); |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 459 | geo.fDevRect = GrTest::TestRect(random); |
| 460 | batch->init(); |
| 461 | return batch; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | #endif |