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 | 2ad37be | 2015-08-18 10:16:01 -0700 | [diff] [blame] | 14 | #include "GrTInstanceBatch.h" |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 15 | #include "GrTypes.h" |
| 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 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 31 | const GrBuffer* get_index_buffer(GrResourceProvider* resourceProvider) { |
joshualitt | 27801bf | 2015-08-12 12:52:47 -0700 | [diff] [blame] | 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 | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 47 | static const GrGeometryProcessor* create_fill_rect_gp( |
| 48 | const SkMatrix& viewMatrix, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 49 | const GrXPOverridesForBatch& overrides, |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 50 | GrDefaultGeoProcFactory::LocalCoords::Type localCoordsType) { |
| 51 | using namespace GrDefaultGeoProcFactory; |
| 52 | |
| 53 | Color color(Color::kAttribute_Type); |
| 54 | Coverage::Type coverageType; |
| 55 | // TODO remove coverage if coverage is ignored |
| 56 | /*if (coverageIgnored) { |
| 57 | coverageType = Coverage::kNone_Type; |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 58 | } else*/ if (overrides.canTweakAlphaForCoverage()) { |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 59 | coverageType = Coverage::kSolid_Type; |
| 60 | } else { |
| 61 | coverageType = Coverage::kAttribute_Type; |
| 62 | } |
| 63 | Coverage coverage(coverageType); |
| 64 | |
| 65 | // We assume the caller has inverted the viewmatrix |
| 66 | if (LocalCoords::kHasExplicit_Type == localCoordsType) { |
| 67 | LocalCoords localCoords(localCoordsType); |
| 68 | return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, SkMatrix::I()); |
| 69 | } else { |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 70 | LocalCoords localCoords(overrides.readsLocalCoords() ? localCoordsType : |
| 71 | LocalCoords::kUnused_Type); |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 72 | return CreateForDeviceSpace(color, coverage, localCoords, viewMatrix); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | static void generate_aa_fill_rect_geometry(intptr_t verts, |
| 77 | size_t vertexStride, |
| 78 | GrColor color, |
| 79 | const SkMatrix& viewMatrix, |
| 80 | const SkRect& rect, |
| 81 | const SkRect& devRect, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 82 | const GrXPOverridesForBatch& overrides, |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 83 | const SkMatrix* localMatrix) { |
| 84 | SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts); |
| 85 | SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride); |
| 86 | |
robertphillips | 0851d2d | 2016-06-02 05:21:34 -0700 | [diff] [blame^] | 87 | SkScalar inset; |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 88 | |
| 89 | if (viewMatrix.rectStaysRect()) { |
robertphillips | 0851d2d | 2016-06-02 05:21:34 -0700 | [diff] [blame^] | 90 | inset = SkMinScalar(devRect.width(), SK_Scalar1); |
| 91 | inset = SK_ScalarHalf * SkMinScalar(inset, devRect.height()); |
| 92 | |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 93 | set_inset_fan(fan0Pos, vertexStride, devRect, -SK_ScalarHalf, -SK_ScalarHalf); |
| 94 | set_inset_fan(fan1Pos, vertexStride, devRect, inset, inset); |
| 95 | } else { |
| 96 | // compute transformed (1, 0) and (0, 1) vectors |
| 97 | SkVector vec[2] = { |
| 98 | { viewMatrix[SkMatrix::kMScaleX], viewMatrix[SkMatrix::kMSkewY] }, |
| 99 | { viewMatrix[SkMatrix::kMSkewX], viewMatrix[SkMatrix::kMScaleY] } |
| 100 | }; |
| 101 | |
robertphillips | 0851d2d | 2016-06-02 05:21:34 -0700 | [diff] [blame^] | 102 | SkScalar len1 = SkPoint::Normalize(&vec[0]); |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 103 | vec[0].scale(SK_ScalarHalf); |
robertphillips | 0851d2d | 2016-06-02 05:21:34 -0700 | [diff] [blame^] | 104 | SkScalar len2 = SkPoint::Normalize(&vec[1]); |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 105 | vec[1].scale(SK_ScalarHalf); |
| 106 | |
robertphillips | 0851d2d | 2016-06-02 05:21:34 -0700 | [diff] [blame^] | 107 | inset = SkMinScalar(len1 * rect.width(), SK_Scalar1); |
| 108 | inset = SK_ScalarHalf * SkMinScalar(inset, len2 * rect.height()); |
| 109 | |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 110 | // create the rotated rect |
| 111 | fan0Pos->setRectFan(rect.fLeft, rect.fTop, |
| 112 | rect.fRight, rect.fBottom, vertexStride); |
| 113 | viewMatrix.mapPointsWithStride(fan0Pos, vertexStride, 4); |
| 114 | |
| 115 | // Now create the inset points and then outset the original |
| 116 | // rotated points |
| 117 | |
| 118 | // TL |
| 119 | *((SkPoint*)((intptr_t)fan1Pos + 0 * vertexStride)) = |
| 120 | *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) + vec[0] + vec[1]; |
| 121 | *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) -= vec[0] + vec[1]; |
| 122 | // BL |
| 123 | *((SkPoint*)((intptr_t)fan1Pos + 1 * vertexStride)) = |
| 124 | *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) + vec[0] - vec[1]; |
| 125 | *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) -= vec[0] - vec[1]; |
| 126 | // BR |
| 127 | *((SkPoint*)((intptr_t)fan1Pos + 2 * vertexStride)) = |
| 128 | *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) - vec[0] - vec[1]; |
| 129 | *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) += vec[0] + vec[1]; |
| 130 | // TR |
| 131 | *((SkPoint*)((intptr_t)fan1Pos + 3 * vertexStride)) = |
| 132 | *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) - vec[0] + vec[1]; |
| 133 | *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) += vec[0] - vec[1]; |
| 134 | } |
| 135 | |
| 136 | if (localMatrix) { |
| 137 | SkMatrix invViewMatrix; |
| 138 | if (!viewMatrix.invert(&invViewMatrix)) { |
| 139 | SkASSERT(false); |
| 140 | invViewMatrix = SkMatrix::I(); |
| 141 | } |
| 142 | SkMatrix localCoordMatrix; |
| 143 | localCoordMatrix.setConcat(*localMatrix, invViewMatrix); |
| 144 | SkPoint* fan0Loc = reinterpret_cast<SkPoint*>(verts + sizeof(SkPoint) + sizeof(GrColor)); |
| 145 | localCoordMatrix.mapPointsWithStride(fan0Loc, fan0Pos, vertexStride, 8); |
| 146 | } |
| 147 | |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 148 | bool tweakAlphaForCoverage = overrides.canTweakAlphaForCoverage(); |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 149 | |
| 150 | // Make verts point to vertex color and then set all the color and coverage vertex attrs |
| 151 | // values. |
| 152 | verts += sizeof(SkPoint); |
| 153 | |
| 154 | // The coverage offset is always the last vertex attribute |
| 155 | intptr_t coverageOffset = vertexStride - sizeof(GrColor) - sizeof(SkPoint); |
| 156 | for (int i = 0; i < 4; ++i) { |
| 157 | if (tweakAlphaForCoverage) { |
| 158 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0; |
| 159 | } else { |
| 160 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; |
| 161 | *reinterpret_cast<float*>(verts + i * vertexStride + coverageOffset) = 0; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | int scale; |
| 166 | if (inset < SK_ScalarHalf) { |
| 167 | scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf)); |
| 168 | SkASSERT(scale >= 0 && scale <= 255); |
| 169 | } else { |
| 170 | scale = 0xff; |
| 171 | } |
| 172 | |
| 173 | verts += 4 * vertexStride; |
| 174 | |
| 175 | float innerCoverage = GrNormalizeByteToFloat(scale); |
| 176 | GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale); |
| 177 | |
| 178 | for (int i = 0; i < 4; ++i) { |
| 179 | if (tweakAlphaForCoverage) { |
| 180 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor; |
| 181 | } else { |
| 182 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; |
| 183 | *reinterpret_cast<float*>(verts + i * vertexStride + |
| 184 | coverageOffset) = innerCoverage; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
joshualitt | 2ad37be | 2015-08-18 10:16:01 -0700 | [diff] [blame] | 189 | // Common functions |
| 190 | class AAFillRectBatchBase { |
| 191 | public: |
| 192 | static const int kVertsPerInstance = kVertsPerAAFillRect; |
| 193 | static const int kIndicesPerInstance = kIndicesPerAAFillRect; |
| 194 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 195 | static void InitInvariantOutputCoverage(GrInitInvariantOutput* out) { |
| 196 | out->setUnknownSingleComponent(); |
| 197 | } |
| 198 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 199 | static const GrBuffer* GetIndexBuffer(GrResourceProvider* rp) { |
joshualitt | 2ad37be | 2015-08-18 10:16:01 -0700 | [diff] [blame] | 200 | return get_index_buffer(rp); |
| 201 | } |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 202 | |
| 203 | template <class Geometry> |
| 204 | static void SetBounds(const Geometry& geo, SkRect* outBounds) { |
| 205 | *outBounds = geo.fDevRect; |
| 206 | } |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 207 | |
| 208 | template <class Geometry> |
| 209 | static void UpdateBoundsAfterAppend(const Geometry& geo, SkRect* outBounds) { |
| 210 | outBounds->join(geo.fDevRect); |
| 211 | } |
joshualitt | 2ad37be | 2015-08-18 10:16:01 -0700 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | class AAFillRectBatchNoLocalMatrixImp : public AAFillRectBatchBase { |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 215 | public: |
| 216 | struct Geometry { |
| 217 | SkMatrix fViewMatrix; |
joshualitt | 40ac15a | 2015-08-14 08:45:39 -0700 | [diff] [blame] | 218 | SkRect fRect; |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 219 | SkRect fDevRect; |
| 220 | GrColor fColor; |
| 221 | }; |
| 222 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 223 | static const char* Name() { return "AAFillRectBatchNoLocalMatrix"; } |
joshualitt | 2ad37be | 2015-08-18 10:16:01 -0700 | [diff] [blame] | 224 | |
robertphillips | 783a4da | 2015-11-19 14:00:02 -0800 | [diff] [blame] | 225 | static SkString DumpInfo(const Geometry& geo, int index) { |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 226 | SkString str; |
robertphillips | 783a4da | 2015-11-19 14:00:02 -0800 | [diff] [blame] | 227 | str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", |
| 228 | index, |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 229 | geo.fColor, |
| 230 | geo.fRect.fLeft, geo.fRect.fTop, geo.fRect.fRight, geo.fRect.fBottom); |
| 231 | return str; |
| 232 | } |
| 233 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 234 | static bool CanCombine(const Geometry& mine, const Geometry& theirs, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 235 | const GrXPOverridesForBatch& overrides) { |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 236 | // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses |
| 237 | // local coords then we won't be able to batch. We could actually upload the viewmatrix |
| 238 | // using vertex attributes in these cases, but haven't investigated that |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 239 | return !overrides.readsLocalCoords() || mine.fViewMatrix.cheapEqualTo(theirs.fViewMatrix); |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 240 | } |
| 241 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 242 | static const GrGeometryProcessor* CreateGP(const Geometry& geo, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 243 | const GrXPOverridesForBatch& overrides) { |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 244 | const GrGeometryProcessor* gp = |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 245 | create_fill_rect_gp(geo.fViewMatrix, overrides, |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 246 | GrDefaultGeoProcFactory::LocalCoords::kUsePosition_Type); |
| 247 | |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 248 | SkASSERT(overrides.canTweakAlphaForCoverage() ? |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 249 | gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorAttr) : |
| 250 | gp->getVertexStride() == |
| 251 | sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr)); |
| 252 | return gp; |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 253 | } |
| 254 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 255 | static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 256 | const GrXPOverridesForBatch& overrides) { |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 257 | generate_aa_fill_rect_geometry(vertices, vertexStride, |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 258 | geo.fColor, geo.fViewMatrix, geo.fRect, geo.fDevRect, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 259 | overrides, nullptr); |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 260 | } |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 261 | }; |
| 262 | |
joshualitt | 2ad37be | 2015-08-18 10:16:01 -0700 | [diff] [blame] | 263 | class AAFillRectBatchLocalMatrixImp : public AAFillRectBatchBase { |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 264 | public: |
| 265 | struct Geometry { |
| 266 | SkMatrix fViewMatrix; |
| 267 | SkMatrix fLocalMatrix; |
joshualitt | 40ac15a | 2015-08-14 08:45:39 -0700 | [diff] [blame] | 268 | SkRect fRect; |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 269 | SkRect fDevRect; |
| 270 | GrColor fColor; |
| 271 | }; |
| 272 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 273 | static const char* Name() { return "AAFillRectBatchLocalMatrix"; } |
joshualitt | 2ad37be | 2015-08-18 10:16:01 -0700 | [diff] [blame] | 274 | |
robertphillips | 783a4da | 2015-11-19 14:00:02 -0800 | [diff] [blame] | 275 | static SkString DumpInfo(const Geometry& geo, int index) { |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 276 | SkString str; |
robertphillips | 783a4da | 2015-11-19 14:00:02 -0800 | [diff] [blame] | 277 | str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", |
| 278 | index, |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 279 | geo.fColor, |
| 280 | geo.fRect.fLeft, geo.fRect.fTop, geo.fRect.fRight, geo.fRect.fBottom); |
| 281 | return str; |
| 282 | } |
| 283 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 284 | static bool CanCombine(const Geometry& mine, const Geometry& theirs, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 285 | const GrXPOverridesForBatch& overrides) { |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 286 | return true; |
| 287 | } |
| 288 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 289 | static const GrGeometryProcessor* CreateGP(const Geometry& geo, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 290 | const GrXPOverridesForBatch& overrides) { |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 291 | const GrGeometryProcessor* gp = |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 292 | create_fill_rect_gp(geo.fViewMatrix, overrides, |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 293 | GrDefaultGeoProcFactory::LocalCoords::kHasExplicit_Type); |
| 294 | |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 295 | SkASSERT(overrides.canTweakAlphaForCoverage() ? |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 296 | gp->getVertexStride() == |
| 297 | sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr) : |
| 298 | gp->getVertexStride() == |
| 299 | sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordCoverage)); |
| 300 | return gp; |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 301 | } |
| 302 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 303 | static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 304 | const GrXPOverridesForBatch& overrides) { |
joshualitt | cd47b71 | 2015-08-18 07:25:38 -0700 | [diff] [blame] | 305 | generate_aa_fill_rect_geometry(vertices, vertexStride, |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 306 | geo.fColor, geo.fViewMatrix, geo.fRect, geo.fDevRect, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 307 | overrides, &geo.fLocalMatrix); |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 308 | } |
| 309 | }; |
| 310 | |
joshualitt | 2ad37be | 2015-08-18 10:16:01 -0700 | [diff] [blame] | 311 | typedef GrTInstanceBatch<AAFillRectBatchNoLocalMatrixImp> AAFillRectBatchNoLocalMatrix; |
| 312 | typedef GrTInstanceBatch<AAFillRectBatchLocalMatrixImp> AAFillRectBatchLocalMatrix; |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 313 | |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 314 | inline static void append_to_batch(AAFillRectBatchNoLocalMatrix* batch, GrColor color, |
| 315 | const SkMatrix& viewMatrix, const SkRect& rect, |
| 316 | const SkRect& devRect) { |
| 317 | AAFillRectBatchNoLocalMatrix::Geometry& geo = batch->geoData()->push_back(); |
| 318 | geo.fColor = color; |
| 319 | geo.fViewMatrix = viewMatrix; |
| 320 | geo.fRect = rect; |
| 321 | geo.fDevRect = devRect; |
| 322 | } |
| 323 | |
| 324 | inline static void append_to_batch(AAFillRectBatchLocalMatrix* batch, GrColor color, |
| 325 | const SkMatrix& viewMatrix, const SkMatrix& localMatrix, |
| 326 | const SkRect& rect, const SkRect& devRect) { |
| 327 | AAFillRectBatchLocalMatrix::Geometry& geo = batch->geoData()->push_back(); |
| 328 | geo.fColor = color; |
| 329 | geo.fViewMatrix = viewMatrix; |
| 330 | geo.fLocalMatrix = localMatrix; |
| 331 | geo.fRect = rect; |
| 332 | geo.fDevRect = devRect; |
| 333 | } |
| 334 | |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 335 | namespace GrAAFillRectBatch { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 336 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 337 | GrDrawBatch* Create(GrColor color, |
| 338 | const SkMatrix& viewMatrix, |
| 339 | const SkRect& rect, |
| 340 | const SkRect& devRect) { |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 341 | AAFillRectBatchNoLocalMatrix* batch = AAFillRectBatchNoLocalMatrix::Create(); |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 342 | append_to_batch(batch, color, viewMatrix, rect, devRect); |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 343 | batch->init(); |
| 344 | return batch; |
| 345 | } |
| 346 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 347 | GrDrawBatch* Create(GrColor color, |
| 348 | const SkMatrix& viewMatrix, |
| 349 | const SkMatrix& localMatrix, |
| 350 | const SkRect& rect, |
| 351 | const SkRect& devRect) { |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 352 | AAFillRectBatchLocalMatrix* batch = AAFillRectBatchLocalMatrix::Create(); |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 353 | append_to_batch(batch, color, viewMatrix, localMatrix, rect, devRect); |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 354 | batch->init(); |
| 355 | return batch; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 356 | } |
| 357 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 358 | GrDrawBatch* Create(GrColor color, |
| 359 | const SkMatrix& viewMatrix, |
| 360 | const SkMatrix& localMatrix, |
| 361 | const SkRect& rect) { |
| 362 | SkRect devRect; |
| 363 | viewMatrix.mapRect(&devRect, rect); |
| 364 | return Create(color, viewMatrix, localMatrix, rect, devRect); |
| 365 | } |
| 366 | |
| 367 | GrDrawBatch* CreateWithLocalRect(GrColor color, |
| 368 | const SkMatrix& viewMatrix, |
| 369 | const SkRect& rect, |
| 370 | const SkRect& localRect) { |
| 371 | SkRect devRect; |
| 372 | viewMatrix.mapRect(&devRect, rect); |
| 373 | SkMatrix localMatrix; |
| 374 | if (!localMatrix.setRectToRect(rect, localRect, SkMatrix::kFill_ScaleToFit)) { |
| 375 | return nullptr; |
| 376 | } |
| 377 | return Create(color, viewMatrix, localMatrix, rect, devRect); |
| 378 | } |
| 379 | |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 380 | void Append(GrBatch* origBatch, |
| 381 | GrColor color, |
| 382 | const SkMatrix& viewMatrix, |
| 383 | const SkRect& rect, |
| 384 | const SkRect& devRect) { |
| 385 | AAFillRectBatchNoLocalMatrix* batch = origBatch->cast<AAFillRectBatchNoLocalMatrix>(); |
| 386 | append_to_batch(batch, color, viewMatrix, rect, devRect); |
| 387 | batch->updateBoundsAfterAppend(); |
| 388 | } |
| 389 | |
| 390 | void Append(GrBatch* origBatch, |
| 391 | GrColor color, |
| 392 | const SkMatrix& viewMatrix, |
| 393 | const SkMatrix& localMatrix, |
| 394 | const SkRect& rect, |
| 395 | const SkRect& devRect) { |
| 396 | AAFillRectBatchLocalMatrix* batch = origBatch->cast<AAFillRectBatchLocalMatrix>(); |
| 397 | append_to_batch(batch, color, viewMatrix, localMatrix, rect, devRect); |
| 398 | batch->updateBoundsAfterAppend(); |
| 399 | } |
| 400 | |
joshualitt | 37eb184 | 2015-08-12 06:36:57 -0700 | [diff] [blame] | 401 | }; |
| 402 | |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 403 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 404 | |
| 405 | #ifdef GR_TEST_UTILS |
| 406 | |
| 407 | #include "GrBatchTest.h" |
| 408 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 409 | DRAW_BATCH_TEST_DEFINE(AAFillRectBatch) { |
joshualitt | 090ae8e | 2015-08-14 09:01:21 -0700 | [diff] [blame] | 410 | GrColor color = GrRandomColor(random); |
| 411 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 412 | SkRect rect = GrTest::TestRect(random); |
| 413 | SkRect devRect = GrTest::TestRect(random); |
| 414 | return GrAAFillRectBatch::Create(color, viewMatrix, rect, devRect); |
joshualitt | 147dc06 | 2015-08-12 11:51:46 -0700 | [diff] [blame] | 415 | } |
| 416 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 417 | DRAW_BATCH_TEST_DEFINE(AAFillRectBatchLocalMatrix) { |
joshualitt | 090ae8e | 2015-08-14 09:01:21 -0700 | [diff] [blame] | 418 | GrColor color = GrRandomColor(random); |
| 419 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 420 | SkMatrix localMatrix = GrTest::TestMatrix(random); |
| 421 | SkRect rect = GrTest::TestRect(random); |
| 422 | SkRect devRect = GrTest::TestRect(random); |
| 423 | return GrAAFillRectBatch::Create(color, viewMatrix, localMatrix, rect, devRect); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | #endif |