blob: 69bfe291e2debd7a4ce403bbfb4b77e2172c38de [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 */
Brian Salomon8c852be2017-01-04 10:44:42 -050031static sk_sp<GrGeometryProcessor> make_gp() {
robertphillips6abd1d12016-07-01 09:06:56 -070032 using namespace GrDefaultGeoProcFactory;
Brian Salomon3de0aee2017-01-29 09:34:17 -050033 return GrDefaultGeoProcFactory::Make(Color::kPremulGrColorAttribute_Type, Coverage::kSolid_Type,
Brian Salomon8c852be2017-01-04 10:44:42 -050034 LocalCoords::kHasExplicit_Type, SkMatrix::I());
robertphillips6abd1d12016-07-01 09:06:56 -070035}
36
joshualitt2244c272015-08-21 10:33:15 -070037static void tesselate(intptr_t vertices,
38 size_t vertexStride,
39 GrColor color,
robertphillips6abd1d12016-07-01 09:06:56 -070040 const SkMatrix* viewMatrix,
joshualitt2244c272015-08-21 10:33:15 -070041 const SkRect& rect,
joshualitt8cce8f12015-08-26 06:23:39 -070042 const GrQuad* localQuad) {
joshualitt2244c272015-08-21 10:33:15 -070043 SkPoint* positions = reinterpret_cast<SkPoint*>(vertices);
44
Brian Salomon6a639042016-12-14 11:08:17 -050045 positions->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vertexStride);
joshualitt2244c272015-08-21 10:33:15 -070046
robertphillips6abd1d12016-07-01 09:06:56 -070047 if (viewMatrix) {
reeda39667c2016-08-22 06:39:49 -070048 SkMatrixPriv::MapPointsWithStride(*viewMatrix, positions, vertexStride, kVertsPerInstance);
joshualitt8cce8f12015-08-26 06:23:39 -070049 }
50
51 // Setup local coords
joshualitt2244c272015-08-21 10:33:15 -070052 // TODO we should only do this if local coords are being read
joshualitt8cce8f12015-08-26 06:23:39 -070053 if (localQuad) {
joshualitt2244c272015-08-21 10:33:15 -070054 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor);
bsalomon08d14152016-06-30 12:45:18 -070055 for (int i = 0; i < kVertsPerInstance; i++) {
Brian Salomon6a639042016-12-14 11:08:17 -050056 SkPoint* coords =
57 reinterpret_cast<SkPoint*>(vertices + kLocalOffset + i * vertexStride);
joshualitt8cce8f12015-08-26 06:23:39 -070058 *coords = localQuad->point(i);
joshualitt2244c272015-08-21 10:33:15 -070059 }
60 }
61
62 static const int kColorOffset = sizeof(SkPoint);
63 GrColor* vertColor = reinterpret_cast<GrColor*>(vertices + kColorOffset);
64 for (int j = 0; j < 4; ++j) {
65 *vertColor = color;
Brian Salomon6a639042016-12-14 11:08:17 -050066 vertColor = (GrColor*)((intptr_t)vertColor + vertexStride);
joshualitt2244c272015-08-21 10:33:15 -070067 }
68}
69
Brian Salomond3ccb0a2017-04-03 10:38:00 -040070class NonAAFillRectOp final : public GrLegacyMeshDrawOp {
joshualitt2244c272015-08-21 10:33:15 -070071public:
Brian Salomon25a88092016-12-01 09:36:50 -050072 DEFINE_OP_CLASS_ID
bsalomon08d14152016-06-30 12:45:18 -070073
Brian Salomon6a639042016-12-14 11:08:17 -050074 NonAAFillRectOp(GrColor color, const SkMatrix& viewMatrix, const SkRect& rect,
75 const SkRect* localRect, const SkMatrix* localMatrix)
bsalomon9d7f1842016-07-01 08:01:36 -070076 : INHERITED(ClassID()) {
Brian Salomon6a639042016-12-14 11:08:17 -050077 SkASSERT(!viewMatrix.hasPerspective() && (!localMatrix || !localMatrix->hasPerspective()));
bsalomon9d7f1842016-07-01 08:01:36 -070078 RectInfo& info = fRects.push_back();
79 info.fColor = color;
80 info.fViewMatrix = viewMatrix;
81 info.fRect = rect;
82 if (localRect && localMatrix) {
83 info.fLocalQuad.setFromMappedRect(*localRect, *localMatrix);
84 } else if (localRect) {
85 info.fLocalQuad.set(*localRect);
86 } else if (localMatrix) {
87 info.fLocalQuad.setFromMappedRect(rect, *localMatrix);
88 } else {
89 info.fLocalQuad.set(rect);
90 }
bsalomon88cf17d2016-07-08 06:40:56 -070091 this->setTransformedBounds(fRects[0].fRect, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
bsalomon9d7f1842016-07-01 08:01:36 -070092 }
bsalomon08d14152016-06-30 12:45:18 -070093
Brian Salomon6a639042016-12-14 11:08:17 -050094 const char* name() const override { return "NonAAFillRectOp"; }
bsalomon08d14152016-06-30 12:45:18 -070095
96 SkString dumpInfo() const override {
97 SkString str;
Robert Phillipsf5442bb2017-04-17 14:18:34 -040098 str.append(INHERITED::dumpInfo());
Brian Salomon53e4c3c2016-12-21 11:38:53 -050099 str.appendf("# combined: %d\n", fRects.count());
bsalomon9d7f1842016-07-01 08:01:36 -0700100 for (int i = 0; i < fRects.count(); ++i) {
101 const RectInfo& info = fRects[i];
Brian Salomon6a639042016-12-14 11:08:17 -0500102 str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i,
103 info.fColor, info.fRect.fLeft, info.fRect.fTop, info.fRect.fRight,
104 info.fRect.fBottom);
bsalomon08d14152016-06-30 12:45:18 -0700105 }
Brian Salomon7c3e7182016-12-01 09:35:30 -0500106 str.append(DumpPipelineInfo(*this->pipeline()));
bsalomon08d14152016-06-30 12:45:18 -0700107 return str;
108 }
109
bsalomon08d14152016-06-30 12:45:18 -0700110private:
Brian Salomon6a639042016-12-14 11:08:17 -0500111 NonAAFillRectOp() : INHERITED(ClassID()) {}
bsalomon08d14152016-06-30 12:45:18 -0700112
Brian Salomona811b122017-03-30 08:21:32 -0400113 void getProcessorAnalysisInputs(GrProcessorAnalysisColor* color,
114 GrProcessorAnalysisCoverage* coverage) const override {
Brian Salomonc0b642c2017-03-27 13:09:36 -0400115 color->setToConstant(fRects[0].fColor);
Brian Salomona811b122017-03-30 08:21:32 -0400116 *coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500117 }
118
Brian Salomone7d30482017-03-29 12:09:15 -0400119 void applyPipelineOptimizations(const PipelineOptimizations& optimizations) override {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500120 optimizations.getOverrideColorIfSet(&fRects[0].fColor);
Brian Salomon92aee3d2016-12-21 09:20:25 -0500121 }
122
bsalomon08d14152016-06-30 12:45:18 -0700123 void onPrepareDraws(Target* target) const override {
Brian Salomon8c852be2017-01-04 10:44:42 -0500124 sk_sp<GrGeometryProcessor> gp = make_gp();
bsalomon08d14152016-06-30 12:45:18 -0700125 if (!gp) {
126 SkDebugf("Couldn't create GrGeometryProcessor\n");
127 return;
128 }
bsalomonbc9b6a42016-07-01 05:35:51 -0700129 SkASSERT(gp->getVertexStride() ==
130 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr));
bsalomon08d14152016-06-30 12:45:18 -0700131
132 size_t vertexStride = gp->getVertexStride();
bsalomon9d7f1842016-07-01 08:01:36 -0700133 int instanceCount = fRects.count();
bsalomon08d14152016-06-30 12:45:18 -0700134
Hal Canary144caf52016-11-07 17:57:18 -0500135 sk_sp<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer());
bsalomon08d14152016-06-30 12:45:18 -0700136 InstancedHelper helper;
Brian Salomon6a639042016-12-14 11:08:17 -0500137 void* vertices =
138 helper.init(target, kTriangles_GrPrimitiveType, vertexStride, indexBuffer.get(),
139 kVertsPerInstance, kIndicesPerInstance, instanceCount);
bsalomon08d14152016-06-30 12:45:18 -0700140 if (!vertices || !indexBuffer) {
141 SkDebugf("Could not allocate vertices\n");
142 return;
143 }
144
145 for (int i = 0; i < instanceCount; i++) {
Brian Salomon6a639042016-12-14 11:08:17 -0500146 intptr_t verts =
147 reinterpret_cast<intptr_t>(vertices) + i * kVertsPerInstance * vertexStride;
robertphillips6abd1d12016-07-01 09:06:56 -0700148 tesselate(verts, vertexStride, fRects[i].fColor, &fRects[i].fViewMatrix,
bsalomon9d7f1842016-07-01 08:01:36 -0700149 fRects[i].fRect, &fRects[i].fLocalQuad);
bsalomon08d14152016-06-30 12:45:18 -0700150 }
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400151 helper.recordDraw(target, gp.get(), this->pipeline());
bsalomon08d14152016-06-30 12:45:18 -0700152 }
153
Brian Salomon25a88092016-12-01 09:36:50 -0500154 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomon6a639042016-12-14 11:08:17 -0500155 NonAAFillRectOp* that = t->cast<NonAAFillRectOp>();
bsalomon08d14152016-06-30 12:45:18 -0700156 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
157 that->bounds(), caps)) {
158 return false;
159 }
160
bsalomon9d7f1842016-07-01 08:01:36 -0700161 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700162 this->joinBounds(*that);
bsalomon08d14152016-06-30 12:45:18 -0700163 return true;
164 }
165
bsalomon9d7f1842016-07-01 08:01:36 -0700166 struct RectInfo {
167 GrColor fColor;
168 SkMatrix fViewMatrix;
169 SkRect fRect;
170 GrQuad fLocalQuad;
171 };
172
bsalomon9d7f1842016-07-01 08:01:36 -0700173 SkSTArray<1, RectInfo, true> fRects;
bsalomon08d14152016-06-30 12:45:18 -0700174
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400175 typedef GrLegacyMeshDrawOp INHERITED;
joshualitt2244c272015-08-21 10:33:15 -0700176};
177
Brian Salomon6a639042016-12-14 11:08:17 -0500178namespace GrNonAAFillRectOp {
bsalomonb8cbd202016-06-30 13:09:48 -0700179
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400180std::unique_ptr<GrLegacyMeshDrawOp> Make(GrColor color,
181 const SkMatrix& viewMatrix,
182 const SkRect& rect,
183 const SkRect* localRect,
184 const SkMatrix* localMatrix) {
185 return std::unique_ptr<GrLegacyMeshDrawOp>(
Brian Salomonf8334782017-01-03 09:42:58 -0500186 new NonAAFillRectOp(color, viewMatrix, rect, localRect, localMatrix));
joshualitt9c80b5f2015-08-13 10:05:51 -0700187}
188};
189
190///////////////////////////////////////////////////////////////////////////////////////////////////
191
Hal Canary6f6961e2017-01-31 13:50:44 -0500192#if GR_TEST_UTILS
joshualitt9c80b5f2015-08-13 10:05:51 -0700193
Brian Salomon5ec9def2016-12-20 15:34:05 -0500194#include "GrDrawOpTest.h"
joshualitt9c80b5f2015-08-13 10:05:51 -0700195
Brian Salomon5ec9def2016-12-20 15:34:05 -0500196DRAW_OP_TEST_DEFINE(NonAAFillRectOp) {
joshualitt2244c272015-08-21 10:33:15 -0700197 GrColor color = GrRandomColor(random);
198 SkRect rect = GrTest::TestRect(random);
199 SkRect localRect = GrTest::TestRect(random);
200 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
201 SkMatrix localMatrix = GrTest::TestMatrix(random);
joshualitt9c80b5f2015-08-13 10:05:51 -0700202
joshualitt2244c272015-08-21 10:33:15 -0700203 bool hasLocalRect = random->nextBool();
204 bool hasLocalMatrix = random->nextBool();
Brian Salomon6a639042016-12-14 11:08:17 -0500205 return GrNonAAFillRectOp::Make(color,
206 viewMatrix,
207 rect,
208 hasLocalRect ? &localRect : nullptr,
Brian Salomon5ec9def2016-12-20 15:34:05 -0500209 hasLocalMatrix ? &localMatrix : nullptr);
joshualitt9c80b5f2015-08-13 10:05:51 -0700210}
211
212#endif