OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrAAFillRectBatch.h" | 8 #include "GrAAFillRectBatch.h" |
9 | 9 |
| 10 #include "GrBatch.h" |
| 11 #include "GrColor.h" |
10 #include "GrDefaultGeoProcFactory.h" | 12 #include "GrDefaultGeoProcFactory.h" |
11 #include "GrResourceKey.h" | 13 #include "GrResourceKey.h" |
12 #include "GrResourceProvider.h" | 14 #include "GrResourceProvider.h" |
| 15 #include "GrTypes.h" |
| 16 #include "SkMatrix.h" |
| 17 #include "SkRect.h" |
13 | 18 |
14 GR_DECLARE_STATIC_UNIQUE_KEY(gAAFillRectIndexBufferKey); | 19 GR_DECLARE_STATIC_UNIQUE_KEY(gAAFillRectIndexBufferKey); |
15 | 20 |
16 static void set_inset_fan(SkPoint* pts, size_t stride, | 21 static void set_inset_fan(SkPoint* pts, size_t stride, |
17 const SkRect& r, SkScalar dx, SkScalar dy) { | 22 const SkRect& r, SkScalar dx, SkScalar dy) { |
18 pts->setRectFan(r.fLeft + dx, r.fTop + dy, | 23 pts->setRectFan(r.fLeft + dx, r.fTop + dy, |
19 r.fRight - dx, r.fBottom - dy, stride); | 24 r.fRight - dx, r.fBottom - dy, stride); |
20 } | 25 } |
21 | 26 |
22 static const GrGeometryProcessor* create_fill_rect_gp(bool tweakAlphaForCoverage
, | 27 static const GrGeometryProcessor* create_fill_rect_gp(bool tweakAlphaForCoverage
, |
(...skipping 11 matching lines...) Expand all Loading... |
34 coverageType = Coverage::kSolid_Type; | 39 coverageType = Coverage::kSolid_Type; |
35 } else { | 40 } else { |
36 coverageType = Coverage::kAttribute_Type; | 41 coverageType = Coverage::kAttribute_Type; |
37 } | 42 } |
38 Coverage coverage(coverageType); | 43 Coverage coverage(coverageType); |
39 LocalCoords localCoords(usesLocalCoords ? LocalCoords::kUsePosition_Type : | 44 LocalCoords localCoords(usesLocalCoords ? LocalCoords::kUsePosition_Type : |
40 LocalCoords::kUnused_Type); | 45 LocalCoords::kUnused_Type); |
41 return CreateForDeviceSpace(color, coverage, localCoords, viewMatrix); | 46 return CreateForDeviceSpace(color, coverage, localCoords, viewMatrix); |
42 } | 47 } |
43 | 48 |
44 void GrAAFillRectBatch::initBatchTracker(const GrPipelineOptimizations& opt) { | 49 class AAFillRectBatch : public GrBatch { |
45 // Handle any color overrides | 50 public: |
46 if (!opt.readsColor()) { | 51 struct Geometry { |
47 fGeoData[0].fColor = GrColor_ILLEGAL; | 52 GrColor fColor; |
48 } | 53 SkMatrix fViewMatrix; |
49 opt.getOverrideColorIfSet(&fGeoData[0].fColor); | 54 SkRect fRect; |
50 | 55 SkRect fDevRect; |
51 // setup batch properties | 56 }; |
52 fBatch.fColorIgnored = !opt.readsColor(); | 57 |
53 fBatch.fColor = fGeoData[0].fColor; | 58 static GrBatch* Create(GrColor color, |
54 fBatch.fUsesLocalCoords = opt.readsLocalCoords(); | 59 const SkMatrix& viewMatrix, |
55 fBatch.fCoverageIgnored = !opt.readsCoverage(); | 60 const SkRect& rect, |
56 fBatch.fCanTweakAlphaForCoverage = opt.canTweakAlphaForCoverage(); | 61 const SkRect& devRect) { |
| 62 return SkNEW_ARGS(AAFillRectBatch, (color, viewMatrix, rect, devRect)); |
| 63 } |
| 64 |
| 65 const char* name() const override { return "AAFillRectBatch"; } |
| 66 |
| 67 void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
| 68 // When this is called on a batch, there is only one geometry bundle |
| 69 out->setKnownFourComponents(fGeoData[0].fColor); |
| 70 } |
| 71 |
| 72 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
| 73 out->setUnknownSingleComponent(); |
| 74 } |
| 75 |
| 76 void initBatchTracker(const GrPipelineOptimizations& opt) override { |
| 77 // Handle any color overrides |
| 78 if (!opt.readsColor()) { |
| 79 fGeoData[0].fColor = GrColor_ILLEGAL; |
| 80 } |
| 81 opt.getOverrideColorIfSet(&fGeoData[0].fColor); |
| 82 |
| 83 // setup batch properties |
| 84 fBatch.fColorIgnored = !opt.readsColor(); |
| 85 fBatch.fColor = fGeoData[0].fColor; |
| 86 fBatch.fUsesLocalCoords = opt.readsLocalCoords(); |
| 87 fBatch.fCoverageIgnored = !opt.readsCoverage(); |
| 88 fBatch.fCanTweakAlphaForCoverage = opt.canTweakAlphaForCoverage(); |
| 89 } |
| 90 |
| 91 void generateGeometry(GrBatchTarget* batchTarget) override { |
| 92 bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage(); |
| 93 |
| 94 SkAutoTUnref<const GrGeometryProcessor> gp(create_fill_rect_gp(canTweakA
lphaForCoverage, |
| 95 this->vie
wMatrix(), |
| 96 this->use
sLocalCoords(), |
| 97 this->cov
erageIgnored())); |
| 98 if (!gp) { |
| 99 SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 100 return; |
| 101 } |
| 102 |
| 103 batchTarget->initDraw(gp, this->pipeline()); |
| 104 |
| 105 size_t vertexStride = gp->getVertexStride(); |
| 106 SkASSERT(canTweakAlphaForCoverage ? |
| 107 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAt
tr) : |
| 108 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCo
verageAttr)); |
| 109 int instanceCount = fGeoData.count(); |
| 110 |
| 111 SkAutoTUnref<const GrIndexBuffer> indexBuffer(this->getIndexBuffer( |
| 112 batchTarget->resourceProvider())); |
| 113 InstancedHelper helper; |
| 114 void* vertices = helper.init(batchTarget, kTriangles_GrPrimitiveType, ve
rtexStride, |
| 115 indexBuffer, kVertsPerAAFillRect, kIndicesP
erAAFillRect, |
| 116 instanceCount); |
| 117 if (!vertices || !indexBuffer) { |
| 118 SkDebugf("Could not allocate vertices\n"); |
| 119 return; |
| 120 } |
| 121 |
| 122 for (int i = 0; i < instanceCount; i++) { |
| 123 const Geometry& args = fGeoData[i]; |
| 124 this->generateAAFillRectGeometry(vertices, |
| 125 i * kVertsPerAAFillRect * vertexStr
ide, |
| 126 vertexStride, |
| 127 args.fColor, |
| 128 args.fViewMatrix, |
| 129 args.fRect, |
| 130 args.fDevRect, |
| 131 canTweakAlphaForCoverage); |
| 132 } |
| 133 |
| 134 helper.issueDraw(batchTarget); |
| 135 } |
| 136 |
| 137 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 138 |
| 139 private: |
| 140 AAFillRectBatch(GrColor color, const SkMatrix& viewMatrix, const SkRect& rec
t, |
| 141 const SkRect& devRect) { |
| 142 this->initClassID<AAFillRectBatch>(); |
| 143 Geometry& geometry = fGeoData.push_back(); |
| 144 geometry.fRect = rect; |
| 145 geometry.fViewMatrix = viewMatrix; |
| 146 geometry.fDevRect = devRect; |
| 147 geometry.fColor = color; |
| 148 |
| 149 this->setBounds(geometry.fDevRect); |
| 150 } |
| 151 |
| 152 static const int kNumAAFillRectsInIndexBuffer = 256; |
| 153 static const int kVertsPerAAFillRect = 8; |
| 154 static const int kIndicesPerAAFillRect = 30; |
| 155 |
| 156 const GrIndexBuffer* getIndexBuffer(GrResourceProvider* resourceProvider) { |
| 157 GR_DEFINE_STATIC_UNIQUE_KEY(gAAFillRectIndexBufferKey); |
| 158 |
| 159 static const uint16_t gFillAARectIdx[] = { |
| 160 0, 1, 5, 5, 4, 0, |
| 161 1, 2, 6, 6, 5, 1, |
| 162 2, 3, 7, 7, 6, 2, |
| 163 3, 0, 4, 4, 7, 3, |
| 164 4, 5, 6, 6, 7, 4, |
| 165 }; |
| 166 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gFillAARectIdx) == kIndicesPerAAFillRect
); |
| 167 return resourceProvider->findOrCreateInstancedIndexBuffer(gFillAARectIdx
, |
| 168 kIndicesPerAAFillRect, kNumAAFillRectsInIndexBuffer, kVertsPerAAFill
Rect, |
| 169 gAAFillRectIndexBufferKey); |
| 170 } |
| 171 |
| 172 GrColor color() const { return fBatch.fColor; } |
| 173 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 174 bool canTweakAlphaForCoverage() const { return fBatch.fCanTweakAlphaForCover
age; } |
| 175 bool colorIgnored() const { return fBatch.fColorIgnored; } |
| 176 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 177 bool coverageIgnored() const { return fBatch.fCoverageIgnored; } |
| 178 |
| 179 bool onCombineIfPossible(GrBatch* t) override { |
| 180 if (!this->pipeline()->isEqual(*t->pipeline())) { |
| 181 return false; |
| 182 } |
| 183 |
| 184 AAFillRectBatch* that = t->cast<AAFillRectBatch>(); |
| 185 |
| 186 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 187 // We apply the viewmatrix to the rect points on the cpu. However, if t
he pipeline uses |
| 188 // local coords then we won't be able to batch. We could actually uploa
d the viewmatrix |
| 189 // using vertex attributes in these cases, but haven't investigated that |
| 190 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->vi
ewMatrix())) { |
| 191 return false; |
| 192 } |
| 193 |
| 194 if (this->color() != that->color()) { |
| 195 fBatch.fColor = GrColor_ILLEGAL; |
| 196 } |
| 197 |
| 198 // In the event of two batches, one who can tweak, one who cannot, we ju
st fall back to |
| 199 // not tweaking |
| 200 if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()
) { |
| 201 fBatch.fCanTweakAlphaForCoverage = false; |
| 202 } |
| 203 |
| 204 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin())
; |
| 205 this->joinBounds(that->bounds()); |
| 206 return true; |
| 207 } |
| 208 |
| 209 void generateAAFillRectGeometry(void* vertices, |
| 210 size_t offset, |
| 211 size_t vertexStride, |
| 212 GrColor color, |
| 213 const SkMatrix& viewMatrix, |
| 214 const SkRect& rect, |
| 215 const SkRect& devRect, |
| 216 bool tweakAlphaForCoverage) const { |
| 217 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset; |
| 218 |
| 219 SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts); |
| 220 SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride); |
| 221 |
| 222 SkScalar inset = SkMinScalar(devRect.width(), SK_Scalar1); |
| 223 inset = SK_ScalarHalf * SkMinScalar(inset, devRect.height()); |
| 224 |
| 225 if (viewMatrix.rectStaysRect()) { |
| 226 set_inset_fan(fan0Pos, vertexStride, devRect, -SK_ScalarHalf, -SK_Sc
alarHalf); |
| 227 set_inset_fan(fan1Pos, vertexStride, devRect, inset, inset); |
| 228 } else { |
| 229 // compute transformed (1, 0) and (0, 1) vectors |
| 230 SkVector vec[2] = { |
| 231 { viewMatrix[SkMatrix::kMScaleX], viewMatrix[SkMatrix::kMSkewY] }, |
| 232 { viewMatrix[SkMatrix::kMSkewX], viewMatrix[SkMatrix::kMScaleY] } |
| 233 }; |
| 234 |
| 235 vec[0].normalize(); |
| 236 vec[0].scale(SK_ScalarHalf); |
| 237 vec[1].normalize(); |
| 238 vec[1].scale(SK_ScalarHalf); |
| 239 |
| 240 // create the rotated rect |
| 241 fan0Pos->setRectFan(rect.fLeft, rect.fTop, |
| 242 rect.fRight, rect.fBottom, vertexStride); |
| 243 viewMatrix.mapPointsWithStride(fan0Pos, vertexStride, 4); |
| 244 |
| 245 // Now create the inset points and then outset the original |
| 246 // rotated points |
| 247 |
| 248 // TL |
| 249 *((SkPoint*)((intptr_t)fan1Pos + 0 * vertexStride)) = |
| 250 *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) + vec[0] + v
ec[1]; |
| 251 *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) -= vec[0] + vec[
1]; |
| 252 // BL |
| 253 *((SkPoint*)((intptr_t)fan1Pos + 1 * vertexStride)) = |
| 254 *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) + vec[0] - v
ec[1]; |
| 255 *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) -= vec[0] - vec[
1]; |
| 256 // BR |
| 257 *((SkPoint*)((intptr_t)fan1Pos + 2 * vertexStride)) = |
| 258 *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) - vec[0] - v
ec[1]; |
| 259 *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) += vec[0] + vec[
1]; |
| 260 // TR |
| 261 *((SkPoint*)((intptr_t)fan1Pos + 3 * vertexStride)) = |
| 262 *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) - vec[0] + v
ec[1]; |
| 263 *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) += vec[0] - vec[
1]; |
| 264 } |
| 265 |
| 266 // Make verts point to vertex color and then set all the color and cover
age vertex attrs |
| 267 // values. |
| 268 verts += 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 + sizeof(GrCo
lor)) = 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) = scaledCo
lor; |
| 294 } else { |
| 295 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; |
| 296 *reinterpret_cast<float*>(verts + i * vertexStride + |
| 297 sizeof(GrColor)) = innerCoverage; |
| 298 } |
| 299 } |
| 300 } |
| 301 |
| 302 struct BatchTracker { |
| 303 GrColor fColor; |
| 304 bool fUsesLocalCoords; |
| 305 bool fColorIgnored; |
| 306 bool fCoverageIgnored; |
| 307 bool fCanTweakAlphaForCoverage; |
| 308 }; |
| 309 |
| 310 BatchTracker fBatch; |
| 311 SkSTArray<1, Geometry, true> fGeoData; |
| 312 }; |
| 313 |
| 314 namespace GrAAFillRectBatch { |
| 315 |
| 316 GrBatch* Create(GrColor color, |
| 317 const SkMatrix& viewMatrix, |
| 318 const SkRect& rect, |
| 319 const SkRect& devRect) { |
| 320 return AAFillRectBatch::Create(color, viewMatrix, rect, devRect); |
57 } | 321 } |
58 | 322 |
59 void GrAAFillRectBatch::generateGeometry(GrBatchTarget* batchTarget) { | 323 }; |
60 bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage(); | |
61 | |
62 SkAutoTUnref<const GrGeometryProcessor> gp(create_fill_rect_gp(canTweakAlpha
ForCoverage, | |
63 this->viewMat
rix(), | |
64 this->usesLoc
alCoords(), | |
65 this->coverag
eIgnored())); | |
66 if (!gp) { | |
67 SkDebugf("Couldn't create GrGeometryProcessor\n"); | |
68 return; | |
69 } | |
70 | |
71 batchTarget->initDraw(gp, this->pipeline()); | |
72 | |
73 size_t vertexStride = gp->getVertexStride(); | |
74 SkASSERT(canTweakAlphaForCoverage ? | |
75 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)
: | |
76 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCovera
geAttr)); | |
77 int instanceCount = fGeoData.count(); | |
78 | |
79 SkAutoTUnref<const GrIndexBuffer> indexBuffer(this->getIndexBuffer( | |
80 batchTarget->resourceProvider())); | |
81 InstancedHelper helper; | |
82 void* vertices = helper.init(batchTarget, kTriangles_GrPrimitiveType, vertex
Stride, | |
83 indexBuffer, kVertsPerAAFillRect, kIndicesPerAA
FillRect, | |
84 instanceCount); | |
85 if (!vertices || !indexBuffer) { | |
86 SkDebugf("Could not allocate vertices\n"); | |
87 return; | |
88 } | |
89 | |
90 for (int i = 0; i < instanceCount; i++) { | |
91 const Geometry& args = fGeoData[i]; | |
92 this->generateAAFillRectGeometry(vertices, | |
93 i * kVertsPerAAFillRect * vertexStride, | |
94 vertexStride, | |
95 args.fColor, | |
96 args.fViewMatrix, | |
97 args.fRect, | |
98 args.fDevRect, | |
99 canTweakAlphaForCoverage); | |
100 } | |
101 | |
102 helper.issueDraw(batchTarget); | |
103 } | |
104 | |
105 const GrIndexBuffer* GrAAFillRectBatch::getIndexBuffer(GrResourceProvider* resou
rceProvider) { | |
106 GR_DEFINE_STATIC_UNIQUE_KEY(gAAFillRectIndexBufferKey); | |
107 | |
108 static const uint16_t gFillAARectIdx[] = { | |
109 0, 1, 5, 5, 4, 0, | |
110 1, 2, 6, 6, 5, 1, | |
111 2, 3, 7, 7, 6, 2, | |
112 3, 0, 4, 4, 7, 3, | |
113 4, 5, 6, 6, 7, 4, | |
114 }; | |
115 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gFillAARectIdx) == kIndicesPerAAFillRect); | |
116 return resourceProvider->findOrCreateInstancedIndexBuffer(gFillAARectIdx, | |
117 kIndicesPerAAFillRect, kNumAAFillRectsInIndexBuffer, kVertsPerAAFillRect
, | |
118 gAAFillRectIndexBufferKey); | |
119 } | |
120 | |
121 bool GrAAFillRectBatch::onCombineIfPossible(GrBatch* t) { | |
122 if (!this->pipeline()->isEqual(*t->pipeline())) { | |
123 return false; | |
124 } | |
125 | |
126 GrAAFillRectBatch* that = t->cast<GrAAFillRectBatch>(); | |
127 | |
128 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); | |
129 // We apply the viewmatrix to the rect points on the cpu. However, if the p
ipeline uses | |
130 // local coords then we won't be able to batch. We could actually upload th
e viewmatrix | |
131 // using vertex attributes in these cases, but haven't investigated that | |
132 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMa
trix())) { | |
133 return false; | |
134 } | |
135 | |
136 if (this->color() != that->color()) { | |
137 fBatch.fColor = GrColor_ILLEGAL; | |
138 } | |
139 | |
140 // In the event of two batches, one who can tweak, one who cannot, we just f
all back to | |
141 // not tweaking | |
142 if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) { | |
143 fBatch.fCanTweakAlphaForCoverage = false; | |
144 } | |
145 | |
146 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); | |
147 this->joinBounds(that->bounds()); | |
148 return true; | |
149 } | |
150 | |
151 void GrAAFillRectBatch::generateAAFillRectGeometry(void* vertices, | |
152 size_t offset, | |
153 size_t vertexStride, | |
154 GrColor color, | |
155 const SkMatrix& viewMatrix, | |
156 const SkRect& rect, | |
157 const SkRect& devRect, | |
158 bool tweakAlphaForCoverage) c
onst { | |
159 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset; | |
160 | |
161 SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts); | |
162 SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride); | |
163 | |
164 SkScalar inset = SkMinScalar(devRect.width(), SK_Scalar1); | |
165 inset = SK_ScalarHalf * SkMinScalar(inset, devRect.height()); | |
166 | |
167 if (viewMatrix.rectStaysRect()) { | |
168 set_inset_fan(fan0Pos, vertexStride, devRect, -SK_ScalarHalf, -SK_Scalar
Half); | |
169 set_inset_fan(fan1Pos, vertexStride, devRect, inset, inset); | |
170 } else { | |
171 // compute transformed (1, 0) and (0, 1) vectors | |
172 SkVector vec[2] = { | |
173 { viewMatrix[SkMatrix::kMScaleX], viewMatrix[SkMatrix::kMSkewY] }, | |
174 { viewMatrix[SkMatrix::kMSkewX], viewMatrix[SkMatrix::kMScaleY] } | |
175 }; | |
176 | |
177 vec[0].normalize(); | |
178 vec[0].scale(SK_ScalarHalf); | |
179 vec[1].normalize(); | |
180 vec[1].scale(SK_ScalarHalf); | |
181 | |
182 // create the rotated rect | |
183 fan0Pos->setRectFan(rect.fLeft, rect.fTop, | |
184 rect.fRight, rect.fBottom, vertexStride); | |
185 viewMatrix.mapPointsWithStride(fan0Pos, vertexStride, 4); | |
186 | |
187 // Now create the inset points and then outset the original | |
188 // rotated points | |
189 | |
190 // TL | |
191 *((SkPoint*)((intptr_t)fan1Pos + 0 * vertexStride)) = | |
192 *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) + vec[0] + vec[1
]; | |
193 *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) -= vec[0] + vec[1]; | |
194 // BL | |
195 *((SkPoint*)((intptr_t)fan1Pos + 1 * vertexStride)) = | |
196 *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) + vec[0] - vec[1
]; | |
197 *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) -= vec[0] - vec[1]; | |
198 // BR | |
199 *((SkPoint*)((intptr_t)fan1Pos + 2 * vertexStride)) = | |
200 *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) - vec[0] - vec[1
]; | |
201 *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) += vec[0] + vec[1]; | |
202 // TR | |
203 *((SkPoint*)((intptr_t)fan1Pos + 3 * vertexStride)) = | |
204 *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) - vec[0] + vec[1
]; | |
205 *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) += vec[0] - vec[1]; | |
206 } | |
207 | |
208 // Make verts point to vertex color and then set all the color and coverage
vertex attrs | |
209 // values. | |
210 verts += sizeof(SkPoint); | |
211 for (int i = 0; i < 4; ++i) { | |
212 if (tweakAlphaForCoverage) { | |
213 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0; | |
214 } else { | |
215 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; | |
216 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)
) = 0; | |
217 } | |
218 } | |
219 | |
220 int scale; | |
221 if (inset < SK_ScalarHalf) { | |
222 scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf)); | |
223 SkASSERT(scale >= 0 && scale <= 255); | |
224 } else { | |
225 scale = 0xff; | |
226 } | |
227 | |
228 verts += 4 * vertexStride; | |
229 | |
230 float innerCoverage = GrNormalizeByteToFloat(scale); | |
231 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale); | |
232 | |
233 for (int i = 0; i < 4; ++i) { | |
234 if (tweakAlphaForCoverage) { | |
235 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor; | |
236 } else { | |
237 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; | |
238 *reinterpret_cast<float*>(verts + i * vertexStride + | |
239 sizeof(GrColor)) = innerCoverage; | |
240 } | |
241 } | |
242 } | |
243 | 324 |
244 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 325 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
245 | 326 |
246 #ifdef GR_TEST_UTILS | 327 #ifdef GR_TEST_UTILS |
247 | 328 |
248 #include "GrBatchTest.h" | 329 #include "GrBatchTest.h" |
249 | 330 |
250 BATCH_TEST_DEFINE(AAFillRectBatch) { | 331 BATCH_TEST_DEFINE(AAFillRectBatch) { |
251 GrAAFillRectBatch::Geometry geo; | 332 GrColor color = GrRandomColor(random); |
252 geo.fColor = GrRandomColor(random); | 333 SkMatrix viewMatrix = GrTest::TestMatrix(random); |
253 geo.fViewMatrix = GrTest::TestMatrix(random); | 334 SkRect rect = GrTest::TestRect(random); |
254 geo.fRect = GrTest::TestRect(random); | 335 SkRect devRect = GrTest::TestRect(random); |
255 geo.fDevRect = GrTest::TestRect(random); | 336 return AAFillRectBatch::Create(color, viewMatrix, rect, devRect); |
256 return GrAAFillRectBatch::Create(geo); | |
257 } | 337 } |
258 | 338 |
259 #endif | 339 #endif |
OLD | NEW |