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