blob: a1b758409a18b2484bc9940455ab92750f869895 [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 Salomon3de0aee2017-01-29 09:34:17 -050020 return GrDefaultGeoProcFactory::Make(Color::kPremulGrColorAttribute_Type, Coverage::kSolid_Type,
Brian Salomon8c852be2017-01-04 10:44:42 -050021 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:
Brian Salomon5298dc82017-02-22 11:52:03 -050064 void getFragmentProcessorAnalysisInputs(FragmentProcessorAnalysisInputs* input) const override {
65 input->colorInput()->setToUnknown();
66 input->coverageInput()->setToSolidCoverage();
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);
Brian Salomon92aee3d2016-12-21 09:20:25 -050071 }
72
joshualitt144c3c82015-11-30 12:30:13 -080073 void onPrepareDraws(Target* target) const override {
Brian Salomon8c852be2017-01-04 10:44:42 -050074 sk_sp<GrGeometryProcessor> gp(create_gp());
joshualitt33a5fce2015-11-18 13:28:51 -080075 if (!gp) {
76 SkDebugf("Couldn't create GrGeometryProcessor\n");
77 return;
78 }
79
joshualitt33a5fce2015-11-18 13:28:51 -080080 size_t vertexStride = gp->getVertexStride();
bsalomona71b8982016-06-30 12:13:52 -070081 int patchCnt = fPatches.count();
msarett10e3d9b2016-08-18 15:46:03 -070082 int numRects = 0;
83 for (int i = 0; i < patchCnt; i++) {
msarett0764efe2016-09-02 11:24:30 -070084 numRects += fPatches[i].fIter->numRectsToDraw();
msarett10e3d9b2016-08-18 15:46:03 -070085 }
joshualitt33a5fce2015-11-18 13:28:51 -080086
Hal Canary144caf52016-11-07 17:57:18 -050087 sk_sp<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer());
joshualitt33a5fce2015-11-18 13:28:51 -080088 InstancedHelper helper;
89 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride,
Brian Salomonfc527d22016-12-14 21:07:01 -050090 indexBuffer.get(), kVertsPerRect, kIndicesPerRect, numRects);
joshualitt33a5fce2015-11-18 13:28:51 -080091 if (!vertices || !indexBuffer) {
92 SkDebugf("Could not allocate vertices\n");
93 return;
94 }
95
msarett10e3d9b2016-08-18 15:46:03 -070096 intptr_t verts = reinterpret_cast<intptr_t>(vertices);
bsalomona71b8982016-06-30 12:13:52 -070097 for (int i = 0; i < patchCnt; i++) {
msarett7fc08582016-08-18 14:29:22 -070098 const Patch& patch = fPatches[i];
msarett10e3d9b2016-08-18 15:46:03 -070099
100 // Apply the view matrix here if it is scale-translate. Otherwise, we need to
101 // wait until we've created the dst rects.
102 bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
103 if (isScaleTranslate) {
104 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
105 }
joshualitt33a5fce2015-11-18 13:28:51 -0800106
107 SkRect srcR, dstR;
msarett10e3d9b2016-08-18 15:46:03 -0700108 intptr_t patchVerts = verts;
109 while (patch.fIter->next(&srcR, &dstR)) {
joshualitt33a5fce2015-11-18 13:28:51 -0800110 SkPoint* positions = reinterpret_cast<SkPoint*>(verts);
Brian Salomonfc527d22016-12-14 21:07:01 -0500111 positions->setRectFan(dstR.fLeft, dstR.fTop, dstR.fRight, dstR.fBottom,
112 vertexStride);
joshualitt33a5fce2015-11-18 13:28:51 -0800113
joshualitt33a5fce2015-11-18 13:28:51 -0800114 // Setup local coords
115 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor);
116 SkPoint* coords = reinterpret_cast<SkPoint*>(verts + kLocalOffset);
117 coords->setRectFan(srcR.fLeft, srcR.fTop, srcR.fRight, srcR.fBottom, vertexStride);
118
119 static const int kColorOffset = sizeof(SkPoint);
120 GrColor* vertColor = reinterpret_cast<GrColor*>(verts + kColorOffset);
121 for (int j = 0; j < 4; ++j) {
bsalomona71b8982016-06-30 12:13:52 -0700122 *vertColor = patch.fColor;
Brian Salomonfc527d22016-12-14 21:07:01 -0500123 vertColor = (GrColor*)((intptr_t)vertColor + vertexStride);
joshualitt33a5fce2015-11-18 13:28:51 -0800124 }
125 verts += kVertsPerRect * vertexStride;
126 }
msarett10e3d9b2016-08-18 15:46:03 -0700127
128 // If we didn't handle it above, apply the matrix here.
129 if (!isScaleTranslate) {
130 SkPoint* positions = reinterpret_cast<SkPoint*>(patchVerts);
Brian Salomonfc527d22016-12-14 21:07:01 -0500131 patch.fViewMatrix.mapPointsWithStride(
132 positions, vertexStride, kVertsPerRect * patch.fIter->numRectsToDraw());
msarett10e3d9b2016-08-18 15:46:03 -0700133 }
joshualitt33a5fce2015-11-18 13:28:51 -0800134 }
bungeman06ca8ec2016-06-09 08:01:03 -0700135 helper.recordDraw(target, gp.get());
joshualitt33a5fce2015-11-18 13:28:51 -0800136 }
137
Brian Salomon25a88092016-12-01 09:36:50 -0500138 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500139 NonAALatticeOp* that = t->cast<NonAALatticeOp>();
joshualitt33a5fce2015-11-18 13:28:51 -0800140 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
141 that->bounds(), caps)) {
142 return false;
143 }
144
145 SkASSERT(this->fImageWidth == that->fImageWidth &&
146 this->fImageHeight == that->fImageHeight);
147
msarett10e3d9b2016-08-18 15:46:03 -0700148 fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700149 this->joinBounds(*that);
joshualitt33a5fce2015-11-18 13:28:51 -0800150 return true;
151 }
152
bsalomona71b8982016-06-30 12:13:52 -0700153 struct Patch {
154 SkMatrix fViewMatrix;
msarett10e3d9b2016-08-18 15:46:03 -0700155 std::unique_ptr<SkLatticeIter> fIter;
bsalomona71b8982016-06-30 12:13:52 -0700156 SkRect fDst;
157 GrColor fColor;
158 };
159
joshualitt33a5fce2015-11-18 13:28:51 -0800160 int fImageWidth;
161 int fImageHeight;
bsalomona71b8982016-06-30 12:13:52 -0700162 SkSTArray<1, Patch, true> fPatches;
joshualitt33a5fce2015-11-18 13:28:51 -0800163
Brian Salomondad29232016-12-01 16:40:24 -0500164 typedef GrMeshDrawOp INHERITED;
joshualitt33a5fce2015-11-18 13:28:51 -0800165};
166
Brian Salomonfc527d22016-12-14 21:07:01 -0500167namespace GrLatticeOp {
Brian Salomon649a3412017-03-09 13:50:43 -0500168std::unique_ptr<GrMeshDrawOp> MakeNonAA(GrColor color, const SkMatrix& viewMatrix, int imageWidth,
169 int imageHeight, std::unique_ptr<SkLatticeIter> iter,
170 const SkRect& dst) {
171 return std::unique_ptr<GrMeshDrawOp>(
Brian Salomonfc527d22016-12-14 21:07:01 -0500172 new NonAALatticeOp(color, viewMatrix, imageWidth, imageHeight, std::move(iter), dst));
joshualitt33a5fce2015-11-18 13:28:51 -0800173}
174};