| 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 "src/gpu/ops/GrDrawAtlasOp.h" |
| 9 | |
| 10 | #include "include/core/SkRSXform.h" |
| 11 | #include "include/gpu/GrRecordingContext.h" |
| 12 | #include "include/utils/SkRandom.h" |
| 13 | #include "src/core/SkMatrixPriv.h" |
| 14 | #include "src/core/SkRectPriv.h" |
| 15 | #include "src/gpu/GrCaps.h" |
| 16 | #include "src/gpu/GrDefaultGeoProcFactory.h" |
| 17 | #include "src/gpu/GrDrawOpTest.h" |
| 18 | #include "src/gpu/GrOpFlushState.h" |
| 19 | #include "src/gpu/GrProgramInfo.h" |
| 20 | #include "src/gpu/GrRecordingContextPriv.h" |
| 21 | #include "src/gpu/SkGr.h" |
| 22 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | class DrawAtlasOp final : public GrMeshDrawOp { |
| 27 | private: |
| 28 | using Helper = GrSimpleMeshDrawOpHelper; |
| 29 | |
| 30 | public: |
| 31 | DEFINE_OP_CLASS_ID |
| 32 | |
| 33 | DrawAtlasOp(const Helper::MakeArgs&, const SkPMColor4f& color, |
| 34 | const SkMatrix& viewMatrix, GrAAType, int spriteCount, const SkRSXform* xforms, |
| 35 | const SkRect* rects, const SkColor* colors); |
| 36 | |
| 37 | const char* name() const override { return "DrawAtlasOp" ; } |
| 38 | |
| 39 | void visitProxies(const VisitProxyFunc& func) const override { |
| 40 | if (fProgramInfo) { |
| 41 | fProgramInfo->visitFPProxies(func); |
| 42 | } else { |
| 43 | fHelper.visitProxies(func); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | FixedFunctionFlags fixedFunctionFlags() const override; |
| 48 | |
| 49 | GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, |
| 50 | bool hasMixedSampledCoverage, GrClampType) override; |
| 51 | |
| 52 | private: |
| 53 | GrProgramInfo* programInfo() override { return fProgramInfo; } |
| 54 | |
| 55 | void onCreateProgramInfo(const GrCaps*, |
| 56 | SkArenaAlloc*, |
| 57 | const GrSurfaceProxyView* writeView, |
| 58 | GrAppliedClip&&, |
| 59 | const GrXferProcessor::DstProxyView&) override; |
| 60 | |
| 61 | void onPrepareDraws(Target*) override; |
| 62 | void onExecute(GrOpFlushState*, const SkRect& chainBounds) override; |
| 63 | #if GR_TEST_UTILS |
| 64 | SkString onDumpInfo() const override; |
| 65 | #endif |
| 66 | |
| 67 | const SkPMColor4f& color() const { return fColor; } |
| 68 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
| 69 | bool hasColors() const { return fHasColors; } |
| 70 | int quadCount() const { return fQuadCount; } |
| 71 | |
| 72 | CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*, const GrCaps&) override; |
| 73 | |
| 74 | struct Geometry { |
| 75 | SkPMColor4f fColor; |
| 76 | SkTArray<uint8_t, true> fVerts; |
| 77 | }; |
| 78 | |
| 79 | SkSTArray<1, Geometry, true> fGeoData; |
| 80 | Helper fHelper; |
| 81 | SkMatrix fViewMatrix; |
| 82 | SkPMColor4f fColor; |
| 83 | int fQuadCount; |
| 84 | bool fHasColors; |
| 85 | |
| 86 | GrSimpleMesh* fMesh = nullptr; |
| 87 | GrProgramInfo* fProgramInfo = nullptr; |
| 88 | |
| 89 | typedef GrMeshDrawOp INHERITED; |
| 90 | }; |
| 91 | |
| 92 | static GrGeometryProcessor* make_gp(SkArenaAlloc* arena, |
| 93 | bool hasColors, |
| 94 | const SkPMColor4f& color, |
| 95 | const SkMatrix& viewMatrix) { |
| 96 | using namespace GrDefaultGeoProcFactory; |
| 97 | Color gpColor(color); |
| 98 | if (hasColors) { |
| 99 | gpColor.fType = Color::kPremulGrColorAttribute_Type; |
| 100 | } |
| 101 | |
| 102 | return GrDefaultGeoProcFactory::Make(arena, gpColor, Coverage::kSolid_Type, |
| 103 | LocalCoords::kHasExplicit_Type, viewMatrix); |
| 104 | } |
| 105 | |
| 106 | DrawAtlasOp::DrawAtlasOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color, |
| 107 | const SkMatrix& viewMatrix, GrAAType aaType, int spriteCount, |
| 108 | const SkRSXform* xforms, const SkRect* rects, const SkColor* colors) |
| 109 | : INHERITED(ClassID()), fHelper(helperArgs, aaType), fColor(color) { |
| 110 | SkASSERT(xforms); |
| 111 | SkASSERT(rects); |
| 112 | |
| 113 | fViewMatrix = viewMatrix; |
| 114 | Geometry& installedGeo = fGeoData.push_back(); |
| 115 | installedGeo.fColor = color; |
| 116 | |
| 117 | // Figure out stride and offsets |
| 118 | // Order within the vertex is: position [color] texCoord |
| 119 | size_t texOffset = sizeof(SkPoint); |
| 120 | size_t vertexStride = 2 * sizeof(SkPoint); |
| 121 | fHasColors = SkToBool(colors); |
| 122 | if (colors) { |
| 123 | texOffset += sizeof(GrColor); |
| 124 | vertexStride += sizeof(GrColor); |
| 125 | } |
| 126 | |
| 127 | // Compute buffer size and alloc buffer |
| 128 | fQuadCount = spriteCount; |
| 129 | int allocSize = static_cast<int>(4 * vertexStride * spriteCount); |
| 130 | installedGeo.fVerts.reset(allocSize); |
| 131 | uint8_t* currVertex = installedGeo.fVerts.begin(); |
| 132 | |
| 133 | SkRect bounds = SkRectPriv::MakeLargestInverted(); |
| 134 | // TODO4F: Preserve float colors |
| 135 | int paintAlpha = GrColorUnpackA(installedGeo.fColor.toBytes_RGBA()); |
| 136 | for (int spriteIndex = 0; spriteIndex < spriteCount; ++spriteIndex) { |
| 137 | // Transform rect |
| 138 | SkPoint strip[4]; |
| 139 | const SkRect& currRect = rects[spriteIndex]; |
| 140 | xforms[spriteIndex].toTriStrip(currRect.width(), currRect.height(), strip); |
| 141 | |
| 142 | // Copy colors if necessary |
| 143 | if (colors) { |
| 144 | // convert to GrColor |
| 145 | SkColor color = colors[spriteIndex]; |
| 146 | if (paintAlpha != 255) { |
| 147 | color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paintAlpha)); |
| 148 | } |
| 149 | GrColor grColor = SkColorToPremulGrColor(color); |
| 150 | |
| 151 | *(reinterpret_cast<GrColor*>(currVertex + sizeof(SkPoint))) = grColor; |
| 152 | *(reinterpret_cast<GrColor*>(currVertex + vertexStride + sizeof(SkPoint))) = grColor; |
| 153 | *(reinterpret_cast<GrColor*>(currVertex + 2 * vertexStride + sizeof(SkPoint))) = |
| 154 | grColor; |
| 155 | *(reinterpret_cast<GrColor*>(currVertex + 3 * vertexStride + sizeof(SkPoint))) = |
| 156 | grColor; |
| 157 | } |
| 158 | |
| 159 | // Copy position and uv to verts |
| 160 | *(reinterpret_cast<SkPoint*>(currVertex)) = strip[0]; |
| 161 | *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) = |
| 162 | SkPoint::Make(currRect.fLeft, currRect.fTop); |
| 163 | SkRectPriv::GrowToInclude(&bounds, strip[0]); |
| 164 | currVertex += vertexStride; |
| 165 | |
| 166 | *(reinterpret_cast<SkPoint*>(currVertex)) = strip[1]; |
| 167 | *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) = |
| 168 | SkPoint::Make(currRect.fLeft, currRect.fBottom); |
| 169 | SkRectPriv::GrowToInclude(&bounds, strip[1]); |
| 170 | currVertex += vertexStride; |
| 171 | |
| 172 | *(reinterpret_cast<SkPoint*>(currVertex)) = strip[2]; |
| 173 | *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) = |
| 174 | SkPoint::Make(currRect.fRight, currRect.fTop); |
| 175 | SkRectPriv::GrowToInclude(&bounds, strip[2]); |
| 176 | currVertex += vertexStride; |
| 177 | |
| 178 | *(reinterpret_cast<SkPoint*>(currVertex)) = strip[3]; |
| 179 | *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) = |
| 180 | SkPoint::Make(currRect.fRight, currRect.fBottom); |
| 181 | SkRectPriv::GrowToInclude(&bounds, strip[3]); |
| 182 | currVertex += vertexStride; |
| 183 | } |
| 184 | |
| 185 | this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsHairline::kNo); |
| 186 | } |
| 187 | |
| 188 | #if GR_TEST_UTILS |
| 189 | SkString DrawAtlasOp::onDumpInfo() const { |
| 190 | SkString string; |
| 191 | for (const auto& geo : fGeoData) { |
| 192 | string.appendf("Color: 0x%08x, Quads: %d\n" , geo.fColor.toBytes_RGBA(), |
| 193 | geo.fVerts.count() / 4); |
| 194 | } |
| 195 | string += fHelper.dumpInfo(); |
| 196 | return string; |
| 197 | } |
| 198 | #endif |
| 199 | |
| 200 | void DrawAtlasOp::onCreateProgramInfo(const GrCaps* caps, |
| 201 | SkArenaAlloc* arena, |
| 202 | const GrSurfaceProxyView* writeView, |
| 203 | GrAppliedClip&& appliedClip, |
| 204 | const GrXferProcessor::DstProxyView& dstProxyView) { |
| 205 | // Setup geometry processor |
| 206 | GrGeometryProcessor* gp = make_gp(arena, |
| 207 | this->hasColors(), |
| 208 | this->color(), |
| 209 | this->viewMatrix()); |
| 210 | |
| 211 | fProgramInfo = fHelper.createProgramInfo(caps, arena, writeView, std::move(appliedClip), |
| 212 | dstProxyView, gp, GrPrimitiveType::kTriangles); |
| 213 | } |
| 214 | |
| 215 | void DrawAtlasOp::onPrepareDraws(Target* target) { |
| 216 | if (!fProgramInfo) { |
| 217 | this->createProgramInfo(target); |
| 218 | } |
| 219 | |
| 220 | int instanceCount = fGeoData.count(); |
| 221 | size_t vertexStride = fProgramInfo->primProc().vertexStride(); |
| 222 | |
| 223 | int numQuads = this->quadCount(); |
| 224 | QuadHelper helper(target, vertexStride, numQuads); |
| 225 | void* verts = helper.vertices(); |
| 226 | if (!verts) { |
| 227 | SkDebugf("Could not allocate vertices\n" ); |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | uint8_t* vertPtr = reinterpret_cast<uint8_t*>(verts); |
| 232 | for (int i = 0; i < instanceCount; i++) { |
| 233 | const Geometry& args = fGeoData[i]; |
| 234 | |
| 235 | size_t allocSize = args.fVerts.count(); |
| 236 | memcpy(vertPtr, args.fVerts.begin(), allocSize); |
| 237 | vertPtr += allocSize; |
| 238 | } |
| 239 | |
| 240 | fMesh = helper.mesh(); |
| 241 | } |
| 242 | |
| 243 | void DrawAtlasOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
| 244 | if (!fProgramInfo || !fMesh) { |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
| 249 | flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline()); |
| 250 | flushState->drawMesh(*fMesh); |
| 251 | } |
| 252 | |
| 253 | GrOp::CombineResult DrawAtlasOp::onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*, |
| 254 | const GrCaps& caps) { |
| 255 | DrawAtlasOp* that = t->cast<DrawAtlasOp>(); |
| 256 | |
| 257 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
| 258 | return CombineResult::kCannotCombine; |
| 259 | } |
| 260 | |
| 261 | // We currently use a uniform viewmatrix for this op. |
| 262 | if (!SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix())) { |
| 263 | return CombineResult::kCannotCombine; |
| 264 | } |
| 265 | |
| 266 | if (this->hasColors() != that->hasColors()) { |
| 267 | return CombineResult::kCannotCombine; |
| 268 | } |
| 269 | |
| 270 | if (!this->hasColors() && this->color() != that->color()) { |
| 271 | return CombineResult::kCannotCombine; |
| 272 | } |
| 273 | |
| 274 | fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin()); |
| 275 | fQuadCount += that->quadCount(); |
| 276 | |
| 277 | return CombineResult::kMerged; |
| 278 | } |
| 279 | |
| 280 | GrDrawOp::FixedFunctionFlags DrawAtlasOp::fixedFunctionFlags() const { |
| 281 | return fHelper.fixedFunctionFlags(); |
| 282 | } |
| 283 | |
| 284 | GrProcessorSet::Analysis DrawAtlasOp::finalize( |
| 285 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
| 286 | GrClampType clampType) { |
| 287 | GrProcessorAnalysisColor gpColor; |
| 288 | if (this->hasColors()) { |
| 289 | gpColor.setToUnknown(); |
| 290 | } else { |
| 291 | gpColor.setToConstant(fColor); |
| 292 | } |
| 293 | auto result = fHelper.finalizeProcessors(caps, clip, hasMixedSampledCoverage, clampType, |
| 294 | GrProcessorAnalysisCoverage::kNone, &gpColor); |
| 295 | if (gpColor.isConstant(&fColor)) { |
| 296 | fHasColors = false; |
| 297 | } |
| 298 | return result; |
| 299 | } |
| 300 | |
| 301 | } // anonymous namespace |
| 302 | |
| 303 | std::unique_ptr<GrDrawOp> GrDrawAtlasOp::Make(GrRecordingContext* context, |
| 304 | GrPaint&& paint, |
| 305 | const SkMatrix& viewMatrix, |
| 306 | GrAAType aaType, |
| 307 | int spriteCount, |
| 308 | const SkRSXform* xforms, |
| 309 | const SkRect* rects, |
| 310 | const SkColor* colors) { |
| 311 | return GrSimpleMeshDrawOpHelper::FactoryHelper<DrawAtlasOp>(context, std::move(paint), |
| 312 | viewMatrix, aaType, |
| 313 | spriteCount, xforms, |
| 314 | rects, colors); |
| 315 | } |
| 316 | |
| 317 | #if GR_TEST_UTILS |
| 318 | |
| 319 | static SkRSXform random_xform(SkRandom* random) { |
| 320 | static const SkScalar kMinExtent = -100.f; |
| 321 | static const SkScalar kMaxExtent = 100.f; |
| 322 | static const SkScalar kMinScale = 0.1f; |
| 323 | static const SkScalar kMaxScale = 100.f; |
| 324 | static const SkScalar kMinRotate = -SK_ScalarPI; |
| 325 | static const SkScalar kMaxRotate = SK_ScalarPI; |
| 326 | |
| 327 | SkRSXform xform = SkRSXform::MakeFromRadians(random->nextRangeScalar(kMinScale, kMaxScale), |
| 328 | random->nextRangeScalar(kMinRotate, kMaxRotate), |
| 329 | random->nextRangeScalar(kMinExtent, kMaxExtent), |
| 330 | random->nextRangeScalar(kMinExtent, kMaxExtent), |
| 331 | random->nextRangeScalar(kMinExtent, kMaxExtent), |
| 332 | random->nextRangeScalar(kMinExtent, kMaxExtent)); |
| 333 | return xform; |
| 334 | } |
| 335 | |
| 336 | static SkRect random_texRect(SkRandom* random) { |
| 337 | static const SkScalar kMinCoord = 0.0f; |
| 338 | static const SkScalar kMaxCoord = 1024.f; |
| 339 | |
| 340 | SkRect texRect = SkRect::MakeLTRB(random->nextRangeScalar(kMinCoord, kMaxCoord), |
| 341 | random->nextRangeScalar(kMinCoord, kMaxCoord), |
| 342 | random->nextRangeScalar(kMinCoord, kMaxCoord), |
| 343 | random->nextRangeScalar(kMinCoord, kMaxCoord)); |
| 344 | texRect.sort(); |
| 345 | return texRect; |
| 346 | } |
| 347 | |
| 348 | static void randomize_params(uint32_t count, SkRandom* random, SkTArray<SkRSXform>* xforms, |
| 349 | SkTArray<SkRect>* texRects, SkTArray<GrColor>* colors, |
| 350 | bool hasColors) { |
| 351 | for (uint32_t v = 0; v < count; v++) { |
| 352 | xforms->push_back(random_xform(random)); |
| 353 | texRects->push_back(random_texRect(random)); |
| 354 | if (hasColors) { |
| 355 | colors->push_back(GrRandomColor(random)); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | GR_DRAW_OP_TEST_DEFINE(DrawAtlasOp) { |
| 361 | uint32_t spriteCount = random->nextRangeU(1, 100); |
| 362 | |
| 363 | SkTArray<SkRSXform> xforms(spriteCount); |
| 364 | SkTArray<SkRect> texRects(spriteCount); |
| 365 | SkTArray<GrColor> colors; |
| 366 | |
| 367 | bool hasColors = random->nextBool(); |
| 368 | |
| 369 | randomize_params(spriteCount, random, &xforms, &texRects, &colors, hasColors); |
| 370 | |
| 371 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 372 | GrAAType aaType = GrAAType::kNone; |
| 373 | if (numSamples > 1 && random->nextBool()) { |
| 374 | aaType = GrAAType::kMSAA; |
| 375 | } |
| 376 | |
| 377 | return GrDrawAtlasOp::Make(context, std::move(paint), viewMatrix, aaType, spriteCount, |
| 378 | xforms.begin(), texRects.begin(), |
| 379 | hasColors ? colors.begin() : nullptr); |
| 380 | } |
| 381 | |
| 382 | #endif |
| 383 | |