blob: 9141b34fe2d175a9e1003669601ee7bccb7339ea [file] [log] [blame]
joshualitt33a5fce2015-11-18 13:28:51 -08001/*
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 Salomonfc527d22016-12-14 21:07:01 -05008#include "GrLatticeOp.h"
joshualitt33a5fce2015-11-18 13:28:51 -08009
joshualitt33a5fce2015-11-18 13:28:51 -080010#include "GrDefaultGeoProcFactory.h"
Brian Salomondad29232016-12-01 16:40:24 -050011#include "GrMeshDrawOp.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050012#include "GrOpFlushState.h"
joshualitt33a5fce2015-11-18 13:28:51 -080013#include "GrResourceProvider.h"
joshualitt33a5fce2015-11-18 13:28:51 -080014#include "SkBitmap.h"
msarettc573a402016-08-02 08:05:56 -070015#include "SkLatticeIter.h"
joshualitt33a5fce2015-11-18 13:28:51 -080016#include "SkRect.h"
17
Brian Salomon8c852be2017-01-04 10:44:42 -050018static sk_sp<GrGeometryProcessor> create_gp() {
joshualitt33a5fce2015-11-18 13:28:51 -080019 using namespace GrDefaultGeoProcFactory;
Brian Salomon8c852be2017-01-04 10:44:42 -050020 return GrDefaultGeoProcFactory::Make(Color::kAttribute_Type, Coverage::kSolid_Type,
21 LocalCoords::kHasExplicit_Type, SkMatrix::I());
joshualitt33a5fce2015-11-18 13:28:51 -080022}
23
Brian Salomonfc527d22016-12-14 21:07:01 -050024class NonAALatticeOp final : public GrMeshDrawOp {
joshualitt33a5fce2015-11-18 13:28:51 -080025public:
Brian Salomon25a88092016-12-01 09:36:50 -050026 DEFINE_OP_CLASS_ID
joshualitt33a5fce2015-11-18 13:28:51 -080027
28 static const int kVertsPerRect = 4;
29 static const int kIndicesPerRect = 6;
joshualitt33a5fce2015-11-18 13:28:51 -080030
Brian Salomonfc527d22016-12-14 21:07:01 -050031 NonAALatticeOp(GrColor color, const SkMatrix& viewMatrix, int imageWidth, int imageHeight,
32 std::unique_ptr<SkLatticeIter> iter, const SkRect& dst)
33 : INHERITED(ClassID()) {
bsalomona71b8982016-06-30 12:13:52 -070034 Patch& patch = fPatches.push_back();
35 patch.fViewMatrix = viewMatrix;
36 patch.fColor = color;
msarett10e3d9b2016-08-18 15:46:03 -070037 patch.fIter = std::move(iter);
bsalomona71b8982016-06-30 12:13:52 -070038 patch.fDst = dst;
joshualitt33a5fce2015-11-18 13:28:51 -080039
40 fImageWidth = imageWidth;
41 fImageHeight = imageHeight;
42
43 // setup bounds
bsalomon88cf17d2016-07-08 06:40:56 -070044 this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
joshualitt33a5fce2015-11-18 13:28:51 -080045 }
46
Brian Salomonfc527d22016-12-14 21:07:01 -050047 const char* name() const override { return "NonAALatticeOp"; }
robertphillips783a4da2015-11-19 14:00:02 -080048
49 SkString dumpInfo() const override {
50 SkString str;
51
bsalomona71b8982016-06-30 12:13:52 -070052 for (int i = 0; i < fPatches.count(); ++i) {
Brian Salomonfc527d22016-12-14 21:07:01 -050053 str.appendf("%d: Color: 0x%08x Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i,
54 fPatches[i].fColor, fPatches[i].fDst.fLeft, fPatches[i].fDst.fTop,
bsalomona71b8982016-06-30 12:13:52 -070055 fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom);
robertphillips783a4da2015-11-19 14:00:02 -080056 }
57
Brian Salomon7c3e7182016-12-01 09:35:30 -050058 str.append(DumpPipelineInfo(*this->pipeline()));
robertphillips783a4da2015-11-19 14:00:02 -080059 str.append(INHERITED::dumpInfo());
60 return str;
61 }
joshualitt33a5fce2015-11-18 13:28:51 -080062
Brian Salomon92aee3d2016-12-21 09:20:25 -050063private:
64 void getPipelineAnalysisInput(GrPipelineAnalysisDrawOpInput* input) const override {
65 input->pipelineColorInput()->setUnknownFourComponents();
66 input->pipelineCoverageInput()->setKnownSingleComponent(0xff);
joshualitt33a5fce2015-11-18 13:28:51 -080067 }
68
Brian Salomon92aee3d2016-12-21 09:20:25 -050069 void applyPipelineOptimizations(const GrPipelineOptimizations& analysioptimizations) override {
70 analysioptimizations.getOverrideColorIfSet(&fPatches[0].fColor);
71 fOptimizations = analysioptimizations;
72 }
73
joshualitt144c3c82015-11-30 12:30:13 -080074 void onPrepareDraws(Target* target) const override {
Brian Salomon8c852be2017-01-04 10:44:42 -050075 sk_sp<GrGeometryProcessor> gp(create_gp());
joshualitt33a5fce2015-11-18 13:28:51 -080076 if (!gp) {
77 SkDebugf("Couldn't create GrGeometryProcessor\n");
78 return;
79 }
80
joshualitt33a5fce2015-11-18 13:28:51 -080081 size_t vertexStride = gp->getVertexStride();
bsalomona71b8982016-06-30 12:13:52 -070082 int patchCnt = fPatches.count();
msarett10e3d9b2016-08-18 15:46:03 -070083 int numRects = 0;
84 for (int i = 0; i < patchCnt; i++) {
msarett0764efe2016-09-02 11:24:30 -070085 numRects += fPatches[i].fIter->numRectsToDraw();
msarett10e3d9b2016-08-18 15:46:03 -070086 }
joshualitt33a5fce2015-11-18 13:28:51 -080087
Hal Canary144caf52016-11-07 17:57:18 -050088 sk_sp<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer());
joshualitt33a5fce2015-11-18 13:28:51 -080089 InstancedHelper helper;
90 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride,
Brian Salomonfc527d22016-12-14 21:07:01 -050091 indexBuffer.get(), kVertsPerRect, kIndicesPerRect, numRects);
joshualitt33a5fce2015-11-18 13:28:51 -080092 if (!vertices || !indexBuffer) {
93 SkDebugf("Could not allocate vertices\n");
94 return;
95 }
96
msarett10e3d9b2016-08-18 15:46:03 -070097 intptr_t verts = reinterpret_cast<intptr_t>(vertices);
bsalomona71b8982016-06-30 12:13:52 -070098 for (int i = 0; i < patchCnt; i++) {
msarett7fc08582016-08-18 14:29:22 -070099 const Patch& patch = fPatches[i];
msarett10e3d9b2016-08-18 15:46:03 -0700100
101 // Apply the view matrix here if it is scale-translate. Otherwise, we need to
102 // wait until we've created the dst rects.
103 bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
104 if (isScaleTranslate) {
105 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
106 }
joshualitt33a5fce2015-11-18 13:28:51 -0800107
108 SkRect srcR, dstR;
msarett10e3d9b2016-08-18 15:46:03 -0700109 intptr_t patchVerts = verts;
110 while (patch.fIter->next(&srcR, &dstR)) {
joshualitt33a5fce2015-11-18 13:28:51 -0800111 SkPoint* positions = reinterpret_cast<SkPoint*>(verts);
Brian Salomonfc527d22016-12-14 21:07:01 -0500112 positions->setRectFan(dstR.fLeft, dstR.fTop, dstR.fRight, dstR.fBottom,
113 vertexStride);
joshualitt33a5fce2015-11-18 13:28:51 -0800114
joshualitt33a5fce2015-11-18 13:28:51 -0800115 // Setup local coords
116 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor);
117 SkPoint* coords = reinterpret_cast<SkPoint*>(verts + kLocalOffset);
118 coords->setRectFan(srcR.fLeft, srcR.fTop, srcR.fRight, srcR.fBottom, vertexStride);
119
120 static const int kColorOffset = sizeof(SkPoint);
121 GrColor* vertColor = reinterpret_cast<GrColor*>(verts + kColorOffset);
122 for (int j = 0; j < 4; ++j) {
bsalomona71b8982016-06-30 12:13:52 -0700123 *vertColor = patch.fColor;
Brian Salomonfc527d22016-12-14 21:07:01 -0500124 vertColor = (GrColor*)((intptr_t)vertColor + vertexStride);
joshualitt33a5fce2015-11-18 13:28:51 -0800125 }
126 verts += kVertsPerRect * vertexStride;
127 }
msarett10e3d9b2016-08-18 15:46:03 -0700128
129 // If we didn't handle it above, apply the matrix here.
130 if (!isScaleTranslate) {
131 SkPoint* positions = reinterpret_cast<SkPoint*>(patchVerts);
Brian Salomonfc527d22016-12-14 21:07:01 -0500132 patch.fViewMatrix.mapPointsWithStride(
133 positions, vertexStride, kVertsPerRect * patch.fIter->numRectsToDraw());
msarett10e3d9b2016-08-18 15:46:03 -0700134 }
joshualitt33a5fce2015-11-18 13:28:51 -0800135 }
bungeman06ca8ec2016-06-09 08:01:03 -0700136 helper.recordDraw(target, gp.get());
joshualitt33a5fce2015-11-18 13:28:51 -0800137 }
138
Brian Salomon25a88092016-12-01 09:36:50 -0500139 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500140 NonAALatticeOp* that = t->cast<NonAALatticeOp>();
joshualitt33a5fce2015-11-18 13:28:51 -0800141 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
142 that->bounds(), caps)) {
143 return false;
144 }
145
146 SkASSERT(this->fImageWidth == that->fImageWidth &&
147 this->fImageHeight == that->fImageHeight);
148
Brian Salomonfc527d22016-12-14 21:07:01 -0500149 // In the event of two ops, one who can tweak, one who cannot, we just fall back to not
150 // tweaking.
Brian Salomon92aee3d2016-12-21 09:20:25 -0500151 if (fOptimizations.canTweakAlphaForCoverage() &&
152 !that->fOptimizations.canTweakAlphaForCoverage()) {
153 fOptimizations = that->fOptimizations;
joshualitt33a5fce2015-11-18 13:28:51 -0800154 }
155
msarett10e3d9b2016-08-18 15:46:03 -0700156 fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700157 this->joinBounds(*that);
joshualitt33a5fce2015-11-18 13:28:51 -0800158 return true;
159 }
160
bsalomona71b8982016-06-30 12:13:52 -0700161 struct Patch {
162 SkMatrix fViewMatrix;
msarett10e3d9b2016-08-18 15:46:03 -0700163 std::unique_ptr<SkLatticeIter> fIter;
bsalomona71b8982016-06-30 12:13:52 -0700164 SkRect fDst;
165 GrColor fColor;
166 };
167
Brian Salomon92aee3d2016-12-21 09:20:25 -0500168 GrPipelineOptimizations fOptimizations;
joshualitt33a5fce2015-11-18 13:28:51 -0800169 int fImageWidth;
170 int fImageHeight;
bsalomona71b8982016-06-30 12:13:52 -0700171 SkSTArray<1, Patch, true> fPatches;
joshualitt33a5fce2015-11-18 13:28:51 -0800172
Brian Salomondad29232016-12-01 16:40:24 -0500173 typedef GrMeshDrawOp INHERITED;
joshualitt33a5fce2015-11-18 13:28:51 -0800174};
175
Brian Salomonfc527d22016-12-14 21:07:01 -0500176namespace GrLatticeOp {
Brian Salomonf8334782017-01-03 09:42:58 -0500177std::unique_ptr<GrDrawOp> MakeNonAA(GrColor color, const SkMatrix& viewMatrix, int imageWidth,
178 int imageHeight, std::unique_ptr<SkLatticeIter> iter,
179 const SkRect& dst) {
180 return std::unique_ptr<GrDrawOp>(
Brian Salomonfc527d22016-12-14 21:07:01 -0500181 new NonAALatticeOp(color, viewMatrix, imageWidth, imageHeight, std::move(iter), dst));
joshualitt33a5fce2015-11-18 13:28:51 -0800182}
183};