1 /* 2 * Copyright 2012 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SkSurface_DEFINED 9 #define SkSurface_DEFINED 10 11 #include "include/core/SkImage.h" 12 #include "include/core/SkPixmap.h" 13 #include "include/core/SkRefCnt.h" 14 #include "include/core/SkSurfaceProps.h" 15 16 #include "include/gpu/GrTypes.h" 17 18 #if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26 19 #include <android/hardware_buffer.h> 20 #endif 21 22 #ifdef SK_METAL 23 #include "include/gpu/mtl/GrMtlTypes.h" 24 #endif 25 26 class SkCanvas; 27 class SkDeferredDisplayList; 28 class SkPaint; 29 class SkSurfaceCharacterization; 30 class GrBackendRenderTarget; 31 class GrBackendSemaphore; 32 class GrBackendSurfaceMutableState; 33 class GrBackendTexture; 34 class GrDirectContext; 35 class GrRecordingContext; 36 class GrRenderTarget; 37 38 /** \class SkSurface 39 SkSurface is responsible for managing the pixels that a canvas draws into. The pixels can be 40 allocated either in CPU memory (a raster surface) or on the GPU (a GrRenderTarget surface). 41 SkSurface takes care of allocating a SkCanvas that will draw into the surface. Call 42 surface->getCanvas() to use that canvas (but don't delete it, it is owned by the surface). 43 SkSurface always has non-zero dimensions. If there is a request for a new surface, and either 44 of the requested dimensions are zero, then nullptr will be returned. 45 */ 46 class SK_API SkSurface : public SkRefCnt { 47 public: 48 49 /** Allocates raster SkSurface. SkCanvas returned by SkSurface draws directly into pixels. 50 51 SkSurface is returned if all parameters are valid. 52 Valid parameters include: 53 info dimensions are greater than zero; 54 info contains SkColorType and SkAlphaType supported by raster surface; 55 pixels is not nullptr; 56 rowBytes is large enough to contain info width pixels of SkColorType. 57 58 Pixel buffer size should be info height times computed rowBytes. 59 Pixels are not initialized. 60 To access pixels after drawing, peekPixels() or readPixels(). 61 62 @param imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace, 63 of raster surface; width and height must be greater than zero 64 @param pixels pointer to destination pixels buffer 65 @param rowBytes interval from one SkSurface row to the next 66 @param surfaceProps LCD striping orientation and setting for device independent fonts; 67 may be nullptr 68 @return SkSurface if all parameters are valid; otherwise, nullptr 69 */ 70 static sk_sp<SkSurface> MakeRasterDirect(const SkImageInfo& imageInfo, void* pixels, 71 size_t rowBytes, 72 const SkSurfaceProps* surfaceProps = nullptr); 73 74 static sk_sp<SkSurface> MakeRasterDirect(const SkPixmap& pm, 75 const SkSurfaceProps* props = nullptr) { 76 return MakeRasterDirect(pm.info(), pm.writable_addr(), pm.rowBytes(), props); 77 } 78 79 /** Allocates raster SkSurface. SkCanvas returned by SkSurface draws directly into pixels. 80 releaseProc is called with pixels and context when SkSurface is deleted. 81 82 SkSurface is returned if all parameters are valid. 83 Valid parameters include: 84 info dimensions are greater than zero; 85 info contains SkColorType and SkAlphaType supported by raster surface; 86 pixels is not nullptr; 87 rowBytes is large enough to contain info width pixels of SkColorType. 88 89 Pixel buffer size should be info height times computed rowBytes. 90 Pixels are not initialized. 91 To access pixels after drawing, call flush() or peekPixels(). 92 93 @param imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace, 94 of raster surface; width and height must be greater than zero 95 @param pixels pointer to destination pixels buffer 96 @param rowBytes interval from one SkSurface row to the next 97 @param releaseProc called when SkSurface is deleted; may be nullptr 98 @param context passed to releaseProc; may be nullptr 99 @param surfaceProps LCD striping orientation and setting for device independent fonts; 100 may be nullptr 101 @return SkSurface if all parameters are valid; otherwise, nullptr 102 */ 103 static sk_sp<SkSurface> MakeRasterDirectReleaseProc(const SkImageInfo& imageInfo, void* pixels, 104 size_t rowBytes, 105 void (*releaseProc)(void* pixels, void* context), 106 void* context, const SkSurfaceProps* surfaceProps = nullptr); 107 108 /** Allocates raster SkSurface. SkCanvas returned by SkSurface draws directly into pixels. 109 Allocates and zeroes pixel memory. Pixel memory size is imageInfo.height() times 110 rowBytes, or times imageInfo.minRowBytes() if rowBytes is zero. 111 Pixel memory is deleted when SkSurface is deleted. 112 113 SkSurface is returned if all parameters are valid. 114 Valid parameters include: 115 info dimensions are greater than zero; 116 info contains SkColorType and SkAlphaType supported by raster surface; 117 rowBytes is large enough to contain info width pixels of SkColorType, or is zero. 118 119 If rowBytes is zero, a suitable value will be chosen internally. 120 121 @param imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace, 122 of raster surface; width and height must be greater than zero 123 @param rowBytes interval from one SkSurface row to the next; may be zero 124 @param surfaceProps LCD striping orientation and setting for device independent fonts; 125 may be nullptr 126 @return SkSurface if all parameters are valid; otherwise, nullptr 127 */ 128 static sk_sp<SkSurface> MakeRaster(const SkImageInfo& imageInfo, size_t rowBytes, 129 const SkSurfaceProps* surfaceProps); 130 131 /** Allocates raster SkSurface. SkCanvas returned by SkSurface draws directly into pixels. 132 Allocates and zeroes pixel memory. Pixel memory size is imageInfo.height() times 133 imageInfo.minRowBytes(). 134 Pixel memory is deleted when SkSurface is deleted. 135 136 SkSurface is returned if all parameters are valid. 137 Valid parameters include: 138 info dimensions are greater than zero; 139 info contains SkColorType and SkAlphaType supported by raster surface. 140 141 @param imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace, 142 of raster surface; width and height must be greater than zero 143 @param props LCD striping orientation and setting for device independent fonts; 144 may be nullptr 145 @return SkSurface if all parameters are valid; otherwise, nullptr 146 */ 147 static sk_sp<SkSurface> MakeRaster(const SkImageInfo& imageInfo, 148 const SkSurfaceProps* props = nullptr) { 149 return MakeRaster(imageInfo, 0, props); 150 } 151 152 /** Allocates raster SkSurface. SkCanvas returned by SkSurface draws directly into pixels. 153 Allocates and zeroes pixel memory. Pixel memory size is height times width times 154 four. Pixel memory is deleted when SkSurface is deleted. 155 156 Internally, sets SkImageInfo to width, height, native color type, and 157 kPremul_SkAlphaType. 158 159 SkSurface is returned if width and height are greater than zero. 160 161 Use to create SkSurface that matches SkPMColor, the native pixel arrangement on 162 the platform. SkSurface drawn to output device skips converting its pixel format. 163 164 @param width pixel column count; must be greater than zero 165 @param height pixel row count; must be greater than zero 166 @param surfaceProps LCD striping orientation and setting for device independent 167 fonts; may be nullptr 168 @return SkSurface if all parameters are valid; otherwise, nullptr 169 */ 170 static sk_sp<SkSurface> MakeRasterN32Premul(int width, int height, 171 const SkSurfaceProps* surfaceProps = nullptr); 172 173 /** Caller data passed to RenderTarget/TextureReleaseProc; may be nullptr. */ 174 typedef void* ReleaseContext; 175 176 /** User function called when supplied render target may be deleted. */ 177 typedef void (*RenderTargetReleaseProc)(ReleaseContext releaseContext); 178 179 /** User function called when supplied texture may be deleted. */ 180 typedef void (*TextureReleaseProc)(ReleaseContext releaseContext); 181 182 /** Wraps a GPU-backed texture into SkSurface. Caller must ensure the texture is 183 valid for the lifetime of returned SkSurface. If sampleCnt greater than zero, 184 creates an intermediate MSAA SkSurface which is used for drawing backendTexture. 185 186 SkSurface is returned if all parameters are valid. backendTexture is valid if 187 its pixel configuration agrees with colorSpace and context; for instance, if 188 backendTexture has an sRGB configuration, then context must support sRGB, 189 and colorSpace must be present. Further, backendTexture width and height must 190 not exceed context capabilities, and the context must be able to support 191 back-end textures. 192 193 Upon success textureReleaseProc is called when it is safe to delete the texture in the 194 backend API (accounting only for use of the texture by this surface). If SkSurface creation 195 fails textureReleaseProc is called before this function returns. 196 197 If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr. 198 199 @param context GPU context 200 @param backendTexture texture residing on GPU 201 @param sampleCnt samples per pixel, or 0 to disable full scene anti-aliasing 202 @param colorSpace range of colors; may be nullptr 203 @param surfaceProps LCD striping orientation and setting for device independent 204 fonts; may be nullptr 205 @param textureReleaseProc function called when texture can be released 206 @param releaseContext state passed to textureReleaseProc 207 @return SkSurface if all parameters are valid; otherwise, nullptr 208 */ 209 static sk_sp<SkSurface> MakeFromBackendTexture(GrRecordingContext* context, 210 const GrBackendTexture& backendTexture, 211 GrSurfaceOrigin origin, int sampleCnt, 212 SkColorType colorType, 213 sk_sp<SkColorSpace> colorSpace, 214 const SkSurfaceProps* surfaceProps, 215 TextureReleaseProc textureReleaseProc = nullptr, 216 ReleaseContext releaseContext = nullptr); 217 218 /** Wraps a GPU-backed buffer into SkSurface. Caller must ensure backendRenderTarget 219 is valid for the lifetime of returned SkSurface. 220 221 SkSurface is returned if all parameters are valid. backendRenderTarget is valid if 222 its pixel configuration agrees with colorSpace and context; for instance, if 223 backendRenderTarget has an sRGB configuration, then context must support sRGB, 224 and colorSpace must be present. Further, backendRenderTarget width and height must 225 not exceed context capabilities, and the context must be able to support 226 back-end render targets. 227 228 Upon success releaseProc is called when it is safe to delete the render target in the 229 backend API (accounting only for use of the render target by this surface). If SkSurface 230 creation fails releaseProc is called before this function returns. 231 232 If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr. 233 234 @param context GPU context 235 @param backendRenderTarget GPU intermediate memory buffer 236 @param colorSpace range of colors 237 @param surfaceProps LCD striping orientation and setting for device independent 238 fonts; may be nullptr 239 @param releaseProc function called when backendRenderTarget can be released 240 @param releaseContext state passed to releaseProc 241 @return SkSurface if all parameters are valid; otherwise, nullptr 242 */ 243 static sk_sp<SkSurface> MakeFromBackendRenderTarget(GrRecordingContext* context, 244 const GrBackendRenderTarget& backendRenderTarget, 245 GrSurfaceOrigin origin, 246 SkColorType colorType, 247 sk_sp<SkColorSpace> colorSpace, 248 const SkSurfaceProps* surfaceProps, 249 RenderTargetReleaseProc releaseProc = nullptr, 250 ReleaseContext releaseContext = nullptr); 251 252 #if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26 253 /** Private. 254 Creates SkSurface from Android hardware buffer. 255 Returned SkSurface takes a reference on the buffer. The ref on the buffer will be released 256 when the SkSurface is destroyed and there is no pending work on the GPU involving the 257 buffer. 258 259 Only available on Android, when __ANDROID_API__ is defined to be 26 or greater. 260 261 Currently this is only supported for buffers that can be textured as well as rendered to. 262 In other words that must have both AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT and 263 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE usage bits. 264 265 @param context GPU context 266 @param hardwareBuffer AHardwareBuffer Android hardware buffer 267 @param colorSpace range of colors; may be nullptr 268 @param surfaceProps LCD striping orientation and setting for device independent 269 fonts; may be nullptr 270 @return created SkSurface, or nullptr 271 */ 272 static sk_sp<SkSurface> MakeFromAHardwareBuffer(GrDirectContext* context, 273 AHardwareBuffer* hardwareBuffer, 274 GrSurfaceOrigin origin, 275 sk_sp<SkColorSpace> colorSpace, 276 const SkSurfaceProps* surfaceProps); 277 #endif 278 279 #ifdef SK_METAL 280 /** Creates SkSurface from CAMetalLayer. 281 Returned SkSurface takes a reference on the CAMetalLayer. The ref on the layer will be 282 released when the SkSurface is destroyed. 283 284 Only available when Metal API is enabled. 285 286 Will grab the current drawable from the layer and use its texture as a backendRT to 287 create a renderable surface. 288 289 @param context GPU context 290 @param layer GrMTLHandle (expected to be a CAMetalLayer*) 291 @param sampleCnt samples per pixel, or 0 to disable full scene anti-aliasing 292 @param colorSpace range of colors; may be nullptr 293 @param surfaceProps LCD striping orientation and setting for device independent 294 fonts; may be nullptr 295 @param drawable Pointer to drawable to be filled in when this surface is 296 instantiated; may not be nullptr 297 @return created SkSurface, or nullptr 298 */ 299 static sk_sp<SkSurface> MakeFromCAMetalLayer(GrRecordingContext* context, 300 GrMTLHandle layer, 301 GrSurfaceOrigin origin, 302 int sampleCnt, 303 SkColorType colorType, 304 sk_sp<SkColorSpace> colorSpace, 305 const SkSurfaceProps* surfaceProps, 306 GrMTLHandle* drawable) 307 SK_API_AVAILABLE_CA_METAL_LAYER; 308 309 /** Creates SkSurface from MTKView. 310 Returned SkSurface takes a reference on the MTKView. The ref on the layer will be 311 released when the SkSurface is destroyed. 312 313 Only available when Metal API is enabled. 314 315 Will grab the current drawable from the layer and use its texture as a backendRT to 316 create a renderable surface. 317 318 @param context GPU context 319 @param layer GrMTLHandle (expected to be a MTKView*) 320 @param sampleCnt samples per pixel, or 0 to disable full scene anti-aliasing 321 @param colorSpace range of colors; may be nullptr 322 @param surfaceProps LCD striping orientation and setting for device independent 323 fonts; may be nullptr 324 @return created SkSurface, or nullptr 325 */ 326 static sk_sp<SkSurface> MakeFromMTKView(GrRecordingContext* context, 327 GrMTLHandle mtkView, 328 GrSurfaceOrigin origin, 329 int sampleCnt, 330 SkColorType colorType, 331 sk_sp<SkColorSpace> colorSpace, 332 const SkSurfaceProps* surfaceProps) 333 SK_API_AVAILABLE(macos(10.11), ios(9.0)); 334 #endif 335 336 /** Returns SkSurface on GPU indicated by context. Allocates memory for 337 pixels, based on the width, height, and SkColorType in SkImageInfo. budgeted 338 selects whether allocation for pixels is tracked by context. imageInfo 339 describes the pixel format in SkColorType, and transparency in 340 SkAlphaType, and color matching in SkColorSpace. 341 342 sampleCount requests the number of samples per pixel. 343 Pass zero to disable multi-sample anti-aliasing. The request is rounded 344 up to the next supported count, or rounded down if it is larger than the 345 maximum supported count. 346 347 surfaceOrigin pins either the top-left or the bottom-left corner to the origin. 348 349 shouldCreateWithMips hints that SkImage returned by makeImageSnapshot() is mip map. 350 351 If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr. 352 353 @param context GPU context 354 @param imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace; 355 width, or height, or both, may be zero 356 @param sampleCount samples per pixel, or 0 to disable full scene anti-aliasing 357 @param surfaceProps LCD striping orientation and setting for device independent 358 fonts; may be nullptr 359 @param shouldCreateWithMips hint that SkSurface will host mip map images 360 @return SkSurface if all parameters are valid; otherwise, nullptr 361 */ 362 static sk_sp<SkSurface> MakeRenderTarget(GrRecordingContext* context, SkBudgeted budgeted, 363 const SkImageInfo& imageInfo, 364 int sampleCount, GrSurfaceOrigin surfaceOrigin, 365 const SkSurfaceProps* surfaceProps, 366 bool shouldCreateWithMips = false); 367 368 /** Returns SkSurface on GPU indicated by context. Allocates memory for 369 pixels, based on the width, height, and SkColorType in SkImageInfo. budgeted 370 selects whether allocation for pixels is tracked by context. imageInfo 371 describes the pixel format in SkColorType, and transparency in 372 SkAlphaType, and color matching in SkColorSpace. 373 374 sampleCount requests the number of samples per pixel. 375 Pass zero to disable multi-sample anti-aliasing. The request is rounded 376 up to the next supported count, or rounded down if it is larger than the 377 maximum supported count. 378 379 SkSurface bottom-left corner is pinned to the origin. 380 381 @param context GPU context 382 @param imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace, 383 of raster surface; width, or height, or both, may be zero 384 @param sampleCount samples per pixel, or 0 to disable multi-sample anti-aliasing 385 @param surfaceProps LCD striping orientation and setting for device independent 386 fonts; may be nullptr 387 @return SkSurface if all parameters are valid; otherwise, nullptr 388 */ MakeRenderTarget(GrRecordingContext * context,SkBudgeted budgeted,const SkImageInfo & imageInfo,int sampleCount,const SkSurfaceProps * surfaceProps)389 static sk_sp<SkSurface> MakeRenderTarget(GrRecordingContext* context, SkBudgeted budgeted, 390 const SkImageInfo& imageInfo, int sampleCount, 391 const SkSurfaceProps* surfaceProps) { 392 return MakeRenderTarget(context, budgeted, imageInfo, sampleCount, 393 kBottomLeft_GrSurfaceOrigin, surfaceProps); 394 } 395 396 /** Returns SkSurface on GPU indicated by context. Allocates memory for 397 pixels, based on the width, height, and SkColorType in SkImageInfo. budgeted 398 selects whether allocation for pixels is tracked by context. imageInfo 399 describes the pixel format in SkColorType, and transparency in 400 SkAlphaType, and color matching in SkColorSpace. 401 402 SkSurface bottom-left corner is pinned to the origin. 403 404 @param context GPU context 405 @param imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace, 406 of raster surface; width, or height, or both, may be zero 407 @return SkSurface if all parameters are valid; otherwise, nullptr 408 */ MakeRenderTarget(GrRecordingContext * context,SkBudgeted budgeted,const SkImageInfo & imageInfo)409 static sk_sp<SkSurface> MakeRenderTarget(GrRecordingContext* context, SkBudgeted budgeted, 410 const SkImageInfo& imageInfo) { 411 if (!imageInfo.width() || !imageInfo.height()) { 412 return nullptr; 413 } 414 return MakeRenderTarget(context, budgeted, imageInfo, 0, kBottomLeft_GrSurfaceOrigin, 415 nullptr); 416 } 417 418 /** Returns SkSurface on GPU indicated by context that is compatible with the provided 419 characterization. budgeted selects whether allocation for pixels is tracked by context. 420 421 @param context GPU context 422 @param characterization description of the desired SkSurface 423 @return SkSurface if all parameters are valid; otherwise, nullptr 424 */ 425 static sk_sp<SkSurface> MakeRenderTarget(GrRecordingContext* context, 426 const SkSurfaceCharacterization& characterization, 427 SkBudgeted budgeted); 428 429 /** Is this surface compatible with the provided characterization? 430 431 This method can be used to determine if an existing SkSurface is a viable destination 432 for an SkDeferredDisplayList. 433 434 @param characterization The characterization for which a compatibility check is desired 435 @return true if this surface is compatible with the characterization; 436 false otherwise 437 */ 438 bool isCompatible(const SkSurfaceCharacterization& characterization) const; 439 440 /** Returns SkSurface without backing pixels. Drawing to SkCanvas returned from SkSurface 441 has no effect. Calling makeImageSnapshot() on returned SkSurface returns nullptr. 442 443 @param width one or greater 444 @param height one or greater 445 @return SkSurface if width and height are positive; otherwise, nullptr 446 447 example: https://fiddle.skia.org/c/@Surface_MakeNull 448 */ 449 static sk_sp<SkSurface> MakeNull(int width, int height); 450 451 /** Returns pixel count in each row; may be zero or greater. 452 453 @return number of pixel columns 454 */ width()455 int width() const { return fWidth; } 456 457 /** Returns pixel row count; may be zero or greater. 458 459 @return number of pixel rows 460 */ height()461 int height() const { return fHeight; } 462 463 /** Returns an ImageInfo describing the surface. 464 */ 465 SkImageInfo imageInfo(); 466 467 /** Returns unique value identifying the content of SkSurface. Returned value changes 468 each time the content changes. Content is changed by drawing, or by calling 469 notifyContentWillChange(). 470 471 @return unique content identifier 472 473 example: https://fiddle.skia.org/c/@Surface_notifyContentWillChange 474 */ 475 uint32_t generationID(); 476 477 /** \enum SkSurface::ContentChangeMode 478 ContentChangeMode members are parameters to notifyContentWillChange(). 479 */ 480 enum ContentChangeMode { 481 kDiscard_ContentChangeMode, //!< discards surface on change 482 kRetain_ContentChangeMode, //!< preserves surface on change 483 }; 484 485 /** Notifies that SkSurface contents will be changed by code outside of Skia. 486 Subsequent calls to generationID() return a different value. 487 488 TODO: Can kRetain_ContentChangeMode be deprecated? 489 490 example: https://fiddle.skia.org/c/@Surface_notifyContentWillChange 491 */ 492 void notifyContentWillChange(ContentChangeMode mode); 493 494 /** Returns the recording context being used by the SkSurface. 495 496 @return the recording context, if available; nullptr otherwise 497 */ 498 GrRecordingContext* recordingContext(); 499 500 enum BackendHandleAccess { 501 kFlushRead_BackendHandleAccess, //!< back-end object is readable 502 kFlushWrite_BackendHandleAccess, //!< back-end object is writable 503 kDiscardWrite_BackendHandleAccess, //!< back-end object must be overwritten 504 }; 505 506 /** Deprecated. 507 */ 508 static const BackendHandleAccess kFlushRead_TextureHandleAccess = 509 kFlushRead_BackendHandleAccess; 510 511 /** Deprecated. 512 */ 513 static const BackendHandleAccess kFlushWrite_TextureHandleAccess = 514 kFlushWrite_BackendHandleAccess; 515 516 /** Deprecated. 517 */ 518 static const BackendHandleAccess kDiscardWrite_TextureHandleAccess = 519 kDiscardWrite_BackendHandleAccess; 520 521 /** Retrieves the back-end texture. If SkSurface has no back-end texture, an invalid 522 object is returned. Call GrBackendTexture::isValid to determine if the result 523 is valid. 524 525 The returned GrBackendTexture should be discarded if the SkSurface is drawn to or deleted. 526 527 @return GPU texture reference; invalid on failure 528 */ 529 GrBackendTexture getBackendTexture(BackendHandleAccess backendHandleAccess); 530 531 /** Retrieves the back-end render target. If SkSurface has no back-end render target, an invalid 532 object is returned. Call GrBackendRenderTarget::isValid to determine if the result 533 is valid. 534 535 The returned GrBackendRenderTarget should be discarded if the SkSurface is drawn to 536 or deleted. 537 538 @return GPU render target reference; invalid on failure 539 */ 540 GrBackendRenderTarget getBackendRenderTarget(BackendHandleAccess backendHandleAccess); 541 542 /** If the surface was made via MakeFromBackendTexture then it's backing texture may be 543 substituted with a different texture. The contents of the previous backing texture are 544 copied into the new texture. SkCanvas state is preserved. The original sample count is 545 used. The GrBackendFormat and dimensions of replacement texture must match that of 546 the original. 547 548 Upon success textureReleaseProc is called when it is safe to delete the texture in the 549 backend API (accounting only for use of the texture by this surface). If SkSurface creation 550 fails textureReleaseProc is called before this function returns. 551 552 @param backendTexture the new backing texture for the surface 553 @param mode Retain or discard current Content 554 @param textureReleaseProc function called when texture can be released 555 @param releaseContext state passed to textureReleaseProc 556 */ 557 bool replaceBackendTexture(const GrBackendTexture& backendTexture, 558 GrSurfaceOrigin origin, 559 ContentChangeMode mode = kRetain_ContentChangeMode, 560 TextureReleaseProc textureReleaseProc = nullptr, 561 ReleaseContext releaseContext = nullptr); 562 563 /** Returns SkCanvas that draws into SkSurface. Subsequent calls return the same SkCanvas. 564 SkCanvas returned is managed and owned by SkSurface, and is deleted when SkSurface 565 is deleted. 566 567 @return drawing SkCanvas for SkSurface 568 569 example: https://fiddle.skia.org/c/@Surface_getCanvas 570 */ 571 SkCanvas* getCanvas(); 572 573 /** Returns a compatible SkSurface, or nullptr. Returned SkSurface contains 574 the same raster, GPU, or null properties as the original. Returned SkSurface 575 does not share the same pixels. 576 577 Returns nullptr if imageInfo width or height are zero, or if imageInfo 578 is incompatible with SkSurface. 579 580 @param imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace, 581 of SkSurface; width and height must be greater than zero 582 @return compatible SkSurface or nullptr 583 584 example: https://fiddle.skia.org/c/@Surface_makeSurface 585 */ 586 sk_sp<SkSurface> makeSurface(const SkImageInfo& imageInfo); 587 588 /** Calls makeSurface(ImageInfo) with the same ImageInfo as this surface, but with the 589 * specified width and height. 590 */ 591 sk_sp<SkSurface> makeSurface(int width, int height); 592 593 /** Returns SkImage capturing SkSurface contents. Subsequent drawing to SkSurface contents 594 are not captured. SkImage allocation is accounted for if SkSurface was created with 595 SkBudgeted::kYes. 596 597 @return SkImage initialized with SkSurface contents 598 599 example: https://fiddle.skia.org/c/@Surface_makeImageSnapshot 600 */ 601 sk_sp<SkImage> makeImageSnapshot(); 602 603 /** 604 * Like the no-parameter version, this returns an image of the current surface contents. 605 * This variant takes a rectangle specifying the subset of the surface that is of interest. 606 * These bounds will be sanitized before being used. 607 * - If bounds extends beyond the surface, it will be trimmed to just the intersection of 608 * it and the surface. 609 * - If bounds does not intersect the surface, then this returns nullptr. 610 * - If bounds == the surface, then this is the same as calling the no-parameter variant. 611 612 example: https://fiddle.skia.org/c/@Surface_makeImageSnapshot_2 613 */ 614 sk_sp<SkImage> makeImageSnapshot(const SkIRect& bounds); 615 616 /** Draws SkSurface contents to canvas, with its top-left corner at (x, y). 617 618 If SkPaint paint is not nullptr, apply SkColorFilter, alpha, SkImageFilter, and SkBlendMode. 619 620 @param canvas SkCanvas drawn into 621 @param x horizontal offset in SkCanvas 622 @param y vertical offset in SkCanvas 623 @param sampling what technique to use when sampling the surface pixels 624 @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter, 625 and so on; or nullptr 626 627 example: https://fiddle.skia.org/c/@Surface_draw 628 */ 629 void draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkSamplingOptions& sampling, 630 const SkPaint* paint); 631 632 void draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint = nullptr) { 633 this->draw(canvas, x, y, SkSamplingOptions(), paint); 634 } 635 636 /** Copies SkSurface pixel address, row bytes, and SkImageInfo to SkPixmap, if address 637 is available, and returns true. If pixel address is not available, return 638 false and leave SkPixmap unchanged. 639 640 pixmap contents become invalid on any future change to SkSurface. 641 642 @param pixmap storage for pixel state if pixels are readable; otherwise, ignored 643 @return true if SkSurface has direct access to pixels 644 645 example: https://fiddle.skia.org/c/@Surface_peekPixels 646 */ 647 bool peekPixels(SkPixmap* pixmap); 648 649 /** Copies SkRect of pixels to dst. 650 651 Source SkRect corners are (srcX, srcY) and SkSurface (width(), height()). 652 Destination SkRect corners are (0, 0) and (dst.width(), dst.height()). 653 Copies each readable pixel intersecting both rectangles, without scaling, 654 converting to dst.colorType() and dst.alphaType() if required. 655 656 Pixels are readable when SkSurface is raster, or backed by a GPU. 657 658 The destination pixel storage must be allocated by the caller. 659 660 Pixel values are converted only if SkColorType and SkAlphaType 661 do not match. Only pixels within both source and destination rectangles 662 are copied. dst contents outside SkRect intersection are unchanged. 663 664 Pass negative values for srcX or srcY to offset pixels across or down destination. 665 666 Does not copy, and returns false if: 667 - Source and destination rectangles do not intersect. 668 - SkPixmap pixels could not be allocated. 669 - dst.rowBytes() is too small to contain one row of pixels. 670 671 @param dst storage for pixels copied from SkSurface 672 @param srcX offset into readable pixels on x-axis; may be negative 673 @param srcY offset into readable pixels on y-axis; may be negative 674 @return true if pixels were copied 675 676 example: https://fiddle.skia.org/c/@Surface_readPixels 677 */ 678 bool readPixels(const SkPixmap& dst, int srcX, int srcY); 679 680 /** Copies SkRect of pixels from SkCanvas into dstPixels. 681 682 Source SkRect corners are (srcX, srcY) and SkSurface (width(), height()). 683 Destination SkRect corners are (0, 0) and (dstInfo.width(), dstInfo.height()). 684 Copies each readable pixel intersecting both rectangles, without scaling, 685 converting to dstInfo.colorType() and dstInfo.alphaType() if required. 686 687 Pixels are readable when SkSurface is raster, or backed by a GPU. 688 689 The destination pixel storage must be allocated by the caller. 690 691 Pixel values are converted only if SkColorType and SkAlphaType 692 do not match. Only pixels within both source and destination rectangles 693 are copied. dstPixels contents outside SkRect intersection are unchanged. 694 695 Pass negative values for srcX or srcY to offset pixels across or down destination. 696 697 Does not copy, and returns false if: 698 - Source and destination rectangles do not intersect. 699 - SkSurface pixels could not be converted to dstInfo.colorType() or dstInfo.alphaType(). 700 - dstRowBytes is too small to contain one row of pixels. 701 702 @param dstInfo width, height, SkColorType, and SkAlphaType of dstPixels 703 @param dstPixels storage for pixels; dstInfo.height() times dstRowBytes, or larger 704 @param dstRowBytes size of one destination row; dstInfo.width() times pixel size, or larger 705 @param srcX offset into readable pixels on x-axis; may be negative 706 @param srcY offset into readable pixels on y-axis; may be negative 707 @return true if pixels were copied 708 */ 709 bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, 710 int srcX, int srcY); 711 712 /** Copies SkRect of pixels from SkSurface into bitmap. 713 714 Source SkRect corners are (srcX, srcY) and SkSurface (width(), height()). 715 Destination SkRect corners are (0, 0) and (bitmap.width(), bitmap.height()). 716 Copies each readable pixel intersecting both rectangles, without scaling, 717 converting to bitmap.colorType() and bitmap.alphaType() if required. 718 719 Pixels are readable when SkSurface is raster, or backed by a GPU. 720 721 The destination pixel storage must be allocated by the caller. 722 723 Pixel values are converted only if SkColorType and SkAlphaType 724 do not match. Only pixels within both source and destination rectangles 725 are copied. dst contents outside SkRect intersection are unchanged. 726 727 Pass negative values for srcX or srcY to offset pixels across or down destination. 728 729 Does not copy, and returns false if: 730 - Source and destination rectangles do not intersect. 731 - SkSurface pixels could not be converted to dst.colorType() or dst.alphaType(). 732 - dst pixels could not be allocated. 733 - dst.rowBytes() is too small to contain one row of pixels. 734 735 @param dst storage for pixels copied from SkSurface 736 @param srcX offset into readable pixels on x-axis; may be negative 737 @param srcY offset into readable pixels on y-axis; may be negative 738 @return true if pixels were copied 739 740 example: https://fiddle.skia.org/c/@Surface_readPixels_3 741 */ 742 bool readPixels(const SkBitmap& dst, int srcX, int srcY); 743 744 using AsyncReadResult = SkImage::AsyncReadResult; 745 746 /** Client-provided context that is passed to client-provided ReadPixelsContext. */ 747 using ReadPixelsContext = void*; 748 749 /** Client-provided callback to asyncRescaleAndReadPixels() or 750 asyncRescaleAndReadPixelsYUV420() that is called when read result is ready or on failure. 751 */ 752 using ReadPixelsCallback = void(ReadPixelsContext, std::unique_ptr<const AsyncReadResult>); 753 754 /** Controls the gamma that rescaling occurs in for asyncRescaleAndReadPixels() and 755 asyncRescaleAndReadPixelsYUV420(). 756 */ 757 using RescaleGamma = SkImage::RescaleGamma; 758 using RescaleMode = SkImage::RescaleMode; 759 760 /** Makes surface pixel data available to caller, possibly asynchronously. It can also rescale 761 the surface pixels. 762 763 Currently asynchronous reads are only supported on the GPU backend and only when the 764 underlying 3D API supports transfer buffers and CPU/GPU synchronization primitives. In all 765 other cases this operates synchronously. 766 767 Data is read from the source sub-rectangle, is optionally converted to a linear gamma, is 768 rescaled to the size indicated by 'info', is then converted to the color space, color type, 769 and alpha type of 'info'. A 'srcRect' that is not contained by the bounds of the surface 770 causes failure. 771 772 When the pixel data is ready the caller's ReadPixelsCallback is called with a 773 AsyncReadResult containing pixel data in the requested color type, alpha type, and color 774 space. The AsyncReadResult will have count() == 1. Upon failure the callback is called 775 with nullptr for AsyncReadResult. For a GPU surface this flushes work but a submit must 776 occur to guarantee a finite time before the callback is called. 777 778 The data is valid for the lifetime of AsyncReadResult with the exception that if the 779 SkSurface is GPU-backed the data is immediately invalidated if the context is abandoned 780 or destroyed. 781 782 @param info info of the requested pixels 783 @param srcRect subrectangle of surface to read 784 @param rescaleGamma controls whether rescaling is done in the surface's gamma or whether 785 the source data is transformed to a linear gamma before rescaling. 786 @param rescaleMode controls the technique of the rescaling 787 @param callback function to call with result of the read 788 @param context passed to callback 789 */ 790 void asyncRescaleAndReadPixels(const SkImageInfo& info, 791 const SkIRect& srcRect, 792 RescaleGamma rescaleGamma, 793 RescaleMode rescaleMode, 794 ReadPixelsCallback callback, 795 ReadPixelsContext context); 796 797 /** 798 Similar to asyncRescaleAndReadPixels but performs an additional conversion to YUV. The 799 RGB->YUV conversion is controlled by 'yuvColorSpace'. The YUV data is returned as three 800 planes ordered y, u, v. The u and v planes are half the width and height of the resized 801 rectangle. The y, u, and v values are single bytes. Currently this fails if 'dstSize' 802 width and height are not even. A 'srcRect' that is not contained by the bounds of the 803 surface causes failure. 804 805 When the pixel data is ready the caller's ReadPixelsCallback is called with a 806 AsyncReadResult containing the planar data. The AsyncReadResult will have count() == 3. 807 Upon failure the callback is called with nullptr for AsyncReadResult. For a GPU surface this 808 flushes work but a submit must occur to guarantee a finite time before the callback is 809 called. 810 811 The data is valid for the lifetime of AsyncReadResult with the exception that if the 812 SkSurface is GPU-backed the data is immediately invalidated if the context is abandoned 813 or destroyed. 814 815 @param yuvColorSpace The transformation from RGB to YUV. Applied to the resized image 816 after it is converted to dstColorSpace. 817 @param dstColorSpace The color space to convert the resized image to, after rescaling. 818 @param srcRect The portion of the surface to rescale and convert to YUV planes. 819 @param dstSize The size to rescale srcRect to 820 @param rescaleGamma controls whether rescaling is done in the surface's gamma or whether 821 the source data is transformed to a linear gamma before rescaling. 822 @param rescaleMode controls the sampling technique of the rescaling 823 @param callback function to call with the planar read result 824 @param context passed to callback 825 */ 826 void asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace, 827 sk_sp<SkColorSpace> dstColorSpace, 828 const SkIRect& srcRect, 829 const SkISize& dstSize, 830 RescaleGamma rescaleGamma, 831 RescaleMode rescaleMode, 832 ReadPixelsCallback callback, 833 ReadPixelsContext context); 834 835 /** Copies SkRect of pixels from the src SkPixmap to the SkSurface. 836 837 Source SkRect corners are (0, 0) and (src.width(), src.height()). 838 Destination SkRect corners are (dstX, dstY) and 839 (dstX + Surface width(), dstY + Surface height()). 840 841 Copies each readable pixel intersecting both rectangles, without scaling, 842 converting to SkSurface colorType() and SkSurface alphaType() if required. 843 844 @param src storage for pixels to copy to SkSurface 845 @param dstX x-axis position relative to SkSurface to begin copy; may be negative 846 @param dstY y-axis position relative to SkSurface to begin copy; may be negative 847 848 example: https://fiddle.skia.org/c/@Surface_writePixels 849 */ 850 void writePixels(const SkPixmap& src, int dstX, int dstY); 851 852 /** Copies SkRect of pixels from the src SkBitmap to the SkSurface. 853 854 Source SkRect corners are (0, 0) and (src.width(), src.height()). 855 Destination SkRect corners are (dstX, dstY) and 856 (dstX + Surface width(), dstY + Surface height()). 857 858 Copies each readable pixel intersecting both rectangles, without scaling, 859 converting to SkSurface colorType() and SkSurface alphaType() if required. 860 861 @param src storage for pixels to copy to SkSurface 862 @param dstX x-axis position relative to SkSurface to begin copy; may be negative 863 @param dstY y-axis position relative to SkSurface to begin copy; may be negative 864 865 example: https://fiddle.skia.org/c/@Surface_writePixels_2 866 */ 867 void writePixels(const SkBitmap& src, int dstX, int dstY); 868 869 /** Returns SkSurfaceProps for surface. 870 871 @return LCD striping orientation and setting for device independent fonts 872 */ props()873 const SkSurfaceProps& props() const { return fProps; } 874 875 /** Call to ensure all reads/writes of the surface have been issued to the underlying 3D API. 876 Skia will correctly order its own draws and pixel operations. This must to be used to ensure 877 correct ordering when the surface backing store is accessed outside Skia (e.g. direct use of 878 the 3D API or a windowing system). GrDirectContext has additional flush and submit methods 879 that apply to all surfaces and images created from a GrDirectContext. This is equivalent to 880 calling SkSurface::flush with a default GrFlushInfo followed by 881 GrDirectContext::submit(syncCpu). 882 */ 883 void flushAndSubmit(bool syncCpu = false); 884 885 enum class BackendSurfaceAccess { 886 kNoAccess, //!< back-end object will not be used by client 887 kPresent, //!< back-end surface will be used for presenting to screen 888 }; 889 890 /** Issues pending SkSurface commands to the GPU-backed API objects and resolves any SkSurface 891 MSAA. A call to GrDirectContext::submit is always required to ensure work is actually sent 892 to the gpu. Some specific API details: 893 GL: Commands are actually sent to the driver, but glFlush is never called. Thus some 894 sync objects from the flush will not be valid until a submission occurs. 895 896 Vulkan/Metal/D3D/Dawn: Commands are recorded to the backend APIs corresponding command 897 buffer or encoder objects. However, these objects are not sent to the gpu until a 898 submission occurs. 899 900 The work that is submitted to the GPU will be dependent on the BackendSurfaceAccess that is 901 passed in. 902 903 If BackendSurfaceAccess::kNoAccess is passed in all commands will be issued to the GPU. 904 905 If BackendSurfaceAccess::kPresent is passed in and the backend API is not Vulkan, it is 906 treated the same as kNoAccess. If the backend API is Vulkan, the VkImage that backs the 907 SkSurface will be transferred back to its original queue. If the SkSurface was created by 908 wrapping a VkImage, the queue will be set to the queue which was originally passed in on 909 the GrVkImageInfo. Additionally, if the original queue was not external or foreign the 910 layout of the VkImage will be set to VK_IMAGE_LAYOUT_PRESENT_SRC_KHR. 911 912 The GrFlushInfo describes additional options to flush. Please see documentation at 913 GrFlushInfo for more info. 914 915 If the return is GrSemaphoresSubmitted::kYes, only initialized GrBackendSemaphores will be 916 submitted to the gpu during the next submit call (it is possible Skia failed to create a 917 subset of the semaphores). The client should not wait on these semaphores until after submit 918 has been called, but must keep them alive until then. If a submit flag was passed in with 919 the flush these valid semaphores can we waited on immediately. If this call returns 920 GrSemaphoresSubmitted::kNo, the GPU backend will not submit any semaphores to be signaled on 921 the GPU. Thus the client should not have the GPU wait on any of the semaphores passed in 922 with the GrFlushInfo. Regardless of whether semaphores were submitted to the GPU or not, the 923 client is still responsible for deleting any initialized semaphores. 924 Regardleess of semaphore submission the context will still be flushed. It should be 925 emphasized that a return value of GrSemaphoresSubmitted::kNo does not mean the flush did not 926 happen. It simply means there were no semaphores submitted to the GPU. A caller should only 927 take this as a failure if they passed in semaphores to be submitted. 928 929 Pending surface commands are flushed regardless of the return result. 930 931 @param access type of access the call will do on the backend object after flush 932 @param info flush options 933 */ 934 GrSemaphoresSubmitted flush(BackendSurfaceAccess access, const GrFlushInfo& info); 935 936 /** Issues pending SkSurface commands to the GPU-backed API objects and resolves any SkSurface 937 MSAA. A call to GrDirectContext::submit is always required to ensure work is actually sent 938 to the gpu. Some specific API details: 939 GL: Commands are actually sent to the driver, but glFlush is never called. Thus some 940 sync objects from the flush will not be valid until a submission occurs. 941 942 Vulkan/Metal/D3D/Dawn: Commands are recorded to the backend APIs corresponding command 943 buffer or encoder objects. However, these objects are not sent to the gpu until a 944 submission occurs. 945 946 The GrFlushInfo describes additional options to flush. Please see documentation at 947 GrFlushInfo for more info. 948 949 If a GrBackendSurfaceMutableState is passed in, at the end of the flush we will transition 950 the surface to be in the state requested by the GrBackendSurfaceMutableState. If the surface 951 (or SkImage or GrBackendSurface wrapping the same backend object) is used again after this 952 flush the state may be changed and no longer match what is requested here. This is often 953 used if the surface will be used for presenting or external use and the client wants backend 954 object to be prepped for that use. A finishedProc or semaphore on the GrFlushInfo will also 955 include the work for any requested state change. 956 957 If the backend API is Vulkan, the caller can set the GrBackendSurfaceMutableState's 958 VkImageLayout to VK_IMAGE_LAYOUT_UNDEFINED or queueFamilyIndex to VK_QUEUE_FAMILY_IGNORED to 959 tell Skia to not change those respective states. 960 961 If the return is GrSemaphoresSubmitted::kYes, only initialized GrBackendSemaphores will be 962 submitted to the gpu during the next submit call (it is possible Skia failed to create a 963 subset of the semaphores). The client should not wait on these semaphores until after submit 964 has been called, but must keep them alive until then. If a submit flag was passed in with 965 the flush these valid semaphores can we waited on immediately. If this call returns 966 GrSemaphoresSubmitted::kNo, the GPU backend will not submit any semaphores to be signaled on 967 the GPU. Thus the client should not have the GPU wait on any of the semaphores passed in 968 with the GrFlushInfo. Regardless of whether semaphores were submitted to the GPU or not, the 969 client is still responsible for deleting any initialized semaphores. 970 Regardleess of semaphore submission the context will still be flushed. It should be 971 emphasized that a return value of GrSemaphoresSubmitted::kNo does not mean the flush did not 972 happen. It simply means there were no semaphores submitted to the GPU. A caller should only 973 take this as a failure if they passed in semaphores to be submitted. 974 975 Pending surface commands are flushed regardless of the return result. 976 977 @param info flush options 978 @param access optional state change request after flush 979 */ 980 GrSemaphoresSubmitted flush(const GrFlushInfo& info, 981 const GrBackendSurfaceMutableState* newState = nullptr); 982 flush()983 void flush() { this->flush({}); } 984 985 /** Inserts a list of GPU semaphores that the current GPU-backed API must wait on before 986 executing any more commands on the GPU for this surface. If this call returns false, then 987 the GPU back-end will not wait on any passed in semaphores, and the client will still own 988 the semaphores, regardless of the value of deleteSemaphoresAfterWait. 989 990 If deleteSemaphoresAfterWait is false then Skia will not delete the semaphores. In this case 991 it is the client's responsibility to not destroy or attempt to reuse the semaphores until it 992 knows that Skia has finished waiting on them. This can be done by using finishedProcs 993 on flush calls. 994 995 @param numSemaphores size of waitSemaphores array 996 @param waitSemaphores array of semaphore containers 997 @paramm deleteSemaphoresAfterWait who owns and should delete the semaphores 998 @return true if GPU is waiting on semaphores 999 */ 1000 bool wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores, 1001 bool deleteSemaphoresAfterWait = true); 1002 1003 /** Initializes SkSurfaceCharacterization that can be used to perform GPU back-end 1004 processing in a separate thread. Typically this is used to divide drawing 1005 into multiple tiles. SkDeferredDisplayListRecorder records the drawing commands 1006 for each tile. 1007 1008 Return true if SkSurface supports characterization. raster surface returns false. 1009 1010 @param characterization properties for parallel drawing 1011 @return true if supported 1012 1013 example: https://fiddle.skia.org/c/@Surface_characterize 1014 */ 1015 bool characterize(SkSurfaceCharacterization* characterization) const; 1016 1017 /** Draws the deferred display list created via a SkDeferredDisplayListRecorder. 1018 If the deferred display list is not compatible with this SkSurface, the draw is skipped 1019 and false is return. 1020 1021 The xOffset and yOffset parameters are experimental and, if not both zero, will cause 1022 the draw to be ignored. 1023 When implemented, if xOffset or yOffset are non-zero, the DDL will be drawn offset by that 1024 amount into the surface. 1025 1026 @param deferredDisplayList drawing commands 1027 @param xOffset x-offset at which to draw the DDL 1028 @param yOffset y-offset at which to draw the DDL 1029 @return false if deferredDisplayList is not compatible 1030 1031 example: https://fiddle.skia.org/c/@Surface_draw_2 1032 */ 1033 bool draw(sk_sp<const SkDeferredDisplayList> deferredDisplayList, 1034 int xOffset = 0, 1035 int yOffset = 0); 1036 1037 protected: 1038 SkSurface(int width, int height, const SkSurfaceProps* surfaceProps); 1039 SkSurface(const SkImageInfo& imageInfo, const SkSurfaceProps* surfaceProps); 1040 1041 // called by subclass if their contents have changed dirtyGenerationID()1042 void dirtyGenerationID() { 1043 fGenerationID = 0; 1044 } 1045 1046 private: 1047 const SkSurfaceProps fProps; 1048 const int fWidth; 1049 const int fHeight; 1050 uint32_t fGenerationID; 1051 1052 using INHERITED = SkRefCnt; 1053 }; 1054 1055 #endif 1056