OLD | NEW |
(Empty) | |
| 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_Ty
pe); |
| 22 LocalCoords localCoords(LocalCoords::kHasExplicit_Type); |
| 23 return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, SkMatri
x::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 imageWi
dth, |
| 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 |
| 57 const char* name() const override { return "GrNonAANinePatchBatch"; } |
| 58 |
| 59 void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
| 60 out->setUnknownFourComponents(); |
| 61 } |
| 62 |
| 63 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
| 64 out->setKnownSingleComponent(0xff); |
| 65 } |
| 66 |
| 67 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 68 |
| 69 private: |
| 70 void onPrepareDraws(Target* target) override { |
| 71 SkAutoTUnref<const GrGeometryProcessor> gp(create_gp(fOpts.readsCoverage
())); |
| 72 if (!gp) { |
| 73 SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 74 return; |
| 75 } |
| 76 |
| 77 target->initDraw(gp, this->pipeline()); |
| 78 |
| 79 size_t vertexStride = gp->getVertexStride(); |
| 80 int instanceCount = fGeoData.count(); |
| 81 |
| 82 SkAutoTUnref<const GrIndexBuffer> indexBuffer( |
| 83 target->resourceProvider()->refQuadIndexBuffer()); |
| 84 InstancedHelper helper; |
| 85 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexS
tride, |
| 86 indexBuffer, kVertsPerRect, |
| 87 kIndicesPerRect, instanceCount * kRectsPerI
nstance); |
| 88 if (!vertices || !indexBuffer) { |
| 89 SkDebugf("Could not allocate vertices\n"); |
| 90 return; |
| 91 } |
| 92 |
| 93 for (int i = 0; i < instanceCount; i++) { |
| 94 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + |
| 95 i * kRectsPerInstance * kVertsPerRect * vertexStrid
e; |
| 96 |
| 97 Geometry& geo = fGeoData[i]; |
| 98 SkNinePatchIter iter(fImageWidth, fImageHeight, geo.fCenter, geo.fDs
t); |
| 99 |
| 100 SkRect srcR, dstR; |
| 101 while (iter.next(&srcR, &dstR)) { |
| 102 SkPoint* positions = reinterpret_cast<SkPoint*>(verts); |
| 103 |
| 104 positions->setRectFan(dstR.fLeft, dstR.fTop, |
| 105 dstR.fRight, dstR.fBottom, vertexStride); |
| 106 |
| 107 SkASSERT(!geo.fViewMatrix.hasPerspective()); |
| 108 geo.fViewMatrix.mapPointsWithStride(positions, vertexStride, kVe
rtsPerRect); |
| 109 |
| 110 // Setup local coords |
| 111 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor
); |
| 112 SkPoint* coords = reinterpret_cast<SkPoint*>(verts + kLocalOffse
t); |
| 113 coords->setRectFan(srcR.fLeft, srcR.fTop, srcR.fRight, srcR.fBot
tom, vertexStride); |
| 114 |
| 115 static const int kColorOffset = sizeof(SkPoint); |
| 116 GrColor* vertColor = reinterpret_cast<GrColor*>(verts + kColorOf
fset); |
| 117 for (int j = 0; j < 4; ++j) { |
| 118 *vertColor = geo.fColor; |
| 119 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride)
; |
| 120 } |
| 121 verts += kVertsPerRect * vertexStride; |
| 122 } |
| 123 } |
| 124 helper.recordDraw(target); |
| 125 } |
| 126 |
| 127 void initBatchTracker(const GrPipelineOptimizations& opt) override { |
| 128 opt.getOverrideColorIfSet(&fGeoData[0].fColor); |
| 129 fOpts = opt; |
| 130 } |
| 131 |
| 132 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
| 133 GrNonAANinePatchBatch* that = t->cast<GrNonAANinePatchBatch>(); |
| 134 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pi
peline(), |
| 135 that->bounds(), caps)) { |
| 136 return false; |
| 137 } |
| 138 |
| 139 SkASSERT(this->fImageWidth == that->fImageWidth && |
| 140 this->fImageHeight == that->fImageHeight); |
| 141 |
| 142 // In the event of two batches, one who can tweak, one who cannot, we ju
st fall back to |
| 143 // not tweaking |
| 144 if (fOpts.canTweakAlphaForCoverage() && !that->fOpts.canTweakAlphaForCov
erage()) { |
| 145 fOpts = that->fOpts; |
| 146 } |
| 147 |
| 148 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin())
; |
| 149 this->joinBounds(that->bounds()); |
| 150 return true; |
| 151 } |
| 152 |
| 153 GrPipelineOptimizations fOpts; |
| 154 int fImageWidth; |
| 155 int fImageHeight; |
| 156 SkSTArray<1, Geometry, true> fGeoData; |
| 157 |
| 158 typedef GrVertexBatch INHERITED; |
| 159 }; |
| 160 |
| 161 namespace GrNinePatch { |
| 162 GrDrawBatch* CreateNonAA(GrColor color, const SkMatrix& viewMatrix, int imageWid
th, int imageHeight, |
| 163 const SkIRect& center, const SkRect& dst) { |
| 164 return new GrNonAANinePatchBatch(color, viewMatrix, imageWidth, imageHeight,
center, dst); |
| 165 } |
| 166 }; |
OLD | NEW |