blob: 522e775c3339d6579b7aa730d2799e070bb81300 [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
8#include "GrNinePatch.h"
9
10#include "GrBatchFlushState.h"
11#include "GrDefaultGeoProcFactory.h"
12#include "GrResourceProvider.h"
13#include "GrVertexBatch.h"
14#include "SkBitmap.h"
msarettc573a402016-08-02 08:05:56 -070015#include "SkLatticeIter.h"
joshualitt33a5fce2015-11-18 13:28:51 -080016#include "SkRect.h"
17
bungeman06ca8ec2016-06-09 08:01:03 -070018static sk_sp<GrGeometryProcessor> create_gp(bool readsCoverage) {
joshualitt33a5fce2015-11-18 13:28:51 -080019 using namespace GrDefaultGeoProcFactory;
20 Color color(Color::kAttribute_Type);
21 Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Type);
22 LocalCoords localCoords(LocalCoords::kHasExplicit_Type);
bungeman06ca8ec2016-06-09 08:01:03 -070023 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMatrix::I());
joshualitt33a5fce2015-11-18 13:28:51 -080024}
25
26class GrNonAANinePatchBatch : public GrVertexBatch {
27public:
28 DEFINE_BATCH_CLASS_ID
29
30 static const int kVertsPerRect = 4;
31 static const int kIndicesPerRect = 6;
joshualitt33a5fce2015-11-18 13:28:51 -080032
joshualitt33a5fce2015-11-18 13:28:51 -080033 GrNonAANinePatchBatch(GrColor color, const SkMatrix& viewMatrix, int imageWidth,
msarett10e3d9b2016-08-18 15:46:03 -070034 int imageHeight, std::unique_ptr<SkLatticeIter> iter, const SkRect &dst)
joshualitt33a5fce2015-11-18 13:28:51 -080035 : INHERITED(ClassID()) {
bsalomona71b8982016-06-30 12:13:52 -070036 Patch& patch = fPatches.push_back();
37 patch.fViewMatrix = viewMatrix;
38 patch.fColor = color;
msarett10e3d9b2016-08-18 15:46:03 -070039 patch.fIter = std::move(iter);
bsalomona71b8982016-06-30 12:13:52 -070040 patch.fDst = dst;
joshualitt33a5fce2015-11-18 13:28:51 -080041
42 fImageWidth = imageWidth;
43 fImageHeight = imageHeight;
44
45 // setup bounds
bsalomon88cf17d2016-07-08 06:40:56 -070046 this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
joshualitt33a5fce2015-11-18 13:28:51 -080047 }
48
robertphillips783a4da2015-11-19 14:00:02 -080049 const char* name() const override { return "NonAANinePatchBatch"; }
50
51 SkString dumpInfo() const override {
52 SkString str;
53
bsalomona71b8982016-06-30 12:13:52 -070054 for (int i = 0; i < fPatches.count(); ++i) {
msarett10e3d9b2016-08-18 15:46:03 -070055 str.appendf("%d: Color: 0x%08x Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
robertphillips783a4da2015-11-19 14:00:02 -080056 i,
bsalomona71b8982016-06-30 12:13:52 -070057 fPatches[i].fColor,
bsalomona71b8982016-06-30 12:13:52 -070058 fPatches[i].fDst.fLeft, fPatches[i].fDst.fTop,
59 fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom);
robertphillips783a4da2015-11-19 14:00:02 -080060 }
61
62 str.append(INHERITED::dumpInfo());
63 return str;
64 }
joshualitt33a5fce2015-11-18 13:28:51 -080065
halcanary9d524f22016-03-29 09:03:52 -070066 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -080067 GrInitInvariantOutput* coverage,
68 GrBatchToXPOverrides* overrides) const override {
69 color->setUnknownFourComponents();
70 coverage->setKnownSingleComponent(0xff);
joshualitt33a5fce2015-11-18 13:28:51 -080071 }
72
joshualitt33a5fce2015-11-18 13:28:51 -080073private:
joshualitt144c3c82015-11-30 12:30:13 -080074 void onPrepareDraws(Target* target) const override {
bungeman06ca8ec2016-06-09 08:01:03 -070075 sk_sp<GrGeometryProcessor> gp(create_gp(fOverrides.readsCoverage()));
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
cdalton397536c2016-03-25 12:15:03 -070088 SkAutoTUnref<const GrBuffer> indexBuffer(
joshualitt33a5fce2015-11-18 13:28:51 -080089 target->resourceProvider()->refQuadIndexBuffer());
90 InstancedHelper helper;
91 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride,
92 indexBuffer, kVertsPerRect,
msarett10e3d9b2016-08-18 15:46:03 -070093 kIndicesPerRect, numRects);
joshualitt33a5fce2015-11-18 13:28:51 -080094 if (!vertices || !indexBuffer) {
95 SkDebugf("Could not allocate vertices\n");
96 return;
97 }
98
msarett10e3d9b2016-08-18 15:46:03 -070099 intptr_t verts = reinterpret_cast<intptr_t>(vertices);
bsalomona71b8982016-06-30 12:13:52 -0700100 for (int i = 0; i < patchCnt; i++) {
msarett7fc08582016-08-18 14:29:22 -0700101 const Patch& patch = fPatches[i];
msarett10e3d9b2016-08-18 15:46:03 -0700102
103 // Apply the view matrix here if it is scale-translate. Otherwise, we need to
104 // wait until we've created the dst rects.
105 bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
106 if (isScaleTranslate) {
107 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
108 }
joshualitt33a5fce2015-11-18 13:28:51 -0800109
110 SkRect srcR, dstR;
msarett10e3d9b2016-08-18 15:46:03 -0700111 intptr_t patchVerts = verts;
112 while (patch.fIter->next(&srcR, &dstR)) {
joshualitt33a5fce2015-11-18 13:28:51 -0800113 SkPoint* positions = reinterpret_cast<SkPoint*>(verts);
joshualitt33a5fce2015-11-18 13:28:51 -0800114 positions->setRectFan(dstR.fLeft, dstR.fTop,
115 dstR.fRight, dstR.fBottom, vertexStride);
116
joshualitt33a5fce2015-11-18 13:28:51 -0800117 // Setup local coords
118 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor);
119 SkPoint* coords = reinterpret_cast<SkPoint*>(verts + kLocalOffset);
120 coords->setRectFan(srcR.fLeft, srcR.fTop, srcR.fRight, srcR.fBottom, vertexStride);
121
122 static const int kColorOffset = sizeof(SkPoint);
123 GrColor* vertColor = reinterpret_cast<GrColor*>(verts + kColorOffset);
124 for (int j = 0; j < 4; ++j) {
bsalomona71b8982016-06-30 12:13:52 -0700125 *vertColor = patch.fColor;
joshualitt33a5fce2015-11-18 13:28:51 -0800126 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride);
127 }
128 verts += kVertsPerRect * vertexStride;
129 }
msarett10e3d9b2016-08-18 15:46:03 -0700130
131 // If we didn't handle it above, apply the matrix here.
132 if (!isScaleTranslate) {
133 SkPoint* positions = reinterpret_cast<SkPoint*>(patchVerts);
134 patch.fViewMatrix.mapPointsWithStride(positions, vertexStride,
msarett0764efe2016-09-02 11:24:30 -0700135 kVertsPerRect * patch.fIter->numRectsToDraw());
msarett10e3d9b2016-08-18 15:46:03 -0700136 }
joshualitt33a5fce2015-11-18 13:28:51 -0800137 }
bungeman06ca8ec2016-06-09 08:01:03 -0700138 helper.recordDraw(target, gp.get());
joshualitt33a5fce2015-11-18 13:28:51 -0800139 }
140
ethannicholasff210322015-11-24 12:10:10 -0800141 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomona71b8982016-06-30 12:13:52 -0700142 overrides.getOverrideColorIfSet(&fPatches[0].fColor);
ethannicholasff210322015-11-24 12:10:10 -0800143 fOverrides = overrides;
joshualitt33a5fce2015-11-18 13:28:51 -0800144 }
145
146 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
147 GrNonAANinePatchBatch* that = t->cast<GrNonAANinePatchBatch>();
148 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
149 that->bounds(), caps)) {
150 return false;
151 }
152
153 SkASSERT(this->fImageWidth == that->fImageWidth &&
154 this->fImageHeight == that->fImageHeight);
155
156 // In the event of two batches, one who can tweak, one who cannot, we just fall back to
157 // not tweaking
ethannicholasff210322015-11-24 12:10:10 -0800158 if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakAlphaForCoverage()) {
159 fOverrides = that->fOverrides;
joshualitt33a5fce2015-11-18 13:28:51 -0800160 }
161
msarett10e3d9b2016-08-18 15:46:03 -0700162 fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700163 this->joinBounds(*that);
joshualitt33a5fce2015-11-18 13:28:51 -0800164 return true;
165 }
166
bsalomona71b8982016-06-30 12:13:52 -0700167 struct Patch {
168 SkMatrix fViewMatrix;
msarett10e3d9b2016-08-18 15:46:03 -0700169 std::unique_ptr<SkLatticeIter> fIter;
bsalomona71b8982016-06-30 12:13:52 -0700170 SkRect fDst;
171 GrColor fColor;
172 };
173
ethannicholasff210322015-11-24 12:10:10 -0800174 GrXPOverridesForBatch fOverrides;
joshualitt33a5fce2015-11-18 13:28:51 -0800175 int fImageWidth;
176 int fImageHeight;
bsalomona71b8982016-06-30 12:13:52 -0700177 SkSTArray<1, Patch, true> fPatches;
joshualitt33a5fce2015-11-18 13:28:51 -0800178
179 typedef GrVertexBatch INHERITED;
180};
181
182namespace GrNinePatch {
183GrDrawBatch* CreateNonAA(GrColor color, const SkMatrix& viewMatrix, int imageWidth, int imageHeight,
msarett10e3d9b2016-08-18 15:46:03 -0700184 std::unique_ptr<SkLatticeIter> iter, const SkRect& dst) {
185 return new GrNonAANinePatchBatch(color, viewMatrix, imageWidth, imageHeight, std::move(iter),
186 dst);
joshualitt33a5fce2015-11-18 13:28:51 -0800187}
188};