joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 1 | /* |
| 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" |
| 15 | #include "SkNinePatchIter.h" |
| 16 | #include "SkRect.h" |
| 17 | |
| 18 | static const GrGeometryProcessor* create_gp(bool readsCoverage) { |
| 19 | 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); |
| 23 | return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, SkMatrix::I()); |
| 24 | } |
| 25 | |
| 26 | class GrNonAANinePatchBatch : public GrVertexBatch { |
| 27 | public: |
| 28 | DEFINE_BATCH_CLASS_ID |
| 29 | |
| 30 | static const int kVertsPerRect = 4; |
| 31 | static const int kIndicesPerRect = 6; |
| 32 | static const int kRectsPerInstance = 9; // We could skip empty rects |
| 33 | |
| 34 | struct Geometry { |
| 35 | SkMatrix fViewMatrix; |
| 36 | SkIRect fCenter; |
| 37 | SkRect fDst; |
| 38 | GrColor fColor; |
| 39 | }; |
| 40 | |
| 41 | GrNonAANinePatchBatch(GrColor color, const SkMatrix& viewMatrix, int imageWidth, |
| 42 | int imageHeight, const SkIRect& center, const SkRect &dst) |
| 43 | : INHERITED(ClassID()) { |
| 44 | Geometry& geo = fGeoData.push_back(); |
| 45 | geo.fViewMatrix = viewMatrix; |
| 46 | geo.fColor = color; |
| 47 | geo.fCenter = center; |
| 48 | geo.fDst = dst; |
| 49 | |
| 50 | fImageWidth = imageWidth; |
| 51 | fImageHeight = imageHeight; |
| 52 | |
| 53 | // setup bounds |
| 54 | geo.fViewMatrix.mapRect(&fBounds, geo.fDst); |
| 55 | } |
| 56 | |
robertphillips | 783a4da | 2015-11-19 14:00:02 -0800 | [diff] [blame] | 57 | const char* name() const override { return "NonAANinePatchBatch"; } |
| 58 | |
| 59 | SkString dumpInfo() const override { |
| 60 | SkString str; |
| 61 | |
| 62 | for (int i = 0; i < fGeoData.count(); ++i) { |
| 63 | str.appendf("%d: Color: 0x%08x Center [L: %d, T: %d, R: %d, B: %d], " |
| 64 | "Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", |
| 65 | i, |
| 66 | fGeoData[i].fColor, |
| 67 | fGeoData[i].fCenter.fLeft, fGeoData[i].fCenter.fTop, |
| 68 | fGeoData[i].fCenter.fRight, fGeoData[i].fCenter.fBottom, |
| 69 | fGeoData[i].fDst.fLeft, fGeoData[i].fDst.fTop, |
| 70 | fGeoData[i].fDst.fRight, fGeoData[i].fDst.fBottom); |
| 71 | } |
| 72 | |
| 73 | str.append(INHERITED::dumpInfo()); |
| 74 | return str; |
| 75 | } |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 76 | |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 77 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
| 78 | GrInitInvariantOutput* coverage, |
| 79 | GrBatchToXPOverrides* overrides) const override { |
| 80 | color->setUnknownFourComponents(); |
| 81 | coverage->setKnownSingleComponent(0xff); |
| 82 | overrides->fUsePLSDstRead = false; |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 86 | |
| 87 | private: |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame^] | 88 | void onPrepareDraws(Target* target) const override { |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 89 | SkAutoTUnref<const GrGeometryProcessor> gp(create_gp(fOverrides.readsCoverage())); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 90 | if (!gp) { |
| 91 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | target->initDraw(gp, this->pipeline()); |
| 96 | |
| 97 | size_t vertexStride = gp->getVertexStride(); |
| 98 | int instanceCount = fGeoData.count(); |
| 99 | |
| 100 | SkAutoTUnref<const GrIndexBuffer> indexBuffer( |
| 101 | target->resourceProvider()->refQuadIndexBuffer()); |
| 102 | InstancedHelper helper; |
| 103 | void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride, |
| 104 | indexBuffer, kVertsPerRect, |
| 105 | kIndicesPerRect, instanceCount * kRectsPerInstance); |
| 106 | if (!vertices || !indexBuffer) { |
| 107 | SkDebugf("Could not allocate vertices\n"); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | for (int i = 0; i < instanceCount; i++) { |
| 112 | intptr_t verts = reinterpret_cast<intptr_t>(vertices) + |
| 113 | i * kRectsPerInstance * kVertsPerRect * vertexStride; |
| 114 | |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame^] | 115 | const Geometry& geo = fGeoData[i]; |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 116 | SkNinePatchIter iter(fImageWidth, fImageHeight, geo.fCenter, geo.fDst); |
| 117 | |
| 118 | SkRect srcR, dstR; |
| 119 | while (iter.next(&srcR, &dstR)) { |
| 120 | SkPoint* positions = reinterpret_cast<SkPoint*>(verts); |
| 121 | |
| 122 | positions->setRectFan(dstR.fLeft, dstR.fTop, |
| 123 | dstR.fRight, dstR.fBottom, vertexStride); |
| 124 | |
| 125 | SkASSERT(!geo.fViewMatrix.hasPerspective()); |
| 126 | geo.fViewMatrix.mapPointsWithStride(positions, vertexStride, kVertsPerRect); |
| 127 | |
| 128 | // Setup local coords |
| 129 | static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); |
| 130 | SkPoint* coords = reinterpret_cast<SkPoint*>(verts + kLocalOffset); |
| 131 | coords->setRectFan(srcR.fLeft, srcR.fTop, srcR.fRight, srcR.fBottom, vertexStride); |
| 132 | |
| 133 | static const int kColorOffset = sizeof(SkPoint); |
| 134 | GrColor* vertColor = reinterpret_cast<GrColor*>(verts + kColorOffset); |
| 135 | for (int j = 0; j < 4; ++j) { |
| 136 | *vertColor = geo.fColor; |
| 137 | vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride); |
| 138 | } |
| 139 | verts += kVertsPerRect * vertexStride; |
| 140 | } |
| 141 | } |
| 142 | helper.recordDraw(target); |
| 143 | } |
| 144 | |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 145 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
| 146 | overrides.getOverrideColorIfSet(&fGeoData[0].fColor); |
| 147 | fOverrides = overrides; |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
| 151 | GrNonAANinePatchBatch* that = t->cast<GrNonAANinePatchBatch>(); |
| 152 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 153 | that->bounds(), caps)) { |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | SkASSERT(this->fImageWidth == that->fImageWidth && |
| 158 | this->fImageHeight == that->fImageHeight); |
| 159 | |
| 160 | // In the event of two batches, one who can tweak, one who cannot, we just fall back to |
| 161 | // not tweaking |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 162 | if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakAlphaForCoverage()) { |
| 163 | fOverrides = that->fOverrides; |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
| 167 | this->joinBounds(that->bounds()); |
| 168 | return true; |
| 169 | } |
| 170 | |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 171 | GrXPOverridesForBatch fOverrides; |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 172 | int fImageWidth; |
| 173 | int fImageHeight; |
| 174 | SkSTArray<1, Geometry, true> fGeoData; |
| 175 | |
| 176 | typedef GrVertexBatch INHERITED; |
| 177 | }; |
| 178 | |
| 179 | namespace GrNinePatch { |
| 180 | GrDrawBatch* CreateNonAA(GrColor color, const SkMatrix& viewMatrix, int imageWidth, int imageHeight, |
| 181 | const SkIRect& center, const SkRect& dst) { |
| 182 | return new GrNonAANinePatchBatch(color, viewMatrix, imageWidth, imageHeight, center, dst); |
| 183 | } |
| 184 | }; |