blob: e67298550778606453fd3392787f9b6d131ff59f [file] [log] [blame]
joshualitt9c80b5f2015-08-13 10:05: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
Brian Salomon6a639042016-12-14 11:08:17 -05008#include "GrNonAAFillRectOp.h"
joshualitt9c80b5f2015-08-13 10:05:51 -07009
joshualitt9c80b5f2015-08-13 10:05:51 -070010#include "GrColor.h"
11#include "GrDefaultGeoProcFactory.h"
Brian Salomondad29232016-12-01 16:40:24 -050012#include "GrMeshDrawOp.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050013#include "GrOpFlushState.h"
joshualitt9c80b5f2015-08-13 10:05:51 -070014#include "GrPrimitiveProcessor.h"
joshualittae5b2c62015-08-19 08:48:41 -070015#include "GrQuad.h"
Brian Salomondad29232016-12-01 16:40:24 -050016#include "GrResourceProvider.h"
joshualitt9c80b5f2015-08-13 10:05:51 -070017
reeda39667c2016-08-22 06:39:49 -070018#include "SkMatrixPriv.h"
19
bsalomon08d14152016-06-30 12:45:18 -070020static const int kVertsPerInstance = 4;
21static const int kIndicesPerInstance = 6;
joshualitt2244c272015-08-21 10:33:15 -070022
23/** We always use per-vertex colors so that rects can be batched across color changes. Sometimes
24 we have explicit local coords and sometimes not. We *could* always provide explicit local
25 coords and just duplicate the positions when the caller hasn't provided a local coord rect,
26 but we haven't seen a use case which frequently switches between local rect and no local
27 rect draws.
28
29 The vertex attrib order is always pos, color, [local coords].
30 */
robertphillips6abd1d12016-07-01 09:06:56 -070031static sk_sp<GrGeometryProcessor> make_gp(bool readsCoverage) {
32 using namespace GrDefaultGeoProcFactory;
33 Color color(Color::kAttribute_Type);
34 Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Type);
35
36 LocalCoords localCoords(LocalCoords::kHasExplicit_Type);
37 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMatrix::I());
38}
39
joshualitt2244c272015-08-21 10:33:15 -070040static void tesselate(intptr_t vertices,
41 size_t vertexStride,
42 GrColor color,
robertphillips6abd1d12016-07-01 09:06:56 -070043 const SkMatrix* viewMatrix,
joshualitt2244c272015-08-21 10:33:15 -070044 const SkRect& rect,
joshualitt8cce8f12015-08-26 06:23:39 -070045 const GrQuad* localQuad) {
joshualitt2244c272015-08-21 10:33:15 -070046 SkPoint* positions = reinterpret_cast<SkPoint*>(vertices);
47
Brian Salomon6a639042016-12-14 11:08:17 -050048 positions->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vertexStride);
joshualitt2244c272015-08-21 10:33:15 -070049
robertphillips6abd1d12016-07-01 09:06:56 -070050 if (viewMatrix) {
reeda39667c2016-08-22 06:39:49 -070051 SkMatrixPriv::MapPointsWithStride(*viewMatrix, positions, vertexStride, kVertsPerInstance);
joshualitt8cce8f12015-08-26 06:23:39 -070052 }
53
54 // Setup local coords
joshualitt2244c272015-08-21 10:33:15 -070055 // TODO we should only do this if local coords are being read
joshualitt8cce8f12015-08-26 06:23:39 -070056 if (localQuad) {
joshualitt2244c272015-08-21 10:33:15 -070057 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor);
bsalomon08d14152016-06-30 12:45:18 -070058 for (int i = 0; i < kVertsPerInstance; i++) {
Brian Salomon6a639042016-12-14 11:08:17 -050059 SkPoint* coords =
60 reinterpret_cast<SkPoint*>(vertices + kLocalOffset + i * vertexStride);
joshualitt8cce8f12015-08-26 06:23:39 -070061 *coords = localQuad->point(i);
joshualitt2244c272015-08-21 10:33:15 -070062 }
63 }
64
65 static const int kColorOffset = sizeof(SkPoint);
66 GrColor* vertColor = reinterpret_cast<GrColor*>(vertices + kColorOffset);
67 for (int j = 0; j < 4; ++j) {
68 *vertColor = color;
Brian Salomon6a639042016-12-14 11:08:17 -050069 vertColor = (GrColor*)((intptr_t)vertColor + vertexStride);
joshualitt2244c272015-08-21 10:33:15 -070070 }
71}
72
Brian Salomon6a639042016-12-14 11:08:17 -050073class NonAAFillRectOp final : public GrMeshDrawOp {
joshualitt2244c272015-08-21 10:33:15 -070074public:
Brian Salomon25a88092016-12-01 09:36:50 -050075 DEFINE_OP_CLASS_ID
bsalomon08d14152016-06-30 12:45:18 -070076
Brian Salomon6a639042016-12-14 11:08:17 -050077 NonAAFillRectOp(GrColor color, const SkMatrix& viewMatrix, const SkRect& rect,
78 const SkRect* localRect, const SkMatrix* localMatrix)
bsalomon9d7f1842016-07-01 08:01:36 -070079 : INHERITED(ClassID()) {
Brian Salomon6a639042016-12-14 11:08:17 -050080 SkASSERT(!viewMatrix.hasPerspective() && (!localMatrix || !localMatrix->hasPerspective()));
bsalomon9d7f1842016-07-01 08:01:36 -070081 RectInfo& info = fRects.push_back();
82 info.fColor = color;
83 info.fViewMatrix = viewMatrix;
84 info.fRect = rect;
85 if (localRect && localMatrix) {
86 info.fLocalQuad.setFromMappedRect(*localRect, *localMatrix);
87 } else if (localRect) {
88 info.fLocalQuad.set(*localRect);
89 } else if (localMatrix) {
90 info.fLocalQuad.setFromMappedRect(rect, *localMatrix);
91 } else {
92 info.fLocalQuad.set(rect);
93 }
bsalomon88cf17d2016-07-08 06:40:56 -070094 this->setTransformedBounds(fRects[0].fRect, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
bsalomon9d7f1842016-07-01 08:01:36 -070095 }
bsalomon08d14152016-06-30 12:45:18 -070096
Brian Salomon6a639042016-12-14 11:08:17 -050097 const char* name() const override { return "NonAAFillRectOp"; }
bsalomon08d14152016-06-30 12:45:18 -070098
99 SkString dumpInfo() const override {
100 SkString str;
bsalomon9d7f1842016-07-01 08:01:36 -0700101 str.appendf("# batched: %d\n", fRects.count());
102 for (int i = 0; i < fRects.count(); ++i) {
103 const RectInfo& info = fRects[i];
Brian Salomon6a639042016-12-14 11:08:17 -0500104 str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i,
105 info.fColor, info.fRect.fLeft, info.fRect.fTop, info.fRect.fRight,
106 info.fRect.fBottom);
bsalomon08d14152016-06-30 12:45:18 -0700107 }
Brian Salomon7c3e7182016-12-01 09:35:30 -0500108 str.append(DumpPipelineInfo(*this->pipeline()));
bsalomon08d14152016-06-30 12:45:18 -0700109 str.append(INHERITED::dumpInfo());
110 return str;
111 }
112
113 void computePipelineOptimizations(GrInitInvariantOutput* color,
114 GrInitInvariantOutput* coverage,
115 GrBatchToXPOverrides* overrides) const override {
Brian Salomon6a639042016-12-14 11:08:17 -0500116 // When this is called there is only one rect.
bsalomon9d7f1842016-07-01 08:01:36 -0700117 color->setKnownFourComponents(fRects[0].fColor);
bsalomonbc9b6a42016-07-01 05:35:51 -0700118 coverage->setKnownSingleComponent(0xff);
bsalomon08d14152016-06-30 12:45:18 -0700119 }
120
121 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomon9d7f1842016-07-01 08:01:36 -0700122 overrides.getOverrideColorIfSet(&fRects[0].fColor);
bsalomon08d14152016-06-30 12:45:18 -0700123 fOverrides = overrides;
124 }
125
bsalomon08d14152016-06-30 12:45:18 -0700126private:
Brian Salomon6a639042016-12-14 11:08:17 -0500127 NonAAFillRectOp() : INHERITED(ClassID()) {}
bsalomon08d14152016-06-30 12:45:18 -0700128
129 void onPrepareDraws(Target* target) const override {
robertphillips6abd1d12016-07-01 09:06:56 -0700130 sk_sp<GrGeometryProcessor> gp = make_gp(fOverrides.readsCoverage());
bsalomon08d14152016-06-30 12:45:18 -0700131 if (!gp) {
132 SkDebugf("Couldn't create GrGeometryProcessor\n");
133 return;
134 }
bsalomonbc9b6a42016-07-01 05:35:51 -0700135 SkASSERT(gp->getVertexStride() ==
136 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr));
bsalomon08d14152016-06-30 12:45:18 -0700137
138 size_t vertexStride = gp->getVertexStride();
bsalomon9d7f1842016-07-01 08:01:36 -0700139 int instanceCount = fRects.count();
bsalomon08d14152016-06-30 12:45:18 -0700140
Hal Canary144caf52016-11-07 17:57:18 -0500141 sk_sp<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer());
bsalomon08d14152016-06-30 12:45:18 -0700142 InstancedHelper helper;
Brian Salomon6a639042016-12-14 11:08:17 -0500143 void* vertices =
144 helper.init(target, kTriangles_GrPrimitiveType, vertexStride, indexBuffer.get(),
145 kVertsPerInstance, kIndicesPerInstance, instanceCount);
bsalomon08d14152016-06-30 12:45:18 -0700146 if (!vertices || !indexBuffer) {
147 SkDebugf("Could not allocate vertices\n");
148 return;
149 }
150
151 for (int i = 0; i < instanceCount; i++) {
Brian Salomon6a639042016-12-14 11:08:17 -0500152 intptr_t verts =
153 reinterpret_cast<intptr_t>(vertices) + i * kVertsPerInstance * vertexStride;
robertphillips6abd1d12016-07-01 09:06:56 -0700154 tesselate(verts, vertexStride, fRects[i].fColor, &fRects[i].fViewMatrix,
bsalomon9d7f1842016-07-01 08:01:36 -0700155 fRects[i].fRect, &fRects[i].fLocalQuad);
bsalomon08d14152016-06-30 12:45:18 -0700156 }
157 helper.recordDraw(target, gp.get());
158 }
159
Brian Salomon25a88092016-12-01 09:36:50 -0500160 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomon6a639042016-12-14 11:08:17 -0500161 NonAAFillRectOp* that = t->cast<NonAAFillRectOp>();
bsalomon08d14152016-06-30 12:45:18 -0700162 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
163 that->bounds(), caps)) {
164 return false;
165 }
166
Brian Salomon6a639042016-12-14 11:08:17 -0500167 // In the event of two ops, one who can tweak, one who cannot, we just fall back to not
168 // tweaking.
bsalomon08d14152016-06-30 12:45:18 -0700169 if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakAlphaForCoverage()) {
170 fOverrides = that->fOverrides;
171 }
172
bsalomon9d7f1842016-07-01 08:01:36 -0700173 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700174 this->joinBounds(*that);
bsalomon08d14152016-06-30 12:45:18 -0700175 return true;
176 }
177
bsalomon9d7f1842016-07-01 08:01:36 -0700178 struct RectInfo {
179 GrColor fColor;
180 SkMatrix fViewMatrix;
181 SkRect fRect;
182 GrQuad fLocalQuad;
183 };
184
bsalomon08d14152016-06-30 12:45:18 -0700185 GrXPOverridesForBatch fOverrides;
bsalomon9d7f1842016-07-01 08:01:36 -0700186 SkSTArray<1, RectInfo, true> fRects;
bsalomon08d14152016-06-30 12:45:18 -0700187
Brian Salomondad29232016-12-01 16:40:24 -0500188 typedef GrMeshDrawOp INHERITED;
joshualitt2244c272015-08-21 10:33:15 -0700189};
190
Brian Salomon6a639042016-12-14 11:08:17 -0500191namespace GrNonAAFillRectOp {
bsalomonb8cbd202016-06-30 13:09:48 -0700192
Brian Salomon6a639042016-12-14 11:08:17 -0500193sk_sp<GrDrawOp> Make(GrColor color,
194 const SkMatrix& viewMatrix,
195 const SkRect& rect,
196 const SkRect* localRect,
197 const SkMatrix* localMatrix) {
198 return sk_sp<GrDrawOp>(new NonAAFillRectOp(color, viewMatrix, rect, localRect, localMatrix));
joshualitt9c80b5f2015-08-13 10:05:51 -0700199}
200};
201
202///////////////////////////////////////////////////////////////////////////////////////////////////
203
204#ifdef GR_TEST_UTILS
205
Brian Salomon5ec9def2016-12-20 15:34:05 -0500206#include "GrDrawOpTest.h"
joshualitt9c80b5f2015-08-13 10:05:51 -0700207
Brian Salomon5ec9def2016-12-20 15:34:05 -0500208DRAW_OP_TEST_DEFINE(NonAAFillRectOp) {
joshualitt2244c272015-08-21 10:33:15 -0700209 GrColor color = GrRandomColor(random);
210 SkRect rect = GrTest::TestRect(random);
211 SkRect localRect = GrTest::TestRect(random);
212 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
213 SkMatrix localMatrix = GrTest::TestMatrix(random);
joshualitt9c80b5f2015-08-13 10:05:51 -0700214
joshualitt2244c272015-08-21 10:33:15 -0700215 bool hasLocalRect = random->nextBool();
216 bool hasLocalMatrix = random->nextBool();
Brian Salomon6a639042016-12-14 11:08:17 -0500217 return GrNonAAFillRectOp::Make(color,
218 viewMatrix,
219 rect,
220 hasLocalRect ? &localRect : nullptr,
Brian Salomon5ec9def2016-12-20 15:34:05 -0500221 hasLocalMatrix ? &localMatrix : nullptr);
joshualitt9c80b5f2015-08-13 10:05:51 -0700222}
223
224#endif