blob: 26bee83cc6037e5ecd4cddf0527e5894b00bb713 [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
Brian Salomon53e4c3c2016-12-21 11:38:53 -050023/** We always use per-vertex colors so that rects can be combined across color changes. Sometimes
joshualitt2244c272015-08-21 10:33:15 -070024 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;
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500101 str.appendf("# combined: %d\n", fRects.count());
bsalomon9d7f1842016-07-01 08:01:36 -0700102 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
bsalomon08d14152016-06-30 12:45:18 -0700113private:
Brian Salomon6a639042016-12-14 11:08:17 -0500114 NonAAFillRectOp() : INHERITED(ClassID()) {}
bsalomon08d14152016-06-30 12:45:18 -0700115
Brian Salomon92aee3d2016-12-21 09:20:25 -0500116 void getPipelineAnalysisInput(GrPipelineAnalysisDrawOpInput* input) const override {
117 input->pipelineColorInput()->setKnownFourComponents(fRects[0].fColor);
118 input->pipelineCoverageInput()->setKnownSingleComponent(0xff);
119 }
120
121 void applyPipelineOptimizations(const GrPipelineOptimizations& optimizations) override {
122 optimizations.getOverrideColorIfSet(&fRects[0].fColor);
123 fOptimizations = optimizations;
124 }
125
bsalomon08d14152016-06-30 12:45:18 -0700126 void onPrepareDraws(Target* target) const override {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500127 sk_sp<GrGeometryProcessor> gp = make_gp(fOptimizations.readsCoverage());
bsalomon08d14152016-06-30 12:45:18 -0700128 if (!gp) {
129 SkDebugf("Couldn't create GrGeometryProcessor\n");
130 return;
131 }
bsalomonbc9b6a42016-07-01 05:35:51 -0700132 SkASSERT(gp->getVertexStride() ==
133 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr));
bsalomon08d14152016-06-30 12:45:18 -0700134
135 size_t vertexStride = gp->getVertexStride();
bsalomon9d7f1842016-07-01 08:01:36 -0700136 int instanceCount = fRects.count();
bsalomon08d14152016-06-30 12:45:18 -0700137
Hal Canary144caf52016-11-07 17:57:18 -0500138 sk_sp<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer());
bsalomon08d14152016-06-30 12:45:18 -0700139 InstancedHelper helper;
Brian Salomon6a639042016-12-14 11:08:17 -0500140 void* vertices =
141 helper.init(target, kTriangles_GrPrimitiveType, vertexStride, indexBuffer.get(),
142 kVertsPerInstance, kIndicesPerInstance, instanceCount);
bsalomon08d14152016-06-30 12:45:18 -0700143 if (!vertices || !indexBuffer) {
144 SkDebugf("Could not allocate vertices\n");
145 return;
146 }
147
148 for (int i = 0; i < instanceCount; i++) {
Brian Salomon6a639042016-12-14 11:08:17 -0500149 intptr_t verts =
150 reinterpret_cast<intptr_t>(vertices) + i * kVertsPerInstance * vertexStride;
robertphillips6abd1d12016-07-01 09:06:56 -0700151 tesselate(verts, vertexStride, fRects[i].fColor, &fRects[i].fViewMatrix,
bsalomon9d7f1842016-07-01 08:01:36 -0700152 fRects[i].fRect, &fRects[i].fLocalQuad);
bsalomon08d14152016-06-30 12:45:18 -0700153 }
154 helper.recordDraw(target, gp.get());
155 }
156
Brian Salomon25a88092016-12-01 09:36:50 -0500157 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomon6a639042016-12-14 11:08:17 -0500158 NonAAFillRectOp* that = t->cast<NonAAFillRectOp>();
bsalomon08d14152016-06-30 12:45:18 -0700159 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
160 that->bounds(), caps)) {
161 return false;
162 }
163
Brian Salomon6a639042016-12-14 11:08:17 -0500164 // In the event of two ops, one who can tweak, one who cannot, we just fall back to not
165 // tweaking.
Brian Salomon92aee3d2016-12-21 09:20:25 -0500166 if (fOptimizations.canTweakAlphaForCoverage() &&
167 !that->fOptimizations.canTweakAlphaForCoverage()) {
168 fOptimizations = that->fOptimizations;
bsalomon08d14152016-06-30 12:45:18 -0700169 }
170
bsalomon9d7f1842016-07-01 08:01:36 -0700171 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700172 this->joinBounds(*that);
bsalomon08d14152016-06-30 12:45:18 -0700173 return true;
174 }
175
bsalomon9d7f1842016-07-01 08:01:36 -0700176 struct RectInfo {
177 GrColor fColor;
178 SkMatrix fViewMatrix;
179 SkRect fRect;
180 GrQuad fLocalQuad;
181 };
182
Brian Salomon92aee3d2016-12-21 09:20:25 -0500183 GrPipelineOptimizations fOptimizations;
bsalomon9d7f1842016-07-01 08:01:36 -0700184 SkSTArray<1, RectInfo, true> fRects;
bsalomon08d14152016-06-30 12:45:18 -0700185
Brian Salomondad29232016-12-01 16:40:24 -0500186 typedef GrMeshDrawOp INHERITED;
joshualitt2244c272015-08-21 10:33:15 -0700187};
188
Brian Salomon6a639042016-12-14 11:08:17 -0500189namespace GrNonAAFillRectOp {
bsalomonb8cbd202016-06-30 13:09:48 -0700190
Brian Salomonf8334782017-01-03 09:42:58 -0500191std::unique_ptr<GrDrawOp> Make(GrColor color,
192 const SkMatrix& viewMatrix,
193 const SkRect& rect,
194 const SkRect* localRect,
195 const SkMatrix* localMatrix) {
196 return std::unique_ptr<GrDrawOp>(
197 new NonAAFillRectOp(color, viewMatrix, rect, localRect, localMatrix));
joshualitt9c80b5f2015-08-13 10:05:51 -0700198}
199};
200
201///////////////////////////////////////////////////////////////////////////////////////////////////
202
203#ifdef GR_TEST_UTILS
204
Brian Salomon5ec9def2016-12-20 15:34:05 -0500205#include "GrDrawOpTest.h"
joshualitt9c80b5f2015-08-13 10:05:51 -0700206
Brian Salomon5ec9def2016-12-20 15:34:05 -0500207DRAW_OP_TEST_DEFINE(NonAAFillRectOp) {
joshualitt2244c272015-08-21 10:33:15 -0700208 GrColor color = GrRandomColor(random);
209 SkRect rect = GrTest::TestRect(random);
210 SkRect localRect = GrTest::TestRect(random);
211 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
212 SkMatrix localMatrix = GrTest::TestMatrix(random);
joshualitt9c80b5f2015-08-13 10:05:51 -0700213
joshualitt2244c272015-08-21 10:33:15 -0700214 bool hasLocalRect = random->nextBool();
215 bool hasLocalMatrix = random->nextBool();
Brian Salomon6a639042016-12-14 11:08:17 -0500216 return GrNonAAFillRectOp::Make(color,
217 viewMatrix,
218 rect,
219 hasLocalRect ? &localRect : nullptr,
Brian Salomon5ec9def2016-12-20 15:34:05 -0500220 hasLocalMatrix ? &localMatrix : nullptr);
joshualitt9c80b5f2015-08-13 10:05:51 -0700221}
222
223#endif