• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
create_gp(bool readsCoverage)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 
GrNonAANinePatchBatch(GrColor color,const SkMatrix & viewMatrix,int imageWidth,int imageHeight,const SkIRect & center,const SkRect & dst)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 
name() const57     const char* name() const override { return "NonAANinePatchBatch"; }
58 
dumpInfo() const59     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     }
76 
computePipelineOptimizations(GrInitInvariantOutput * color,GrInitInvariantOutput * coverage,GrBatchToXPOverrides * overrides) const77     void computePipelineOptimizations(GrInitInvariantOutput* color,
78                                       GrInitInvariantOutput* coverage,
79                                       GrBatchToXPOverrides* overrides) const override {
80         color->setUnknownFourComponents();
81         coverage->setKnownSingleComponent(0xff);
82     }
83 
geoData()84     SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
85 
86 private:
onPrepareDraws(Target * target) const87     void onPrepareDraws(Target* target) const override {
88         SkAutoTUnref<const GrGeometryProcessor> gp(create_gp(fOverrides.readsCoverage()));
89         if (!gp) {
90             SkDebugf("Couldn't create GrGeometryProcessor\n");
91             return;
92         }
93 
94         target->initDraw(gp, this->pipeline());
95 
96         size_t vertexStride = gp->getVertexStride();
97         int instanceCount = fGeoData.count();
98 
99         SkAutoTUnref<const GrIndexBuffer> indexBuffer(
100                 target->resourceProvider()->refQuadIndexBuffer());
101         InstancedHelper helper;
102         void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride,
103                                      indexBuffer, kVertsPerRect,
104                                      kIndicesPerRect, instanceCount * kRectsPerInstance);
105         if (!vertices || !indexBuffer) {
106             SkDebugf("Could not allocate vertices\n");
107             return;
108         }
109 
110         for (int i = 0; i < instanceCount; i++) {
111             intptr_t verts = reinterpret_cast<intptr_t>(vertices) +
112                              i * kRectsPerInstance * kVertsPerRect * vertexStride;
113 
114             const Geometry& geo = fGeoData[i];
115             SkNinePatchIter iter(fImageWidth, fImageHeight, geo.fCenter, geo.fDst);
116 
117             SkRect srcR, dstR;
118             while (iter.next(&srcR, &dstR)) {
119                 SkPoint* positions = reinterpret_cast<SkPoint*>(verts);
120 
121                 positions->setRectFan(dstR.fLeft, dstR.fTop,
122                                       dstR.fRight, dstR.fBottom, vertexStride);
123 
124                 SkASSERT(!geo.fViewMatrix.hasPerspective());
125                 geo.fViewMatrix.mapPointsWithStride(positions, vertexStride, kVertsPerRect);
126 
127                 // Setup local coords
128                 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor);
129                 SkPoint* coords = reinterpret_cast<SkPoint*>(verts + kLocalOffset);
130                 coords->setRectFan(srcR.fLeft, srcR.fTop, srcR.fRight, srcR.fBottom, vertexStride);
131 
132                 static const int kColorOffset = sizeof(SkPoint);
133                 GrColor* vertColor = reinterpret_cast<GrColor*>(verts + kColorOffset);
134                 for (int j = 0; j < 4; ++j) {
135                     *vertColor = geo.fColor;
136                     vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride);
137                 }
138                 verts += kVertsPerRect * vertexStride;
139             }
140         }
141         helper.recordDraw(target);
142     }
143 
initBatchTracker(const GrXPOverridesForBatch & overrides)144     void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
145         overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
146         fOverrides = overrides;
147     }
148 
onCombineIfPossible(GrBatch * t,const GrCaps & caps)149     bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
150         GrNonAANinePatchBatch* that = t->cast<GrNonAANinePatchBatch>();
151         if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
152                                     that->bounds(), caps)) {
153             return false;
154         }
155 
156         SkASSERT(this->fImageWidth == that->fImageWidth &&
157                  this->fImageHeight == that->fImageHeight);
158 
159         // In the event of two batches, one who can tweak, one who cannot, we just fall back to
160         // not tweaking
161         if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakAlphaForCoverage()) {
162             fOverrides = that->fOverrides;
163         }
164 
165         fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
166         this->joinBounds(that->bounds());
167         return true;
168     }
169 
170     GrXPOverridesForBatch fOverrides;
171     int fImageWidth;
172     int fImageHeight;
173     SkSTArray<1, Geometry, true> fGeoData;
174 
175     typedef GrVertexBatch INHERITED;
176 };
177 
178 namespace GrNinePatch {
CreateNonAA(GrColor color,const SkMatrix & viewMatrix,int imageWidth,int imageHeight,const SkIRect & center,const SkRect & dst)179 GrDrawBatch* CreateNonAA(GrColor color, const SkMatrix& viewMatrix, int imageWidth, int imageHeight,
180                          const SkIRect& center, const SkRect& dst) {
181     return new GrNonAANinePatchBatch(color, viewMatrix, imageWidth, imageHeight, center, dst);
182 }
183 };
184