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