blob: d0976a0b219a05ef4142d0379a486d6cee004704 [file] [log] [blame]
joshualitt9ff64252015-08-10 09:03:51 -07001/*
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
joshualitt37eb1842015-08-12 06:36:57 -07008#include "GrColor.h"
joshualitt9ff64252015-08-10 09:03:51 -07009#include "GrDefaultGeoProcFactory.h"
Brian Salomondad29232016-12-01 16:40:24 -050010#include "GrMeshDrawOp.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050011#include "GrOpFlushState.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040012#include "GrRectOpFactory.h"
joshualitt9ff64252015-08-10 09:03:51 -070013#include "GrResourceKey.h"
14#include "GrResourceProvider.h"
joshualitt37eb1842015-08-12 06:36:57 -070015#include "GrTypes.h"
16#include "SkMatrix.h"
17#include "SkRect.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040018#include "ops/GrSimpleMeshDrawOpHelper.h"
joshualitt9ff64252015-08-10 09:03:51 -070019
20GR_DECLARE_STATIC_UNIQUE_KEY(gAAFillRectIndexBufferKey);
21
Brian Salomonbaaf4392017-06-15 09:59:23 -040022static inline bool view_matrix_ok_for_aa_fill_rect(const SkMatrix& viewMatrix) {
23 return viewMatrix.preservesRightAngles();
24}
25
26static inline void set_inset_fan(SkPoint* pts, size_t stride, const SkRect& r, SkScalar dx,
27 SkScalar dy) {
Brian Salomon6a639042016-12-14 11:08:17 -050028 pts->setRectFan(r.fLeft + dx, r.fTop + dy, r.fRight - dx, r.fBottom - dy, stride);
joshualitt9ff64252015-08-10 09:03:51 -070029}
30
joshualitt27801bf2015-08-12 12:52:47 -070031static const int kNumAAFillRectsInIndexBuffer = 256;
32static const int kVertsPerAAFillRect = 8;
33static const int kIndicesPerAAFillRect = 30;
34
cdalton397536c2016-03-25 12:15:03 -070035const GrBuffer* get_index_buffer(GrResourceProvider* resourceProvider) {
joshualitt27801bf2015-08-12 12:52:47 -070036 GR_DEFINE_STATIC_UNIQUE_KEY(gAAFillRectIndexBufferKey);
37
Brian Salomon6a639042016-12-14 11:08:17 -050038 // clang-format off
joshualitt27801bf2015-08-12 12:52:47 -070039 static const uint16_t gFillAARectIdx[] = {
40 0, 1, 5, 5, 4, 0,
41 1, 2, 6, 6, 5, 1,
42 2, 3, 7, 7, 6, 2,
43 3, 0, 4, 4, 7, 3,
44 4, 5, 6, 6, 7, 4,
45 };
Brian Salomon6a639042016-12-14 11:08:17 -050046 // clang-format on
47
joshualitt27801bf2015-08-12 12:52:47 -070048 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gFillAARectIdx) == kIndicesPerAAFillRect);
Chris Daltonff926502017-05-03 14:36:54 -040049 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -050050 gFillAARectIdx, kIndicesPerAAFillRect, kNumAAFillRectsInIndexBuffer,
51 kVertsPerAAFillRect, gAAFillRectIndexBufferKey);
joshualitt27801bf2015-08-12 12:52:47 -070052}
53
joshualittcd47b712015-08-18 07:25:38 -070054static void generate_aa_fill_rect_geometry(intptr_t verts,
55 size_t vertexStride,
56 GrColor color,
57 const SkMatrix& viewMatrix,
58 const SkRect& rect,
59 const SkRect& devRect,
Brian Salomonecea86a2017-01-04 13:25:17 -050060 bool tweakAlphaForCoverage,
joshualittcd47b712015-08-18 07:25:38 -070061 const SkMatrix* localMatrix) {
62 SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts);
63 SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride);
64
robertphillips0851d2d2016-06-02 05:21:34 -070065 SkScalar inset;
joshualittcd47b712015-08-18 07:25:38 -070066
67 if (viewMatrix.rectStaysRect()) {
robertphillips0851d2d2016-06-02 05:21:34 -070068 inset = SkMinScalar(devRect.width(), SK_Scalar1);
69 inset = SK_ScalarHalf * SkMinScalar(inset, devRect.height());
70
joshualittcd47b712015-08-18 07:25:38 -070071 set_inset_fan(fan0Pos, vertexStride, devRect, -SK_ScalarHalf, -SK_ScalarHalf);
Brian Salomon6a639042016-12-14 11:08:17 -050072 set_inset_fan(fan1Pos, vertexStride, devRect, inset, inset);
joshualittcd47b712015-08-18 07:25:38 -070073 } else {
74 // compute transformed (1, 0) and (0, 1) vectors
Brian Salomon6a639042016-12-14 11:08:17 -050075 SkVector vec[2] = {{viewMatrix[SkMatrix::kMScaleX], viewMatrix[SkMatrix::kMSkewY]},
76 {viewMatrix[SkMatrix::kMSkewX], viewMatrix[SkMatrix::kMScaleY]}};
joshualittcd47b712015-08-18 07:25:38 -070077
robertphillips0851d2d2016-06-02 05:21:34 -070078 SkScalar len1 = SkPoint::Normalize(&vec[0]);
joshualittcd47b712015-08-18 07:25:38 -070079 vec[0].scale(SK_ScalarHalf);
robertphillips0851d2d2016-06-02 05:21:34 -070080 SkScalar len2 = SkPoint::Normalize(&vec[1]);
joshualittcd47b712015-08-18 07:25:38 -070081 vec[1].scale(SK_ScalarHalf);
82
robertphillips0851d2d2016-06-02 05:21:34 -070083 inset = SkMinScalar(len1 * rect.width(), SK_Scalar1);
84 inset = SK_ScalarHalf * SkMinScalar(inset, len2 * rect.height());
85
joshualittcd47b712015-08-18 07:25:38 -070086 // create the rotated rect
Brian Salomon6a639042016-12-14 11:08:17 -050087 fan0Pos->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vertexStride);
joshualittcd47b712015-08-18 07:25:38 -070088 viewMatrix.mapPointsWithStride(fan0Pos, vertexStride, 4);
89
90 // Now create the inset points and then outset the original
91 // rotated points
92
93 // TL
94 *((SkPoint*)((intptr_t)fan1Pos + 0 * vertexStride)) =
Brian Salomon6a639042016-12-14 11:08:17 -050095 *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) + vec[0] + vec[1];
joshualittcd47b712015-08-18 07:25:38 -070096 *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) -= vec[0] + vec[1];
97 // BL
98 *((SkPoint*)((intptr_t)fan1Pos + 1 * vertexStride)) =
Brian Salomon6a639042016-12-14 11:08:17 -050099 *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) + vec[0] - vec[1];
joshualittcd47b712015-08-18 07:25:38 -0700100 *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) -= vec[0] - vec[1];
101 // BR
102 *((SkPoint*)((intptr_t)fan1Pos + 2 * vertexStride)) =
Brian Salomon6a639042016-12-14 11:08:17 -0500103 *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) - vec[0] - vec[1];
joshualittcd47b712015-08-18 07:25:38 -0700104 *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) += vec[0] + vec[1];
105 // TR
106 *((SkPoint*)((intptr_t)fan1Pos + 3 * vertexStride)) =
Brian Salomon6a639042016-12-14 11:08:17 -0500107 *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) - vec[0] + vec[1];
joshualittcd47b712015-08-18 07:25:38 -0700108 *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) += vec[0] - vec[1];
109 }
110
111 if (localMatrix) {
112 SkMatrix invViewMatrix;
113 if (!viewMatrix.invert(&invViewMatrix)) {
bsalomon4be9a302016-07-06 07:03:26 -0700114 SkDebugf("View matrix is non-invertible, local coords will be wrong.");
joshualittcd47b712015-08-18 07:25:38 -0700115 invViewMatrix = SkMatrix::I();
116 }
117 SkMatrix localCoordMatrix;
118 localCoordMatrix.setConcat(*localMatrix, invViewMatrix);
119 SkPoint* fan0Loc = reinterpret_cast<SkPoint*>(verts + sizeof(SkPoint) + sizeof(GrColor));
120 localCoordMatrix.mapPointsWithStride(fan0Loc, fan0Pos, vertexStride, 8);
121 }
122
joshualittcd47b712015-08-18 07:25:38 -0700123 // Make verts point to vertex color and then set all the color and coverage vertex attrs
124 // values.
125 verts += sizeof(SkPoint);
126
127 // The coverage offset is always the last vertex attribute
128 intptr_t coverageOffset = vertexStride - sizeof(GrColor) - sizeof(SkPoint);
129 for (int i = 0; i < 4; ++i) {
130 if (tweakAlphaForCoverage) {
131 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0;
132 } else {
133 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
134 *reinterpret_cast<float*>(verts + i * vertexStride + coverageOffset) = 0;
135 }
136 }
137
138 int scale;
139 if (inset < SK_ScalarHalf) {
140 scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf));
141 SkASSERT(scale >= 0 && scale <= 255);
142 } else {
143 scale = 0xff;
144 }
145
146 verts += 4 * vertexStride;
147
148 float innerCoverage = GrNormalizeByteToFloat(scale);
149 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale);
150
151 for (int i = 0; i < 4; ++i) {
152 if (tweakAlphaForCoverage) {
153 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
154 } else {
155 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
Brian Salomon6a639042016-12-14 11:08:17 -0500156 *reinterpret_cast<float*>(verts + i * vertexStride + coverageOffset) = innerCoverage;
joshualittcd47b712015-08-18 07:25:38 -0700157 }
158 }
159}
Brian Salomon6a639042016-12-14 11:08:17 -0500160
Brian Salomonbaaf4392017-06-15 09:59:23 -0400161namespace {
162
163class AAFillRectOp final : public GrMeshDrawOp {
164private:
165 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
166
joshualitt2ad37be2015-08-18 10:16:01 -0700167public:
Brian Salomon25a88092016-12-01 09:36:50 -0500168 DEFINE_OP_CLASS_ID
bsalomon4be9a302016-07-06 07:03:26 -0700169
Brian Salomonbaaf4392017-06-15 09:59:23 -0400170 static std::unique_ptr<GrDrawOp> Make(GrPaint&& paint,
171 const SkMatrix& viewMatrix,
172 const SkRect& rect,
173 const SkRect& devRect,
174 const SkMatrix* localMatrix,
175 const GrUserStencilSettings* stencil) {
176 SkASSERT(view_matrix_ok_for_aa_fill_rect(viewMatrix));
177 return Helper::FactoryHelper<AAFillRectOp>(std::move(paint), viewMatrix, rect, devRect,
178 localMatrix, stencil);
179 }
180
181 AAFillRectOp(const Helper::MakeArgs& helperArgs,
182 GrColor color,
Brian Salomon6a639042016-12-14 11:08:17 -0500183 const SkMatrix& viewMatrix,
184 const SkRect& rect,
185 const SkRect& devRect,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400186 const SkMatrix* localMatrix,
187 const GrUserStencilSettings* stencil)
188 : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencil) {
bsalomon4be9a302016-07-06 07:03:26 -0700189 if (localMatrix) {
190 void* mem = fRectData.push_back_n(sizeof(RectWithLocalMatrixInfo));
191 new (mem) RectWithLocalMatrixInfo(color, viewMatrix, rect, devRect, *localMatrix);
192 } else {
193 void* mem = fRectData.push_back_n(sizeof(RectInfo));
194 new (mem) RectInfo(color, viewMatrix, rect, devRect);
195 }
Brian Salomon6a639042016-12-14 11:08:17 -0500196 IsZeroArea zeroArea =
197 (!rect.width() || !rect.height()) ? IsZeroArea::kYes : IsZeroArea::kNo;
bsalomon88cf17d2016-07-08 06:40:56 -0700198 this->setBounds(devRect, HasAABloat::kYes, zeroArea);
bsalomon4be9a302016-07-06 07:03:26 -0700199 fRectCnt = 1;
bsalomon0fdec8a2016-07-01 06:31:25 -0700200 }
bsalomon08d14152016-06-30 12:45:18 -0700201
Brian Salomon6a639042016-12-14 11:08:17 -0500202 const char* name() const override { return "AAFillRectOp"; }
bsalomon08d14152016-06-30 12:45:18 -0700203
204 SkString dumpInfo() const override {
205 SkString str;
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400206 str.append(INHERITED::dumpInfo());
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500207 str.appendf("# combined: %d\n", fRectCnt);
bsalomon4be9a302016-07-06 07:03:26 -0700208 const RectInfo* info = this->first();
209 for (int i = 0; i < fRectCnt; ++i) {
210 const SkRect& rect = info->rect();
Brian Salomon6a639042016-12-14 11:08:17 -0500211 str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i,
212 info->color(), rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
bsalomon4be9a302016-07-06 07:03:26 -0700213 info = this->next(info);
bsalomon08d14152016-06-30 12:45:18 -0700214 }
Brian Salomon82dfd3d2017-06-14 12:30:35 -0400215 str += fHelper.dumpInfo();
216 str += INHERITED::dumpInfo();
bsalomon08d14152016-06-30 12:45:18 -0700217 return str;
218 }
219
Brian Salomonbaaf4392017-06-15 09:59:23 -0400220 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
221
Brian Salomonf86d37b2017-06-16 10:04:34 -0400222 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400223 GrColor color = this->first()->color();
Brian Salomonf86d37b2017-06-16 10:04:34 -0400224 auto result = fHelper.xpRequiresDstTexture(
Brian Salomonbaaf4392017-06-15 09:59:23 -0400225 caps, clip, GrProcessorAnalysisCoverage::kSingleChannel, &color);
226 this->first()->setColor(color);
227 return result;
bsalomon08d14152016-06-30 12:45:18 -0700228 }
229
bsalomon08d14152016-06-30 12:45:18 -0700230private:
Brian Salomon91326c32017-08-09 16:02:19 -0400231 void onPrepareDraws(Target* target) override {
bsalomon4be9a302016-07-06 07:03:26 -0700232 using namespace GrDefaultGeoProcFactory;
233
Brian Salomon3de0aee2017-01-29 09:34:17 -0500234 Color color(Color::kPremulGrColorAttribute_Type);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400235 Coverage::Type coverageType = fHelper.compatibleWithAlphaAsCoverage()
236 ? Coverage::kSolid_Type
237 : Coverage::kAttribute_Type;
238 LocalCoords lc = fHelper.usesLocalCoords() ? LocalCoords::kHasExplicit_Type
239 : LocalCoords::kUnused_Type;
Brian Salomon6a639042016-12-14 11:08:17 -0500240 sk_sp<GrGeometryProcessor> gp =
Brian Salomon8c852be2017-01-04 10:44:42 -0500241 GrDefaultGeoProcFactory::Make(color, coverageType, lc, SkMatrix::I());
bsalomon08d14152016-06-30 12:45:18 -0700242 if (!gp) {
243 SkDebugf("Couldn't create GrGeometryProcessor\n");
244 return;
245 }
246
247 size_t vertexStride = gp->getVertexStride();
bsalomon08d14152016-06-30 12:45:18 -0700248
Hal Canary144caf52016-11-07 17:57:18 -0500249 sk_sp<const GrBuffer> indexBuffer(get_index_buffer(target->resourceProvider()));
Chris Dalton3809bab2017-06-13 10:55:06 -0600250 PatternHelper helper(GrPrimitiveType::kTriangles);
Brian Salomon6a639042016-12-14 11:08:17 -0500251 void* vertices =
Chris Daltonbca46e22017-05-15 11:03:26 -0600252 helper.init(target, vertexStride, indexBuffer.get(), kVertsPerAAFillRect,
253 kIndicesPerAAFillRect, fRectCnt);
bsalomon08d14152016-06-30 12:45:18 -0700254 if (!vertices || !indexBuffer) {
255 SkDebugf("Could not allocate vertices\n");
256 return;
257 }
258
bsalomon4be9a302016-07-06 07:03:26 -0700259 const RectInfo* info = this->first();
260 const SkMatrix* localMatrix = nullptr;
261 for (int i = 0; i < fRectCnt; i++) {
Brian Salomon6a639042016-12-14 11:08:17 -0500262 intptr_t verts =
263 reinterpret_cast<intptr_t>(vertices) + i * kVertsPerAAFillRect * vertexStride;
Brian Salomonbaaf4392017-06-15 09:59:23 -0400264 if (fHelper.usesLocalCoords()) {
bsalomon4be9a302016-07-06 07:03:26 -0700265 if (info->hasLocalMatrix()) {
266 localMatrix = &static_cast<const RectWithLocalMatrixInfo*>(info)->localMatrix();
267 } else {
268 localMatrix = &SkMatrix::I();
269 }
270 }
Brian Salomon6a639042016-12-14 11:08:17 -0500271 generate_aa_fill_rect_geometry(verts, vertexStride, info->color(), info->viewMatrix(),
Brian Salomonbaaf4392017-06-15 09:59:23 -0400272 info->rect(), info->devRect(),
273 fHelper.compatibleWithAlphaAsCoverage(), localMatrix);
bsalomon4be9a302016-07-06 07:03:26 -0700274 info = this->next(info);
bsalomon08d14152016-06-30 12:45:18 -0700275 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400276 helper.recordDraw(target, gp.get(), fHelper.makePipeline(target));
bsalomon08d14152016-06-30 12:45:18 -0700277 }
278
Brian Salomon25a88092016-12-01 09:36:50 -0500279 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomon6a639042016-12-14 11:08:17 -0500280 AAFillRectOp* that = t->cast<AAFillRectOp>();
Brian Salomonbaaf4392017-06-15 09:59:23 -0400281 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
benjaminwagnerd87a6b22016-07-04 11:30:01 -0700282 return false;
283 }
284
bsalomon4be9a302016-07-06 07:03:26 -0700285 fRectData.push_back_n(that->fRectData.count(), that->fRectData.begin());
286 fRectCnt += that->fRectCnt;
bsalomon88cf17d2016-07-08 06:40:56 -0700287 this->joinBounds(*that);
benjaminwagnerd87a6b22016-07-04 11:30:01 -0700288 return true;
289 }
290
291 struct RectInfo {
bsalomon4be9a302016-07-06 07:03:26 -0700292 public:
293 RectInfo(GrColor color, const SkMatrix& viewMatrix, const SkRect& rect,
294 const SkRect& devRect)
Brian Salomon6a639042016-12-14 11:08:17 -0500295 : RectInfo(color, viewMatrix, rect, devRect, HasLocalMatrix::kNo) {}
bsalomon4be9a302016-07-06 07:03:26 -0700296 bool hasLocalMatrix() const { return HasLocalMatrix::kYes == fHasLocalMatrix; }
297 GrColor color() const { return fColor; }
298 const SkMatrix& viewMatrix() const { return fViewMatrix; }
299 const SkRect& rect() const { return fRect; }
300 const SkRect& devRect() const { return fDevRect; }
301
302 void setColor(GrColor color) { fColor = color; }
Brian Salomon6a639042016-12-14 11:08:17 -0500303
bsalomon4be9a302016-07-06 07:03:26 -0700304 protected:
305 enum class HasLocalMatrix : uint32_t { kNo, kYes };
306
307 RectInfo(GrColor color, const SkMatrix& viewMatrix, const SkRect& rect,
308 const SkRect& devRect, HasLocalMatrix hasLM)
309 : fHasLocalMatrix(hasLM)
310 , fColor(color)
311 , fViewMatrix(viewMatrix)
312 , fRect(rect)
313 , fDevRect(devRect) {}
314
315 HasLocalMatrix fHasLocalMatrix;
benjaminwagnerd87a6b22016-07-04 11:30:01 -0700316 GrColor fColor;
317 SkMatrix fViewMatrix;
318 SkRect fRect;
319 SkRect fDevRect;
320 };
321
bsalomon4be9a302016-07-06 07:03:26 -0700322 struct RectWithLocalMatrixInfo : public RectInfo {
323 public:
324 RectWithLocalMatrixInfo(GrColor color, const SkMatrix& viewMatrix, const SkRect& rect,
325 const SkRect& devRect, const SkMatrix& localMatrix)
Brian Salomon6a639042016-12-14 11:08:17 -0500326 : RectInfo(color, viewMatrix, rect, devRect, HasLocalMatrix::kYes)
327 , fLocalMatrix(localMatrix) {}
bsalomon4be9a302016-07-06 07:03:26 -0700328 const SkMatrix& localMatrix() const { return fLocalMatrix; }
Brian Salomon6a639042016-12-14 11:08:17 -0500329
bsalomon4be9a302016-07-06 07:03:26 -0700330 private:
benjaminwagnerd87a6b22016-07-04 11:30:01 -0700331 SkMatrix fLocalMatrix;
bsalomon0fdec8a2016-07-01 06:31:25 -0700332 };
333
bsalomon4be9a302016-07-06 07:03:26 -0700334 RectInfo* first() { return reinterpret_cast<RectInfo*>(fRectData.begin()); }
335 const RectInfo* first() const { return reinterpret_cast<const RectInfo*>(fRectData.begin()); }
336 const RectInfo* next(const RectInfo* prev) const {
Brian Salomon6a639042016-12-14 11:08:17 -0500337 intptr_t next =
338 reinterpret_cast<intptr_t>(prev) +
339 (prev->hasLocalMatrix() ? sizeof(RectWithLocalMatrixInfo) : sizeof(RectInfo));
bsalomon4be9a302016-07-06 07:03:26 -0700340 return reinterpret_cast<const RectInfo*>(next);
341 }
342
bsalomon4be9a302016-07-06 07:03:26 -0700343 SkSTArray<4 * sizeof(RectWithLocalMatrixInfo), uint8_t, true> fRectData;
Brian Salomonbaaf4392017-06-15 09:59:23 -0400344 Helper fHelper;
bsalomon4be9a302016-07-06 07:03:26 -0700345 int fRectCnt;
bsalomon08d14152016-06-30 12:45:18 -0700346
Brian Salomonbaaf4392017-06-15 09:59:23 -0400347 typedef GrMeshDrawOp INHERITED;
joshualitt147dc062015-08-12 11:51:46 -0700348};
349
Brian Salomonbaaf4392017-06-15 09:59:23 -0400350} // anonymous namespace
joshualitt9ff64252015-08-10 09:03:51 -0700351
Brian Salomonbaaf4392017-06-15 09:59:23 -0400352namespace GrRectOpFactory {
joshualitt147dc062015-08-12 11:51:46 -0700353
Brian Salomonbaaf4392017-06-15 09:59:23 -0400354std::unique_ptr<GrDrawOp> MakeAAFill(GrPaint&& paint, const SkMatrix& viewMatrix,
355 const SkRect& rect, const GrUserStencilSettings* stencil) {
356 if (!view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
357 return nullptr;
358 }
bsalomonc55271f2015-11-09 11:55:57 -0800359 SkRect devRect;
360 viewMatrix.mapRect(&devRect, rect);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400361 return AAFillRectOp::Make(std::move(paint), viewMatrix, rect, devRect, nullptr, stencil);
bsalomonc55271f2015-11-09 11:55:57 -0800362}
363
Brian Salomonbaaf4392017-06-15 09:59:23 -0400364std::unique_ptr<GrDrawOp> MakeAAFillWithLocalMatrix(GrPaint&& paint, const SkMatrix& viewMatrix,
365 const SkMatrix& localMatrix,
366 const SkRect& rect) {
367 if (!view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
368 return nullptr;
369 }
370 SkRect devRect;
371 viewMatrix.mapRect(&devRect, rect);
372 return AAFillRectOp::Make(std::move(paint), viewMatrix, rect, devRect, &localMatrix, nullptr);
373}
374
375std::unique_ptr<GrDrawOp> MakeAAFillWithLocalRect(GrPaint&& paint, const SkMatrix& viewMatrix,
376 const SkRect& rect, const SkRect& localRect) {
377 if (!view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
378 return nullptr;
379 }
bsalomonc55271f2015-11-09 11:55:57 -0800380 SkRect devRect;
381 viewMatrix.mapRect(&devRect, rect);
382 SkMatrix localMatrix;
383 if (!localMatrix.setRectToRect(rect, localRect, SkMatrix::kFill_ScaleToFit)) {
384 return nullptr;
385 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400386 return AAFillRectOp::Make(std::move(paint), viewMatrix, rect, devRect, &localMatrix, nullptr);
bsalomonc55271f2015-11-09 11:55:57 -0800387}
Brian Salomonbaaf4392017-06-15 09:59:23 -0400388
389} // namespace GrRectOpFactory
joshualitt37eb1842015-08-12 06:36:57 -0700390
joshualitt9ff64252015-08-10 09:03:51 -0700391///////////////////////////////////////////////////////////////////////////////////////////////////
392
Hal Canary6f6961e2017-01-31 13:50:44 -0500393#if GR_TEST_UTILS
joshualitt9ff64252015-08-10 09:03:51 -0700394
Brian Salomon5ec9def2016-12-20 15:34:05 -0500395#include "GrDrawOpTest.h"
joshualitt9ff64252015-08-10 09:03:51 -0700396
Brian Salomonbaaf4392017-06-15 09:59:23 -0400397GR_DRAW_OP_TEST_DEFINE(AAFillRectOp) {
398 SkMatrix viewMatrix;
399 do {
400 viewMatrix = GrTest::TestMatrixInvertible(random);
401 } while (!view_matrix_ok_for_aa_fill_rect(viewMatrix));
joshualitt090ae8e2015-08-14 09:01:21 -0700402 SkRect rect = GrTest::TestRect(random);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400403 SkRect devRect;
404 viewMatrix.mapRect(&devRect, rect);
405 const SkMatrix* localMatrix = nullptr;
406 SkMatrix m;
407 if (random->nextBool()) {
408 m = GrTest::TestMatrix(random);
409 }
410 const GrUserStencilSettings* stencil =
411 random->nextBool() ? nullptr : GrGetRandomStencil(random, context);
412 return AAFillRectOp::Make(std::move(paint), viewMatrix, rect, devRect, localMatrix, stencil);
joshualitt9ff64252015-08-10 09:03:51 -0700413}
414
415#endif