| 1 |
/* |
| 2 |
* Copyright 2010 The Android Open Source Project |
| 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 SkDevice_DEFINED |
| 9 |
#define SkDevice_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkBlender.h" // IWYU pragma: keep |
| 12 |
#include "include/core/SkCanvas.h" |
| 13 |
#include "include/core/SkClipOp.h" |
| 14 |
#include "include/core/SkColor.h" |
| 15 |
#include "include/core/SkImageInfo.h" |
| 16 |
#include "include/core/SkM44.h" |
| 17 |
#include "include/core/SkMatrix.h" |
| 18 |
#include "include/core/SkPoint.h" |
| 19 |
#include "include/core/SkRect.h" |
| 20 |
#include "include/core/SkRefCnt.h" |
| 21 |
#include "include/core/SkRegion.h" |
| 22 |
#include "include/core/SkSamplingOptions.h" |
| 23 |
#include "include/core/SkShader.h" |
| 24 |
#include "include/core/SkSize.h" |
| 25 |
#include "include/core/SkSpan.h" |
| 26 |
#include "include/core/SkSurfaceProps.h" |
| 27 |
#include "include/private/base/SkAssert.h" |
| 28 |
#include "include/private/base/SkNoncopyable.h" |
| 29 |
#include "include/private/base/SkTArray.h" |
| 30 |
#include "src/core/SkMatrixPriv.h" |
| 31 |
#include "src/shaders/SkShaderBase.h" |
| 32 |
|
| 33 |
#include <cstdint> |
| 34 |
#include <utility> |
| 35 |
|
| 36 |
struct SkArc; |
| 37 |
class SkColorSpace; |
| 38 |
class SkMesh; |
| 39 |
struct SkDrawShadowRec; |
| 40 |
class SkImageFilter; |
| 41 |
class SkRasterHandleAllocator; |
| 42 |
class SkSpecialImage; |
| 43 |
class GrRecordingContext; |
| 44 |
class SkData; |
| 45 |
class SkDrawable; |
| 46 |
class SkImage; |
| 47 |
class SkPaint; |
| 48 |
class SkPath; |
| 49 |
class SkPixmap; |
| 50 |
class SkRRect; |
| 51 |
class SkRecorder; |
| 52 |
class SkSurface; |
| 53 |
class SkVertices; |
| 54 |
enum SkColorType : int; |
| 55 |
enum class SkBlendMode; |
| 56 |
enum class SkScalerContextFlags : uint32_t; |
| 57 |
struct SkRSXform; |
| 58 |
|
| 59 |
namespace sktext { |
| 60 |
class GlyphRunList; |
| 61 |
} |
| 62 |
|
| 63 |
namespace skif { |
| 64 |
class Backend; |
| 65 |
class Mapping; |
| 66 |
} |
| 67 |
namespace skgpu::ganesh { |
| 68 |
class Device; |
| 69 |
} |
| 70 |
namespace skgpu::graphite { |
| 71 |
class Device; |
| 72 |
class Recorder; |
| 73 |
} |
| 74 |
namespace sktext::gpu { |
| 75 |
class SubRunControl; |
| 76 |
class Slug; |
| 77 |
} |
| 78 |
|
| 79 |
struct SkStrikeDeviceInfo { |
| 80 |
const SkSurfaceProps fSurfaceProps; |
| 81 |
const SkScalerContextFlags fScalerContextFlags; |
| 82 |
// This is a pointer so this can be compiled without SK_GPU_SUPPORT. |
| 83 |
const sktext::gpu::SubRunControl* const fSubRunControl; |
| 84 |
}; |
| 85 |
|
| 86 |
/** |
| 87 |
* SkDevice is the internal API and implementation that SkCanvas will use to perform rendering and |
| 88 |
* implement the saveLayer abstraction. A device wraps some pixel allocation (for non-document based |
| 89 |
* devices) or wraps some other container that stores rendering operations. The drawing operations |
| 90 |
* perform equivalently to their corresponding functions in SkCanvas except that the canvas is |
| 91 |
* responsible for all SkImageFilters. An image filter is applied by automatically creating a layer, |
| 92 |
* drawing the filter-less paint into the layer, and then evaluating the filter on the layer's |
| 93 |
* image. |
| 94 |
* |
| 95 |
* Each layer in an SkCanvas stack is represented by an SkDevice instance that was created by the |
| 96 |
* parent SkDevice (up to the canvas's base device). In most cases these devices will be pixel |
| 97 |
* aligned with one another but may differ in size based on the known extent of the active clip. In |
| 98 |
* complex image filtering scenarios, they may not be axis aligned, although the effective pixel |
| 99 |
* size should remain approximately equal across all devices in a canvas. |
| 100 |
* |
| 101 |
* While SkCanvas manages a single stack of layers and canvas transforms, SkDevice does not have a |
| 102 |
* stack of transforms. Instead, it has a single active transform that is modified as needed by |
| 103 |
* SkCanvas. However, SkDevices are the means by which SkCanvas manages the clip stack because each |
| 104 |
* layer's clip stack starts anew (although the layer's results are then clipped by its parent's |
| 105 |
* stack when it is restored). |
| 106 |
*/ |
| 107 |
class SkDevice : public SkRefCnt { |
| 108 |
public: |
| 109 |
SkDevice(const SkImageInfo&, const SkSurfaceProps&); |
| 110 |
|
| 111 |
// -- Surface properties and metadata |
| 112 |
|
| 113 |
/** |
| 114 |
* Return ImageInfo for this device. If the canvas is not backed by pixels |
| 115 |
* (cpu or gpu), then the info's ColorType will be kUnknown_SkColorType. |
| 116 |
*/ |
| 117 |
const SkImageInfo& imageInfo() const { return fInfo; } |
| 118 |
|
| 119 |
int width() const { return this->imageInfo().width(); } |
| 120 |
int height() const { return this->imageInfo().height(); } |
| 121 |
|
| 122 |
bool isOpaque() const { return this->imageInfo().isOpaque(); } |
| 123 |
|
| 124 |
// NOTE: Image dimensions as a rect, *not* the current restricted clip bounds. |
| 125 |
SkIRect bounds() const { return SkIRect::MakeWH(this->width(), this->height()); } |
| 126 |
SkISize size() const { return this->imageInfo().dimensions(); } |
| 127 |
|
| 128 |
/** |
| 129 |
* Return SurfaceProps for this device. |
| 130 |
*/ |
| 131 |
const SkSurfaceProps& surfaceProps() const { |
| 132 |
return fSurfaceProps; |
| 133 |
} |
| 134 |
|
| 135 |
SkScalerContextFlags scalerContextFlags() const; |
| 136 |
|
| 137 |
virtual SkStrikeDeviceInfo strikeDeviceInfo() const { |
| 138 |
return {fSurfaceProps, this->scalerContextFlags(), nullptr}; |
| 139 |
} |
| 140 |
|
| 141 |
// -- Direct pixel manipulation |
| 142 |
|
| 143 |
/** |
| 144 |
* Write the pixels in 'src' into this Device at the specified x,y offset. The caller is |
| 145 |
* responsible for "pre-clipping" the src. |
| 146 |
*/ |
| 147 |
bool writePixels(const SkPixmap& src, int x, int y) { return this->onWritePixels(src, x, y); } |
| 148 |
|
| 149 |
/** |
| 150 |
* Read pixels from this Device at the specified x,y offset into dst. The caller is |
| 151 |
* responsible for "pre-clipping" the dst |
| 152 |
*/ |
| 153 |
bool readPixels(const SkPixmap& dst, int x, int y) { return this->onReadPixels(dst, x, y); } |
| 154 |
|
| 155 |
/** |
| 156 |
* Try to get write-access to the pixels behind the device. If successful, this returns true |
| 157 |
* and fills-out the pixmap parameter. On success it also bumps the genID of the underlying |
| 158 |
* bitmap. |
| 159 |
* |
| 160 |
* On failure, returns false and ignores the pixmap parameter. |
| 161 |
*/ |
| 162 |
bool accessPixels(SkPixmap* pmap); |
| 163 |
|
| 164 |
/** |
| 165 |
* Try to get read-only-access to the pixels behind the device. If successful, this returns |
| 166 |
* true and fills-out the pixmap parameter. |
| 167 |
* |
| 168 |
* On failure, returns false and ignores the pixmap parameter. |
| 169 |
*/ |
| 170 |
bool peekPixels(SkPixmap*); |
| 171 |
|
| 172 |
|
| 173 |
// -- Device's transform (both current transform affecting draws, and its fixed global mapping) |
| 174 |
|
| 175 |
/** |
| 176 |
* Returns the transformation that maps from the local space to the device's coordinate space. |
| 177 |
*/ |
| 178 |
const SkM44& localToDevice44() const { return fLocalToDevice; } |
| 179 |
const SkMatrix& localToDevice() const { return fLocalToDevice33; } |
| 180 |
|
| 181 |
/** |
| 182 |
* Return the device's coordinate space transform: this maps from the device's coordinate space |
| 183 |
* into the global canvas' space (or root device space). This includes the translation |
| 184 |
* necessary to account for the device's origin. |
| 185 |
*/ |
| 186 |
const SkM44& deviceToGlobal() const { return fDeviceToGlobal; } |
| 187 |
/** |
| 188 |
* Return the inverse of getDeviceToGlobal(), mapping from the global canvas' space (or root |
| 189 |
* device space) into this device's coordinate space. |
| 190 |
*/ |
| 191 |
const SkM44& globalToDevice() const { return fGlobalToDevice; } |
| 192 |
/** |
| 193 |
* DEPRECATED: This asserts that 'getDeviceToGlobal' is a translation matrix with integer |
| 194 |
* components. In the future some SkDevices will have more complex device-to-global transforms, |
| 195 |
* so getDeviceToGlobal() or getRelativeTransform() should be used instead. |
| 196 |
*/ |
| 197 |
SkIPoint getOrigin() const; |
| 198 |
/** |
| 199 |
* Returns true when this device's pixel grid is axis aligned with the global coordinate space, |
| 200 |
* and any relative translation between the two spaces is in integer pixel units. |
| 201 |
*/ |
| 202 |
bool isPixelAlignedToGlobal() const; |
| 203 |
/** |
| 204 |
* Get the transformation from this device's coordinate system to the provided device space. |
| 205 |
* This transform can be used to draw this device into the provided device, such that once |
| 206 |
* that device is drawn to the root device, the net effect will be that this device's contents |
| 207 |
* have been transformed by the global CTM. |
| 208 |
*/ |
| 209 |
SkM44 getRelativeTransform(const SkDevice&) const; |
| 210 |
|
| 211 |
void setLocalToDevice(const SkM44& localToDevice) { |
| 212 |
fLocalToDevice = localToDevice; |
| 213 |
fLocalToDevice33 = fLocalToDevice.asM33(); |
| 214 |
fLocalToDeviceDirty = true; |
| 215 |
} |
| 216 |
void setGlobalCTM(const SkM44& ctm); |
| 217 |
|
| 218 |
// -- Device's clip bounds and stack manipulation |
| 219 |
|
| 220 |
/** |
| 221 |
* Return the bounds of the device in the coordinate space of the root canvas. The root device |
| 222 |
* will have its top-left at 0,0, but other devices such as those associated with saveLayer may |
| 223 |
* have a non-zero origin. |
| 224 |
*/ |
| 225 |
void getGlobalBounds(SkIRect* bounds) const { |
| 226 |
SkASSERT(bounds); |
| 227 |
*bounds = SkMatrixPriv::MapRect(fDeviceToGlobal, SkRect::Make(this->bounds())).roundOut(); |
| 228 |
} |
| 229 |
|
| 230 |
SkIRect getGlobalBounds() const { |
| 231 |
SkIRect bounds; |
| 232 |
this->getGlobalBounds(&bounds); |
| 233 |
return bounds; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Returns the bounding box of the current clip, in this device's coordinate space. No pixels |
| 238 |
* outside of these bounds will be touched by draws unless the clip is further modified (at |
| 239 |
* which point this will return the updated bounds). |
| 240 |
*/ |
| 241 |
virtual SkIRect devClipBounds() const = 0; |
| 242 |
|
| 243 |
virtual void pushClipStack() = 0; |
| 244 |
virtual void popClipStack() = 0; |
| 245 |
|
| 246 |
virtual void clipRect(const SkRect& rect, SkClipOp op, bool aa) = 0; |
| 247 |
virtual void clipRRect(const SkRRect& rrect, SkClipOp op, bool aa) = 0; |
| 248 |
virtual void clipPath(const SkPath& path, SkClipOp op, bool aa) = 0; |
| 249 |
virtual void clipRegion(const SkRegion& region, SkClipOp op) = 0; |
| 250 |
|
| 251 |
void clipShader(sk_sp<SkShader> sh, SkClipOp op) { |
| 252 |
sh = as_SB(sh)->makeWithCTM(this->localToDevice()); |
| 253 |
if (op == SkClipOp::kDifference) { |
| 254 |
sh = as_SB(sh)->makeInvertAlpha(); |
| 255 |
} |
| 256 |
this->onClipShader(std::move(sh)); |
| 257 |
} |
| 258 |
|
| 259 |
virtual void replaceClip(const SkIRect& rect) = 0; |
| 260 |
|
| 261 |
virtual bool isClipAntiAliased() const = 0; |
| 262 |
virtual bool isClipEmpty() const = 0; |
| 263 |
virtual bool isClipRect() const = 0; |
| 264 |
virtual bool isClipWideOpen() const = 0; |
| 265 |
|
| 266 |
virtual void android_utils_clipAsRgn(SkRegion*) const = 0; |
| 267 |
virtual bool android_utils_clipWithStencil() { return false; } |
| 268 |
|
| 269 |
// -- Device reflection |
| 270 |
|
| 271 |
// TEMPORARY: Whether or not SkCanvas should use an layer and image filters to simulate |
| 272 |
// mask filters and then draw the filtered mask using drawCoverageMask. Unlike regular |
| 273 |
// layers, the color type passed to SkDevice::createDevice() will always be an alpha-only |
| 274 |
// color type. Eventually this will be the only way that mask filters are handled (barring |
| 275 |
// dedicated fast-paths for blurs on [r]rects and text). |
| 276 |
virtual bool useDrawCoverageMaskForMaskFilters() const { return false; } |
| 277 |
|
| 278 |
// SkCanvas uses NoPixelsDevice when createDevice fails; but then it needs to be able to |
| 279 |
// inspect a layer's device to know if calling drawDevice() later is allowed. |
| 280 |
virtual bool isNoPixelsDevice() const { return false; } |
| 281 |
|
| 282 |
virtual void* getRasterHandle() const { return nullptr; } |
| 283 |
|
| 284 |
virtual GrRecordingContext* recordingContext() const { return nullptr; } |
| 285 |
virtual skgpu::graphite::Recorder* recorder() const { return nullptr; } |
| 286 |
virtual SkRecorder* baseRecorder() const { return nullptr; } |
| 287 |
|
| 288 |
virtual skgpu::ganesh::Device* asGaneshDevice() { return nullptr; } |
| 289 |
virtual skgpu::graphite::Device* asGraphiteDevice() { return nullptr; } |
| 290 |
|
| 291 |
// Marking an SkDevice immutable declares the intent that rendering to the device is |
| 292 |
// complete, allowing it to be sampled as an image without requiring a copy. Drawing |
| 293 |
// operations may not function and may assert if invoked after setImmutable() is called. |
| 294 |
virtual void setImmutable() {} |
| 295 |
|
| 296 |
virtual sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&); |
| 297 |
|
| 298 |
struct CreateInfo { |
| 299 |
CreateInfo(const SkImageInfo& info, |
| 300 |
SkPixelGeometry geo, |
| 301 |
SkRasterHandleAllocator* allocator) |
| 302 |
: fInfo(info) |
| 303 |
, fPixelGeometry(geo) |
| 304 |
, fAllocator(allocator) |
| 305 |
{} |
| 306 |
|
| 307 |
const SkImageInfo fInfo; |
| 308 |
const SkPixelGeometry fPixelGeometry; |
| 309 |
SkRasterHandleAllocator* fAllocator = nullptr; |
| 310 |
}; |
| 311 |
|
| 312 |
/** |
| 313 |
* Create a new device based on CreateInfo. If the paint is not null, then it represents a |
| 314 |
* preview of how the new device will be composed with its creator device (this). |
| 315 |
* |
| 316 |
* The subclass may be handed this device in drawDevice(), so it must always return a device |
| 317 |
* that it knows how to draw, and that it knows how to identify if it is not of the same |
| 318 |
* subclass (since drawDevice is passed a SkDevice*). If the subclass cannot fulfill that |
| 319 |
* contract (e.g. PDF cannot support some settings on the paint) it should return NULL, and the |
| 320 |
* caller may then decide to explicitly create a bitmapdevice, knowing that later it could not |
| 321 |
* call drawDevice with it (but it could call drawSprite or drawBitmap). |
| 322 |
*/ |
| 323 |
virtual sk_sp<SkDevice> createDevice(const CreateInfo&, const SkPaint*) { return nullptr; } |
| 324 |
|
| 325 |
// -- Drawing routines (called after saveLayers and imagefilter operations are applied) |
| 326 |
|
| 327 |
// Ensure that non-RSXForm runs are passed to onDrawGlyphRunList. |
| 328 |
void drawGlyphRunList(SkCanvas*, |
| 329 |
const sktext::GlyphRunList& glyphRunList, |
| 330 |
const SkPaint& paint); |
| 331 |
// Slug handling routines. |
| 332 |
virtual sk_sp<sktext::gpu::Slug> convertGlyphRunListToSlug( |
| 333 |
const sktext::GlyphRunList& glyphRunList, const SkPaint& paint); |
| 334 |
virtual void drawSlug(SkCanvas*, const sktext::gpu::Slug* slug, const SkPaint& paint); |
| 335 |
|
| 336 |
virtual void drawPaint(const SkPaint& paint) = 0; |
| 337 |
virtual void drawPoints(SkCanvas::PointMode, SkSpan<const SkPoint>, const SkPaint&) = 0; |
| 338 |
virtual void drawRect(const SkRect& r, |
| 339 |
const SkPaint& paint) = 0; |
| 340 |
virtual void drawRegion(const SkRegion& r, |
| 341 |
const SkPaint& paint); |
| 342 |
virtual void drawOval(const SkRect& oval, |
| 343 |
const SkPaint& paint) = 0; |
| 344 |
/** By the time this is called we know that abs(sweepAngle) is in the range [0, 360). */ |
| 345 |
virtual void drawArc(const SkArc& arc, const SkPaint& paint); |
| 346 |
virtual void drawRRect(const SkRRect& rr, |
| 347 |
const SkPaint& paint) = 0; |
| 348 |
|
| 349 |
// Default impl calls drawPath() |
| 350 |
virtual void drawDRRect(const SkRRect& outer, |
| 351 |
const SkRRect& inner, const SkPaint&); |
| 352 |
|
| 353 |
/** |
| 354 |
* If pathIsMutable, then the implementation is allowed to cast path to a |
| 355 |
* non-const pointer and modify it in place (as an optimization). Canvas |
| 356 |
* may do this to implement helpers such as drawOval, by placing a temp |
| 357 |
* path on the stack to hold the representation of the oval. |
| 358 |
*/ |
| 359 |
virtual void drawPath(const SkPath& path, |
| 360 |
const SkPaint& paint) = 0; |
| 361 |
|
| 362 |
virtual void drawImageRect(const SkImage*, const SkRect* src, const SkRect& dst, |
| 363 |
const SkSamplingOptions&, const SkPaint&, |
| 364 |
SkCanvas::SrcRectConstraint) = 0; |
| 365 |
// Return true if canvas calls to drawImage or drawImageRect should try to |
| 366 |
// be drawn in a tiled way. |
| 367 |
virtual bool shouldDrawAsTiledImageRect() const { return false; } |
| 368 |
virtual bool drawAsTiledImageRect(SkCanvas*, |
| 369 |
const SkImage*, |
| 370 |
const SkRect* src, |
| 371 |
const SkRect& dst, |
| 372 |
const SkSamplingOptions&, |
| 373 |
const SkPaint&, |
| 374 |
SkCanvas::SrcRectConstraint) { return false; } |
| 375 |
|
| 376 |
virtual void drawImageLattice(const SkImage*, const SkCanvas::Lattice&, |
| 377 |
const SkRect& dst, SkFilterMode, const SkPaint&); |
| 378 |
|
| 379 |
/** |
| 380 |
* If skipColorXform is true, then the implementation should assume that the provided |
| 381 |
* vertex colors are already in the destination color space. |
| 382 |
*/ |
| 383 |
virtual void drawVertices(const SkVertices*, |
| 384 |
sk_sp<SkBlender>, |
| 385 |
const SkPaint&, |
| 386 |
bool skipColorXform = false) = 0; |
| 387 |
virtual void drawMesh(const SkMesh& mesh, sk_sp<SkBlender>, const SkPaint&) = 0; |
| 388 |
virtual void drawShadow(SkCanvas*, const SkPath&, const SkDrawShadowRec&); |
| 389 |
|
| 390 |
// default implementation calls drawVertices |
| 391 |
virtual void drawPatch(const SkPoint cubics[12], const SkColor colors[4], |
| 392 |
const SkPoint texCoords[4], sk_sp<SkBlender>, const SkPaint& paint); |
| 393 |
|
| 394 |
// default implementation calls drawVertices |
| 395 |
virtual void drawAtlas(SkSpan<const SkRSXform>, SkSpan<const SkRect>, SkSpan<const SkColor>, |
| 396 |
sk_sp<SkBlender>, const SkPaint&); |
| 397 |
|
| 398 |
virtual void drawAnnotation(const SkRect&, const char[], SkData*) {} |
| 399 |
|
| 400 |
// Default impl always calls drawRect() with a solid-color paint, setting it to anti-aliased |
| 401 |
// only when all edge flags are set. If there's a clip region, it draws that using drawPath, |
| 402 |
// or uses clipPath(). |
| 403 |
virtual void drawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], |
| 404 |
SkCanvas::QuadAAFlags aaFlags, const SkColor4f& color, |
| 405 |
SkBlendMode mode); |
| 406 |
// Default impl uses drawImageRect per entry, being anti-aliased only when an entry's edge flags |
| 407 |
// are all set. If there's a clip region, it will be applied using clipPath(). |
| 408 |
virtual void drawEdgeAAImageSet(const SkCanvas::ImageSetEntry[], int count, |
| 409 |
const SkPoint dstClips[], const SkMatrix preViewMatrices[], |
| 410 |
const SkSamplingOptions&, const SkPaint&, |
| 411 |
SkCanvas::SrcRectConstraint); |
| 412 |
|
| 413 |
virtual void drawDrawable(SkCanvas*, SkDrawable*, const SkMatrix*); |
| 414 |
|
| 415 |
// -- "Special" drawing and image routines |
| 416 |
|
| 417 |
// Snap the 'subset' contents from this device, possibly as a read-only view. If 'forceCopy' |
| 418 |
// is true then the returned image's pixels must not be affected by subsequent draws into the |
| 419 |
// device. When 'forceCopy' is false, the image can be a view into the device's pixels |
| 420 |
// (avoiding a copy for performance, at the expense of safety). Default returns null. |
| 421 |
virtual sk_sp<SkSpecialImage> snapSpecial(const SkIRect& subset, bool forceCopy = false); |
| 422 |
// Can return null if unable to perform scaling as part of the copy, even if snapSpecial() w/o |
| 423 |
// scaling would succeed. |
| 424 |
virtual sk_sp<SkSpecialImage> snapSpecialScaled(const SkIRect& subset, const SkISize& dstDims); |
| 425 |
// Get a view of the entire device's current contents as an image. |
| 426 |
sk_sp<SkSpecialImage> snapSpecial(); |
| 427 |
|
| 428 |
/** |
| 429 |
* The SkDevice passed will be an SkDevice which was returned by a call to |
| 430 |
* createDevice on this device with kNeverTile_TileExpectation. |
| 431 |
* |
| 432 |
* The default implementation calls snapSpecial() and drawSpecial() with the relative transform |
| 433 |
* from the input device to this device. The provided SkPaint cannot have a mask filter or |
| 434 |
* image filter, and any shader is ignored. |
| 435 |
*/ |
| 436 |
virtual void drawDevice(SkDevice*, const SkSamplingOptions&, const SkPaint&); |
| 437 |
|
| 438 |
/** |
| 439 |
* Draw the special image's subset to this device, subject to the given matrix transform instead |
| 440 |
* of the device's current local to device matrix. |
| 441 |
* |
| 442 |
* If 'constraint' is kFast, the rendered geometry of the image still reflects the extent of |
| 443 |
* the SkSpecialImage's subset, but it's assumed that the pixel data beyond the subset is valid |
| 444 |
* (e.g. SkSpecialImage::makeSubset() was called to crop a larger image). |
| 445 |
*/ |
| 446 |
virtual void drawSpecial(SkSpecialImage*, const SkMatrix& localToDevice, |
| 447 |
const SkSamplingOptions&, const SkPaint&, |
| 448 |
SkCanvas::SrcRectConstraint constraint = |
| 449 |
SkCanvas::kStrict_SrcRectConstraint); |
| 450 |
|
| 451 |
/** |
| 452 |
* Draw the special image's subset to this device, treating its alpha channel as coverage for |
| 453 |
* the draw and ignoring any RGB channels that might be present. This will be drawn using the |
| 454 |
* provided matrix transform instead of the device's current local to device matrix. |
| 455 |
* |
| 456 |
* Coverage values beyond the image's subset are treated as 0 (i.e. kDecal tiling). Color values |
| 457 |
* before coverage are determined as normal by the SkPaint, ignoring style, path effects, |
| 458 |
* mask filters and image filters. The local coords of any SkShader on the paint should be |
| 459 |
* relative to the SkDevice's current matrix (i.e. 'maskToDevice' determines how the coverage |
| 460 |
* mask aligns with device-space, but otherwise shading proceeds like other draws). |
| 461 |
*/ |
| 462 |
virtual void drawCoverageMask(const SkSpecialImage*, const SkMatrix& maskToDevice, |
| 463 |
const SkSamplingOptions&, const SkPaint&); |
| 464 |
|
| 465 |
/** |
| 466 |
* Draw rrect with an optimized path for analytic blurs, if provided by the device. |
| 467 |
*/ |
| 468 |
virtual bool drawBlurredRRect(const SkRRect&, const SkPaint&, float deviceSigma) { |
| 469 |
return false; |
| 470 |
} |
| 471 |
|
| 472 |
/** |
| 473 |
* Evaluate 'filter' and draw the final output into this device using 'paint'. The 'mapping' |
| 474 |
* defines the parameter-to-layer space transform used to evaluate the image filter on 'src', |
| 475 |
* and the layer-to-device space transform that is used to draw the result into this device. |
| 476 |
* Since 'mapping' fully specifies the transform, this draw function ignores the current |
| 477 |
* local-to-device matrix (i.e. just like drawSpecial and drawDevice). |
| 478 |
* |
| 479 |
* The final paint must not have an image filter or mask filter set on it; a shader is ignored. |
| 480 |
* The provided color type will be used for any intermediate surfaces that need to be created as |
| 481 |
* part of filter evaluation. It does not have to be src's color type or this Device's type. |
| 482 |
*/ |
| 483 |
void drawFilteredImage(const skif::Mapping& mapping, SkSpecialImage* src, SkColorType ct, |
| 484 |
const SkImageFilter*, const SkSamplingOptions&, const SkPaint&); |
| 485 |
|
| 486 |
protected: |
| 487 |
// Configure the device's coordinate spaces, specifying both how its device image maps back to |
| 488 |
// the global space (via 'deviceToGlobal') and the initial CTM of the device (via |
| 489 |
// 'localToDevice', i.e. what geometry drawn into this device will be transformed with). |
| 490 |
// |
| 491 |
// (bufferOriginX, bufferOriginY) defines where the (0,0) pixel the device's backing buffer |
| 492 |
// is anchored in the device space. The final device-to-global matrix stored by the SkDevice |
| 493 |
// will include a pre-translation by T(deviceOriginX, deviceOriginY), and the final |
| 494 |
// local-to-device matrix will have a post-translation of T(-deviceOriginX, -deviceOriginY). |
| 495 |
void setDeviceCoordinateSystem(const SkM44& deviceToGlobal, |
| 496 |
const SkM44& globalToDevice, |
| 497 |
const SkM44& localToDevice, |
| 498 |
int bufferOriginX, |
| 499 |
int bufferOriginY); |
| 500 |
// Convenience to configure the device to be axis-aligned with the root canvas, but with a |
| 501 |
// unique origin. |
| 502 |
void setOrigin(const SkM44& globalCTM, int x, int y) { |
| 503 |
this->setDeviceCoordinateSystem(SkM44(), SkM44(), globalCTM, x, y); |
| 504 |
} |
| 505 |
|
| 506 |
// Returns whether or not localToDevice() has changed since the last call to this function. |
| 507 |
bool checkLocalToDeviceDirty() { |
| 508 |
bool wasDirty = fLocalToDeviceDirty; |
| 509 |
fLocalToDeviceDirty = false; |
| 510 |
return wasDirty; |
| 511 |
} |
| 512 |
|
| 513 |
private: |
| 514 |
friend class SkCanvas; // for setOrigin/setDeviceCoordinateSystem |
| 515 |
friend class DeviceTestingAccess; |
| 516 |
|
| 517 |
// Defaults to a CPU image filtering backend. |
| 518 |
virtual sk_sp<skif::Backend> createImageFilteringBackend(const SkSurfaceProps& surfaceProps, |
| 519 |
SkColorType colorType) const; |
| 520 |
|
| 521 |
// Implementations can assume that the device from (x,y) to (w,h) will fit within dst. |
| 522 |
virtual bool onReadPixels(const SkPixmap&, int x, int y) { return false; } |
| 523 |
|
| 524 |
// Implementations can assume that the src image placed at 'x,y' will fit within the device. |
| 525 |
virtual bool onWritePixels(const SkPixmap&, int x, int y) { return false; } |
| 526 |
|
| 527 |
virtual bool onAccessPixels(SkPixmap*) { return false; } |
| 528 |
|
| 529 |
virtual bool onPeekPixels(SkPixmap*) { return false; } |
| 530 |
|
| 531 |
virtual void onClipShader(sk_sp<SkShader>) = 0; |
| 532 |
|
| 533 |
// Only called with glyphRunLists that do not contain RSXForm. |
| 534 |
virtual void onDrawGlyphRunList(SkCanvas*, |
| 535 |
const sktext::GlyphRunList&, |
| 536 |
const SkPaint& paint) = 0; |
| 537 |
|
| 538 |
void simplifyGlyphRunRSXFormAndRedraw(SkCanvas*, |
| 539 |
const sktext::GlyphRunList&, |
| 540 |
const SkPaint& paint); |
| 541 |
|
| 542 |
const SkImageInfo fInfo; |
| 543 |
const SkSurfaceProps fSurfaceProps; |
| 544 |
SkM44 fLocalToDevice; |
| 545 |
// fDeviceToGlobal and fGlobalToDevice are inverses of each other; there are never that many |
| 546 |
// SkDevices, so pay the memory cost to avoid recalculating the inverse. |
| 547 |
SkM44 fDeviceToGlobal; |
| 548 |
SkM44 fGlobalToDevice; |
| 549 |
|
| 550 |
// fLocalToDevice but as a 3x3. |
| 551 |
SkMatrix fLocalToDevice33; |
| 552 |
|
| 553 |
// fLocalToDevice is the device CTM, not the global CTM. |
| 554 |
// It maps from local space to the device's coordinate space. |
| 555 |
// fDeviceToGlobal * fLocalToDevice will match the canvas' CTM. |
| 556 |
// |
| 557 |
// setGlobalCTM and setLocalToDevice are intentionally not virtual for performance reasons. |
| 558 |
// However, track a dirty bit for subclasses that want to defer local-to-device dependent |
| 559 |
// calculations until needed for a clip or draw. |
| 560 |
bool fLocalToDeviceDirty = true; |
| 561 |
}; |
| 562 |
|
| 563 |
class SkNoPixelsDevice : public SkDevice { |
| 564 |
public: |
| 565 |
SkNoPixelsDevice(const SkIRect& bounds, const SkSurfaceProps& props); |
| 566 |
SkNoPixelsDevice(const SkIRect& bounds, const SkSurfaceProps& props, |
| 567 |
sk_sp<SkColorSpace> colorSpace); |
| 568 |
|
| 569 |
// Returns false if the device could not be reset; this should only be called on a root device. |
| 570 |
bool resetForNextPicture(const SkIRect& bounds); |
| 571 |
|
| 572 |
// SkNoPixelsDevice tracks the clip conservatively in order to respond to some queries as |
| 573 |
// accurately as possible while emphasizing performance |
| 574 |
void pushClipStack() override; |
| 575 |
void popClipStack() override; |
| 576 |
void clipRect(const SkRect& rect, SkClipOp op, bool aa) override; |
| 577 |
void clipRRect(const SkRRect& rrect, SkClipOp op, bool aa) override; |
| 578 |
void clipPath(const SkPath& path, SkClipOp op, bool aa) override; |
| 579 |
void clipRegion(const SkRegion& globalRgn, SkClipOp op) override; |
| 580 |
void replaceClip(const SkIRect& rect) override; |
| 581 |
bool isClipAntiAliased() const override { return this->clip().fIsAA; } |
| 582 |
bool isClipEmpty() const override { return this->devClipBounds().isEmpty(); } |
| 583 |
bool isClipRect() const override { return this->clip().fIsRect && !this->isClipEmpty(); } |
| 584 |
bool isClipWideOpen() const override { |
| 585 |
return this->clip().fIsRect && |
| 586 |
this->devClipBounds() == this->bounds(); |
| 587 |
} |
| 588 |
void android_utils_clipAsRgn(SkRegion* rgn) const override { |
| 589 |
rgn->setRect(this->devClipBounds()); |
| 590 |
} |
| 591 |
SkIRect devClipBounds() const override { return this->clip().fClipBounds; } |
| 592 |
|
| 593 |
protected: |
| 594 |
|
| 595 |
void drawPaint(const SkPaint& paint) override {} |
| 596 |
void drawPoints(SkCanvas::PointMode, SkSpan<const SkPoint>, const SkPaint&) override {} |
| 597 |
void drawImageRect(const SkImage*, const SkRect*, const SkRect&, |
| 598 |
const SkSamplingOptions&, const SkPaint&, |
| 599 |
SkCanvas::SrcRectConstraint) override {} |
| 600 |
void drawRect(const SkRect&, const SkPaint&) override {} |
| 601 |
void drawOval(const SkRect&, const SkPaint&) override {} |
| 602 |
void drawRRect(const SkRRect&, const SkPaint&) override {} |
| 603 |
void drawPath(const SkPath&, const SkPaint&) override {} |
| 604 |
void drawDevice(SkDevice*, const SkSamplingOptions&, const SkPaint&) override {} |
| 605 |
void drawVertices(const SkVertices*, sk_sp<SkBlender>, const SkPaint&, bool) override {} |
| 606 |
void drawMesh(const SkMesh&, sk_sp<SkBlender>, const SkPaint&) override {} |
| 607 |
|
| 608 |
void drawSlug(SkCanvas*, const sktext::gpu::Slug*, const SkPaint&) override {} |
| 609 |
void onDrawGlyphRunList(SkCanvas*, const sktext::GlyphRunList&, const SkPaint&) override {} |
| 610 |
|
| 611 |
bool isNoPixelsDevice() const override { return true; } |
| 612 |
|
| 613 |
private: |
| 614 |
struct ClipState { |
| 615 |
SkIRect fClipBounds; |
| 616 |
int fDeferredSaveCount; |
| 617 |
bool fIsAA; |
| 618 |
bool fIsRect; |
| 619 |
|
| 620 |
ClipState(const SkIRect& bounds, bool isAA, bool isRect) |
| 621 |
: fClipBounds(bounds) |
| 622 |
, fDeferredSaveCount(0) |
| 623 |
, fIsAA(isAA) |
| 624 |
, fIsRect(isRect) {} |
| 625 |
|
| 626 |
void op(SkClipOp op, const SkM44& transform, const SkRect& bounds, |
| 627 |
bool isAA, bool fillsBounds); |
| 628 |
}; |
| 629 |
|
| 630 |
void onClipShader(sk_sp<SkShader> shader) override; |
| 631 |
|
| 632 |
const ClipState& clip() const { return fClipStack.back(); } |
| 633 |
ClipState& writableClip(); |
| 634 |
|
| 635 |
skia_private::STArray<4, ClipState> fClipStack; |
| 636 |
}; |
| 637 |
|
| 638 |
class [[nodiscard]] SkAutoDeviceTransformRestore : SkNoncopyable { |
| 639 |
public: |
| 640 |
SkAutoDeviceTransformRestore(SkDevice* device, const SkM44& localToDevice) |
| 641 |
: fDevice(device) |
| 642 |
, fPrevLocalToDevice(device->localToDevice()) |
| 643 |
{ |
| 644 |
fDevice->setLocalToDevice(localToDevice); |
| 645 |
} |
| 646 |
~SkAutoDeviceTransformRestore() { |
| 647 |
fDevice->setLocalToDevice(fPrevLocalToDevice); |
| 648 |
} |
| 649 |
|
| 650 |
private: |
| 651 |
SkDevice* fDevice; |
| 652 |
const SkM44 fPrevLocalToDevice; |
| 653 |
}; |
| 654 |
|
| 655 |
#endif |