skia2/site/user/api/SkSurface_Reference.md
Cary Clark 09d80c0337 docs with more pop
Replace a hunk of documentation in docs/*.bmh
with #Populate, which instructs bookmaker to
retrieve the documentation from include/core.

Check spelling for all documentation retrieved
from include/core against Skia declarations
and a list of words in spelling.txt.

TBR=caryclark@google.com
Docs-Preview: https://skia.org/?cl=163491
Bug: skia:
Change-Id: If057c3a1336e312ad59c084a3a130f0276802496
Reviewed-on: https://skia-review.googlesource.com/c/163491
Commit-Queue: Cary Clark <caryclark@skia.org>
Reviewed-by: Cary Clark <caryclark@skia.org>
Auto-Submit: Cary Clark <caryclark@skia.org>
2018-10-31 16:45:32 +00:00

107 KiB

SkSurface Reference


class SkSurface : public SkRefCnt {
public:
    static sk_sp<SkSurface> MakeRasterDirect(const SkImageInfo& imageInfo, void* pixels,
                                      size_t rowBytes,
                                      const SkSurfaceProps* surfaceProps = nullptr);
    static sk_sp<SkSurface> MakeRasterDirectReleaseProc(const SkImageInfo& imageInfo, void* pixels,
                                    size_t rowBytes,
                                    void (*releaseProc)(void* pixels, void* context),
                                    void* context, const SkSurfaceProps* surfaceProps = nullptr);
    static sk_sp<SkSurface> MakeRaster(const SkImageInfo& imageInfo, size_t rowBytes,
                                       const SkSurfaceProps* surfaceProps);
    static sk_sp<SkSurface> MakeRaster(const SkImageInfo& imageInfo,
                                       const SkSurfaceProps* props = nullptr);
    static sk_sp<SkSurface> MakeRasterN32Premul(int width, int height,
                                                const SkSurfaceProps* surfaceProps = nullptr);
    static sk_sp<SkSurface> MakeFromBackendTexture(GrContext* context,
                                                   const GrBackendTexture& backendTexture,
                                                   GrSurfaceOrigin origin, int sampleCnt,
                                                   SkColorType colorType,
                                                   sk_sp<SkColorSpace> colorSpace,
                                                   const SkSurfaceProps* surfaceProps);
    static sk_sp<SkSurface> MakeFromBackendRenderTarget(GrContext* context,
                                                const GrBackendRenderTarget& backendRenderTarget,
                                                GrSurfaceOrigin origin,
                                                SkColorType colorType,
                                                sk_sp<SkColorSpace> colorSpace,
                                                const SkSurfaceProps* surfaceProps);
    static sk_sp<SkSurface> MakeFromBackendTextureAsRenderTarget(GrContext* context,
                                                            const GrBackendTexture& backendTexture,
                                                            GrSurfaceOrigin origin,
                                                            int sampleCnt,
                                                            SkColorType colorType,
                                                            sk_sp<SkColorSpace> colorSpace,
                                                            const SkSurfaceProps* surfaceProps);
    static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
                                             const SkImageInfo& imageInfo,
                                             int sampleCount, GrSurfaceOrigin surfaceOrigin,
                                             const SkSurfaceProps* surfaceProps,
                                             bool shouldCreateWithMips = false);
    static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
                                             const SkImageInfo& imageInfo, int sampleCount,
                                             const SkSurfaceProps* props);
    static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
                                             const SkImageInfo& imageInfo);
    static sk_sp<SkSurface> MakeRenderTarget(GrContext* context,
                                             const SkSurfaceCharacterization& characterization,
                                             SkBudgeted budgeted);
    static sk_sp<SkSurface> MakeNull(int width, int height);
    int width() const;
    int height() const;
    uint32_t generationID();

    enum ContentChangeMode {
        kDiscard_ContentChangeMode,
        kRetain_ContentChangeMode,
    };

    void notifyContentWillChange(ContentChangeMode mode);

    enum BackendHandleAccess {
        kFlushRead_BackendHandleAccess,
        kFlushWrite_BackendHandleAccess,
        kDiscardWrite_BackendHandleAccess,
    };

    static const BackendHandleAccess kFlushRead_TextureHandleAccess =
            kFlushRead_BackendHandleAccess;
    static const BackendHandleAccess kFlushWrite_TextureHandleAccess =
            kFlushWrite_BackendHandleAccess;
    static const BackendHandleAccess kDiscardWrite_TextureHandleAccess =
            kDiscardWrite_BackendHandleAccess;
    GrBackendTexture getBackendTexture(BackendHandleAccess backendHandleAccess);
    GrBackendRenderTarget getBackendRenderTarget(BackendHandleAccess backendHandleAccess);
    SkCanvas* getCanvas();
    sk_sp<SkSurface> makeSurface(const SkImageInfo& imageInfo);
    sk_sp<SkImage> makeImageSnapshot();
    void draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint);
    bool peekPixels(SkPixmap* pixmap);
    bool readPixels(const SkPixmap& dst, int srcX, int srcY);
    bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
                    int srcX, int srcY);
    bool readPixels(const SkBitmap& dst, int srcX, int srcY);
    void writePixels(const SkPixmap& src, int dstX, int dstY);
    void writePixels(const SkBitmap& src, int dstX, int dstY);
    const SkSurfaceProps& props() const;
    void prepareForExternalIO();
    void flush();
    GrSemaphoresSubmitted flushAndSignalSemaphores(int numSemaphores,
                                                   GrBackendSemaphore signalSemaphores[]);
    bool wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores);
    bool characterize(SkSurfaceCharacterization* characterization) const;
    bool draw(SkDeferredDisplayList* deferredDisplayList);
};

SkSurface is responsible for managing the pixels that a canvas draws into. The pixels can be allocated either in CPU memory (a raster surface) or on the GPU (a GrRenderTarget surface). SkSurface takes care of allocating a SkCanvas that will draw into the surface. Call surface->getCanvas() to use that canvas (but don't delete it, it is owned by the surface). SkSurface always has non-zero dimensions. If there is a request for a new surface, and either of the requested dimensions are zero, then nullptr will be returned.


static sk sp<SkSurface> MakeRasterDirect(const SkImageInfo& imageInfo, void* pixels, size_t rowBytes,
                                         const SkSurfaceProps* surfaceProps = nullptr)

Allocates raster SkSurface. SkCanvas returned by SkSurface draws directly into pixels.

SkSurface is returned if all parameters are valid. Valid parameters include: info dimensions are greater than zero; info contains SkColorType and SkAlphaType supported by raster surface; pixels is not nullptr; rowBytes is large enough to contain info width pixels of SkColorType.

Pixel buffer size should be info height times computed rowBytes. Pixels are not initialized. To access pixels after drawing, call flush() or peekPixels().

Parameters

imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace,

of raster surface; width and height must be greater than zero

Parameters

pixels pointer to destination pixels buffer
rowBytes interval from one SkSurface row to the next
surfaceProps LCD striping orientation and setting for device independent fonts;

may be nullptr

Return Value

SkSurface if all parameters are valid; otherwise, nullptr

Example

Example Output

---
-x-
---

See Also

MakeRasterDirectReleaseProc MakeRaster[2] MakeRasterN32Premul SkCanvas::MakeRasterDirect


static sk sp<SkSurface> MakeRasterDirectReleaseProc(const SkImageInfo& imageInfo, void* pixels,
                                           size_t rowBytes, void (*releaseProc) (void* pixels,
                                           void* context) , void* context,
                                           const SkSurfaceProps* surfaceProps = nullptr)

Allocates raster SkSurface. SkCanvas returned by SkSurface draws directly into pixels. releaseProc is called with pixels and context when SkSurface is deleted.

SkSurface is returned if all parameters are valid. Valid parameters include: info dimensions are greater than zero; info contains SkColorType and SkAlphaType supported by raster surface; pixels is not nullptr; rowBytes is large enough to contain info width pixels of SkColorType.

Pixel buffer size should be info height times computed rowBytes. Pixels are not initialized. To access pixels after drawing, call flush() or peekPixels().

Parameters

imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace,

of raster surface; width and height must be greater than zero

Parameters

pixels pointer to destination pixels buffer
rowBytes interval from one SkSurface row to the next
releaseProc called when SkSurface is deleted; may be nullptr
context passed to releaseProc; may be nullptr
surfaceProps LCD striping orientation and setting for device independent fonts;

may be nullptr

Return Value

SkSurface if all parameters are valid; otherwise, nullptr

Example

Example Output

---
-x-
---
expected release context

See Also

MakeRasterDirect MakeRasterN32Premul MakeRaster[2]


static sk sp<SkSurface> MakeRaster(const SkImageInfo& imageInfo, size_t rowBytes,
                                   const SkSurfaceProps* surfaceProps)

Allocates raster SkSurface. SkCanvas returned by SkSurface draws directly into pixels. Allocates and zeroes pixel memory. Pixel memory size is imageInfo.height() times rowBytes, or times imageInfo.minRowBytes() if rowBytes is zero. Pixel memory is deleted when SkSurface is deleted.

SkSurface is returned if all parameters are valid. Valid parameters include: info dimensions are greater than zero; info contains SkColorType and SkAlphaType supported by raster surface; rowBytes is large enough to contain info width pixels of SkColorType, or is zero.

If rowBytes is not zero, subsequent images returned by makeImageSnapshot() have the same rowBytes.

Parameters

imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace,

of raster surface; width and height must be greater than zero

Parameters

rowBytes interval from one SkSurface row to the next; may be zero
surfaceProps LCD striping orientation and setting for device independent fonts;

may be nullptr

Return Value

SkSurface if all parameters are valid; otherwise, nullptr

Example

Example Output

---
-x-
---

See Also

MakeRasterDirect MakeRasterN32Premul MakeRasterDirectReleaseProc


static sk sp<SkSurface> MakeRaster(const SkImageInfo& imageInfo,
                                   const SkSurfaceProps* props = nullptr)

Allocates raster SkSurface. SkCanvas returned by SkSurface draws directly into pixels. Allocates and zeroes pixel memory. Pixel memory size is imageInfo.height() times imageInfo.minRowBytes(). Pixel memory is deleted when SkSurface is deleted.

SkSurface is returned if all parameters are valid. Valid parameters include: info dimensions are greater than zero; info contains SkColorType and SkAlphaType supported by raster surface.

Parameters

imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace,

of raster surface; width and height must be greater than zero

Parameters

props LCD striping orientation and setting for device independent fonts;

may be nullptr

Return Value

SkSurface if all parameters are valid; otherwise, nullptr

Example

See Also

MakeRasterDirect MakeRasterN32Premul MakeRasterDirectReleaseProc


static sk sp<SkSurface> MakeRasterN32Premul(int width, int height,
                                            const SkSurfaceProps* surfaceProps = nullptr)

Allocates raster SkSurface. SkCanvas returned by SkSurface draws directly into pixels. Allocates and zeroes pixel memory. Pixel memory size is height times width times four. Pixel memory is deleted when SkSurface is deleted.

Internally, sets SkImageInfo to width, height, native color type, and kPremul_SkAlphaType.

SkSurface is returned if width and height are greater than zero.

Use to create SkSurface that matches SkPMColor, the native pixel arrangement on the platform. SkSurface drawn to output device skips converting its pixel format.

Parameters

width pixel column count; must be greater than zero
height pixel row count; must be greater than zero
surfaceProps LCD striping orientation and setting for device independent

fonts; may be nullptr

Return Value

SkSurface if all parameters are valid; otherwise, nullptr

Example

Example Output

---
-x-
---

See Also

MakeRasterDirect MakeRasterN32Premul MakeRasterDirectReleaseProc


static sk sp<SkSurface> MakeFromBackendTexture(GrContext* context,
                                               const GrBackendTexture& backendTexture,
                                               GrSurfaceOrigin origin, int sampleCnt,
                                               SkColorType colorType,
                                               sk sp<SkColorSpace> colorSpace,
                                               const SkSurfaceProps* surfaceProps)

Wraps a GPU-backed texture into SkSurface. Caller must ensure the texture is valid for the lifetime of returned SkSurface. If sampleCnt greater than zero, creates an intermediate MSAA SkSurface which is used for drawing backendTexture.

SkSurface is returned if all parameters are valid. backendTexture is valid if its pixel configuration agrees with colorSpace and context; for instance, if backendTexture has an sRGB configuration, then context must support sRGB, and colorSpace must be present. Further, backendTexture width and height must not exceed context capabilities, and the context must be able to support back-end textures.

If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.

Parameters

context GPU context
backendTexture texture residing on GPU
origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin
sampleCnt samples per pixel, or 0 to disable full scene anti-aliasing
colorType one of:

kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType, kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kRGB_888x_SkColorType, kBGRA_8888_SkColorType, kRGBA_1010102_SkColorType, kRGB_101010x_SkColorType, kGray_8_SkColorType, kRGBA_F16_SkColorType

Parameters

colorSpace range of colors; may be nullptr
surfaceProps LCD striping orientation and setting for device independent

fonts; may be nullptr

Return Value

SkSurface if all parameters are valid; otherwise, nullptr

Example

See Also

GrBackendTexture MakeFromBackendRenderTarget MakeRenderTarget[2][3][4]


static sk sp<SkSurface> MakeFromBackendRenderTarget(GrContext* context,
                                                   const GrBackendRenderTarget& backendRenderTarget,
                                                   GrSurfaceOrigin origin, SkColorType colorType,
                                                   sk sp<SkColorSpace> colorSpace,
                                                   const SkSurfaceProps* surfaceProps)

Wraps a GPU-backed buffer into SkSurface. Caller must ensure backendRenderTarget is valid for the lifetime of returned SkSurface.

SkSurface is returned if all parameters are valid. backendRenderTarget is valid if its pixel configuration agrees with colorSpace and context; for instance, if backendRenderTarget has an sRGB configuration, then context must support sRGB, and colorSpace must be present. Further, backendRenderTarget width and height must not exceed context capabilities, and the context must be able to support back-end render targets.

If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.

Parameters

context GPU context
backendRenderTarget GPU intermediate memory buffer
origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin
colorType one of:

kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType, kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kRGB_888x_SkColorType, kBGRA_8888_SkColorType, kRGBA_1010102_SkColorType, kRGB_101010x_SkColorType, kGray_8_SkColorType, kRGBA_F16_SkColorType

Parameters

colorSpace range of colors
surfaceProps LCD striping orientation and setting for device independent

fonts; may be nullptr

Return Value

SkSurface if all parameters are valid; otherwise, nullptr

Example


    SkPaint paint;
    paint.setTextSize(32);
    GrContext* context = canvas->getGrContext();
    if (!context) {
         canvas->drawString("GPU only!", 20, 40, paint);
         return;
    }
    sk_sp gpuSurface = SkSurface::MakeFromBackendRenderTarget(context,
            backEndRenderTarget, kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
            nullptr, nullptr);
    auto surfaceCanvas = gpuSurface->getCanvas();
    surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
    sk_sp image(gpuSurface->makeImageSnapshot());
    canvas->drawImage(image, 0, 0);

See Also

MakeFromBackendTexture MakeRenderTarget[2][3][4]


static sk sp<SkSurface> MakeFromBackendTextureAsRenderTarget(GrContext* context,
                                            const GrBackendTexture& backendTexture,
                                            GrSurfaceOrigin origin, int sampleCnt,
                                            SkColorType colorType, sk sp<SkColorSpace> colorSpace,
                                            const SkSurfaceProps* surfaceProps)

Wraps a GPU-backed texture into SkSurface. Caller must ensure backendTexture is valid for the lifetime of returned SkSurface. If sampleCnt greater than zero, creates an intermediate MSAA SkSurface which is used for drawing backendTexture.

SkSurface is returned if all parameters are valid. backendTexture is valid if its pixel configuration agrees with colorSpace and context; for instance, if backendTexture has an sRGB configuration, then context must support sRGB, and colorSpace must be present. Further, backendTexture width and height must not exceed context capabilities.

Returned SkSurface is available only for drawing into, and cannot generate an SkImage.

If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.

Parameters

context GPU context
backendTexture texture residing on GPU
origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin
sampleCnt samples per pixel, or 0 to disable full scene anti-aliasing
colorType one of:

kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType, kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kRGB_888x_SkColorType, kBGRA_8888_SkColorType, kRGBA_1010102_SkColorType, kRGB_101010x_SkColorType, kGray_8_SkColorType, kRGBA_F16_SkColorType

Parameters

colorSpace range of colors; may be nullptr
surfaceProps LCD striping orientation and setting for device independent

fonts; may be nullptr

Return Value

SkSurface if all parameters are valid; otherwise, nullptr

Example

See Also

MakeFromBackendRenderTarget MakeRenderTarget[2][3][4]


static sk sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
                                         const SkImageInfo& imageInfo, int sampleCount,
                                         GrSurfaceOrigin surfaceOrigin,
                                         const SkSurfaceProps* surfaceProps,
                                         bool shouldCreateWithMips = false)

Returns SkSurface on GPU indicated by context. Allocates memory for pixels, based on the width, height, and SkColorType in SkImageInfo. budgeted selects whether allocation for pixels is tracked by context. imageInfo describes the pixel format in SkColorType, and transparency in SkAlphaType, and color matching in SkColorSpace.

sampleCount requests the number of samples per pixel. Pass zero to disable multi-sample anti-aliasing. The request is rounded up to the next supported count, or rounded down if it is larger than the maximum supported count.

surfaceOrigin pins either the top-left or the bottom-left corner to the origin.

shouldCreateWithMips hints that SkImage returned by makeImageSnapshot() is mip map.

If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.

Parameters

context GPU context
budgeted one of: SkBudgeted::kNo, SkBudgeted::kYes
imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace;

width, or height, or both, may be zero

Parameters

sampleCount samples per pixel, or 0 to disable full scene anti-aliasing
surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin
surfaceProps LCD striping orientation and setting for device independent

fonts; may be nullptr

Parameters

shouldCreateWithMips hint that SkSurface will host mip map images

Return Value

SkSurface if all parameters are valid; otherwise, nullptr

Example

See Also

MakeFromBackendRenderTarget MakeFromBackendTextureAsRenderTarget


static sk sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
                                         const SkImageInfo& imageInfo, int sampleCount,
                                         const SkSurfaceProps* props)

Returns SkSurface on GPU indicated by context. Allocates memory for pixels, based on the width, height, and SkColorType in SkImageInfo. budgeted selects whether allocation for pixels is tracked by context. imageInfo describes the pixel format in SkColorType, and transparency in SkAlphaType, and color matching in SkColorSpace.

sampleCount requests the number of samples per pixel. Pass zero to disable multi-sample anti-aliasing. The request is rounded up to the next supported count, or rounded down if it is larger than the maximum supported count.

SkSurface bottom-left corner is pinned to the origin.

Parameters

context GPU context
budgeted one of: SkBudgeted::kNo, SkBudgeted::kYes
imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace,

of raster surface; width, or height, or both, may be zero

Parameters

sampleCount samples per pixel, or 0 to disable multi-sample anti-aliasing
props LCD striping orientation and setting for device independent

fonts; may be nullptr

Return Value

SkSurface if all parameters are valid; otherwise, nullptr

Example

LCD text takes advantage of raster striping to improve resolution. Only one of the four combinations is correct, depending on whether monitor LCD striping is horizontal or vertical, and whether the order of the stripes is red blue green or red green blue.

See Also

MakeFromBackendRenderTarget MakeFromBackendTextureAsRenderTarget


static sk sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
                                         const SkImageInfo& imageInfo)

Returns SkSurface on GPU indicated by context. Allocates memory for pixels, based on the width, height, and SkColorType in SkImageInfo. budgeted selects whether allocation for pixels is tracked by context. imageInfo describes the pixel format in SkColorType, and transparency in SkAlphaType, and color matching in SkColorSpace.

SkSurface bottom-left corner is pinned to the origin.

Parameters

context GPU context
budgeted one of: SkBudgeted::kNo, SkBudgeted::kYes
imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace,

of raster surface; width, or height, or both, may be zero

Return Value

SkSurface if all parameters are valid; otherwise, nullptr

Example

See Also

MakeFromBackendRenderTarget MakeFromBackendTextureAsRenderTarget


static sk sp<SkSurface> MakeRenderTarget(GrContext* context,
                                         const SkSurfaceCharacterization& characterization,
                                         SkBudgeted budgeted)

Returns SkSurface on GPU indicated by context that is compatible with the provided characterization. budgeted selects whether allocation for pixels is tracked by context.

Parameters

context GPU context
characterization description of the desired SkSurface
budgeted one of: SkBudgeted::kNo, SkBudgeted::kYes

Return Value

SkSurface if all parameters are valid; otherwise, nullptr

See Also

MakeFromBackendRenderTarget MakeFromBackendTextureAsRenderTarget


static sk sp<SkSurface> MakeNull(int width, int height)

Returns SkSurface without backing pixels. Drawing to SkCanvas returned from SkSurface has no effect. Calling makeImageSnapshot() on returned SkSurface returns nullptr.

Parameters

width one or greater
height one or greater

Return Value

SkSurface if width and height are positive; otherwise, nullptr

Example

Example Output

SkSurface::MakeNull(0, 0) == nullptr
surf->makeImageSnapshot() == nullptr

See Also

MakeRaster[2] MakeRenderTarget[2][3][4]


int width() const

Returns pixel count in each row; may be zero or greater.

Return Value

number of pixel columns

Example

Example Output

surface width=37  canvas width=37

See Also

height()


int height() const

Returns pixel row count; may be zero or greater.

Return Value

number of pixel rows

Example

Example Output

surface height=1000  canvas height=1000

See Also

width()


uint32_t generationID()

Returns unique value identifying the content of SkSurface. Returned value changes each time the content changes. Content is changed by drawing, or by calling notifyContentWillChange().

Return Value

unique content identifier

Example

Example Output

surface generationID: 1
surface generationID: 2
surface generationID: 3

See Also

notifyContentWillChange ContentChangeMode getCanvas


    enum ContentChangeMode {
        kDiscard_ContentChangeMode,
        kRetain_ContentChangeMode,
    };

ContentChangeMode members are parameters to notifyContentWillChange.

Constants

Const Value Description
SkSurface::kDiscard_ContentChangeMode #Line # discards surface on change ## Pass to notifyContentWillChange to discard surface contents when the surface is cleared or overwritten.
SkSurface::kRetain_ContentChangeMode #Line # preserves surface on change ## Pass to notifyContentWillChange when to preserve surface contents. If a snapshot has been generated, this copies the Surface contents.

See Also

notifyContentWillChange generationID


void notifyContentWillChange(ContentChangeMode mode)

Notifies that Surface contents will be changed by code outside of Skia. Subsequent calls to generationID return a different value.

mode is normally passed as kRetain ContentChangeMode.

Private: Can we deprecate this?

Parameters

mode one of: kDiscard ContentChangeMode, kRetain ContentChangeMode

Example

See Also

ContentChangeMode generationID


    enum BackendHandleAccess {
        kFlushRead_BackendHandleAccess,
        kFlushWrite_BackendHandleAccess,
        kDiscardWrite_BackendHandleAccess,
    };

    static const BackendHandleAccess kFlushRead_TextureHandleAccess =
            kFlushRead_BackendHandleAccess;
    static const BackendHandleAccess kFlushWrite_TextureHandleAccess =
            kFlushWrite_BackendHandleAccess;
    static const BackendHandleAccess kDiscardWrite_TextureHandleAccess =
            kDiscardWrite_BackendHandleAccess;

Constants

Const Value Description
SkSurface::kFlushRead_BackendHandleAccess 0 Caller may read from the back-end object.
SkSurface::kFlushWrite_BackendHandleAccess 1 Caller may write to the back-end object.
SkSurface::kDiscardWrite_BackendHandleAccess 2 Caller must overwrite the entire back-end object.
SkSurface::kFlushRead_TextureHandleAccess 0 Deprecated.
SkSurface::kFlushWrite_TextureHandleAccess 1 Deprecated.
SkSurface::kDiscardWrite_TextureHandleAccess 2 Deprecated.

See Also

getBackendTexture getBackendRenderTarget


GrBackendTexture getBackendTexture(BackendHandleAccess backendHandleAccess)

Retrieves the back-end texture. If SkSurface has no back-end texture, an invalid object is returned. Call GrBackendTexture::isValid to determine if the result is valid.

The returned GrBackendTexture should be discarded if the SkSurface is drawn to or deleted.

Parameters

backendHandleAccess one of: kFlushRead_BackendHandleAccess,

kFlushWrite_BackendHandleAccess, kDiscardWrite_BackendHandleAccess

Return Value

GPU texture reference; invalid on failure

See Also

GrBackendTexture BackendHandleAccess getBackendRenderTarget


GrBackendRenderTarget getBackendRenderTarget(BackendHandleAccess backendHandleAccess)

Retrieves the back-end render target. If SkSurface has no back-end render target, an invalid object is returned. Call GrBackendRenderTarget::isValid to determine if the result is valid.

The returned GrBackendRenderTarget should be discarded if the SkSurface is drawn to or deleted.

Parameters

backendHandleAccess one of: kFlushRead_BackendHandleAccess,

kFlushWrite_BackendHandleAccess, kDiscardWrite_BackendHandleAccess

Return Value

GPU render target reference; invalid on failure

See Also

GrBackendRenderTarget BackendHandleAccess getBackendTexture


SkCanvas* getCanvas()

Returns SkCanvas that draws into SkSurface. Subsequent calls return the same SkCanvas. SkCanvas returned is managed and owned by SkSurface, and is deleted when SkSurface is deleted.

Return Value

drawing SkCanvas for SkSurface

Example

See Also

makeSurface makeImageSnapshot draw[2]


sk sp<SkSurface> makeSurface(const SkImageInfo& imageInfo)

Returns a compatible SkSurface, or nullptr. Returned SkSurface contains the same raster, GPU, or null properties as the original. Returned SkSurface does not share the same pixels.

Returns nullptr if imageInfo width or height are zero, or if imageInfo is incompatible with SkSurface.

Parameters

imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace,

of SkSurface; width and height must be greater than zero

Return Value

compatible SkSurface or nullptr

Example

See Also

makeImageSnapshot getCanvas draw[2]


sk sp<SkImage> makeImageSnapshot()

Returns SkImage capturing SkSurface contents. Subsequent drawing to SkSurface contents are not captured. SkImage allocation is accounted for if SkSurface was created with SkBudgeted::kYes.

Return Value

SkImage initialized with SkSurface contents

Example

See Also

draw[2] getCanvas


void draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint)

Draws SkSurface contents to canvas, with its top-left corner at (x, y).

If SkPaint paint is not nullptr, apply SkColorFilter, alpha, SkImageFilter, SkBlendMode, and SkDrawLooper.

Parameters

canvas SkCanvas drawn into
x horizontal offset in SkCanvas
y vertical offset in SkCanvas
paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter,

and so on; or nullptr

Example

See Also

makeImageSnapshot getCanvas


bool peekPixels(SkPixmap* pixmap)

Copies SkSurface pixel address, row bytes, and SkImageInfo to SkPixmap, if address is available, and returns true. If pixel address is not available, return false and leave SkPixmap unchanged.

pixmap contents become invalid on any future change to SkSurface.

Parameters

pixmap storage for pixel state if pixels are readable; otherwise, ignored

Return Value

true if SkSurface has direct access to pixels

Example

See Also

readPixels[2][3] writePixels[2]


bool readPixels(const SkPixmap& dst, int srcX, int srcY)

Copies Rect of pixels to dst.

Source Rect corners are (srcX, srcY) and Surface (width(), height()). Destination Rect corners are (0, 0) and (dst.width(), dst.height()). Copies each readable pixel intersecting both rectangles, without scaling, converting to dst.colorType() and dst.alphaType() if required.

Pixels are readable when Surface is raster, or backed by a GPU.

The destination pixel storage must be allocated by the caller.

Pixel values are converted only if Color Type and Alpha Type do not match. Only pixels within both source and destination rectangles are copied. dst contents outside Rect intersection are unchanged.

Pass negative values for srcX or srcY to offset pixels across or down destination.

Does not copy, and returns false if:

Source and destination rectangles do not intersect.
Pixmap pixels could not be allocated.
dst.rowBytes() is too small to contain one row of pixels.

Parameters

dst storage for pixels copied from Surface
srcX offset into readable pixels on x-axis; may be negative
srcY offset into readable pixels on y-axis; may be negative

Return Value

true if pixels were copied

Example

See Also

peekPixels writePixels[2]


bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, int srcX, int srcY)

Copies Rect of pixels from Canvas into dstPixels.

Source Rect corners are (srcX, srcY) and Surface (width(), height()). Destination Rect corners are (0, 0) and (dstInfo.width(), dstInfo.height()). Copies each readable pixel intersecting both rectangles, without scaling, converting to dstInfo.colorType() and dstInfo.alphaType() if required.

Pixels are readable when Surface is raster, or backed by a GPU.

The destination pixel storage must be allocated by the caller.

Pixel values are converted only if Color Type and Alpha Type do not match. Only pixels within both source and destination rectangles are copied. dstPixels contents outside Rect intersection are unchanged.

Pass negative values for srcX or srcY to offset pixels across or down destination.

Does not copy, and returns false if:

Source and destination rectangles do not intersect.
Surface pixels could not be converted to dstInfo.colorType() or dstInfo.alphaType().
dstRowBytes is too small to contain one row of pixels.

Parameters

dstInfo width, height, Color Type, and Alpha Type of dstPixels
dstPixels storage for pixels; dstInfo.height() times dstRowBytes, or larger
dstRowBytes size of one destination row; dstInfo.width() times pixel size, or larger
srcX offset into readable pixels on x-axis; may be negative
srcY offset into readable pixels on y-axis; may be negative

Return Value

true if pixels were copied

Example

A black oval drawn on a red background provides an image to copy. readPixels copies one quarter of the Surface into each of the four corners. The copied quarter ovals overdraw the original oval.

See Also

peekPixels writePixels[2]


bool readPixels(const SkBitmap& dst, int srcX, int srcY)

Copies Rect of pixels from Surface into bitmap.

Source Rect corners are (srcX, srcY) and Surface (width(), height()). Destination Rect corners are (0, 0) and (bitmap.width(), bitmap.height()). Copies each readable pixel intersecting both rectangles, without scaling, converting to bitmap.colorType() and bitmap.alphaType() if required.

Pixels are readable when Surface is raster, or backed by a GPU.

The destination pixel storage must be allocated by the caller.

Pixel values are converted only if Color Type and Alpha Type do not match. Only pixels within both source and destination rectangles are copied. dst contents outside Rect intersection are unchanged.

Pass negative values for srcX or srcY to offset pixels across or down destination.

Does not copy, and returns false if:

Source and destination rectangles do not intersect.
Surface pixels could not be converted to dst.colorType() or dst.alphaType().
dst pixels could not be allocated.
dst.rowBytes() is too small to contain one row of pixels.

Parameters

dst storage for pixels copied from Surface
srcX offset into readable pixels on x-axis; may be negative
srcY offset into readable pixels on y-axis; may be negative

Return Value

true if pixels were copied

Example

See Also

peekPixels writePixels[2]


void writePixels(const SkPixmap& src, int dstX, int dstY)

Copies Rect of pixels from the src Pixmap to the Surface.

Source Rect corners are (0, 0) and (src.width(), src.height()). Destination Rect corners are (dstX, dstY) and <a href='#SkSurface_writePixels_dstX'>dstX</a>&nbsp;\+&nbsp;<a href='#Surface'>Surface</a> <a href='#SkSurface_width'>width</a>(, dstY + Surface height()).

Copies each readable pixel intersecting both rectangles, without scaling, converting to Surface colorType() and Surface alphaType() if required.

Parameters

src storage for pixels to copy to Surface
dstX x-axis position relative to Surface to begin copy; may be negative
dstY y-axis position relative to Surface to begin copy; may be negative

Example

See Also

readPixels[2][3] peekPixels


void writePixels(const SkBitmap& src, int dstX, int dstY)

Copies Rect of pixels from the src Bitmap to the Surface.

Source Rect corners are (0, 0) and (src.width(), src.height()). Destination Rect corners are (dstX, dstY) and <a href='#SkSurface_writePixels_2_dstX'>dstX</a>&nbsp;\+&nbsp;<a href='#Surface'>Surface</a> <a href='#SkSurface_width'>width</a>(, dstY + Surface height()).

Copies each readable pixel intersecting both rectangles, without scaling, converting to Surface colorType() and Surface alphaType() if required.

Parameters

src storage for pixels to copy to Surface
dstX x-axis position relative to Surface to begin copy; may be negative
dstY y-axis position relative to Surface to begin copy; may be negative

Example

See Also

readPixels[2][3] peekPixels


const SkSurfaceProps& props() const

Returns SkSurfaceProps for surface.

Return Value

LCD striping orientation and setting for device independent fonts

Example

Example Output

surf.props(): kRGB_H_SkPixelGeometry

See Also

SkSurfaceProps


void prepareForExternalIO()

To be deprecated soon.


void flush()

Issues pending SkSurface commands to the GPU-backed API and resolves any SkSurface MSAA.

Skia flushes as needed, so it is not necessary to call this if Skia manages drawing and object lifetime. Call when interleaving Skia calls with native GPU calls.

See Also

GrBackendSemaphore


GrSemaphoresSubmitted flushAndSignalSemaphores(int numSemaphores,
                                               GrBackendSemaphore signalSemaphores[])

Issues pending SkSurface commands to the GPU-backed API and resolves any SkSurface MSAA. After issuing all commands, signalSemaphores of count numSemaphores semaphores are signaled by the GPU.

For each GrBackendSemaphore in signalSemaphores: if GrBackendSemaphore is initialized, the GPU back-end uses the semaphore as is; otherwise, a new semaphore is created and initializes GrBackendSemaphore.

The caller must delete the semaphores created and returned in signalSemaphores. GrBackendSemaphore can be deleted as soon as this function returns.

If the back-end API is OpenGL only uninitialized backend semaphores are supported.

If the back-end API is Vulkan semaphores may be initialized or uninitialized. If uninitialized, created semaphores are valid only with the VkDevice with which they were created.

If GrSemaphoresSubmitted::kNo is returned, the GPU back-end did not create or add any semaphores to signal on the GPU; the caller should not instruct the GPU to wait on any of the semaphores.

Pending surface commands are flushed regardless of the return result.

Parameters

numSemaphores size of signalSemaphores array
signalSemaphores array of semaphore containers

Return Value

one of: GrSemaphoresSubmitted::kYes, GrSemaphoresSubmitted::kNo

See Also

wait GrBackendSemaphore


bool wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores)

Inserts a list of GPU semaphores that the current GPU-backed API must wait on before executing any more commands on the GPU for this surface. Skia will take ownership of the underlying semaphores and delete them once they have been signaled and waited on. If this call returns false, then the GPU back-end will not wait on any passed in semaphores, and the client will still own the semaphores.

Parameters

numSemaphores size of waitSemaphores array
waitSemaphores array of semaphore containers

Return Value

true if GPU is waiting on semaphores

See Also

flushAndSignalSemaphores GrBackendSemaphore


bool characterize(SkSurfaceCharacterization* characterization) const

Initializes SkSurfaceCharacterization that can be used to perform GPU back-end processing in a separate thread. Typically this is used to divide drawing into multiple tiles. SkDeferredDisplayListRecorder records the drawing commands for each tile.

Return true if SkSurface supports characterization. raster surface returns false.

Parameters

characterization properties for parallel drawing

Return Value

true if supported

Example

See Also

draw[2]() SkSurfaceCharacterization SkDeferredDisplayList


bool draw(SkDeferredDisplayList* deferredDisplayList)

Draws deferred display list created using SkDeferredDisplayListRecorder. Has no effect and returns false if SkSurfaceCharacterization stored in deferredDisplayList is not compatible with SkSurface.

raster surface returns false.

Parameters

deferredDisplayList drawing commands

Return Value

false if deferredDisplayList is not compatible

Example

See Also

characterize() SkSurfaceCharacterization SkDeferredDisplayList