blob: 4b8f5e3c73cc718e73da88fd255343cc77aae9b4 [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 Salomond3ccb0a2017-04-03 10:38:00 -040024class NonAALatticeOp final : public GrLegacyMeshDrawOp {
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 Salomona811b122017-03-30 08:21:32 -040064 void getProcessorAnalysisInputs(GrProcessorAnalysisColor* color,
65 GrProcessorAnalysisCoverage* coverage) const override {
Brian Salomonc0b642c2017-03-27 13:09:36 -040066 color->setToUnknown();
Brian Salomona811b122017-03-30 08:21:32 -040067 *coverage = GrProcessorAnalysisCoverage::kNone;
joshualitt33a5fce2015-11-18 13:28:51 -080068 }
69
Brian Salomone7d30482017-03-29 12:09:15 -040070 void applyPipelineOptimizations(const PipelineOptimizations& analysioptimizations) override {
Brian Salomon92aee3d2016-12-21 09:20:25 -050071 analysioptimizations.getOverrideColorIfSet(&fPatches[0].fColor);
Brian Salomon92aee3d2016-12-21 09:20:25 -050072 }
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 }
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400136 helper.recordDraw(target, gp.get(), this->pipeline());
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
msarett10e3d9b2016-08-18 15:46:03 -0700149 fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700150 this->joinBounds(*that);
joshualitt33a5fce2015-11-18 13:28:51 -0800151 return true;
152 }
153
bsalomona71b8982016-06-30 12:13:52 -0700154 struct Patch {
155 SkMatrix fViewMatrix;
msarett10e3d9b2016-08-18 15:46:03 -0700156 std::unique_ptr<SkLatticeIter> fIter;
bsalomona71b8982016-06-30 12:13:52 -0700157 SkRect fDst;
158 GrColor fColor;
159 };
160
joshualitt33a5fce2015-11-18 13:28:51 -0800161 int fImageWidth;
162 int fImageHeight;
bsalomona71b8982016-06-30 12:13:52 -0700163 SkSTArray<1, Patch, true> fPatches;
joshualitt33a5fce2015-11-18 13:28:51 -0800164
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400165 typedef GrLegacyMeshDrawOp INHERITED;
joshualitt33a5fce2015-11-18 13:28:51 -0800166};
167
Brian Salomonfc527d22016-12-14 21:07:01 -0500168namespace GrLatticeOp {
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400169std::unique_ptr<GrLegacyMeshDrawOp> MakeNonAA(GrColor color, const SkMatrix& viewMatrix,
170 int imageWidth, int imageHeight,
171 std::unique_ptr<SkLatticeIter> iter,
172 const SkRect& dst) {
173 return std::unique_ptr<GrLegacyMeshDrawOp>(
Brian Salomonfc527d22016-12-14 21:07:01 -0500174 new NonAALatticeOp(color, viewMatrix, imageWidth, imageHeight, std::move(iter), dst));
joshualitt33a5fce2015-11-18 13:28:51 -0800175}
176};