| 1 |
/* |
| 2 |
* Copyright 2014 Google Inc. |
| 3 |
* Copyright 2017 ARM Ltd. |
| 4 |
* |
| 5 |
* Use of this source code is governed by a BSD-style license that can be |
| 6 |
* found in the LICENSE file. |
| 7 |
*/ |
| 8 |
#include "src/gpu/ganesh/ops/SmallPathRenderer.h" |
| 9 |
|
| 10 |
#include "include/core/SkImageInfo.h" |
| 11 |
#include "include/core/SkMatrix.h" |
| 12 |
#include "include/core/SkPaint.h" |
| 13 |
#include "include/core/SkPath.h" |
| 14 |
#include "include/core/SkPixmap.h" |
| 15 |
#include "include/core/SkPoint3.h" |
| 16 |
#include "include/core/SkRect.h" |
| 17 |
#include "include/core/SkRefCnt.h" |
| 18 |
#include "include/core/SkSamplingOptions.h" |
| 19 |
#include "include/core/SkScalar.h" |
| 20 |
#include "include/core/SkString.h" |
| 21 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 22 |
#include "include/private/base/SkAssert.h" |
| 23 |
#include "include/private/base/SkDebug.h" |
| 24 |
#include "include/private/base/SkMalloc.h" |
| 25 |
#include "include/private/base/SkMath.h" |
| 26 |
#include "include/private/base/SkPoint_impl.h" |
| 27 |
#include "include/private/base/SkTArray.h" |
| 28 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 29 |
#include "src/base/SkAutoMalloc.h" |
| 30 |
#include "src/core/SkAutoPixmapStorage.h" |
| 31 |
#include "src/core/SkColorData.h" |
| 32 |
#include "src/core/SkDistanceFieldGen.h" |
| 33 |
#include "src/core/SkDraw.h" |
| 34 |
#include "src/core/SkMatrixPriv.h" |
| 35 |
#include "src/core/SkRasterClip.h" |
| 36 |
#include "src/gpu/AtlasTypes.h" |
| 37 |
#include "src/gpu/BufferWriter.h" |
| 38 |
#include "src/gpu/ganesh/GrAppliedClip.h" |
| 39 |
#include "src/gpu/ganesh/GrAuditTrail.h" |
| 40 |
#include "src/gpu/ganesh/GrBuffer.h" |
| 41 |
#include "src/gpu/ganesh/GrCaps.h" |
| 42 |
#include "src/gpu/ganesh/GrColorSpaceXform.h" |
| 43 |
#include "src/gpu/ganesh/GrDeferredUpload.h" |
| 44 |
#include "src/gpu/ganesh/GrDistanceFieldGenFromVector.h" |
| 45 |
#include "src/gpu/ganesh/GrDrawOpAtlas.h" |
| 46 |
#include "src/gpu/ganesh/GrDrawOpTest.h" |
| 47 |
#include "src/gpu/ganesh/GrGeometryProcessor.h" |
| 48 |
#include "src/gpu/ganesh/GrMeshDrawTarget.h" |
| 49 |
#include "src/gpu/ganesh/GrOpFlushState.h" |
| 50 |
#include "src/gpu/ganesh/GrPaint.h" |
| 51 |
#include "src/gpu/ganesh/GrProcessorAnalysis.h" |
| 52 |
#include "src/gpu/ganesh/GrProcessorSet.h" |
| 53 |
#include "src/gpu/ganesh/GrRecordingContextPriv.h" |
| 54 |
#include "src/gpu/ganesh/GrResourceProvider.h" |
| 55 |
#include "src/gpu/ganesh/GrSamplerState.h" |
| 56 |
#include "src/gpu/ganesh/GrShaderCaps.h" |
| 57 |
#include "src/gpu/ganesh/GrSimpleMesh.h" |
| 58 |
#include "src/gpu/ganesh/GrStyle.h" |
| 59 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" |
| 60 |
#include "src/gpu/ganesh/SurfaceDrawContext.h" |
| 61 |
#include "src/gpu/ganesh/effects/GrBitmapTextGeoProc.h" |
| 62 |
#include "src/gpu/ganesh/effects/GrDistanceFieldGeoProc.h" |
| 63 |
#include "src/gpu/ganesh/geometry/GrStyledShape.h" |
| 64 |
#include "src/gpu/ganesh/ops/GrMeshDrawOp.h" |
| 65 |
#include "src/gpu/ganesh/ops/GrOp.h" |
| 66 |
#include "src/gpu/ganesh/ops/GrSimpleMeshDrawOpHelperWithStencil.h" |
| 67 |
#include "src/gpu/ganesh/ops/SmallPathAtlasMgr.h" |
| 68 |
#include "src/gpu/ganesh/ops/SmallPathShapeData.h" |
| 69 |
|
| 70 |
#if defined(GPU_TEST_UTILS) |
| 71 |
#include "src/base/SkRandom.h" |
| 72 |
#include "src/gpu/ganesh/GrTestUtils.h" |
| 73 |
#endif |
| 74 |
|
| 75 |
#include <algorithm> |
| 76 |
#include <cstddef> |
| 77 |
#include <cstdint> |
| 78 |
#include <utility> |
| 79 |
|
| 80 |
class GrDstProxyView; |
| 81 |
class GrProgramInfo; |
| 82 |
class GrSurfaceProxy; |
| 83 |
class SkArenaAlloc; |
| 84 |
enum class GrXferBarrierFlags; |
| 85 |
struct GrUserStencilSettings; |
| 86 |
|
| 87 |
using namespace skia_private; |
| 88 |
|
| 89 |
#if !defined(SK_ENABLE_OPTIMIZE_SIZE) |
| 90 |
|
| 91 |
using MaskFormat = skgpu::MaskFormat; |
| 92 |
|
| 93 |
namespace skgpu::ganesh { |
| 94 |
|
| 95 |
namespace { |
| 96 |
|
| 97 |
// mip levels |
| 98 |
static constexpr SkScalar kIdealMinMIP = 12; |
| 99 |
static constexpr SkScalar kMaxMIP = 162; |
| 100 |
|
| 101 |
static constexpr SkScalar kMaxDim = 73; |
| 102 |
static constexpr SkScalar kMinSize = SK_ScalarHalf; |
| 103 |
static constexpr SkScalar kMaxSize = 2*kMaxMIP; |
| 104 |
|
| 105 |
//////////////////////////////////////////////////////////////////////////////// |
| 106 |
|
| 107 |
// padding around path bounds to allow for antialiased pixels |
| 108 |
static const int kAntiAliasPad = 1; |
| 109 |
|
| 110 |
class SmallPathOp final : public GrMeshDrawOp { |
| 111 |
private: |
| 112 |
using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 113 |
|
| 114 |
public: |
| 115 |
DEFINE_OP_CLASS_ID |
| 116 |
|
| 117 |
static GrOp::Owner Make(GrRecordingContext* context, |
| 118 |
GrPaint&& paint, |
| 119 |
const GrStyledShape& shape, |
| 120 |
const SkMatrix& viewMatrix, |
| 121 |
bool gammaCorrect, |
| 122 |
const GrUserStencilSettings* stencilSettings) { |
| 123 |
return Helper::FactoryHelper<SmallPathOp>(context, std::move(paint), shape, viewMatrix, |
| 124 |
gammaCorrect, stencilSettings); |
| 125 |
} |
| 126 |
|
| 127 |
SmallPathOp(GrProcessorSet* processorSet, const SkPMColor4f& color, const GrStyledShape& shape, |
| 128 |
const SkMatrix& viewMatrix, bool gammaCorrect, |
| 129 |
const GrUserStencilSettings* stencilSettings) |
| 130 |
: INHERITED(ClassID()) |
| 131 |
, fHelper(processorSet, GrAAType::kCoverage, stencilSettings) { |
| 132 |
SkASSERT(shape.hasUnstyledKey()); |
| 133 |
// Compute bounds |
| 134 |
this->setTransformedBounds(shape.bounds(), viewMatrix, HasAABloat::kYes, IsHairline::kNo); |
| 135 |
|
| 136 |
#if defined(SK_BUILD_FOR_ANDROID) && !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) |
| 137 |
fUsesDistanceField = true; |
| 138 |
#else |
| 139 |
// only use distance fields on desktop and Android framework to save space in the atlas |
| 140 |
fUsesDistanceField = this->bounds().width() > kMaxMIP || this->bounds().height() > kMaxMIP; |
| 141 |
#endif |
| 142 |
// always use distance fields if in perspective |
| 143 |
fUsesDistanceField = fUsesDistanceField || viewMatrix.hasPerspective(); |
| 144 |
|
| 145 |
fShapes.emplace_back(Entry{color, shape, viewMatrix}); |
| 146 |
|
| 147 |
fGammaCorrect = gammaCorrect; |
| 148 |
} |
| 149 |
|
| 150 |
const char* name() const override { return "SmallPathOp"; } |
| 151 |
|
| 152 |
void visitProxies(const GrVisitProxyFunc& func) const override { |
| 153 |
fHelper.visitProxies(func); |
| 154 |
} |
| 155 |
|
| 156 |
FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 157 |
|
| 158 |
GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 159 |
GrClampType clampType) override { |
| 160 |
return fHelper.finalizeProcessors(caps, clip, clampType, |
| 161 |
GrProcessorAnalysisCoverage::kSingleChannel, |
| 162 |
&fShapes.front().fColor, &fWideColor); |
| 163 |
} |
| 164 |
|
| 165 |
private: |
| 166 |
struct FlushInfo { |
| 167 |
sk_sp<const GrBuffer> fVertexBuffer; |
| 168 |
sk_sp<const GrBuffer> fIndexBuffer; |
| 169 |
GrGeometryProcessor* fGeometryProcessor; |
| 170 |
const GrSurfaceProxy** fPrimProcProxies; |
| 171 |
int fVertexOffset; |
| 172 |
int fInstancesToFlush; |
| 173 |
}; |
| 174 |
|
| 175 |
GrProgramInfo* programInfo() override { |
| 176 |
// TODO [PI]: implement |
| 177 |
return nullptr; |
| 178 |
} |
| 179 |
|
| 180 |
void onCreateProgramInfo(const GrCaps*, |
| 181 |
SkArenaAlloc*, |
| 182 |
const GrSurfaceProxyView& writeView, |
| 183 |
bool usesMSAASurface, |
| 184 |
GrAppliedClip&&, |
| 185 |
const GrDstProxyView&, |
| 186 |
GrXferBarrierFlags renderPassXferBarriers, |
| 187 |
GrLoadOp colorLoadOp) override { |
| 188 |
// We cannot surface the SmallPathOp's programInfo at record time. As currently |
| 189 |
// implemented, the GP is modified at flush time based on the number of pages in the |
| 190 |
// atlas. |
| 191 |
} |
| 192 |
|
| 193 |
void onPrePrepareDraws(GrRecordingContext*, |
| 194 |
const GrSurfaceProxyView& writeView, |
| 195 |
GrAppliedClip*, |
| 196 |
const GrDstProxyView&, |
| 197 |
GrXferBarrierFlags renderPassXferBarriers, |
| 198 |
GrLoadOp colorLoadOp) override { |
| 199 |
// TODO [PI]: implement |
| 200 |
} |
| 201 |
|
| 202 |
void onPrepareDraws(GrMeshDrawTarget* target) override { |
| 203 |
int instanceCount = fShapes.size(); |
| 204 |
|
| 205 |
auto atlasMgr = target->smallPathAtlasManager(); |
| 206 |
if (!atlasMgr) { |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
static constexpr int kMaxTextures = GrDistanceFieldPathGeoProc::kMaxTextures; |
| 211 |
static_assert(GrBitmapTextGeoProc::kMaxTextures == kMaxTextures); |
| 212 |
|
| 213 |
FlushInfo flushInfo; |
| 214 |
flushInfo.fPrimProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures); |
| 215 |
|
| 216 |
int numActiveProxies; |
| 217 |
const GrSurfaceProxyView* views = atlasMgr->getViews(&numActiveProxies); |
| 218 |
for (int i = 0; i < numActiveProxies; ++i) { |
| 219 |
// This op does not know its atlas proxies when it is added to a OpsTasks, so the |
| 220 |
// proxies don't get added during the visitProxies call. Thus we add them here. |
| 221 |
flushInfo.fPrimProcProxies[i] = views[i].proxy(); |
| 222 |
target->sampledProxyArray()->push_back(views[i].proxy()); |
| 223 |
} |
| 224 |
|
| 225 |
// Setup GrGeometryProcessor |
| 226 |
const SkMatrix& ctm = fShapes[0].fViewMatrix; |
| 227 |
SkMatrix invert; |
| 228 |
if (fHelper.usesLocalCoords()) { |
| 229 |
if (!ctm.invert(&invert)) { |
| 230 |
return; |
| 231 |
} |
| 232 |
} |
| 233 |
if (fUsesDistanceField) { |
| 234 |
uint32_t flags = 0; |
| 235 |
// Still need to key off of ctm to pick the right shader for the transformed quad |
| 236 |
flags |= ctm.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0; |
| 237 |
flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0; |
| 238 |
flags |= fGammaCorrect ? kGammaCorrect_DistanceFieldEffectFlag : 0; |
| 239 |
flags |= fWideColor ? kWideColor_DistanceFieldEffectFlag : 0; |
| 240 |
// We always use Point3 for position |
| 241 |
flags |= kPerspective_DistanceFieldEffectFlag; |
| 242 |
|
| 243 |
flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make( |
| 244 |
target->allocator(), *target->caps().shaderCaps(), |
| 245 |
views, numActiveProxies, GrSamplerState::Filter::kLinear, |
| 246 |
invert, flags); |
| 247 |
} else { |
| 248 |
flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make( |
| 249 |
target->allocator(), *target->caps().shaderCaps(), this->color(), fWideColor, |
| 250 |
/*colorSpaceXform=*/nullptr, views, numActiveProxies, |
| 251 |
GrSamplerState::Filter::kNearest, MaskFormat::kA8, invert, false); |
| 252 |
} |
| 253 |
|
| 254 |
// allocate vertices |
| 255 |
const size_t kVertexStride = flushInfo.fGeometryProcessor->vertexStride(); |
| 256 |
|
| 257 |
// We need to make sure we don't overflow a 32 bit int when we request space in the |
| 258 |
// makeVertexSpace call below. |
| 259 |
if (instanceCount > SK_MaxS32 / GrResourceProvider::NumVertsPerNonAAQuad()) { |
| 260 |
return; |
| 261 |
} |
| 262 |
VertexWriter vertices = target->makeVertexWriter( |
| 263 |
kVertexStride, GrResourceProvider::NumVertsPerNonAAQuad() * instanceCount, |
| 264 |
&flushInfo.fVertexBuffer, &flushInfo.fVertexOffset); |
| 265 |
|
| 266 |
flushInfo.fIndexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer(); |
| 267 |
if (!vertices || !flushInfo.fIndexBuffer) SK_UNLIKELY { |
| 268 |
SkDebugf("Could not allocate vertices\n"); |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
flushInfo.fInstancesToFlush = 0; |
| 273 |
for (int i = 0; i < instanceCount; i++) { |
| 274 |
const Entry& args = fShapes[i]; |
| 275 |
|
| 276 |
skgpu::ganesh::SmallPathShapeData* shapeData; |
| 277 |
if (fUsesDistanceField) { |
| 278 |
// get mip level |
| 279 |
SkScalar maxScale; |
| 280 |
const SkRect& bounds = args.fShape.bounds(); |
| 281 |
if (args.fViewMatrix.hasPerspective()) { |
| 282 |
// approximate the scale since we can't get it from the matrix |
| 283 |
SkRect xformedBounds; |
| 284 |
args.fViewMatrix.mapRect(&xformedBounds, bounds); |
| 285 |
maxScale = SkScalarAbs(std::max(xformedBounds.width() / bounds.width(), |
| 286 |
xformedBounds.height() / bounds.height())); |
| 287 |
} else { |
| 288 |
maxScale = SkScalarAbs(args.fViewMatrix.getMaxScale()); |
| 289 |
} |
| 290 |
SkScalar maxDim = std::max(bounds.width(), bounds.height()); |
| 291 |
// We try to create the DF at a 2^n scaled path resolution (1/2, 1, 2, 4, etc.) |
| 292 |
// In the majority of cases this will yield a crisper rendering. |
| 293 |
SkScalar mipScale = 1.0f; |
| 294 |
// Our mipscale is the maxScale clamped to the next highest power of 2 |
| 295 |
if (maxScale <= SK_ScalarHalf) { |
| 296 |
SkScalar log = SkScalarFloorToScalar(SkScalarLog2(SkScalarInvert(maxScale))); |
| 297 |
mipScale = SkScalarPow(2, -log); |
| 298 |
} else if (maxScale > SK_Scalar1) { |
| 299 |
SkScalar log = SkScalarCeilToScalar(SkScalarLog2(maxScale)); |
| 300 |
mipScale = SkScalarPow(2, log); |
| 301 |
} |
| 302 |
// Log2 isn't very precise at values close to a power of 2, |
| 303 |
// so add a little tolerance here. A little bit of scaling up is fine. |
| 304 |
SkASSERT(maxScale <= mipScale + SK_ScalarNearlyZero); |
| 305 |
|
| 306 |
SkScalar mipSize = mipScale*SkScalarAbs(maxDim); |
| 307 |
// For sizes less than kIdealMinMIP we want to use as large a distance field as we can |
| 308 |
// so we can preserve as much detail as possible. However, we can't scale down more |
| 309 |
// than a 1/4 of the size without artifacts. So the idea is that we pick the mipsize |
| 310 |
// just bigger than the ideal, and then scale down until we are no more than 4x the |
| 311 |
// original mipsize. |
| 312 |
if (mipSize < kIdealMinMIP) { |
| 313 |
SkScalar newMipSize = mipSize; |
| 314 |
do { |
| 315 |
newMipSize *= 2; |
| 316 |
} while (newMipSize < kIdealMinMIP); |
| 317 |
while (newMipSize > 4 * mipSize) { |
| 318 |
newMipSize *= 0.25f; |
| 319 |
} |
| 320 |
mipSize = newMipSize; |
| 321 |
} |
| 322 |
|
| 323 |
SkScalar desiredDimension = std::min(mipSize, kMaxMIP); |
| 324 |
int ceilDesiredDimension = SkScalarCeilToInt(desiredDimension); |
| 325 |
|
| 326 |
// check to see if df path is cached |
| 327 |
shapeData = atlasMgr->findOrCreate(args.fShape, ceilDesiredDimension); |
| 328 |
if (!shapeData->fAtlasLocator.plotLocator().isValid()) { |
| 329 |
SkScalar scale = desiredDimension / maxDim; |
| 330 |
|
| 331 |
if (!this->addDFPathToAtlas(target, |
| 332 |
&flushInfo, |
| 333 |
atlasMgr, |
| 334 |
shapeData, |
| 335 |
args.fShape, |
| 336 |
ceilDesiredDimension, |
| 337 |
scale)) { |
| 338 |
atlasMgr->deleteCacheEntry(shapeData); |
| 339 |
continue; |
| 340 |
} |
| 341 |
} |
| 342 |
} else { |
| 343 |
// check to see if bitmap path is cached |
| 344 |
shapeData = atlasMgr->findOrCreate(args.fShape, args.fViewMatrix); |
| 345 |
if (!shapeData->fAtlasLocator.plotLocator().isValid()) { |
| 346 |
if (!this->addBMPathToAtlas(target, |
| 347 |
&flushInfo, |
| 348 |
atlasMgr, |
| 349 |
shapeData, |
| 350 |
args.fShape, |
| 351 |
args.fViewMatrix)) { |
| 352 |
atlasMgr->deleteCacheEntry(shapeData); |
| 353 |
continue; |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
auto uploadTarget = target->deferredUploadTarget(); |
| 359 |
atlasMgr->setUseToken(shapeData, uploadTarget->tokenTracker()->nextDrawToken()); |
| 360 |
|
| 361 |
this->writePathVertices(vertices, VertexColor(args.fColor, fWideColor), |
| 362 |
args.fViewMatrix, shapeData); |
| 363 |
flushInfo.fInstancesToFlush++; |
| 364 |
} |
| 365 |
|
| 366 |
this->flush(target, &flushInfo); |
| 367 |
} |
| 368 |
|
| 369 |
bool addToAtlasWithRetry(GrMeshDrawTarget* target, |
| 370 |
FlushInfo* flushInfo, |
| 371 |
skgpu::ganesh::SmallPathAtlasMgr* atlasMgr, |
| 372 |
int width, |
| 373 |
int height, |
| 374 |
const void* image, |
| 375 |
const SkRect& bounds, |
| 376 |
int srcInset, |
| 377 |
skgpu::ganesh::SmallPathShapeData* shapeData) const { |
| 378 |
auto resourceProvider = target->resourceProvider(); |
| 379 |
auto uploadTarget = target->deferredUploadTarget(); |
| 380 |
|
| 381 |
auto code = atlasMgr->addToAtlas(resourceProvider, uploadTarget, width, height, |
| 382 |
image, &shapeData->fAtlasLocator); |
| 383 |
if (GrDrawOpAtlas::ErrorCode::kError == code) { |
| 384 |
return false; |
| 385 |
} |
| 386 |
|
| 387 |
if (GrDrawOpAtlas::ErrorCode::kTryAgain == code) { |
| 388 |
this->flush(target, flushInfo); |
| 389 |
|
| 390 |
code = atlasMgr->addToAtlas(resourceProvider, uploadTarget, width, height, |
| 391 |
image, &shapeData->fAtlasLocator); |
| 392 |
} |
| 393 |
|
| 394 |
shapeData->fAtlasLocator.insetSrc(srcInset); |
| 395 |
shapeData->fBounds = bounds; |
| 396 |
|
| 397 |
return GrDrawOpAtlas::ErrorCode::kSucceeded == code; |
| 398 |
} |
| 399 |
|
| 400 |
bool addDFPathToAtlas(GrMeshDrawTarget* target, |
| 401 |
FlushInfo* flushInfo, |
| 402 |
skgpu::ganesh::SmallPathAtlasMgr* atlasMgr, |
| 403 |
skgpu::ganesh::SmallPathShapeData* shapeData, |
| 404 |
const GrStyledShape& shape, |
| 405 |
uint32_t dimension, |
| 406 |
SkScalar scale) const { |
| 407 |
const SkRect& bounds = shape.bounds(); |
| 408 |
|
| 409 |
// generate bounding rect for bitmap draw |
| 410 |
SkRect scaledBounds = bounds; |
| 411 |
// scale to mip level size |
| 412 |
scaledBounds.fLeft *= scale; |
| 413 |
scaledBounds.fTop *= scale; |
| 414 |
scaledBounds.fRight *= scale; |
| 415 |
scaledBounds.fBottom *= scale; |
| 416 |
// subtract out integer portion of origin |
| 417 |
// (SDF created will be placed with fractional offset burnt in) |
| 418 |
SkScalar dx = SkScalarFloorToScalar(scaledBounds.fLeft); |
| 419 |
SkScalar dy = SkScalarFloorToScalar(scaledBounds.fTop); |
| 420 |
scaledBounds.offset(-dx, -dy); |
| 421 |
// get integer boundary |
| 422 |
SkIRect devPathBounds; |
| 423 |
scaledBounds.roundOut(&devPathBounds); |
| 424 |
// place devBounds at origin with padding to allow room for antialiasing |
| 425 |
int width = devPathBounds.width() + 2 * kAntiAliasPad; |
| 426 |
int height = devPathBounds.height() + 2 * kAntiAliasPad; |
| 427 |
devPathBounds = SkIRect::MakeWH(width, height); |
| 428 |
SkScalar translateX = kAntiAliasPad - dx; |
| 429 |
SkScalar translateY = kAntiAliasPad - dy; |
| 430 |
|
| 431 |
// draw path to bitmap |
| 432 |
SkMatrix drawMatrix; |
| 433 |
drawMatrix.setScale(scale, scale); |
| 434 |
drawMatrix.postTranslate(translateX, translateY); |
| 435 |
|
| 436 |
SkASSERT(devPathBounds.fLeft == 0); |
| 437 |
SkASSERT(devPathBounds.fTop == 0); |
| 438 |
SkASSERT(devPathBounds.width() > 0); |
| 439 |
SkASSERT(devPathBounds.height() > 0); |
| 440 |
|
| 441 |
// setup signed distance field storage |
| 442 |
SkIRect dfBounds = devPathBounds.makeOutset(SK_DistanceFieldPad, SK_DistanceFieldPad); |
| 443 |
width = dfBounds.width(); |
| 444 |
height = dfBounds.height(); |
| 445 |
// TODO We should really generate this directly into the plot somehow |
| 446 |
SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char)); |
| 447 |
|
| 448 |
SkPath path = shape.asPath(); |
| 449 |
// Generate signed distance field directly from SkPath |
| 450 |
bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dfStorage.get(), |
| 451 |
path, drawMatrix, width, height, |
| 452 |
width * sizeof(unsigned char)); |
| 453 |
if (!succeed) { |
| 454 |
// setup bitmap backing |
| 455 |
SkAutoPixmapStorage dst; |
| 456 |
if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(), devPathBounds.height()))) { |
| 457 |
return false; |
| 458 |
} |
| 459 |
sk_bzero(dst.writable_addr(), dst.computeByteSize()); |
| 460 |
|
| 461 |
// rasterize path |
| 462 |
SkPaint paint; |
| 463 |
paint.setStyle(SkPaint::kFill_Style); |
| 464 |
paint.setAntiAlias(true); |
| 465 |
|
| 466 |
skcpu::Draw draw; |
| 467 |
|
| 468 |
SkRasterClip rasterClip; |
| 469 |
rasterClip.setRect(devPathBounds); |
| 470 |
draw.fRC = &rasterClip; |
| 471 |
draw.fCTM = &drawMatrix; |
| 472 |
draw.fDst = dst; |
| 473 |
|
| 474 |
draw.drawPathCoverage(path, paint); |
| 475 |
|
| 476 |
// Generate signed distance field |
| 477 |
SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(), |
| 478 |
(const unsigned char*)dst.addr(), |
| 479 |
dst.width(), dst.height(), dst.rowBytes()); |
| 480 |
} |
| 481 |
|
| 482 |
SkRect drawBounds = SkRect::Make(devPathBounds).makeOffset(-translateX, -translateY); |
| 483 |
drawBounds.fLeft /= scale; |
| 484 |
drawBounds.fTop /= scale; |
| 485 |
drawBounds.fRight /= scale; |
| 486 |
drawBounds.fBottom /= scale; |
| 487 |
|
| 488 |
return this->addToAtlasWithRetry(target, flushInfo, atlasMgr, |
| 489 |
width, height, dfStorage.get(), |
| 490 |
drawBounds, SK_DistanceFieldPad, shapeData); |
| 491 |
} |
| 492 |
|
| 493 |
bool addBMPathToAtlas(GrMeshDrawTarget* target, |
| 494 |
FlushInfo* flushInfo, |
| 495 |
skgpu::ganesh::SmallPathAtlasMgr* atlasMgr, |
| 496 |
skgpu::ganesh::SmallPathShapeData* shapeData, |
| 497 |
const GrStyledShape& shape, |
| 498 |
const SkMatrix& ctm) const { |
| 499 |
const SkRect& bounds = shape.bounds(); |
| 500 |
if (bounds.isEmpty()) { |
| 501 |
return false; |
| 502 |
} |
| 503 |
SkMatrix drawMatrix(ctm); |
| 504 |
SkScalar tx = ctm.getTranslateX(); |
| 505 |
SkScalar ty = ctm.getTranslateY(); |
| 506 |
tx -= SkScalarFloorToScalar(tx); |
| 507 |
ty -= SkScalarFloorToScalar(ty); |
| 508 |
drawMatrix.set(SkMatrix::kMTransX, tx); |
| 509 |
drawMatrix.set(SkMatrix::kMTransY, ty); |
| 510 |
SkRect shapeDevBounds; |
| 511 |
drawMatrix.mapRect(&shapeDevBounds, bounds); |
| 512 |
SkScalar dx = SkScalarFloorToScalar(shapeDevBounds.fLeft); |
| 513 |
SkScalar dy = SkScalarFloorToScalar(shapeDevBounds.fTop); |
| 514 |
|
| 515 |
// get integer boundary |
| 516 |
SkIRect devPathBounds; |
| 517 |
shapeDevBounds.roundOut(&devPathBounds); |
| 518 |
// place devBounds at origin with padding to allow room for antialiasing |
| 519 |
int width = devPathBounds.width() + 2 * kAntiAliasPad; |
| 520 |
int height = devPathBounds.height() + 2 * kAntiAliasPad; |
| 521 |
devPathBounds = SkIRect::MakeWH(width, height); |
| 522 |
SkScalar translateX = kAntiAliasPad - dx; |
| 523 |
SkScalar translateY = kAntiAliasPad - dy; |
| 524 |
|
| 525 |
SkASSERT(devPathBounds.fLeft == 0); |
| 526 |
SkASSERT(devPathBounds.fTop == 0); |
| 527 |
SkASSERT(devPathBounds.width() > 0); |
| 528 |
SkASSERT(devPathBounds.height() > 0); |
| 529 |
|
| 530 |
SkPath path = shape.asPath(); |
| 531 |
// setup bitmap backing |
| 532 |
SkAutoPixmapStorage dst; |
| 533 |
if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(), devPathBounds.height()))) { |
| 534 |
return false; |
| 535 |
} |
| 536 |
sk_bzero(dst.writable_addr(), dst.computeByteSize()); |
| 537 |
|
| 538 |
// rasterize path |
| 539 |
SkPaint paint; |
| 540 |
paint.setStyle(SkPaint::kFill_Style); |
| 541 |
paint.setAntiAlias(true); |
| 542 |
|
| 543 |
skcpu::Draw draw; |
| 544 |
|
| 545 |
SkRasterClip rasterClip; |
| 546 |
rasterClip.setRect(devPathBounds); |
| 547 |
drawMatrix.postTranslate(translateX, translateY); |
| 548 |
draw.fRC = &rasterClip; |
| 549 |
draw.fCTM = &drawMatrix; |
| 550 |
draw.fDst = dst; |
| 551 |
|
| 552 |
draw.drawPathCoverage(path, paint); |
| 553 |
|
| 554 |
SkRect drawBounds = SkRect::Make(devPathBounds).makeOffset(-translateX, -translateY); |
| 555 |
|
| 556 |
return this->addToAtlasWithRetry(target, flushInfo, atlasMgr, |
| 557 |
dst.width(), dst.height(), dst.addr(), |
| 558 |
drawBounds, 0, shapeData); |
| 559 |
} |
| 560 |
|
| 561 |
void writePathVertices(VertexWriter& vertices, |
| 562 |
const VertexColor& color, |
| 563 |
const SkMatrix& ctm, |
| 564 |
const skgpu::ganesh::SmallPathShapeData* shapeData) const { |
| 565 |
SkRect translatedBounds(shapeData->fBounds); |
| 566 |
if (!fUsesDistanceField) { |
| 567 |
translatedBounds.offset(SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransX)), |
| 568 |
SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransY))); |
| 569 |
} |
| 570 |
|
| 571 |
// set up texture coordinates |
| 572 |
auto texCoords = VertexWriter::TriStripFromUVs(shapeData->fAtlasLocator.getUVs()); |
| 573 |
|
| 574 |
if (fUsesDistanceField) { |
| 575 |
SkPoint3 out[4]; |
| 576 |
ctm.mapPointsToHomogeneous(out, translatedBounds.toQuad()); |
| 577 |
|
| 578 |
vertices << out[0] << color << texCoords.l << texCoords.t; |
| 579 |
vertices << out[3] << color << texCoords.l << texCoords.b; |
| 580 |
vertices << out[1] << color << texCoords.r << texCoords.t; |
| 581 |
vertices << out[2] << color << texCoords.r << texCoords.b; |
| 582 |
} else { |
| 583 |
vertices.writeQuad(VertexWriter::TriStripFromRect(translatedBounds), |
| 584 |
color, |
| 585 |
texCoords); |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
void flush(GrMeshDrawTarget* target, FlushInfo* flushInfo) const { |
| 590 |
auto atlasMgr = target->smallPathAtlasManager(); |
| 591 |
if (!atlasMgr) { |
| 592 |
return; |
| 593 |
} |
| 594 |
|
| 595 |
int numActiveProxies; |
| 596 |
const GrSurfaceProxyView* views = atlasMgr->getViews(&numActiveProxies); |
| 597 |
|
| 598 |
GrGeometryProcessor* gp = flushInfo->fGeometryProcessor; |
| 599 |
if (gp->numTextureSamplers() != numActiveProxies) { |
| 600 |
for (int i = gp->numTextureSamplers(); i < numActiveProxies; ++i) { |
| 601 |
flushInfo->fPrimProcProxies[i] = views[i].proxy(); |
| 602 |
// This op does not know its atlas proxies when it is added to a OpsTasks, so the |
| 603 |
// proxies don't get added during the visitProxies call. Thus we add them here. |
| 604 |
target->sampledProxyArray()->push_back(views[i].proxy()); |
| 605 |
} |
| 606 |
// During preparation the number of atlas pages has increased. |
| 607 |
// Update the proxies used in the GP to match. |
| 608 |
if (fUsesDistanceField) { |
| 609 |
reinterpret_cast<GrDistanceFieldPathGeoProc*>(gp)->addNewViews( |
| 610 |
views, numActiveProxies, GrSamplerState::Filter::kLinear); |
| 611 |
} else { |
| 612 |
reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews( |
| 613 |
views, numActiveProxies, GrSamplerState::Filter::kNearest); |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
if (flushInfo->fInstancesToFlush) { |
| 618 |
GrSimpleMesh* mesh = target->allocMesh(); |
| 619 |
mesh->setIndexedPatterned(flushInfo->fIndexBuffer, |
| 620 |
GrResourceProvider::NumIndicesPerNonAAQuad(), |
| 621 |
flushInfo->fInstancesToFlush, |
| 622 |
GrResourceProvider::MaxNumNonAAQuads(), |
| 623 |
flushInfo->fVertexBuffer, |
| 624 |
GrResourceProvider::NumVertsPerNonAAQuad(), |
| 625 |
flushInfo->fVertexOffset); |
| 626 |
target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies, |
| 627 |
GrPrimitiveType::kTriangles); |
| 628 |
flushInfo->fVertexOffset += GrResourceProvider::NumVertsPerNonAAQuad() * |
| 629 |
flushInfo->fInstancesToFlush; |
| 630 |
flushInfo->fInstancesToFlush = 0; |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
| 635 |
auto pipeline = fHelper.createPipeline(flushState); |
| 636 |
|
| 637 |
flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline, |
| 638 |
fHelper.stencilSettings()); |
| 639 |
} |
| 640 |
|
| 641 |
const SkPMColor4f& color() const { return fShapes[0].fColor; } |
| 642 |
bool usesDistanceField() const { return fUsesDistanceField; } |
| 643 |
|
| 644 |
CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override { |
| 645 |
SmallPathOp* that = t->cast<SmallPathOp>(); |
| 646 |
if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
| 647 |
return CombineResult::kCannotCombine; |
| 648 |
} |
| 649 |
|
| 650 |
if (this->usesDistanceField() != that->usesDistanceField()) { |
| 651 |
return CombineResult::kCannotCombine; |
| 652 |
} |
| 653 |
|
| 654 |
const SkMatrix& thisCtm = this->fShapes[0].fViewMatrix; |
| 655 |
const SkMatrix& thatCtm = that->fShapes[0].fViewMatrix; |
| 656 |
|
| 657 |
if (this->usesDistanceField()) { |
| 658 |
// Need to make sure local matrices are identical |
| 659 |
if (fHelper.usesLocalCoords() && !SkMatrixPriv::CheapEqual(thisCtm, thatCtm)) { |
| 660 |
return CombineResult::kCannotCombine; |
| 661 |
} |
| 662 |
|
| 663 |
// Depending on the ctm we may have a different shader for SDF paths |
| 664 |
if (thisCtm.isScaleTranslate() != thatCtm.isScaleTranslate() || |
| 665 |
thisCtm.isSimilarity() != thatCtm.isSimilarity()) { |
| 666 |
return CombineResult::kCannotCombine; |
| 667 |
} |
| 668 |
} else { |
| 669 |
if (thisCtm.hasPerspective() != thatCtm.hasPerspective()) { |
| 670 |
return CombineResult::kCannotCombine; |
| 671 |
} |
| 672 |
|
| 673 |
// We can position on the cpu unless we're in perspective, |
| 674 |
// but also need to make sure local matrices are identical |
| 675 |
if ((thisCtm.hasPerspective() || fHelper.usesLocalCoords()) && |
| 676 |
!SkMatrixPriv::CheapEqual(thisCtm, thatCtm)) { |
| 677 |
return CombineResult::kCannotCombine; |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
fShapes.push_back_n(that->fShapes.size(), that->fShapes.begin()); |
| 682 |
fWideColor |= that->fWideColor; |
| 683 |
return CombineResult::kMerged; |
| 684 |
} |
| 685 |
|
| 686 |
#if defined(GPU_TEST_UTILS) |
| 687 |
SkString onDumpInfo() const override { |
| 688 |
SkString string; |
| 689 |
for (const auto& geo : fShapes) { |
| 690 |
string.appendf("Color: 0x%08x\n", geo.fColor.toBytes_RGBA()); |
| 691 |
} |
| 692 |
string += fHelper.dumpInfo(); |
| 693 |
return string; |
| 694 |
} |
| 695 |
#endif |
| 696 |
|
| 697 |
bool fUsesDistanceField; |
| 698 |
|
| 699 |
struct Entry { |
| 700 |
SkPMColor4f fColor; |
| 701 |
GrStyledShape fShape; |
| 702 |
SkMatrix fViewMatrix; |
| 703 |
}; |
| 704 |
|
| 705 |
STArray<1, Entry> fShapes; |
| 706 |
Helper fHelper; |
| 707 |
bool fGammaCorrect; |
| 708 |
bool fWideColor; |
| 709 |
|
| 710 |
using INHERITED = GrMeshDrawOp; |
| 711 |
}; |
| 712 |
|
| 713 |
} // anonymous namespace |
| 714 |
|
| 715 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 716 |
|
| 717 |
PathRenderer::CanDrawPath SmallPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
| 718 |
if (!args.fCaps->shaderCaps()->fShaderDerivativeSupport) { |
| 719 |
return CanDrawPath::kNo; |
| 720 |
} |
| 721 |
// If the shape has no key then we won't get any reuse. |
| 722 |
if (!args.fShape->hasUnstyledKey()) { |
| 723 |
return CanDrawPath::kNo; |
| 724 |
} |
| 725 |
// This only supports filled paths, however, the caller may apply the style to make a filled |
| 726 |
// path and try again. |
| 727 |
if (!args.fShape->style().isSimpleFill()) { |
| 728 |
return CanDrawPath::kNo; |
| 729 |
} |
| 730 |
// This does non-inverse coverage-based antialiased fills. |
| 731 |
if (GrAAType::kCoverage != args.fAAType) { |
| 732 |
return CanDrawPath::kNo; |
| 733 |
} |
| 734 |
// TODO: Support inverse fill |
| 735 |
if (args.fShape->inverseFilled()) { |
| 736 |
return CanDrawPath::kNo; |
| 737 |
} |
| 738 |
|
| 739 |
SkScalar scaleFactors[2] = { 1, 1 }; |
| 740 |
// TODO: handle perspective distortion |
| 741 |
if (!args.fViewMatrix->hasPerspective() && !args.fViewMatrix->getMinMaxScales(scaleFactors)) { |
| 742 |
return CanDrawPath::kNo; |
| 743 |
} |
| 744 |
// For affine transformations, too much shear can produce artifacts. |
| 745 |
if (!scaleFactors[0] || scaleFactors[1]/scaleFactors[0] > 4) { |
| 746 |
return CanDrawPath::kNo; |
| 747 |
} |
| 748 |
// Only support paths with bounds within kMaxDim by kMaxDim, |
| 749 |
// scaled to have bounds within kMaxSize by kMaxSize. |
| 750 |
// The goal is to accelerate rendering of lots of small paths that may be scaling. |
| 751 |
SkRect bounds = args.fShape->styledBounds(); |
| 752 |
SkScalar minDim = std::min(bounds.width(), bounds.height()); |
| 753 |
SkScalar maxDim = std::max(bounds.width(), bounds.height()); |
| 754 |
SkScalar minSize = minDim * SkScalarAbs(scaleFactors[0]); |
| 755 |
SkScalar maxSize = maxDim * SkScalarAbs(scaleFactors[1]); |
| 756 |
if (maxDim > kMaxDim || kMinSize > minSize || maxSize > kMaxSize) { |
| 757 |
return CanDrawPath::kNo; |
| 758 |
} |
| 759 |
|
| 760 |
return CanDrawPath::kYes; |
| 761 |
} |
| 762 |
|
| 763 |
bool SmallPathRenderer::onDrawPath(const DrawPathArgs& args) { |
| 764 |
GR_AUDIT_TRAIL_AUTO_FRAME(args.fContext->priv().auditTrail(), |
| 765 |
"SmallPathRenderer::onDrawPath"); |
| 766 |
|
| 767 |
// we've already bailed on inverse filled paths, so this is safe |
| 768 |
SkASSERT(!args.fShape->isEmpty()); |
| 769 |
SkASSERT(args.fShape->hasUnstyledKey()); |
| 770 |
|
| 771 |
GrOp::Owner op = SmallPathOp::Make( |
| 772 |
args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix, |
| 773 |
args.fGammaCorrect, args.fUserStencilSettings); |
| 774 |
args.fSurfaceDrawContext->addDrawOp(args.fClip, std::move(op)); |
| 775 |
|
| 776 |
return true; |
| 777 |
} |
| 778 |
|
| 779 |
} // namespace skgpu::ganesh |
| 780 |
|
| 781 |
#if defined(GPU_TEST_UTILS) |
| 782 |
|
| 783 |
GR_DRAW_OP_TEST_DEFINE(SmallPathOp) { |
| 784 |
SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 785 |
bool gammaCorrect = random->nextBool(); |
| 786 |
|
| 787 |
// This path renderer only allows fill styles. |
| 788 |
GrStyledShape shape(GrTest::TestPath(random), GrStyle::SimpleFill()); |
| 789 |
return skgpu::ganesh::SmallPathOp::Make(context, |
| 790 |
std::move(paint), |
| 791 |
shape, |
| 792 |
viewMatrix, |
| 793 |
gammaCorrect, |
| 794 |
GrGetRandomStencil(random, context)); |
| 795 |
} |
| 796 |
|
| 797 |
#endif // defined(GPU_TEST_UTILS) |
| 798 |
|
| 799 |
#endif // SK_ENABLE_OPTIMIZE_SIZE |