| 1 |
/* |
| 2 |
* Copyright 2021 Google LLC |
| 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 |
#ifndef skgpu_graphite_Device_DEFINED |
| 9 |
#define skgpu_graphite_Device_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkBlender.h" |
| 12 |
#include "include/core/SkCanvas.h" |
| 13 |
#include "include/core/SkColor.h" |
| 14 |
#include "include/core/SkRect.h" |
| 15 |
#include "include/core/SkRefCnt.h" |
| 16 |
#include "include/core/SkSamplingOptions.h" |
| 17 |
#include "include/gpu/graphite/Recorder.h" |
| 18 |
#include "include/private/base/SkDebug.h" |
| 19 |
#include "include/private/base/SkSpan_impl.h" |
| 20 |
#include "src/base/SkEnumBitMask.h" |
| 21 |
#include "src/core/SkDevice.h" |
| 22 |
#include "src/gpu/graphite/ClipStack.h" |
| 23 |
#include "src/gpu/graphite/DrawOrder.h" |
| 24 |
#include "src/gpu/graphite/ResourceTypes.h" |
| 25 |
#include "src/gpu/graphite/geom/Rect.h" |
| 26 |
#include "src/gpu/graphite/geom/Transform.h" |
| 27 |
#include "src/text/gpu/SubRunContainer.h" |
| 28 |
#include "src/text/gpu/SubRunControl.h" |
| 29 |
|
| 30 |
#include <cstdint> |
| 31 |
#include <memory> |
| 32 |
#include <string_view> |
| 33 |
#include <utility> |
| 34 |
|
| 35 |
class SkColorInfo; |
| 36 |
class SkDrawable; |
| 37 |
class SkImage; |
| 38 |
class SkMatrix; |
| 39 |
class SkMesh; |
| 40 |
class SkPaint; |
| 41 |
class SkPath; |
| 42 |
class SkPixmap; |
| 43 |
class SkRRect; |
| 44 |
class SkRecorder; |
| 45 |
class SkRegion; |
| 46 |
class SkShader; |
| 47 |
class SkSpecialImage; |
| 48 |
class SkStrokeRec; |
| 49 |
class SkSurface; |
| 50 |
class SkSurfaceProps; |
| 51 |
class SkVertices; |
| 52 |
enum SkColorType : int; |
| 53 |
enum class SkBackingFit; |
| 54 |
enum class SkBlendMode; |
| 55 |
enum class SkClipOp; |
| 56 |
struct SkArc; |
| 57 |
struct SkISize; |
| 58 |
struct SkImageInfo; |
| 59 |
struct SkPoint; |
| 60 |
struct SkRSXform; |
| 61 |
|
| 62 |
namespace skgpu { |
| 63 |
enum class Budgeted : bool; |
| 64 |
enum class Mipmapped : bool; |
| 65 |
} |
| 66 |
namespace skif { class Backend; } |
| 67 |
namespace sktext { |
| 68 |
class GlyphRunList; |
| 69 |
namespace gpu { class Slug; } |
| 70 |
} |
| 71 |
|
| 72 |
namespace skgpu::graphite { |
| 73 |
|
| 74 |
class BoundsManager; |
| 75 |
class Clip; |
| 76 |
class DrawContext; |
| 77 |
class Geometry; |
| 78 |
class Image; |
| 79 |
class PaintParams; |
| 80 |
class PathAtlas; |
| 81 |
class Renderer; |
| 82 |
class Shape; |
| 83 |
class Task; |
| 84 |
class TextureProxy; |
| 85 |
class TextureProxyView; |
| 86 |
|
| 87 |
class Device final : public SkDevice { |
| 88 |
public: |
| 89 |
~Device() override; |
| 90 |
|
| 91 |
// If 'registerWithRecorder' is false, it is meant to be a short-lived Device that is managed |
| 92 |
// by the caller within a limited scope (such that it is guaranteed to go out of scope before |
| 93 |
// the Recorder can be snapped). |
| 94 |
static sk_sp<Device> Make(Recorder* recorder, |
| 95 |
sk_sp<TextureProxy>, |
| 96 |
SkISize deviceSize, |
| 97 |
const SkColorInfo&, |
| 98 |
const SkSurfaceProps&, |
| 99 |
LoadOp initialLoadOp, |
| 100 |
bool registerWithRecorder=true); |
| 101 |
// Convenience factory to create the underlying TextureProxy based on the configuration provided |
| 102 |
static sk_sp<Device> Make(Recorder*, |
| 103 |
const SkImageInfo&, |
| 104 |
Budgeted, |
| 105 |
Mipmapped, |
| 106 |
SkBackingFit, |
| 107 |
const SkSurfaceProps&, |
| 108 |
LoadOp initialLoadOp, |
| 109 |
std::string_view label, |
| 110 |
bool registerWithRecorder=true); |
| 111 |
|
| 112 |
Device* asGraphiteDevice() override { return this; } |
| 113 |
|
| 114 |
Recorder* recorder() const override { return fRecorder; } |
| 115 |
SkRecorder* baseRecorder() const override { return fRecorder; } |
| 116 |
|
| 117 |
// This call is triggered from the Recorder on its registered Devices. It is typically called |
| 118 |
// when the Recorder is abandoned or deleted. |
| 119 |
void abandonRecorder() { fRecorder = nullptr; } |
| 120 |
|
| 121 |
// Ensures clip elements are drawn that will clip previous draw calls, snaps all pending work |
| 122 |
// from the DrawContext as a RenderPassTask and records it in the Device's recorder. |
| 123 |
// |
| 124 |
// The behavior of this function depends on whether a drawContext is provided: |
| 125 |
// - If a drawContext is provided, then any flushed tasks will be added to that drawContext's |
| 126 |
// task list. Note, no lastTask will be recorded in this case. |
| 127 |
// - Else, flushed tasks are added to the root task list, and if this device is a scratch |
| 128 |
// device, the last task will be recorded. |
| 129 |
void flushPendingWork(DrawContext*); |
| 130 |
|
| 131 |
const Transform& localToDeviceTransform(); |
| 132 |
|
| 133 |
// Flushes any pending work to the recorder and then deregisters and abandons the recorder. |
| 134 |
void setImmutable() override; |
| 135 |
|
| 136 |
SkStrikeDeviceInfo strikeDeviceInfo() const override; |
| 137 |
|
| 138 |
TextureProxy* target(); |
| 139 |
// May be null if target is not sampleable. |
| 140 |
TextureProxyView readSurfaceView() const; |
| 141 |
// Can succeed if target is readable but not sampleable. Assumes 'subset' is contained in bounds |
| 142 |
sk_sp<Image> makeImageCopy(const SkIRect& subset, Budgeted, Mipmapped, SkBackingFit); |
| 143 |
|
| 144 |
// True if this Device represents an internal renderable surface that will go out of scope |
| 145 |
// before the next Recorder snap. |
| 146 |
// NOTE: Currently, there are two different notions of "scratch" that are being merged together. |
| 147 |
// 1. Devices whose targets are not instantiated (Device::Make). |
| 148 |
// 2. Devices that are not registered with the Recorder (Surface::MakeScratch). |
| 149 |
// |
| 150 |
// This function reflects notion #1, since the long-term plan will be that all Devices that are |
| 151 |
// not instantiated will also not be registered with the Recorder. For the time being, due to |
| 152 |
// shared atlas management, layer-backing Devices need to be registered with the Recorder but |
| 153 |
// are otherwise the canonical scratch device. |
| 154 |
// |
| 155 |
// Existing uses of Surface::MakeScratch() will migrate to using un-instantiated Devices with |
| 156 |
// the requirement that if the Device's target is being returned in a client-owned object |
| 157 |
// (e.g. SkImages::MakeWithFilter), that it should then be explicitly instantiated. Once scratch |
| 158 |
// tasks are fully organized in a graph and not automatically appended to the root task list, |
| 159 |
// this explicit instantiation will be responsible for moving the scratch tasks to the root list |
| 160 |
bool isScratchDevice() const; |
| 161 |
|
| 162 |
// Only used for scratch devices. |
| 163 |
sk_sp<Task> lastDrawTask() const; |
| 164 |
|
| 165 |
// Called by an Image wrapping this Device to mark that the pending contents of this Device |
| 166 |
// will be read by `recorder`, and specifically by `drawContext` (if non-null). Flushes any |
| 167 |
// necessary work (depending on scratch state) and records task dependencies. Returns true if |
| 168 |
// the caller does not need to track the Device on the Image anymore. |
| 169 |
bool notifyInUse(Recorder* recorder, DrawContext* drawContext); |
| 170 |
|
| 171 |
// Returns true if the Device has pending reads to the given texture |
| 172 |
bool hasPendingReads(const TextureProxy* texture) const; |
| 173 |
|
| 174 |
bool useDrawCoverageMaskForMaskFilters() const override { return true; } |
| 175 |
|
| 176 |
// Clipping |
| 177 |
void pushClipStack() override { fClip.save(); } |
| 178 |
void popClipStack() override { fClip.restore(); } |
| 179 |
|
| 180 |
bool isClipWideOpen() const override { |
| 181 |
return fClip.clipState() == ClipStack::ClipState::kWideOpen; |
| 182 |
} |
| 183 |
bool isClipEmpty() const override { |
| 184 |
return fClip.clipState() == ClipStack::ClipState::kEmpty; |
| 185 |
} |
| 186 |
bool isClipRect() const override { |
| 187 |
return fClip.clipState() == ClipStack::ClipState::kDeviceRect || |
| 188 |
fClip.clipState() == ClipStack::ClipState::kWideOpen; |
| 189 |
} |
| 190 |
|
| 191 |
bool isClipAntiAliased() const override; |
| 192 |
SkIRect devClipBounds() const override; |
| 193 |
void android_utils_clipAsRgn(SkRegion*) const override; |
| 194 |
|
| 195 |
void clipRect(const SkRect& rect, SkClipOp, bool aa) override; |
| 196 |
void clipRRect(const SkRRect& rrect, SkClipOp, bool aa) override; |
| 197 |
void clipPath(const SkPath& path, SkClipOp, bool aa) override; |
| 198 |
|
| 199 |
void clipRegion(const SkRegion& globalRgn, SkClipOp) override; |
| 200 |
void replaceClip(const SkIRect& rect) override; |
| 201 |
|
| 202 |
// Drawing |
| 203 |
void drawPaint(const SkPaint&) override; |
| 204 |
void drawRect(const SkRect& r, const SkPaint&) override; |
| 205 |
void drawOval(const SkRect& oval, const SkPaint&) override; |
| 206 |
void drawRRect(const SkRRect& rr, const SkPaint&) override; |
| 207 |
void drawArc(const SkArc& arc, const SkPaint&) override; |
| 208 |
void drawPoints(SkCanvas::PointMode, SkSpan<const SkPoint>, const SkPaint&) override; |
| 209 |
void drawPath(const SkPath& path, const SkPaint&) override; |
| 210 |
void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint&) override; |
| 211 |
|
| 212 |
// No need to specialize drawRegion or drawPatch as the default impls all route to drawPath, |
| 213 |
// drawRect, or drawVertices as desired. |
| 214 |
|
| 215 |
void drawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], |
| 216 |
SkCanvas::QuadAAFlags aaFlags, const SkColor4f& color, |
| 217 |
SkBlendMode mode) override; |
| 218 |
|
| 219 |
void drawEdgeAAImageSet(const SkCanvas::ImageSetEntry[], int count, |
| 220 |
const SkPoint dstClips[], const SkMatrix preViewMatrices[], |
| 221 |
const SkSamplingOptions&, const SkPaint&, |
| 222 |
SkCanvas::SrcRectConstraint) override; |
| 223 |
|
| 224 |
void drawImageRect(const SkImage*, const SkRect* src, const SkRect& dst, |
| 225 |
const SkSamplingOptions&, const SkPaint&, |
| 226 |
SkCanvas::SrcRectConstraint) override; |
| 227 |
|
| 228 |
void drawVertices(const SkVertices*, sk_sp<SkBlender>, const SkPaint&, bool) override; |
| 229 |
bool drawAsTiledImageRect(SkCanvas*, |
| 230 |
const SkImage*, |
| 231 |
const SkRect* src, |
| 232 |
const SkRect& dst, |
| 233 |
const SkSamplingOptions&, |
| 234 |
const SkPaint&, |
| 235 |
SkCanvas::SrcRectConstraint) override; |
| 236 |
// TODO: Implement these using per-edge AA quads and an inlined image shader program. |
| 237 |
void drawImageLattice(const SkImage*, const SkCanvas::Lattice&, |
| 238 |
const SkRect& dst, SkFilterMode, const SkPaint&) override {} |
| 239 |
void drawAtlas(SkSpan<const SkRSXform>, SkSpan<const SkRect>, SkSpan<const SkColor>, |
| 240 |
sk_sp<SkBlender>, const SkPaint&) override {} |
| 241 |
|
| 242 |
void drawDrawable(SkCanvas*, SkDrawable*, const SkMatrix*) override {} |
| 243 |
void drawMesh(const SkMesh&, sk_sp<SkBlender>, const SkPaint&) override {} |
| 244 |
|
| 245 |
// Special images and layers |
| 246 |
sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override; |
| 247 |
|
| 248 |
sk_sp<SkDevice> createDevice(const CreateInfo&, const SkPaint*) override; |
| 249 |
|
| 250 |
sk_sp<SkSpecialImage> snapSpecial(const SkIRect& subset, bool forceCopy = false) override; |
| 251 |
|
| 252 |
void drawSpecial(SkSpecialImage*, const SkMatrix& localToDevice, |
| 253 |
const SkSamplingOptions&, const SkPaint&, |
| 254 |
SkCanvas::SrcRectConstraint) override; |
| 255 |
void drawCoverageMask(const SkSpecialImage*, const SkMatrix& localToDevice, |
| 256 |
const SkSamplingOptions&, const SkPaint&) override; |
| 257 |
|
| 258 |
bool drawBlurredRRect(const SkRRect&, const SkPaint&, float deviceSigma) override; |
| 259 |
|
| 260 |
#if defined(GPU_TEST_UTILS) |
| 261 |
int testingOnly_pendingRenderSteps() const; |
| 262 |
#endif |
| 263 |
|
| 264 |
private: |
| 265 |
class IntersectionTreeSet; |
| 266 |
|
| 267 |
Device(Recorder*, sk_sp<DrawContext>); |
| 268 |
|
| 269 |
bool onReadPixels(const SkPixmap&, int x, int y) override; |
| 270 |
|
| 271 |
bool onWritePixels(const SkPixmap&, int x, int y) override; |
| 272 |
|
| 273 |
void onDrawGlyphRunList(SkCanvas*, const sktext::GlyphRunList&, const SkPaint&) override; |
| 274 |
|
| 275 |
void onClipShader(sk_sp<SkShader> shader) override; |
| 276 |
|
| 277 |
sk_sp<skif::Backend> createImageFilteringBackend(const SkSurfaceProps& surfaceProps, |
| 278 |
SkColorType colorType) const override; |
| 279 |
|
| 280 |
// Applies any path effect and modifies the geometry and style before calling drawGeometry(), |
| 281 |
// or forwards to drawGeometry directly if `pathEffect` is null. |
| 282 |
void drawGeometryWithPathEffect(const Transform&, |
| 283 |
Geometry&&, |
| 284 |
const PaintParams&, |
| 285 |
SkStrokeRec, |
| 286 |
const SkPathEffect* pathEffect); |
| 287 |
|
| 288 |
// Record a draw with the given style and paint effects, applying any analytic clipping or |
| 289 |
// depth-based clipping automatically based on the current clip stack state. |
| 290 |
// |
| 291 |
// All overridden SkDevice::draw() functions should bottom-out with calls to drawGeometry(). |
| 292 |
void drawGeometry(const Transform&, |
| 293 |
Geometry&&, |
| 294 |
const PaintParams&, |
| 295 |
SkStrokeRec); |
| 296 |
|
| 297 |
// Like drawGeometry() but is Shape-only, depth-only, fill-only, and lets the ClipStack define |
| 298 |
// the transform, clip, and DrawOrder (although Device still tracks stencil buffer usage). |
| 299 |
void drawClipShape(const Transform&, const Shape&, const Clip&, DrawOrder); |
| 300 |
|
| 301 |
sktext::gpu::AtlasDrawDelegate atlasDelegate(); |
| 302 |
// Handles primitive processing for atlas-based text |
| 303 |
void drawAtlasSubRun(const sktext::gpu::AtlasSubRun*, |
| 304 |
SkPoint drawOrigin, |
| 305 |
const SkPaint& paint, |
| 306 |
sk_sp<SkRefCnt> subRunStorage, |
| 307 |
sktext::gpu::RendererData); |
| 308 |
|
| 309 |
sk_sp<sktext::gpu::Slug> convertGlyphRunListToSlug(const sktext::GlyphRunList& glyphRunList, |
| 310 |
const SkPaint& paint) override; |
| 311 |
|
| 312 |
void drawSlug(SkCanvas*, const sktext::gpu::Slug* slug, const SkPaint& paint) override; |
| 313 |
|
| 314 |
// Returns the Renderer to draw the shape in the given style. If SkStrokeRec is a |
| 315 |
// stroke-and-fill, this returns the Renderer used for the fill portion and it can be assumed |
| 316 |
// that Renderer::TessellatedStrokes() will be used for the stroke portion. |
| 317 |
// |
| 318 |
// Depending on the preferred anti-aliasing quality and platform capabilities (such as compute |
| 319 |
// shader support), an atlas handler for path rendering may be returned alongside the chosen |
| 320 |
// Renderer. In that case, all fill, stroke, and stroke-and-fill styles should be rendered with |
| 321 |
// a single recorded CoverageMask draw and the shape data should be added to the provided atlas |
| 322 |
// handler to be scheduled for a coverage mask render. |
| 323 |
// |
| 324 |
// TODO: Renderers may have fallbacks (e.g. pre-chop large paths, or convert stroke to fill). |
| 325 |
// Are those handled inside ChooseRenderer() where it can modify the shape, stroke? or does it |
| 326 |
// return a retry error code? or does drawGeometry() handle all the fallbacks, knowing that |
| 327 |
// a particular shape type needs to be pre-chopped? |
| 328 |
// TODO: Move this into a RendererSelector object provided by the Context. |
| 329 |
std::pair<const Renderer*, PathAtlas*> chooseRenderer(const Transform& localToDevice, |
| 330 |
const Geometry&, |
| 331 |
const SkStrokeRec&, |
| 332 |
const Rect& drawBounds) const; |
| 333 |
|
| 334 |
// Ignoring specialized Shape renderers and the selected PathRendererStrategy, choose a |
| 335 |
// MSAA-requiring tessellation-based renderer for the shape and style. |
| 336 |
const Renderer* chooseMSAARenderer(const Shape&, |
| 337 |
const SkStrokeRec&, |
| 338 |
const Rect& drawBounds) const; |
| 339 |
|
| 340 |
bool needsFlushBeforeDraw(int numNewRenderSteps, DstReadStrategy); |
| 341 |
|
| 342 |
// Flush internal work, such as pending clip draws and atlas uploads, into the Device's DrawTask |
| 343 |
void internalFlush(); |
| 344 |
|
| 345 |
Recorder* fRecorder; |
| 346 |
sk_sp<DrawContext> fDC; |
| 347 |
// Scratch devices hold on to their last snapped DrawTask so that they can be directly |
| 348 |
// referenced when the device image is drawn into some other surface. |
| 349 |
// NOTE: For now, this task is still added to the root task list when the Device is flushed, but |
| 350 |
// in the long-term, these scratch draw tasks will only be executed if they are referenced by |
| 351 |
// some other task chain that makes it to the root list. |
| 352 |
sk_sp<Task> fLastTask; |
| 353 |
|
| 354 |
ClipStack fClip; |
| 355 |
|
| 356 |
// Tracks accumulated intersections for ordering dependent use of the color and depth attachment |
| 357 |
// (i.e. depth-based clipping, and transparent blending) |
| 358 |
std::unique_ptr<BoundsManager> fColorDepthBoundsManager; |
| 359 |
// Tracks disjoint stencil indices for all recordered draws |
| 360 |
std::unique_ptr<IntersectionTreeSet> fDisjointStencilSet; |
| 361 |
|
| 362 |
// Lazily updated Transform constructed from localToDevice()'s SkM44 |
| 363 |
Transform fCachedLocalToDevice; |
| 364 |
|
| 365 |
// The max depth value sent to the DrawContext, incremented so each draw has a unique value. |
| 366 |
PaintersDepth fCurrentDepth; |
| 367 |
|
| 368 |
// Even when MSAA is supported, small paths may be sent to the atlas for higher quality and to |
| 369 |
// avoid triggering MSAA overhead on a render pass. However, the number of paths is capped |
| 370 |
// per Device flush. |
| 371 |
int fAtlasedPathCount = 0; |
| 372 |
// True if this Device has been drawn into another Device, in which case that other Device |
| 373 |
// depends on this Device's prior contents, so flushing this device with pending work must |
| 374 |
// also flush anything else that samples from it. If this is false, it's safe to skip checking |
| 375 |
// tracked devices for dependencies. |
| 376 |
bool fMustFlushDependencies = false; |
| 377 |
|
| 378 |
const sktext::gpu::SubRunControl fSubRunControl; |
| 379 |
|
| 380 |
#if defined(SK_DEBUG) |
| 381 |
// Tracks the flushing state to ensure recursive flushing does not occur. |
| 382 |
bool fIsFlushing = false; |
| 383 |
|
| 384 |
// When not 0, this Device is an unregistered scratch device that is intended to go out of |
| 385 |
// scope before the Recorder is snapped. Assuming controlling code is valid, that means the |
| 386 |
// Device's recorder's next recording ID should still be the the recording ID at the time the |
| 387 |
// Device was created. If not, it means the Device lived too long and may not be flushing tasks |
| 388 |
// in the expected order. |
| 389 |
uint32_t fScopedRecordingID = 0; |
| 390 |
#endif |
| 391 |
|
| 392 |
friend class ClipStack; // for drawClipShape |
| 393 |
}; |
| 394 |
|
| 395 |
} // namespace skgpu::graphite |
| 396 |
|
| 397 |
#endif // skgpu_graphite_Device_DEFINED |