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, 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 const Patch& patch = fPatches[i]; |
101 i * kRectsPerInstance * kVertsPerRect * vertexStrid
e; | |
102 | 102 |
103 const Patch& patch = fPatches[i]; | 103 // Apply the view matrix here if it is scale-translate. Otherwise,
we need to |
104 SkLatticeIter iter(fImageWidth, fImageHeight, patch.fCenter, patch.f
Dst); | 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 } |
105 | 109 |
106 SkRect srcR, dstR; | 110 SkRect srcR, dstR; |
107 while (iter.next(&srcR, &dstR)) { | 111 intptr_t patchVerts = verts; |
| 112 while (patch.fIter->next(&srcR, &dstR)) { |
108 SkPoint* positions = reinterpret_cast<SkPoint*>(verts); | 113 SkPoint* positions = reinterpret_cast<SkPoint*>(verts); |
109 | |
110 positions->setRectFan(dstR.fLeft, dstR.fTop, | 114 positions->setRectFan(dstR.fLeft, dstR.fTop, |
111 dstR.fRight, dstR.fBottom, vertexStride); | 115 dstR.fRight, dstR.fBottom, vertexStride); |
112 | 116 |
113 SkASSERT(!patch.fViewMatrix.hasPerspective()); | |
114 patch.fViewMatrix.mapPointsWithStride(positions, vertexStride, k
VertsPerRect); | |
115 | |
116 // Setup local coords | 117 // Setup local coords |
117 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor
); | 118 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor
); |
118 SkPoint* coords = reinterpret_cast<SkPoint*>(verts + kLocalOffse
t); | 119 SkPoint* coords = reinterpret_cast<SkPoint*>(verts + kLocalOffse
t); |
119 coords->setRectFan(srcR.fLeft, srcR.fTop, srcR.fRight, srcR.fBot
tom, vertexStride); | 120 coords->setRectFan(srcR.fLeft, srcR.fTop, srcR.fRight, srcR.fBot
tom, vertexStride); |
120 | 121 |
121 static const int kColorOffset = sizeof(SkPoint); | 122 static const int kColorOffset = sizeof(SkPoint); |
122 GrColor* vertColor = reinterpret_cast<GrColor*>(verts + kColorOf
fset); | 123 GrColor* vertColor = reinterpret_cast<GrColor*>(verts + kColorOf
fset); |
123 for (int j = 0; j < 4; ++j) { | 124 for (int j = 0; j < 4; ++j) { |
124 *vertColor = patch.fColor; | 125 *vertColor = patch.fColor; |
125 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride)
; | 126 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride)
; |
126 } | 127 } |
127 verts += kVertsPerRect * vertexStride; | 128 verts += kVertsPerRect * vertexStride; |
128 } | 129 } |
| 130 |
| 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, |
| 135 kVertsPerRect * patch.fIte
r->numRects()); |
| 136 } |
129 } | 137 } |
130 helper.recordDraw(target, gp.get()); | 138 helper.recordDraw(target, gp.get()); |
131 } | 139 } |
132 | 140 |
133 void initBatchTracker(const GrXPOverridesForBatch& overrides) override { | 141 void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
134 overrides.getOverrideColorIfSet(&fPatches[0].fColor); | 142 overrides.getOverrideColorIfSet(&fPatches[0].fColor); |
135 fOverrides = overrides; | 143 fOverrides = overrides; |
136 } | 144 } |
137 | 145 |
138 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { | 146 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
139 GrNonAANinePatchBatch* that = t->cast<GrNonAANinePatchBatch>(); | 147 GrNonAANinePatchBatch* that = t->cast<GrNonAANinePatchBatch>(); |
140 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pi
peline(), | 148 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pi
peline(), |
141 that->bounds(), caps)) { | 149 that->bounds(), caps)) { |
142 return false; | 150 return false; |
143 } | 151 } |
144 | 152 |
145 SkASSERT(this->fImageWidth == that->fImageWidth && | 153 SkASSERT(this->fImageWidth == that->fImageWidth && |
146 this->fImageHeight == that->fImageHeight); | 154 this->fImageHeight == that->fImageHeight); |
147 | 155 |
148 // In the event of two batches, one who can tweak, one who cannot, we ju
st fall back to | 156 // In the event of two batches, one who can tweak, one who cannot, we ju
st fall back to |
149 // not tweaking | 157 // not tweaking |
150 if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakA
lphaForCoverage()) { | 158 if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakA
lphaForCoverage()) { |
151 fOverrides = that->fOverrides; | 159 fOverrides = that->fOverrides; |
152 } | 160 } |
153 | 161 |
154 fPatches.push_back_n(that->fPatches.count(), that->fPatches.begin()); | 162 fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin()); |
155 this->joinBounds(*that); | 163 this->joinBounds(*that); |
156 return true; | 164 return true; |
157 } | 165 } |
158 | 166 |
159 struct Patch { | 167 struct Patch { |
160 SkMatrix fViewMatrix; | 168 SkMatrix fViewMatrix; |
161 SkIRect fCenter; | 169 std::unique_ptr<SkLatticeIter> fIter; |
162 SkRect fDst; | 170 SkRect fDst; |
163 GrColor fColor; | 171 GrColor fColor; |
164 }; | 172 }; |
165 | 173 |
166 GrXPOverridesForBatch fOverrides; | 174 GrXPOverridesForBatch fOverrides; |
167 int fImageWidth; | 175 int fImageWidth; |
168 int fImageHeight; | 176 int fImageHeight; |
169 SkSTArray<1, Patch, true> fPatches; | 177 SkSTArray<1, Patch, true> fPatches; |
170 | 178 |
171 typedef GrVertexBatch INHERITED; | 179 typedef GrVertexBatch INHERITED; |
172 }; | 180 }; |
173 | 181 |
174 namespace GrNinePatch { | 182 namespace GrNinePatch { |
175 GrDrawBatch* CreateNonAA(GrColor color, const SkMatrix& viewMatrix, int imageWid
th, int imageHeight, | 183 GrDrawBatch* CreateNonAA(GrColor color, const SkMatrix& viewMatrix, int imageWid
th, int imageHeight, |
176 const SkIRect& center, const SkRect& dst) { | 184 std::unique_ptr<SkLatticeIter> iter, const SkRect& dst)
{ |
177 return new GrNonAANinePatchBatch(color, viewMatrix, imageWidth, imageHeight,
center, dst); | 185 return new GrNonAANinePatchBatch(color, viewMatrix, imageWidth, imageHeight,
std::move(iter), |
| 186 dst); |
178 } | 187 } |
179 }; | 188 }; |
OLD | NEW |