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