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 "GrDrawAtlasBatch.h" 9 #include "GrBatchFlushState.h" 10 #include "GrBatchTest.h" 11 #include "SkGr.h" 12 #include "SkRandom.h" 13 #include "SkRSXform.h" 14 15 void GrDrawAtlasBatch::initBatchTracker(const GrXPOverridesForBatch& overrides) { 16 SkASSERT(fGeoData.count() == 1); 17 // Handle any color overrides 18 if (!overrides.readsColor()) { 19 fGeoData[0].fColor = GrColor_ILLEGAL; 20 } 21 if (overrides.getOverrideColorIfSet(&fGeoData[0].fColor) && fHasColors) { 22 size_t vertexStride = sizeof(SkPoint) + sizeof(SkPoint) + 23 (this->hasColors() ? sizeof(GrColor) : 0); 24 uint8_t* currVertex = fGeoData[0].fVerts.begin(); 25 for (int i = 0; i < 4*fQuadCount; ++i) { 26 *(reinterpret_cast<GrColor*>(currVertex + sizeof(SkPoint))) = fGeoData[0].fColor; 27 currVertex += vertexStride; 28 } 29 } 30 31 // setup batch properties 32 fColorIgnored = !overrides.readsColor(); 33 fColor = fGeoData[0].fColor; 34 // We'd like to assert this, but we can't because of GLPrograms test 35 //SkASSERT(init.readsLocalCoords()); 36 fCoverageIgnored = !overrides.readsCoverage(); 37 } 38 39 static const GrGeometryProcessor* set_vertex_attributes(bool hasColors, 40 GrColor color, 41 const SkMatrix& viewMatrix, 42 bool coverageIgnored) { 43 using namespace GrDefaultGeoProcFactory; 44 Color gpColor(color); 45 if (hasColors) { 46 gpColor.fType = Color::kAttribute_Type; 47 } 48 49 Coverage coverage(coverageIgnored ? Coverage::kNone_Type : Coverage::kSolid_Type); 50 LocalCoords localCoords(LocalCoords::kHasExplicit_Type); 51 return GrDefaultGeoProcFactory::Create(gpColor, coverage, localCoords, viewMatrix); 52 } 53 54 void GrDrawAtlasBatch::onPrepareDraws(Target* target) const { 55 // Setup geometry processor 56 SkAutoTUnref<const GrGeometryProcessor> gp(set_vertex_attributes(this->hasColors(), 57 this->color(), 58 this->viewMatrix(), 59 this->coverageIgnored())); 60 61 target->initDraw(gp, this->pipeline()); 62 63 int instanceCount = fGeoData.count(); 64 size_t vertexStride = gp->getVertexStride(); 65 SkASSERT(vertexStride == sizeof(SkPoint) + sizeof(SkPoint) 66 + (this->hasColors() ? sizeof(GrColor) : 0)); 67 68 QuadHelper helper; 69 int numQuads = this->quadCount(); 70 void* verts = helper.init(target, vertexStride, numQuads); 71 if (!verts) { 72 SkDebugf("Could not allocate vertices\n"); 73 return; 74 } 75 76 uint8_t* vertPtr = reinterpret_cast<uint8_t*>(verts); 77 for (int i = 0; i < instanceCount; i++) { 78 const Geometry& args = fGeoData[i]; 79 80 size_t allocSize = args.fVerts.count(); 81 memcpy(vertPtr, args.fVerts.begin(), allocSize); 82 vertPtr += allocSize; 83 } 84 helper.recordDraw(target); 85 } 86 87 GrDrawAtlasBatch::GrDrawAtlasBatch(const Geometry& geometry, const SkMatrix& viewMatrix, 88 int spriteCount, const SkRSXform* xforms, const SkRect* rects, 89 const SkColor* colors) 90 : INHERITED(ClassID()) { 91 SkASSERT(xforms); 92 SkASSERT(rects); 93 94 fViewMatrix = viewMatrix; 95 Geometry& installedGeo = fGeoData.push_back(geometry); 96 97 // Figure out stride and offsets 98 // Order within the vertex is: position [color] texCoord 99 size_t texOffset = sizeof(SkPoint); 100 size_t vertexStride = 2*sizeof(SkPoint); 101 fHasColors = SkToBool(colors); 102 if (colors) { 103 texOffset += sizeof(GrColor); 104 vertexStride += sizeof(GrColor); 105 } 106 107 // Compute buffer size and alloc buffer 108 fQuadCount = spriteCount; 109 int allocSize = static_cast<int>(4*vertexStride*spriteCount); 110 installedGeo.fVerts.reset(allocSize); 111 uint8_t* currVertex = installedGeo.fVerts.begin(); 112 113 SkRect bounds; 114 bounds.setLargestInverted(); 115 int paintAlpha = GrColorUnpackA(installedGeo.fColor); 116 for (int spriteIndex = 0; spriteIndex < spriteCount; ++spriteIndex) { 117 // Transform rect 118 SkPoint quad[4]; 119 const SkRect& currRect = rects[spriteIndex]; 120 xforms[spriteIndex].toQuad(currRect.width(), currRect.height(), quad); 121 122 // Copy colors if necessary 123 if (colors) { 124 // convert to GrColor 125 SkColor color = colors[spriteIndex]; 126 if (paintAlpha != 255) { 127 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paintAlpha)); 128 } 129 GrColor grColor = SkColorToPremulGrColor(color); 130 131 *(reinterpret_cast<GrColor*>(currVertex+sizeof(SkPoint))) = grColor; 132 *(reinterpret_cast<GrColor*>(currVertex+vertexStride+sizeof(SkPoint))) = grColor; 133 *(reinterpret_cast<GrColor*>(currVertex+2*vertexStride+sizeof(SkPoint))) = grColor; 134 *(reinterpret_cast<GrColor*>(currVertex+3*vertexStride+sizeof(SkPoint))) = grColor; 135 } 136 137 // Copy position and uv to verts 138 *(reinterpret_cast<SkPoint*>(currVertex)) = quad[0]; 139 *(reinterpret_cast<SkPoint*>(currVertex+texOffset)) = SkPoint::Make(currRect.fLeft, 140 currRect.fTop); 141 bounds.growToInclude(quad[0].fX, quad[0].fY); 142 currVertex += vertexStride; 143 144 *(reinterpret_cast<SkPoint*>(currVertex)) = quad[1]; 145 *(reinterpret_cast<SkPoint*>(currVertex+texOffset)) = SkPoint::Make(currRect.fRight, 146 currRect.fTop); 147 bounds.growToInclude(quad[1].fX, quad[1].fY); 148 currVertex += vertexStride; 149 150 *(reinterpret_cast<SkPoint*>(currVertex)) = quad[2]; 151 *(reinterpret_cast<SkPoint*>(currVertex+texOffset)) = SkPoint::Make(currRect.fRight, 152 currRect.fBottom); 153 bounds.growToInclude(quad[2].fX, quad[2].fY); 154 currVertex += vertexStride; 155 156 *(reinterpret_cast<SkPoint*>(currVertex)) = quad[3]; 157 *(reinterpret_cast<SkPoint*>(currVertex+texOffset)) = SkPoint::Make(currRect.fLeft, 158 currRect.fBottom); 159 bounds.growToInclude(quad[3].fX, quad[3].fY); 160 currVertex += vertexStride; 161 } 162 163 viewMatrix.mapRect(&bounds); 164 // Outset for a half pixel in each direction to account for snapping in non-AA case 165 bounds.outset(0.5f, 0.5f); 166 this->setBounds(bounds); 167 } 168 169 bool GrDrawAtlasBatch::onCombineIfPossible(GrBatch* t, const GrCaps& caps) { 170 GrDrawAtlasBatch* that = t->cast<GrDrawAtlasBatch>(); 171 172 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), 173 that->bounds(), caps)) { 174 return false; 175 } 176 177 // We currently use a uniform viewmatrix for this batch 178 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) { 179 return false; 180 } 181 182 if (this->hasColors() != that->hasColors()) { 183 return false; 184 } 185 186 if (!this->hasColors() && this->color() != that->color()) { 187 return false; 188 } 189 190 if (this->color() != that->color()) { 191 fColor = GrColor_ILLEGAL; 192 } 193 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); 194 fQuadCount += that->quadCount(); 195 196 this->joinBounds(that->bounds()); 197 return true; 198 } 199 200 #ifdef GR_TEST_UTILS 201 202 static SkRSXform random_xform(SkRandom* random) { 203 static const SkScalar kMinExtent = -100.f; 204 static const SkScalar kMaxExtent = 100.f; 205 static const SkScalar kMinScale = 0.1f; 206 static const SkScalar kMaxScale = 100.f; 207 static const SkScalar kMinRotate = -SK_ScalarPI; 208 static const SkScalar kMaxRotate = SK_ScalarPI; 209 210 SkRSXform xform = SkRSXform::MakeFromRadians(random->nextRangeScalar(kMinScale, kMaxScale), 211 random->nextRangeScalar(kMinRotate, kMaxRotate), 212 random->nextRangeScalar(kMinExtent, kMaxExtent), 213 random->nextRangeScalar(kMinExtent, kMaxExtent), 214 random->nextRangeScalar(kMinExtent, kMaxExtent), 215 random->nextRangeScalar(kMinExtent, kMaxExtent)); 216 return xform; 217 } 218 219 static SkRect random_texRect(SkRandom* random) { 220 static const SkScalar kMinCoord = 0.0f; 221 static const SkScalar kMaxCoord = 1024.f; 222 223 SkRect texRect = SkRect::MakeLTRB(random->nextRangeScalar(kMinCoord, kMaxCoord), 224 random->nextRangeScalar(kMinCoord, kMaxCoord), 225 random->nextRangeScalar(kMinCoord, kMaxCoord), 226 random->nextRangeScalar(kMinCoord, kMaxCoord)); 227 texRect.sort(); 228 return texRect; 229 } 230 231 static void randomize_params(uint32_t count, SkRandom* random, 232 SkTArray<SkRSXform>* xforms, 233 SkTArray<SkRect>* texRects, 234 SkTArray<GrColor>* colors, bool hasColors) { 235 for (uint32_t v = 0; v < count; v++) { 236 xforms->push_back(random_xform(random)); 237 texRects->push_back(random_texRect(random)); 238 if (hasColors) { 239 colors->push_back(GrRandomColor(random)); 240 } 241 } 242 } 243 244 DRAW_BATCH_TEST_DEFINE(GrDrawAtlasBatch) { 245 uint32_t spriteCount = random->nextRangeU(1, 100); 246 247 SkTArray<SkRSXform> xforms(spriteCount); 248 SkTArray<SkRect> texRects(spriteCount); 249 SkTArray<GrColor> colors; 250 251 bool hasColors = random->nextBool(); 252 253 randomize_params(spriteCount, 254 random, 255 &xforms, 256 &texRects, 257 &colors, hasColors); 258 259 SkMatrix viewMatrix = GrTest::TestMatrix(random); 260 261 GrDrawAtlasBatch::Geometry geometry; 262 geometry.fColor = GrRandomColor(random); 263 return GrDrawAtlasBatch::Create(geometry, viewMatrix, spriteCount, xforms.begin(), 264 texRects.begin(), hasColors ? colors.begin() : nullptr); 265 } 266 267 #endif 268