Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrNinePatch.h" | 8 #include "GrNinePatch.h" |
| 9 | 9 |
| 10 #include "GrBatchFlushState.h" | 10 #include "GrBatchFlushState.h" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 LocalCoords localCoords(LocalCoords::kHasExplicit_Type); | 22 LocalCoords localCoords(LocalCoords::kHasExplicit_Type); |
| 23 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMatrix: :I()); | 23 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMatrix: :I()); |
| 24 } | 24 } |
| 25 | 25 |
| 26 class GrNonAANinePatchBatch : public GrVertexBatch { | 26 class GrNonAANinePatchBatch : public GrVertexBatch { |
| 27 public: | 27 public: |
| 28 DEFINE_BATCH_CLASS_ID | 28 DEFINE_BATCH_CLASS_ID |
| 29 | 29 |
| 30 static const int kVertsPerRect = 4; | 30 static const int kVertsPerRect = 4; |
| 31 static const int kIndicesPerRect = 6; | 31 static const int kIndicesPerRect = 6; |
| 32 static const int kRectsPerInstance = 9; // We could skip empty rects | |
| 33 | 32 |
| 34 GrNonAANinePatchBatch(GrColor color, const SkMatrix& viewMatrix, int imageWi dth, | 33 GrNonAANinePatchBatch(GrColor color, const SkMatrix& viewMatrix, int imageWi dth, |
| 35 int imageHeight, const SkIRect& center, const SkRect & dst) | 34 int imageHeight, std::unique_ptr<SkLatticeIter> iter, const SkRect &dst) |
| 36 : INHERITED(ClassID()) { | 35 : INHERITED(ClassID()) { |
| 37 Patch& patch = fPatches.push_back(); | 36 Patch& patch = fPatches.push_back(); |
| 38 patch.fViewMatrix = viewMatrix; | 37 patch.fViewMatrix = viewMatrix; |
| 39 patch.fColor = color; | 38 patch.fColor = color; |
| 40 patch.fCenter = center; | 39 patch.fIter = std::move(iter); |
| 41 patch.fDst = dst; | 40 patch.fDst = dst; |
| 42 | 41 |
| 43 fImageWidth = imageWidth; | 42 fImageWidth = imageWidth; |
| 44 fImageHeight = imageHeight; | 43 fImageHeight = imageHeight; |
| 45 | 44 |
| 46 // setup bounds | 45 // setup bounds |
| 47 this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsZe roArea::kNo); | 46 this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsZe roArea::kNo); |
| 48 } | 47 } |
| 49 | 48 |
| 50 const char* name() const override { return "NonAANinePatchBatch"; } | 49 const char* name() const override { return "NonAANinePatchBatch"; } |
| 51 | 50 |
| 52 SkString dumpInfo() const override { | 51 SkString dumpInfo() const override { |
| 53 SkString str; | 52 SkString str; |
| 54 | 53 |
| 55 for (int i = 0; i < fPatches.count(); ++i) { | 54 for (int i = 0; i < fPatches.count(); ++i) { |
| 56 str.appendf("%d: Color: 0x%08x Center [L: %d, T: %d, R: %d, B: %d], " | 55 str.appendf("%d: Color: 0x%08x Dst [L: %.2f, T: %.2f, R: %.2f, B: %. 2f]\n", |
| 57 "Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", | |
| 58 i, | 56 i, |
| 59 fPatches[i].fColor, | 57 fPatches[i].fColor, |
| 60 fPatches[i].fCenter.fLeft, fPatches[i].fCenter.fTop, | |
| 61 fPatches[i].fCenter.fRight, fPatches[i].fCenter.fBottom, | |
| 62 fPatches[i].fDst.fLeft, fPatches[i].fDst.fTop, | 58 fPatches[i].fDst.fLeft, fPatches[i].fDst.fTop, |
| 63 fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom); | 59 fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom); |
| 64 } | 60 } |
| 65 | 61 |
| 66 str.append(INHERITED::dumpInfo()); | 62 str.append(INHERITED::dumpInfo()); |
| 67 return str; | 63 return str; |
| 68 } | 64 } |
| 69 | 65 |
| 70 void computePipelineOptimizations(GrInitInvariantOutput* color, | 66 void computePipelineOptimizations(GrInitInvariantOutput* color, |
| 71 GrInitInvariantOutput* coverage, | 67 GrInitInvariantOutput* coverage, |
| 72 GrBatchToXPOverrides* overrides) const ove rride { | 68 GrBatchToXPOverrides* overrides) const ove rride { |
| 73 color->setUnknownFourComponents(); | 69 color->setUnknownFourComponents(); |
| 74 coverage->setKnownSingleComponent(0xff); | 70 coverage->setKnownSingleComponent(0xff); |
| 75 } | 71 } |
| 76 | 72 |
| 77 private: | 73 private: |
| 78 void onPrepareDraws(Target* target) const override { | 74 void onPrepareDraws(Target* target) const override { |
| 79 sk_sp<GrGeometryProcessor> gp(create_gp(fOverrides.readsCoverage())); | 75 sk_sp<GrGeometryProcessor> gp(create_gp(fOverrides.readsCoverage())); |
| 80 if (!gp) { | 76 if (!gp) { |
| 81 SkDebugf("Couldn't create GrGeometryProcessor\n"); | 77 SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 82 return; | 78 return; |
| 83 } | 79 } |
| 84 | 80 |
| 85 size_t vertexStride = gp->getVertexStride(); | 81 size_t vertexStride = gp->getVertexStride(); |
| 86 int patchCnt = fPatches.count(); | 82 int patchCnt = fPatches.count(); |
| 83 int numRects = 0; | |
| 84 for (int i = 0; i < patchCnt; i++) { | |
| 85 numRects += fPatches[i].fIter->numRects(); | |
| 86 } | |
| 87 | 87 |
| 88 SkAutoTUnref<const GrBuffer> indexBuffer( | 88 SkAutoTUnref<const GrBuffer> indexBuffer( |
| 89 target->resourceProvider()->refQuadIndexBuffer()); | 89 target->resourceProvider()->refQuadIndexBuffer()); |
| 90 InstancedHelper helper; | 90 InstancedHelper helper; |
| 91 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexS tride, | 91 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexS tride, |
| 92 indexBuffer, kVertsPerRect, | 92 indexBuffer, kVertsPerRect, |
| 93 kIndicesPerRect, patchCnt * kRectsPerInstan ce); | 93 kIndicesPerRect, patchCnt * numRects); |
| 94 if (!vertices || !indexBuffer) { | 94 if (!vertices || !indexBuffer) { |
| 95 SkDebugf("Could not allocate vertices\n"); | 95 SkDebugf("Could not allocate vertices\n"); |
| 96 return; | 96 return; |
| 97 } | 97 } |
| 98 | 98 |
| 99 intptr_t verts = reinterpret_cast<intptr_t>(vertices); | |
| 99 for (int i = 0; i < patchCnt; i++) { | 100 for (int i = 0; i < patchCnt; i++) { |
| 100 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + | |
| 101 i * kRectsPerInstance * kVertsPerRect * vertexStrid e; | |
| 102 | |
| 103 const Patch& patch = fPatches[i]; | 101 const Patch& patch = fPatches[i]; |
| 104 SkLatticeIter iter(fImageWidth, fImageHeight, patch.fCenter, patch.f Dst); | |
| 105 | |
| 106 SkRect srcR, dstR; | 102 SkRect srcR, dstR; |
| 107 while (iter.next(&srcR, &dstR)) { | 103 while (patch.fIter->next(&srcR, &dstR)) { |
| 108 SkPoint* positions = reinterpret_cast<SkPoint*>(verts); | 104 SkPoint* positions = reinterpret_cast<SkPoint*>(verts); |
| 109 | 105 |
| 110 positions->setRectFan(dstR.fLeft, dstR.fTop, | 106 positions->setRectFan(dstR.fLeft, dstR.fTop, |
| 111 dstR.fRight, dstR.fBottom, vertexStride); | 107 dstR.fRight, dstR.fBottom, vertexStride); |
| 112 | 108 |
| 113 SkASSERT(!patch.fViewMatrix.hasPerspective()); | 109 SkASSERT(!patch.fViewMatrix.hasPerspective()); |
| 114 patch.fViewMatrix.mapPointsWithStride(positions, vertexStride, k VertsPerRect); | 110 patch.fViewMatrix.mapPointsWithStride(positions, vertexStride, k VertsPerRect); |
|
bsalomon
2016/08/18 15:40:26
I wonder if we should lift this out of the loop, s
msarett
2016/08/18 17:49:20
Good idea - I think this is a nice opt for the sca
| |
| 115 | 111 |
| 116 // Setup local coords | 112 // Setup local coords |
| 117 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor ); | 113 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor ); |
| 118 SkPoint* coords = reinterpret_cast<SkPoint*>(verts + kLocalOffse t); | 114 SkPoint* coords = reinterpret_cast<SkPoint*>(verts + kLocalOffse t); |
| 119 coords->setRectFan(srcR.fLeft, srcR.fTop, srcR.fRight, srcR.fBot tom, vertexStride); | 115 coords->setRectFan(srcR.fLeft, srcR.fTop, srcR.fRight, srcR.fBot tom, vertexStride); |
| 120 | 116 |
| 121 static const int kColorOffset = sizeof(SkPoint); | 117 static const int kColorOffset = sizeof(SkPoint); |
| 122 GrColor* vertColor = reinterpret_cast<GrColor*>(verts + kColorOf fset); | 118 GrColor* vertColor = reinterpret_cast<GrColor*>(verts + kColorOf fset); |
| 123 for (int j = 0; j < 4; ++j) { | 119 for (int j = 0; j < 4; ++j) { |
| 124 *vertColor = patch.fColor; | 120 *vertColor = patch.fColor; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 144 | 140 |
| 145 SkASSERT(this->fImageWidth == that->fImageWidth && | 141 SkASSERT(this->fImageWidth == that->fImageWidth && |
| 146 this->fImageHeight == that->fImageHeight); | 142 this->fImageHeight == that->fImageHeight); |
| 147 | 143 |
| 148 // In the event of two batches, one who can tweak, one who cannot, we ju st fall back to | 144 // In the event of two batches, one who can tweak, one who cannot, we ju st fall back to |
| 149 // not tweaking | 145 // not tweaking |
| 150 if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakA lphaForCoverage()) { | 146 if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakA lphaForCoverage()) { |
| 151 fOverrides = that->fOverrides; | 147 fOverrides = that->fOverrides; |
| 152 } | 148 } |
| 153 | 149 |
| 154 fPatches.push_back_n(that->fPatches.count(), that->fPatches.begin()); | 150 for (int i = 0; i < that->fPatches.count(); i++) { |
| 151 fPatches.push_back(std::move(that->fPatches[i])); | |
|
bsalomon
2016/08/18 15:40:26
Should we have a bulk mover? move_back_n(T*, int)
msarett
2016/08/18 17:49:20
Yes! I've added one.
| |
| 152 } | |
| 155 this->joinBounds(*that); | 153 this->joinBounds(*that); |
| 156 return true; | 154 return true; |
| 157 } | 155 } |
| 158 | 156 |
| 159 struct Patch { | 157 struct Patch { |
| 160 SkMatrix fViewMatrix; | 158 SkMatrix fViewMatrix; |
| 161 SkIRect fCenter; | 159 std::unique_ptr<SkLatticeIter> fIter; |
| 162 SkRect fDst; | 160 SkRect fDst; |
| 163 GrColor fColor; | 161 GrColor fColor; |
| 164 }; | 162 }; |
| 165 | 163 |
| 166 GrXPOverridesForBatch fOverrides; | 164 GrXPOverridesForBatch fOverrides; |
| 167 int fImageWidth; | 165 int fImageWidth; |
| 168 int fImageHeight; | 166 int fImageHeight; |
| 169 SkSTArray<1, Patch, true> fPatches; | 167 SkSTArray<1, Patch, true> fPatches; |
| 170 | 168 |
| 171 typedef GrVertexBatch INHERITED; | 169 typedef GrVertexBatch INHERITED; |
| 172 }; | 170 }; |
| 173 | 171 |
| 174 namespace GrNinePatch { | 172 namespace GrNinePatch { |
| 175 GrDrawBatch* CreateNonAA(GrColor color, const SkMatrix& viewMatrix, int imageWid th, int imageHeight, | 173 GrDrawBatch* CreateNonAA(GrColor color, const SkMatrix& viewMatrix, int imageWid th, int imageHeight, |
| 176 const SkIRect& center, const SkRect& dst) { | 174 std::unique_ptr<SkLatticeIter> iter, const SkRect& dst) { |
| 177 return new GrNonAANinePatchBatch(color, viewMatrix, imageWidth, imageHeight, center, dst); | 175 return new GrNonAANinePatchBatch(color, viewMatrix, imageWidth, imageHeight, std::move(iter), |
| 176 dst); | |
| 178 } | 177 } |
| 179 }; | 178 }; |
| OLD | NEW |