diff --git a/src/gpu/ganesh/Device.cpp b/src/gpu/ganesh/Device.cpp index d61e230914..70f1944868 100644 --- a/src/gpu/ganesh/Device.cpp +++ b/src/gpu/ganesh/Device.cpp @@ -168,11 +168,12 @@ bool init_vertices_paint(skgpu::ganesh::SurfaceDrawContext* sdc, const SkMatrix& ctm, SkBlender* blender, bool hasColors, + SkIPoint ditherOrigin, GrPaint* grPaint) { if (hasColors) { - return SkPaintToGrPaintWithBlend(sdc, skPaint, ctm, blender, grPaint); + return SkPaintToGrPaintWithBlend(sdc, skPaint, ctm, blender, ditherOrigin, grPaint); } else { - return SkPaintToGrPaint(sdc, skPaint, ctm, grPaint); + return SkPaintToGrPaint(sdc, skPaint, ctm, ditherOrigin, grPaint); } } @@ -434,6 +435,7 @@ void Device::drawPaint(const SkPaint& paint) { if (!SkPaintToGrPaint(fSurfaceDrawContext.get(), paint, this->localToDevice(), + this->ditherOrigin(), &grPaint)) { return; } @@ -474,6 +476,7 @@ void Device::drawPoints(SkCanvas::PointMode mode, SkSpan pts, con if (SkPaintToGrPaint(fSurfaceDrawContext.get(), paint, this->localToDevice(), + this->ditherOrigin(), &grPaint)) { SkPath path = SkPath::Line(pts[0], pts[1]); path.setIsVolatile(true); @@ -494,6 +497,7 @@ void Device::drawPoints(SkCanvas::PointMode mode, SkSpan pts, con if (SkPaintToGrPaint(fSurfaceDrawContext.get(), paint, this->localToDevice(), + this->ditherOrigin(), &grPaint)) { fSurfaceDrawContext->drawStrokedLine(this->clip(), std::move(grPaint), @@ -535,7 +539,7 @@ void Device::drawPoints(SkCanvas::PointMode mode, SkSpan pts, con } GrPaint grPaint; - if (!SkPaintToGrPaint(fSurfaceDrawContext.get(), paint, this->localToDevice(), &grPaint)) { + if (!SkPaintToGrPaint(fSurfaceDrawContext.get(), paint, this->localToDevice(), this->ditherOrigin(), &grPaint)) { return; } @@ -561,12 +565,13 @@ void Device::drawRect(const SkRect& rect, const SkPaint& paint) { GrStyledShape shape(rect, style); GrBlurUtils::DrawShapeWithMaskFilter(fContext.get(), fSurfaceDrawContext.get(), - this->clip(), paint, this->localToDevice(), shape); + this->clip(), paint, this->localToDevice(), shape, + this->ditherOrigin()); return; } GrPaint grPaint; - if (!SkPaintToGrPaint(fSurfaceDrawContext.get(), paint, this->localToDevice(), &grPaint)) { + if (!SkPaintToGrPaint(fSurfaceDrawContext.get(), paint, this->localToDevice(), this->ditherOrigin(), &grPaint)) { return; } @@ -629,7 +634,8 @@ void Device::drawRRect(const SkRRect& rrect, const SkPaint& paint) { GrStyledShape shape(rrect, style); GrBlurUtils::DrawShapeWithMaskFilter(fContext.get(), fSurfaceDrawContext.get(), - this->clip(), paint, this->localToDevice(), shape); + this->clip(), paint, this->localToDevice(), shape, + this->ditherOrigin()); return; } @@ -639,6 +645,7 @@ void Device::drawRRect(const SkRRect& rrect, const SkPaint& paint) { if (!SkPaintToGrPaint(fSurfaceDrawContext.get(), paint, this->localToDevice(), + this->ditherOrigin(), &grPaint)) { return; } @@ -671,6 +678,7 @@ void Device::drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPain if (!SkPaintToGrPaint(fSurfaceDrawContext.get(), paint, this->localToDevice(), + this->ditherOrigin(), &grPaint)) { return; } @@ -694,7 +702,7 @@ void Device::drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPain GrStyledShape shape(path, paint); GrBlurUtils::DrawShapeWithMaskFilter(fContext.get(), fSurfaceDrawContext.get(), this->clip(), - paint, this->localToDevice(), shape); + paint, this->localToDevice(), shape, this->ditherOrigin()); } ///////////////////////////////////////////////////////////////////////////// @@ -712,6 +720,7 @@ void Device::drawRegion(const SkRegion& region, const SkPaint& paint) { if (!SkPaintToGrPaint(fSurfaceDrawContext.get(), paint, this->localToDevice(), + this->ditherOrigin(), &grPaint)) { return; } @@ -732,7 +741,7 @@ void Device::drawOval(const SkRect& oval, const SkPaint& paint) { } GrPaint grPaint; - if (!SkPaintToGrPaint(fSurfaceDrawContext.get(), paint, this->localToDevice(), &grPaint)) { + if (!SkPaintToGrPaint(fSurfaceDrawContext.get(), paint, this->localToDevice(), this->ditherOrigin(), &grPaint)) { return; } @@ -749,7 +758,7 @@ void Device::drawArc(const SkArc& arc, const SkPaint& paint) { return; } GrPaint grPaint; - if (!SkPaintToGrPaint(fSurfaceDrawContext.get(), paint, this->localToDevice(), &grPaint)) { + if (!SkPaintToGrPaint(fSurfaceDrawContext.get(), paint, this->localToDevice(), this->ditherOrigin(), &grPaint)) { return; } @@ -777,6 +786,7 @@ void Device::drawPath(const SkPath& origSrcPath, const SkPaint& paint) { if (!SkPaintToGrPaint(fSurfaceDrawContext.get(), paint, this->localToDevice(), + this->ditherOrigin(), &grPaint)) { return; } @@ -789,7 +799,7 @@ void Device::drawPath(const SkPath& origSrcPath, const SkPaint& paint) { GrStyledShape shape(origSrcPath, paint); GrBlurUtils::DrawShapeWithMaskFilter(fContext.get(), fSurfaceDrawContext.get(), this->clip(), - paint, this->localToDevice(), shape); + paint, this->localToDevice(), shape, this->ditherOrigin()); } sk_sp Device::createImageFilteringBackend(const SkSurfaceProps& surfaceProps, @@ -978,6 +988,7 @@ void Device::drawViewLattice(GrSurfaceProxyView view, *paint, this->localToDevice(), /*shaderFP=*/nullptr, + this->ditherOrigin(), &grPaint)) { return; } @@ -1041,6 +1052,7 @@ void Device::drawVertices(const SkVertices* vertices, this->localToDevice(), blender.get(), info.hasColors(), + this->ditherOrigin(), &grPaint)) { return; } @@ -1065,6 +1077,7 @@ void Device::drawMesh(const SkMesh& mesh, sk_sp blender, const SkPain this->localToDevice(), blender.get(), SkMeshSpecificationPriv::HasColors(*mesh.spec()), + this->ditherOrigin(), &grPaint)) { return; } @@ -1113,6 +1126,7 @@ void Device::drawAtlas(SkSpan xform, paint, this->localToDevice(), blender.get(), + this->ditherOrigin(), &grPaint)) { return; } @@ -1120,6 +1134,7 @@ void Device::drawAtlas(SkSpan xform, if (!SkPaintToGrPaint(fSurfaceDrawContext.get(), paint, this->localToDevice(), + this->ditherOrigin(), &grPaint)) { return; } @@ -1151,7 +1166,8 @@ void Device::onDrawGlyphRunList(SkCanvas* canvas, this->localToDevice(), glyphRunList, this->strikeDeviceInfo(), - paint); + paint, + this->ditherOrigin()); } } @@ -1447,10 +1463,14 @@ void Device::drawSlug(SkCanvas* canvas, const sktext::gpu::Slug* slug, const SkP const SkPaint& paint, sk_sp subRunStorage, sktext::gpu::RendererData) { - auto[drawingClip, op] = AtlasTextOp::Make( - fSurfaceDrawContext.get(), subRun, - this->clip(), this->localToDevice(), drawOrigin, paint, - std::move(subRunStorage)); + auto[drawingClip, op] = AtlasTextOp::Make(fSurfaceDrawContext.get(), + subRun, + this->clip(), + this->localToDevice(), + drawOrigin, + paint, + this->ditherOrigin(), + std::move(subRunStorage)); if (op != nullptr) { fSurfaceDrawContext->addDrawOp(drawingClip, std::move(op)); } diff --git a/src/gpu/ganesh/Device.h b/src/gpu/ganesh/Device.h index 91f67fa7c4..638562d75b 100644 --- a/src/gpu/ganesh/Device.h +++ b/src/gpu/ganesh/Device.h @@ -83,8 +83,8 @@ namespace gpu { namespace skgpu::ganesh { class SurfaceContext; -class SurfaceFillContext; class SurfaceDrawContext; +class SurfaceFillContext; /** * Subclass of SkDevice, which directs all drawing to the GrGpu owned by the canvas. @@ -99,6 +99,12 @@ public: GrSurfaceProxyView readSurfaceView(); GrRenderTargetProxy* targetProxy(); + // Offset from this device's pixels to the global pixel grid, used to keep dithering in + // phase across layers. Zero when the device is not pixel-aligned to global. + SkIPoint ditherOrigin() const { + return this->isPixelAlignedToGlobal() ? this->getOrigin() : SkIPoint{0, 0}; + } + GrRecordingContext* recordingContext() const override { return fContext.get(); } SkRecorder* baseRecorder() const override { SkASSERT(fContext); diff --git a/src/gpu/ganesh/Device_drawTexture.cpp b/src/gpu/ganesh/Device_drawTexture.cpp index f8dfe8f34a..c5dfab4e15 100644 --- a/src/gpu/ganesh/Device_drawTexture.cpp +++ b/src/gpu/ganesh/Device_drawTexture.cpp @@ -366,7 +366,8 @@ void Device::drawEdgeAAImage(const SkImage* image, } GrPaint grPaint; - if (!SkPaintToGrPaintReplaceShader(sdc, paint, localToDevice, std::move(fp), &grPaint)) { + if (!SkPaintToGrPaintReplaceShader(sdc, paint, localToDevice, std::move(fp), + this->ditherOrigin(), &grPaint)) { return; } @@ -498,7 +499,7 @@ void Device::drawCoverageMask(const SkSpecialImage* mask, SurfaceDrawContext* sdc = fSurfaceDrawContext.get(); GrPaint grPaint; // Any shading is done in local space which we want to draw to the device. - SkPaintToGrPaint(sdc, paint, localToDevice, &grPaint); + SkPaintToGrPaint(sdc, paint, localToDevice, this->ditherOrigin(), &grPaint); grPaint.setCoverageFragmentProcessor(std::move(coverageFP)); GrAA aa = fSurfaceDrawContext->chooseAA(paint); @@ -739,7 +740,7 @@ bool Device::drawBlurredRRect(const SkRRect& rrect, const SkPaint& paint, SkPaint skPaint = paint; skPaint.setMaskFilter(nullptr); GrPaint grPaint; - SkPaintToGrPaint(sdc, skPaint, localToDevice, &grPaint); + SkPaintToGrPaint(sdc, skPaint, localToDevice, this->ditherOrigin(), &grPaint); if (skgpu::BlurIsEffectivelyIdentity(deviceSigma)) { sdc->drawShape(clip, std::move(grPaint), GrAA::kYes, localToDevice, GrStyledShape(rrect)); diff --git a/src/gpu/ganesh/GrBlurUtils.cpp b/src/gpu/ganesh/GrBlurUtils.cpp index 5a823fb810..961e4e3d2b 100644 --- a/src/gpu/ganesh/GrBlurUtils.cpp +++ b/src/gpu/ganesh/GrBlurUtils.cpp @@ -1588,13 +1588,14 @@ void DrawShapeWithMaskFilter(GrRecordingContext* rContext, const GrClip* clip, const SkPaint& paint, const SkMatrix& ctm, - const GrStyledShape& shape) { + const GrStyledShape& shape, + SkIPoint ditherOrigin) { if (rContext->abandoned()) { return; } GrPaint grPaint; - if (!SkPaintToGrPaint(sdc, paint, ctm, &grPaint)) { + if (!SkPaintToGrPaint(sdc, paint, ctm, ditherOrigin, &grPaint)) { return; } diff --git a/src/gpu/ganesh/GrBlurUtils.h b/src/gpu/ganesh/GrBlurUtils.h index 1f84751d89..d9a27c40af 100644 --- a/src/gpu/ganesh/GrBlurUtils.h +++ b/src/gpu/ganesh/GrBlurUtils.h @@ -8,6 +8,7 @@ #ifndef GrBlurUtils_DEFINED #define GrBlurUtils_DEFINED +#include "include/core/SkPoint.h" #include "include/core/SkRefCnt.h" #include "include/core/SkScalar.h" #include "src/gpu/SkBackingFit.h" @@ -70,7 +71,8 @@ void DrawShapeWithMaskFilter(GrRecordingContext*, const GrClip*, const SkPaint&, const SkMatrix&, - const GrStyledShape&); + const GrStyledShape&, + SkIPoint ditherOrigin); /** * Draw a shape handling the mask filter. The mask filter is not optional. diff --git a/src/gpu/ganesh/SkGr.cpp b/src/gpu/ganesh/SkGr.cpp index 6f8324ec98..cf85e67621 100644 --- a/src/gpu/ganesh/SkGr.cpp +++ b/src/gpu/ganesh/SkGr.cpp @@ -308,7 +308,8 @@ static std::unique_ptr make_dither_effect( GrRecordingContext* rContext, std::unique_ptr inputFP, float range, - const GrCaps* caps) { + const GrCaps* caps, + SkIPoint ditherOrigin) { if (range == 0 || inputFP == nullptr) { return inputFP; } @@ -348,11 +349,14 @@ static std::unique_ptr make_dither_effect( std::move(tex), kPremul_SkAlphaType, SkMatrix::I(), sampler, *caps); static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, "uniform half range;" + // Offset from this render target's pixels to the global pixel grid, so that a layer + // dithers in the same phase as the surface it is composited into. + "uniform float2 ditherOrigin;" "uniform shader inputFP;" "uniform shader table;" "half4 main(float2 xy) {" "half4 color = inputFP.eval(xy);" - "half value = table.eval(sk_FragCoord.xy).a - 0.5;" // undo the bias in the table + "half value = table.eval(sk_FragCoord.xy + ditherOrigin).a - 0.5;" // undo the bias // For each color channel, add the random offset to the channel value and then clamp // between 0 and alpha to keep the color premultiplied. "return half4(clamp(color.rgb + value * range, 0.0, color.a), color.a);" @@ -361,6 +365,7 @@ static std::unique_ptr make_dither_effect( return GrSkSLFP::Make(effect, "Dither", /*inputFP=*/nullptr, GrSkSLFP::OptFlags::kPreservesOpaqueInput, "range", range, + "ditherOrigin", SkV2{(float)ditherOrigin.x(), (float)ditherOrigin.y()}, "inputFP", std::move(inputFP), "table", GrSkSLFP::IgnoreOptFlags(std::move(te))); } @@ -372,6 +377,7 @@ static inline bool skpaint_to_grpaint_impl( const SkMatrix& ctm, std::optional> shaderFP, SkBlender* primColorBlender, + SkIPoint ditherOrigin, GrPaint* grPaint) { const GrColorInfo& dstColorInfo = sdc->colorInfo(); const SkSurfaceProps& surfaceProps = sdc->surfaceProps(); @@ -512,7 +518,7 @@ static inline bool skpaint_to_grpaint_impl( float ditherRange = skgpu::DitherRangeForConfig(ct); GrRecordingContext* context = sdc->recordingContext(); paintFP = make_dither_effect( - context, std::move(paintFP), ditherRange, context->priv().caps()); + context, std::move(paintFP), ditherRange, context->priv().caps(), ditherOrigin); } #endif @@ -561,12 +567,14 @@ static inline bool skpaint_to_grpaint_impl( bool SkPaintToGrPaint(skgpu::ganesh::SurfaceDrawContext* sdc, const SkPaint& skPaint, const SkMatrix& ctm, + SkIPoint ditherOrigin, GrPaint* grPaint) { return skpaint_to_grpaint_impl(sdc, skPaint, ctm, /*shaderFP=*/std::nullopt, /*primColorBlender=*/nullptr, + ditherOrigin, grPaint); } @@ -575,12 +583,14 @@ bool SkPaintToGrPaintReplaceShader(skgpu::ganesh::SurfaceDrawContext* sdc, const SkPaint& skPaint, const SkMatrix& ctm, std::unique_ptr shaderFP, + SkIPoint ditherOrigin, GrPaint* grPaint) { return skpaint_to_grpaint_impl(sdc, skPaint, ctm, std::move(shaderFP), /*primColorBlender=*/nullptr, + ditherOrigin, grPaint); } @@ -590,11 +600,13 @@ bool SkPaintToGrPaintWithBlend(skgpu::ganesh::SurfaceDrawContext* sdc, const SkPaint& skPaint, const SkMatrix& ctm, SkBlender* primColorBlender, + SkIPoint ditherOrigin, GrPaint* grPaint) { return skpaint_to_grpaint_impl(sdc, skPaint, ctm, /*shaderFP=*/std::nullopt, primColorBlender, + ditherOrigin, grPaint); } diff --git a/src/gpu/ganesh/SkGr.h b/src/gpu/ganesh/SkGr.h index 0a1965eebf..381c424253 100644 --- a/src/gpu/ganesh/SkGr.h +++ b/src/gpu/ganesh/SkGr.h @@ -10,6 +10,7 @@ #include "include/core/SkBlendMode.h" #include "include/core/SkColor.h" +#include "include/core/SkPoint.h" #include "include/core/SkRefCnt.h" #include "include/core/SkSamplingOptions.h" #include "include/core/SkTileMode.h" @@ -27,9 +28,9 @@ #include #include -class GrMippedBitmap; class GrColorInfo; class GrFragmentProcessor; +class GrMippedBitmap; class GrPaint; class GrRecordingContext; class GrSurfaceProxy; @@ -94,6 +95,7 @@ static constexpr GrSamplerState::WrapMode SkTileModeToWrapMode(SkTileMode tileMo bool SkPaintToGrPaint(skgpu::ganesh::SurfaceDrawContext*, const SkPaint& skPaint, const SkMatrix& ctm, + SkIPoint ditherOrigin, GrPaint* grPaint); /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor, if not null. @@ -103,6 +105,7 @@ bool SkPaintToGrPaintReplaceShader(skgpu::ganesh::SurfaceDrawContext*, const SkPaint& skPaint, const SkMatrix& ctm, std::unique_ptr shaderFP, + SkIPoint ditherOrigin, GrPaint* grPaint); /** Blends the SkPaint's shader (or color if no shader) with the color which specified via a @@ -111,6 +114,7 @@ bool SkPaintToGrPaintWithBlend(skgpu::ganesh::SurfaceDrawContext* context, const SkPaint& skPaint, const SkMatrix& ctm, SkBlender* primColorBlender, + SkIPoint ditherOrigin, GrPaint* grPaint); //////////////////////////////////////////////////////////////////////////////// diff --git a/src/gpu/ganesh/SurfaceDrawContext.cpp b/src/gpu/ganesh/SurfaceDrawContext.cpp index c6caae4d25..45e6c26f6d 100644 --- a/src/gpu/ganesh/SurfaceDrawContext.cpp +++ b/src/gpu/ganesh/SurfaceDrawContext.cpp @@ -346,7 +346,8 @@ void SurfaceDrawContext::drawGlyphRunList(SkCanvas* canvas, const SkMatrix& viewMatrix, const sktext::GlyphRunList& glyphRunList, SkStrikeDeviceInfo strikeDeviceInfo, - const SkPaint& paint) { + const SkPaint& paint, + SkIPoint ditherOrigin) { ASSERT_SINGLE_OWNER RETURN_IF_ABANDONED SkDEBUGCODE(this->validate();) @@ -366,8 +367,14 @@ void SurfaceDrawContext::drawGlyphRunList(SkCanvas* canvas, const SkPaint& paint, sk_sp subRunStorage, sktext::gpu::RendererData) { - auto [drawingClip, op] = AtlasTextOp::Make( - this, subRun, clip, viewMatrix, drawOrigin, paint, std::move(subRunStorage)); + auto [drawingClip, op] = AtlasTextOp::Make(this, + subRun, + clip, + viewMatrix, + drawOrigin, + paint, + ditherOrigin, + std::move(subRunStorage)); if (op != nullptr) { this->addDrawOp(drawingClip, std::move(op)); } diff --git a/src/gpu/ganesh/SurfaceDrawContext.h b/src/gpu/ganesh/SurfaceDrawContext.h index 24e3838064..3b013551fb 100644 --- a/src/gpu/ganesh/SurfaceDrawContext.h +++ b/src/gpu/ganesh/SurfaceDrawContext.h @@ -507,13 +507,16 @@ public: * * @param viewMatrix transformation matrix * @param glyphRunList text, text positions, and paint. + * @param ditherOrigin offset from the target's pixels to the global pixel grid, so a + * dithered shader on the glyphs stays in phase inside a layer. */ void drawGlyphRunList(SkCanvas*, const GrClip*, const SkMatrix& viewMatrix, const sktext::GlyphRunList& glyphRunList, SkStrikeDeviceInfo strikeDeviceInfo, - const SkPaint& paint); + const SkPaint& paint, + SkIPoint ditherOrigin); /** * Adds the necessary signal and wait semaphores and adds the passed in SkDrawable to the diff --git a/src/gpu/ganesh/ops/AtlasTextOp.cpp b/src/gpu/ganesh/ops/AtlasTextOp.cpp index f6eb45f839..b197421089 100644 --- a/src/gpu/ganesh/ops/AtlasTextOp.cpp +++ b/src/gpu/ganesh/ops/AtlasTextOp.cpp @@ -96,13 +96,14 @@ SkPMColor4f calculate_colors(skgpu::ganesh::SurfaceDrawContext* sdc, const SkPaint& paint, const SkMatrix& matrix, MaskFormat maskFormat, + SkIPoint ditherOrigin, GrPaint* grPaint) { if (maskFormat == MaskFormat::kARGB) { - SkPaintToGrPaintReplaceShader(sdc, paint, matrix, nullptr, grPaint); + SkPaintToGrPaintReplaceShader(sdc, paint, matrix, nullptr, ditherOrigin, grPaint); float a = grPaint->getColor4f().fA; return {a, a, a, a}; } - SkPaintToGrPaint(sdc, paint, matrix, grPaint); + SkPaintToGrPaint(sdc, paint, matrix, ditherOrigin, grPaint); return grPaint->getColor4f(); } @@ -213,6 +214,7 @@ std::tuple AtlasTextOp::Make(SurfaceDrawContext* sdc const SkMatrix& viewMatrix, SkPoint drawOrigin, const SkPaint& paint, + SkIPoint ditherOrigin, sk_sp&& subRunStorage) { SkASSERT(subrun->glyphCount() != 0); const SkMatrix& positionMatrix = position_matrix(viewMatrix, drawOrigin); @@ -254,6 +256,7 @@ std::tuple AtlasTextOp::Make(SurfaceDrawContext* sdc paint, viewMatrix, subrun->maskFormat(), + ditherOrigin, &grPaint); auto geometry = AtlasTextOp::Geometry::Make(*subrun, diff --git a/src/gpu/ganesh/ops/AtlasTextOp.h b/src/gpu/ganesh/ops/AtlasTextOp.h index 3a2a265318..7dd226e4e6 100644 --- a/src/gpu/ganesh/ops/AtlasTextOp.h +++ b/src/gpu/ganesh/ops/AtlasTextOp.h @@ -63,6 +63,7 @@ public: const SkMatrix& viewMatrix, SkPoint drawOrigin, const SkPaint&, + SkIPoint ditherOrigin, sk_sp&& subRunStorage); ~AtlasTextOp() override { diff --git a/src/gpu/graphite/Device.cpp b/src/gpu/graphite/Device.cpp index 2a15400ef5..bfb8266fdb 100644 --- a/src/gpu/graphite/Device.cpp +++ b/src/gpu/graphite/Device.cpp @@ -1833,6 +1833,7 @@ void Device::drawGeometry(const Transform& localToDevice, localToDevice.matrix(), clip.drawBounds().asSkRect(), fDC->colorInfo(), + this->ditherOrigin(), keyGenFlags, paint.color()}; auto keyResult = shading.toKey(keyContext); diff --git a/src/gpu/graphite/Device.h b/src/gpu/graphite/Device.h index 68875c1b33..b944dfabe9 100644 --- a/src/gpu/graphite/Device.h +++ b/src/gpu/graphite/Device.h @@ -275,6 +275,12 @@ public: int testingOnly_pendingRenderSteps() const; #endif + // Offset from this device's pixels to the global pixel grid, used to keep dithering in + // phase across layers. Zero when the device is not pixel-aligned to global. + SkIPoint ditherOrigin() const { + return this->isPixelAlignedToGlobal() ? this->getOrigin() : SkIPoint{0, 0}; + } + private: class IntersectionTreeSet; diff --git a/src/gpu/graphite/KeyContext.cpp b/src/gpu/graphite/KeyContext.cpp index fce686a2ef..685de06c98 100644 --- a/src/gpu/graphite/KeyContext.cpp +++ b/src/gpu/graphite/KeyContext.cpp @@ -37,6 +37,7 @@ KeyContext::KeyContext(skgpu::graphite::Recorder* recorder, const SkM44& local2Dev, const SkRect& clipDrawBounds, const SkColorInfo& dstColorInfo, + SkIPoint ditherOrigin, SkEnumBitMask initialFlags, const SkColor4f& paintColor) : fCaps(recorder->priv().caps()) @@ -48,6 +49,7 @@ KeyContext::KeyContext(skgpu::graphite::Recorder* recorder, , fRTEffectDict(recorder->priv().runtimeEffectDictionary()) , fLocal2Dev(local2Dev) , fClipDrawBounds(clipDrawBounds) + , fDitherOrigin(ditherOrigin) , fLocalMatrix(nullptr) , fDstColorInfo(dstColorInfo) , fKeyGenFlags(initialFlags) { @@ -66,6 +68,7 @@ KeyContext::KeyContext(const KeyContext& other, , fRTEffectDict(other.fRTEffectDict) , fLocal2Dev(other.fLocal2Dev) , fClipDrawBounds(other.fClipDrawBounds) + , fDitherOrigin(other.fDitherOrigin) , fLocalMatrix(other.fLocalMatrix) , fDstColorInfo(other.fDstColorInfo) , fPaintColor(other.fPaintColor) diff --git a/src/gpu/graphite/KeyContext.h b/src/gpu/graphite/KeyContext.h index 3c5c1ef126..1b845ff09a 100644 --- a/src/gpu/graphite/KeyContext.h +++ b/src/gpu/graphite/KeyContext.h @@ -11,6 +11,7 @@ #include "include/core/SkImageInfo.h" #include "include/core/SkM44.h" #include "include/core/SkMatrix.h" +#include "include/core/SkPoint.h" #include "include/core/SkRect.h" #include "include/private/SkEnumBitMask.h" #include "include/private/SkOnlyStackAllocatable.h" @@ -76,6 +77,7 @@ public: const SkM44& local2Dev, const SkRect& clipDrawBounds, const SkColorInfo& dstColorInfo, + SkIPoint ditherOrigin, SkEnumBitMask initialFlags, const SkColor4f& paintColor); @@ -131,6 +133,9 @@ public: const SkRect& clipDrawBounds() const { return fClipDrawBounds; } + // Offset from this device's pixels to the global pixel grid that dithering is anchored to. + SkIPoint ditherOrigin() const { return fDitherOrigin; } + private: // Fields which will not change over the course of building a paint key const Caps* fCaps; @@ -142,6 +147,7 @@ private: sk_sp fRTEffectDict; SkM44 fLocal2Dev; SkRect fClipDrawBounds; + SkIPoint fDitherOrigin = {0, 0}; protected: // Fields that can be modified while walking a paint's effects for a key diff --git a/src/gpu/graphite/KeyHelpers.cpp b/src/gpu/graphite/KeyHelpers.cpp index 650b0ae2b5..d85ebf38f7 100644 --- a/src/gpu/graphite/KeyHelpers.cpp +++ b/src/gpu/graphite/KeyHelpers.cpp @@ -915,7 +915,9 @@ void add_dither_uniform_data(const KeyContext& keyContext, const DitherShaderBlock::DitherData& ditherData) { BEGIN_WRITE_UNIFORMS(keyContext, BuiltInCodeSnippetID::kDitherShader) - keyContext.pipelineDataGatherer()->writeHalf(ditherData.fRange); + auto gatherer = keyContext.pipelineDataGatherer(); + gatherer->writeHalf(ditherData.fRange); + gatherer->write(SkPoint::Make(ditherData.fOrigin.x(), ditherData.fOrigin.y())); } } // anonymous namespace @@ -2805,7 +2807,9 @@ void AddDitherBlock(const KeyContext& keyContext, SkColorType ct) { return; } - DitherShaderBlock::DitherData data(skgpu::DitherRangeForConfig(ct), std::move(proxy)); + DitherShaderBlock::DitherData data(skgpu::DitherRangeForConfig(ct), + keyContext.ditherOrigin(), + std::move(proxy)); DitherShaderBlock::AddBlock(keyContext, data); } diff --git a/src/gpu/graphite/KeyHelpers.h b/src/gpu/graphite/KeyHelpers.h index 181fe81034..a1eb11a93c 100644 --- a/src/gpu/graphite/KeyHelpers.h +++ b/src/gpu/graphite/KeyHelpers.h @@ -11,6 +11,7 @@ #include "include/core/SkBitmap.h" #include "include/core/SkBlendMode.h" #include "include/core/SkM44.h" +#include "include/core/SkPoint.h" #include "include/core/SkPoint3.h" #include "include/core/SkSamplingOptions.h" #include "include/core/SkShader.h" @@ -208,11 +209,14 @@ struct CoordClampShaderBlock { struct DitherShaderBlock { struct DitherData { - DitherData(float range, sk_sp proxy) + DitherData(float range, SkIPoint origin, sk_sp proxy) : fRange(range) + , fOrigin(origin) , fLUTProxy(std::move(proxy)) {} float fRange; + // Offset from this device's pixels to the global pixel grid the dither is anchored to. + SkIPoint fOrigin; sk_sp fLUTProxy; }; diff --git a/src/gpu/graphite/PaintParams.cpp b/src/gpu/graphite/PaintParams.cpp index f4411e085b..064f308bf9 100644 --- a/src/gpu/graphite/PaintParams.cpp +++ b/src/gpu/graphite/PaintParams.cpp @@ -679,6 +679,7 @@ UniquePaintParamsID ShadingParams::validateOpacityOptimization(const KeyContext& keyContext.local2Dev(), keyContext.clipDrawBounds(), keyContext.dstColorInfo(), + keyContext.ditherOrigin(), keyContext.flags(), opaqueShading.fPaint.color()}; diff --git a/src/gpu/graphite/ShaderCodeDictionary.cpp b/src/gpu/graphite/ShaderCodeDictionary.cpp index 5d2babbb9d..c695989a41 100644 --- a/src/gpu/graphite/ShaderCodeDictionary.cpp +++ b/src/gpu/graphite/ShaderCodeDictionary.cpp @@ -1541,7 +1541,8 @@ ShaderCodeDictionary::ShaderCodeDictionary( /*name=*/"Dither", /*staticFn=*/"sk_dither", SnippetRequirementFlags::kPriorStageOutput, - /*uniforms=*/{{{ "range", SkSLType::kHalf }}}, + /*uniforms=*/{{{ "range", SkSLType::kHalf }, + { "ditherOrigin", SkSLType::kFloat2 }}}, /*texturesAndSamplers=*/{ { "ditherLUT" } } }; diff --git a/src/sksl/sksl_graphite_frag.sksl b/src/sksl/sksl_graphite_frag.sksl index 72cbfcc834..4ae4ba6829 100644 --- a/src/sksl/sksl_graphite_frag.sksl +++ b/src/sksl/sksl_graphite_frag.sksl @@ -743,10 +743,12 @@ $pure half4 sk_hw_yuv_no_swizzle_image_shader(float2 coords, yuvToRGBMatrix, yuvToRGBXlateAlphaParam.xyz); } -$pure half4 sk_dither(half4 colorIn, half range, sampler2D lut) { +$pure half4 sk_dither(half4 colorIn, half range, float2 ditherOrigin, sampler2D lut) { const float kImgSize = 8; - half value = sample(lut, sk_FragCoord.xy / kImgSize).r - 0.5; // undo the bias in the table + // ditherOrigin anchors the pattern to the global pixel grid rather than to this device, so a + // layer dithers in the same phase as the surface it is composited into. + half value = sample(lut, (sk_FragCoord.xy + ditherOrigin) / kImgSize).r - 0.5; // undo the bias // For each color channel, add the random offset to the channel value and then clamp // between 0 and alpha to keep the color premultiplied. return half4(clamp(colorIn.rgb + value * range, 0.0, colorIn.a), colorIn.a); diff --git a/tools/ganesh/AtlasTextOpTools.cpp b/tools/ganesh/AtlasTextOpTools.cpp index 17b3cfe72c..25d4b13390 100644 --- a/tools/ganesh/AtlasTextOpTools.cpp +++ b/tools/ganesh/AtlasTextOpTools.cpp @@ -66,7 +66,14 @@ GrOp::Owner AtlasTextOpTools::CreateOp(skgpu::ganesh::SurfaceDrawContext* sdc, GrOp::Owner op; std::tie(std::ignore, op) = - AtlasTextOp::Make(sdc, subRun, nullptr, ctm, glyphRunList.origin(), skPaint, blob); + AtlasTextOp::Make(sdc, + subRun, + nullptr, + ctm, + glyphRunList.origin(), + skPaint, + /*ditherOrigin=*/SkIPoint{0, 0}, + blob); return op; }