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