Surface

class Surface

Surface 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). Surface takes care of allocating a Canvas that will draw into the surface. Call getCanvas() to use that canvas (but don’t delete it, it is owned by the surface). Surface 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.

Example:

surface = skia.Surface(640, 480)
with surface as canvas:
    draw(canvas)
image = surface.makeImageSnapshot()

Classes

AsyncReadResult

The result from Surface.asyncRescaleAndReadPixels() or Surface.asyncRescaleAndReadPixelsYUV420().

BackendHandleAccess

Members:

BackendSurfaceAccess

Members:

ContentChangeMode

Members:

RescaleGamma

Controls the gamma that rescaling occurs in for

Methods

MakeFromBackendRenderTarget

Wraps a GPU-backed buffer into Surface.

MakeFromBackendTexture

Wraps a GPU-backed texture into Surface.

MakeNull

Returns Surface without backing pixels.

MakeRaster

Allocates raster Surface.

MakeRasterDirect

Allocates raster Surface.

MakeRasterN32Premul

Allocates raster Surface.

MakeRenderTarget

Overloaded function.

__init__

__init__(*args, **kwargs) Overloaded function.

asyncRescaleAndReadPixels

Makes surface pixel data available to caller, possibly asynchronously.

asyncRescaleAndReadPixelsYUV420

Similar to asyncRescaleAndReadPixels() but performs an additional conversion to YUV.

characterize

Initializes SurfaceCharacterization that can be used to perform GPU back-end processing in a separate thread.

draw

Overloaded function.

flush

Overloaded function.

flushAndSubmit

Call to ensure all reads/writes of the surface have been issued to the underlying 3D API.

generationID

Returns unique value identifying the content of Surface.

getBackendRenderTarget

Retrieves the back-end render target.

getBackendTexture

Retrieves the back-end texture.

getCanvas

Returns Canvas that draws into Surface.

height

Returns pixel row count; may be zero or greater.

imageInfo

Returns an ImageInfo describing the surface.

isCompatible

Is this surface compatible with the provided characterization?

makeImageSnapshot

Overloaded function.

makeSurface

Overloaded function.

notifyContentWillChange

Notifies that Surface contents will be changed by code outside of Skia.

peekPixels

Copies Surface pixel address, row bytes, and ImageInfo to Pixmap, if address is available, and returns true.

props

Returns SurfaceProps for surface.

readPixels

Overloaded function.

recordingContext

Returns the recording context being used by the Surface.

ref

Increment the reference count.

replaceBackendTexture

If the surface was made via MakeFromBackendTexture() then it’s backing texture may be substituted with a different texture.

toarray

Exports a numpy.ndarray.

unique

May return true if the caller is the only owner.

unref

Decrement the reference count.

width

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

writePixels

Overloaded function.

Attributes

kDiscardWrite_BackendHandleAccess

kDiscard_ContentChangeMode

kFlushRead_BackendHandleAccess

kFlushWrite_BackendHandleAccess

kLinear

kNoAccess

kPresent

kRetain_ContentChangeMode

kSrc

Methods

static Surface.MakeFromBackendRenderTarget(context: skia.GrContext, backendRenderTarget: skia.GrBackendRenderTarget, origin: skia.GrSurfaceOrigin, colorType: skia.ColorType, colorSpace: skia.ColorSpace, surfaceProps: skia.SurfaceProps = None) skia.Surface

Wraps a GPU-backed buffer into Surface.

Caller must ensure backendRenderTarget is valid for the lifetime of returned Surface.

Surface 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.

Upon success releaseProc is called when it is safe to delete the render target in the backend API (accounting only for use of the render target by this surface). If Surface creation fails releaseProc is called before this function returns.

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

Parameters
  • context – GPU context

  • backendRenderTarget – GPU intermediate memory buffer

  • colorSpace – range of colors

  • surfaceProps – LCD striping orientation and setting for device independent fonts; may be nullptr

Returns

Surface if all parameters are valid; otherwise, nullptr

static Surface.MakeFromBackendTexture(context: skia.GrContext, backendTexture: skia.GrBackendTexture, origin: skia.GrSurfaceOrigin, sampleCnt: int, colorType: skia.ColorType, colorSpace: skia.ColorSpace, surfaceProps: skia.SurfaceProps) skia.Surface

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

Surface 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.

Upon success textureReleaseProc is called when it is safe to delete the texture in the backend API (accounting only for use of the texture by this surface). If Surface creation fails textureReleaseProc is called before this function returns.

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

Context

GPU context

BackendTexture

texture residing on GPU

SampleCnt

samples per pixel, or 0 to disable full scene anti-aliasing

ColorSpace

range of colors; may be nullptr

SurfaceProps

LCD striping orientation and setting for device independent fonts; may be nullptr

TextureReleaseProc

function called when texture can be released

ReleaseContext

state passed to textureReleaseProc

Returns

Surface if all parameters are valid; otherwise, nullptr

static Surface.MakeNull(width: int, height: int) skia.Surface

Returns Surface without backing pixels.

Drawing to Canvas returned from Surface has no effect. Calling makeImageSnapshot() on returned Surface returns nullptr.

Parameters
  • width (int) – one or greater

  • height (int) – one or greater

Returns

Surface if width and height are positive; otherwise, nullptr

static Surface.MakeRaster(imageInfo: skia.ImageInfo, rowBytes: int = 0, surfaceProps: skia.SurfaceProps = None) skia.Surface

Allocates raster Surface.

Canvas returned by Surface 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 Surface is deleted.

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

If rowBytes is zero, a suitable value will be chosen internally.

ImageInfo

width, height, ColorType, AlphaType, ColorSpace, of raster surface; width and height must be greater than zero

RowBytes

interval from one Surface row to the next; may be zero

Skia.SurfaceProps surfaceProps

LCD striping orientation and setting for device independent fonts; may be nullptr

Returns

Surface if all parameters are valid; otherwise, nullptr

static Surface.MakeRasterDirect(info: skia.ImageInfo, pixels: buffer, rowBytes: int = 0, surfaceProps: skia.SurfaceProps = None) skia.Surface

Allocates raster Surface.

Canvas returned by Surface draws directly into pixels.

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

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

Parameters
  • imageInfo (skia.ImageInfo) – width, height, ColorType, AlphaType, ColorSpace, of raster surface; width and height must be greater than zero

  • pixels (Union[bytes,bytearray,memoryview]) – pointer to destination pixels buffer

  • rowBytes – interval from one Surface row to the next

  • surfaceProps – LCD striping orientation and setting for device independent fonts; may be nullptr

Returns

Surface if all parameters are valid; otherwise, nullptr

static Surface.MakeRasterN32Premul(width: int, height: int, surfaceProps: skia.SurfaceProps = None) skia.Surface

Allocates raster Surface.

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

Internally, sets ImageInfo to width, height, native color type, and AlphaType.kPremul.

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

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

Parameters
  • width (int) – pixel column count; must be greater than zero

  • height (int) – pixel row count; must be greater than zero

  • surfaceProps (skia.SurfaceProps) – LCD striping orientation and setting for device independent fonts; may be nullptr

Returns

Surface if all parameters are valid; otherwise, nullptr

static Surface.MakeRenderTarget(*args, **kwargs)

Overloaded function.

  1. MakeRenderTarget(context: skia.GrRecordingContext, budgeted: skia.Budgeted, imageInfo: skia.ImageInfo, sampleCount: int = 0, surfaceOrigin: skia.GrSurfaceOrigin = <GrSurfaceOrigin.kBottomLeft_GrSurfaceOrigin: 1>, surfaceProps: skia.SurfaceProps = None, shouldCreateWithMips: bool = False) -> skia.Surface

    Returns Surface on GPU indicated by context.

    Allocates memory for pixels, based on the width, height, and ColorType in ImageInfo. budgeted selects whether allocation for pixels is tracked by context. imageInfo describes the pixel format in ColorType, and transparency in AlphaType, and color matching in ColorSpace.

    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 Image returned by makeImageSnapshot() is mip map.

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

    param skia.GrContext context

    GPU context

    param skia.ImageInfo imageInfo

    width, height, ColorType, AlphaType, ColorSpace; width, or height, or both, may be zero

    param int sampleCount

    samples per pixel, or 0 to disable full scene anti-aliasing

    param skia.GrSurfaceOrigin surfaceOrigin

    kTopLeft or kBottomLeft

    param skia.SurfaceProps surfaceProps

    LCD striping orientation and setting for device independent fonts; may be nullptr

    param shouldCreateWithMips

    hint that Surface will host mip map images

    return

    Surface if all parameters are valid; otherwise, nullptr

  2. MakeRenderTarget(context: skia.GrRecordingContext, budgeted: skia.Budgeted, imageInfo: skia.ImageInfo, sampleCount: int, surfaceProps: skia.SurfaceProps) -> skia.Surface

    Returns Surface on GPU indicated by context.

    Allocates memory for pixels, based on the width, height, and ColorType in ImageInfo. budgeted selects whether allocation for pixels is tracked by context. imageInfo describes the pixel format in ColorType, and transparency in AlphaType, and color matching in ColorSpace.

    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.

    Surface bottom-left corner is pinned to the origin.

    context

    GPU context

    imageInfo

    width, height, ColorType, AlphaType, ColorSpace; width, or height, or both, may be zero

    sampleCount

    samples per pixel, or 0 to disable full scene anti-aliasing

    surfaceProps

    LCD striping orientation and setting for device independent fonts; may be nullptr

    return

    Surface if all parameters are valid; otherwise, nullptr

  3. MakeRenderTarget(context: skia.GrRecordingContext, budgeted: skia.Budgeted, imageInfo: skia.ImageInfo) -> skia.Surface

    Returns Surface on GPU indicated by context.

    Allocates memory for pixels, based on the width, height, and ColorType in ImageInfo. budgeted selects whether allocation for pixels is tracked by context. imageInfo describes the pixel format in ColorType, and transparency in AlphaType, and color matching in ColorSpace.

    Surface bottom-left corner is pinned to the origin.

    context

    GPU context

    imageInfo

    width, height, ColorType, AlphaType, ColorSpace; width, or height, or both, may be zero

  4. MakeRenderTarget(context: skia.GrRecordingContext, characterization: skia.SurfaceCharacterization, budgeted: skia.Budgeted) -> skia.Surface

    Returns Surface on GPU indicated by context that is compatible with the provided characterization.

    budgeted selects whether allocation for pixels is tracked by context.

    context

    GPU context

    characterization

    description of the desired Surface

    return

    Surface if all parameters are valid; otherwise, nullptr

Surface.__init__(*args, **kwargs)

__init__(*args, **kwargs) Overloaded function.

  1. __init__(self: skia.Surface, width: int, height: int, surfaceProps: skia.SurfaceProps = None) -> None

  2. __init__(self: skia.Surface, array: numpy.ndarray, colorType: skia.ColorType = <ColorType.kRGBA_8888_ColorType: 4>, alphaType: skia.AlphaType = <AlphaType.kUnpremul_AlphaType: 3>, colorSpace: skia.ColorSpace = None, surfaceProps: skia.SurfaceProps = None) -> None

    Create a raster surface on numpy array.

    Input array must have compatible shape with the given color type.

    Do not destroy numpy array while using this surface.

    array

    numpy ndarray of shape=(height, width, channels) and appropriate dtype. Must have non-zero width and height, and the valid number of channels for the specified color type.

    colorType

    color type of the array

    alphaType

    alpha type of the array

    colorSpace

    range of colors; may be nullptr

Surface.asyncRescaleAndReadPixels(self: skia.Surface, info: skia.ImageInfo, srcRect: skia.IRect, rescaleGamma: skia.Surface.RescaleGamma, rescaleQuality: skia.FilterQuality, callback: function) None

Makes surface pixel data available to caller, possibly asynchronously.

It can also rescale the surface pixels.

Currently asynchronous reads are only supported on the GPU backend and only when the underlying 3D API supports transfer buffers and CPU/GPU synchronization primitives. In all other cases this operates synchronously.

Data is read from the source sub-rectangle, is optionally converted to a linear gamma, is rescaled to the size indicated by ‘info’, is then converted to the color space, color type, and alpha type of ‘info’. A ‘srcRect’ that is not contained by the bounds of the surface causes failure.

When the pixel data is ready the caller’s callback function is called with a AsyncReadResult containing pixel data in the requested color type, alpha type, and color space. The AsyncReadResult will have count() == 1. Upon failure the callback is called with nullptr for AsyncReadResult. For a GPU surface this flushes work but a submit must occur to guarantee a finite time before the callback is called.

The data is valid for the lifetime of AsyncReadResult with the exception that if the Surface is GPU-backed the data is immediately invalidated if the GrContext is abandoned or destroyed.

Parameters
  • info – info of the requested pixels

  • srcRect – subrectangle of surface to read

  • rescaleGamma – controls whether rescaling is done in the surface’s gamma or whether the source data is transformed to a linear gamma before rescaling.

  • rescaleQuality – controls the quality (and cost) of the rescaling

  • callback – function to call with result of the read. The callback takes one argument of Surface.AsyncReadResult

Surface.asyncRescaleAndReadPixelsYUV420(self: skia.Surface, yuvColorSpace: skia.YUVColorSpace, dstColorSpace: skia.ColorSpace, srcRect: skia.IRect, dstSize: skia.ISize, rescaleGamma: skia.Surface.RescaleGamma, rescaleQuality: skia.FilterQuality, callback: function) None

Similar to asyncRescaleAndReadPixels() but performs an additional conversion to YUV.

The RGB->YUV conversion is controlled by ‘yuvColorSpace’. The YUV data is returned as three planes ordered y, u, v. The u and v planes are half the width and height of the resized rectangle. The y, u, and v values are single bytes. Currently this fails if ‘dstSize’ width and height are not even. A ‘srcRect’ that is not contained by the bounds of the surface causes failure.

When the pixel data is ready the caller’s callback function is called with a AsyncReadResult containing the planar data. The AsyncReadResult will have count() == 3. Upon failure the callback is called with nullptr for AsyncReadResult. For a GPU surface this flushes work but a submit must occur to guarantee a finite time before the callback is called.

The data is valid for the lifetime of AsyncReadResult with the exception that if the Surface is GPU-backed the data is immediately invalidated if the GrContext is abandoned or destroyed.

Parameters
  • yuvColorSpace – The transformation from RGB to YUV. Applied to the resized image after it is converted to dstColorSpace.

  • dstColorSpace – The color space to convert the resized image to, after rescaling.

  • srcRect – The portion of the surface to rescale and convert to YUV planes.

  • dstSize – The size to rescale srcRect to

  • rescaleGamma – controls whether rescaling is done in the surface’s gamma or whether the source data is transformed to a linear gamma before rescaling.

  • rescaleQuality – controls the quality (and cost) of the rescaling

  • callback – function to call with the planar read result

Surface.characterize(self: skia.Surface, characterization: skia.SurfaceCharacterization) bool

Initializes SurfaceCharacterization that can be used to perform GPU back-end processing in a separate thread.

Typically this is used to divide drawing into multiple tiles. DeferredDisplayListRecorder records the drawing commands for each tile.

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

Parameters

characterization (skia.SurfaceCharacterization) – properties for parallel drawing

Returns

true if supported

Surface.draw(*args, **kwargs)

Overloaded function.

  1. draw(self: skia.Surface, canvas: skia.Canvas, x: float, y: float, paint: skia.Paint = None) -> None

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

    If Paint paint is not nullptr, apply ColorFilter, alpha, ImageFilter, BlendMode, and DrawLooper.

    canvas

    Canvas drawn into

    x

    horizontal offset in Canvas

    y

    vertical offset in Canvas

    paint

    Paint containing BlendMode, ColorFilter, ImageFilter, and so on; or nullptr

  2. draw(self: skia.Surface, deferredDisplayList: SkDeferredDisplayList) -> bool

    Draws deferred display list created using DeferredDisplayListRecorder.

    Has no effect and returns false if SurfaceCharacterization stored in deferredDisplayList is not compatible with Surface.

    raster surface returns false.

    deferredDisplayList

    drawing commands

    return

    false if deferredDisplayList is not compatible

Surface.flush(*args, **kwargs)

Overloaded function.

  1. flush(self: skia.Surface, access: skia.Surface.BackendSurfaceAccess, info: skia.GrFlushInfo) -> skia.GrSemaphoresSubmitted

    Issues pending Surface commands to the GPU-backed API objects and resolves any Surface MSAA. A call to GrContext.submit() is always required to ensure work is actually sent to the gpu. Some specific API details:

    GL

    Commands are actually sent to the driver, but glFlush is never called. Thus some sync objects from the flush will not be valid until a submission occurs.

    Vulkan/Metal/D3D/Dawn

    Commands are recorded to the backend APIs corresponding command buffer or encoder objects. However, these objects are not sent to the gpu until a submission occurs.

    The work that is submitted to the GPU will be dependent on the BackendSurfaceAccess that is passed in.

    If BackendSurfaceAccess.kNoAccess is passed in all commands will be issued to the GPU.

    If BackendSurfaceAccess.kPresent is passed in and the backend API is not Vulkan, it is treated the same as kNoAccess. If the backend API is Vulkan, the VkImage that backs the Surface will be transferred back to its original queue. If the Surface was created by wrapping a VkImage, the queue will be set to the queue which was originally passed in on the GrVkImageInfo. Additionally, if the original queue was not external or foreign the layout of the VkImage will be set to VK_IMAGE_LAYOUT_PRESENT_SRC_KHR.

    The GrFlushInfo describes additional options to flush. Please see documentation at GrFlushInfo for more info.

    If the return is GrSemaphoresSubmitted.kYes, only initialized GrBackendSemaphores will be submitted to the gpu during the next submit call (it is possible Skia failed to create a subset of the semaphores). The client should not wait on these semaphores until after submit has been called, but must keep them alive until then. If a submit flag was passed in with the flush these valid semaphores can we waited on immediately. If this call returns GrSemaphoresSubmitted.kNo, the GPU backend will not submit any semaphores to be signaled on the GPU. Thus the client should not have the GPU wait on any of the semaphores passed in with the GrFlushInfo. Regardless of whether semaphores were submitted to the GPU or not, the client is still responsible for deleting any initialized semaphores. Regardleess of semaphore submission the context will still be flushed. It should be emphasized that a return value of GrSemaphoresSubmitted.kNo does not mean the flush did not happen. It simply means there were no semaphores submitted to the GPU. A caller should only take this as a failure if they passed in semaphores to be submitted.

    Pending surface commands are flushed regardless of the return result.

    param access

    type of access the call will do on the backend object after flush

    param info

    flush options

  2. flush(self: skia.Surface, info: skia.GrFlushInfo, newState: skia.GrBackendSurfaceMutableState = None) -> skia.GrSemaphoresSubmitted

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

    A call to GrContext.submit() is always required to ensure work is ctually sent to the gpu. Some specific API details:

    GL

    Commands are actually sent to the driver, but glFlush is never called. Thus some sync objects from the flush will not be valid until a submission occurs.

    Vulkan/Metal/D3D/Dawn

    Commands are recorded to the backend APIs corresponding command buffer or encoder objects. However, these objects are not sent to the gpu until a submission occurs.

    The GrFlushInfo describes additional options to flush. Please see documentation at GrFlushInfo for more info.

    If a GrBackendSurfaceMutableState is passed in, at the end of the flush we will transition the surface to be in the state requested by the GrBackendSurfaceMutableState. If the surface (or Image or GrBackendSurface wrapping the same backend object) is used again after this flush the state may be changed and no longer match what is requested here. This is often used if the surface will be used for presenting or external use and the client wants backend object to be prepped for that use. A finishedProc or semaphore on the GrFlushInfo will also include the work for any requested state change.

    If the return is GrSemaphoresSubmitted::kYes, only initialized GrBackendSemaphores will be submitted to the gpu during the next submit call (it is possible Skia failed to create a subset of the semaphores). The client should not wait on these semaphores until after submit has been called, but must keep them alive until then. If a submit flag was passed in with the flush these valid semaphores can we waited on immediately. If this call returns GrSemaphoresSubmitted::kNo, the GPU backend will not submit any semaphores to be signaled on the GPU. Thus the client should not have the GPU wait on any of the semaphores passed in with the GrFlushInfo. Regardless of whether semaphores were submitted to the GPU or not, the client is still responsible for deleting any initialized semaphores. Regardleess of semaphore submission the context will still be flushed. It should be emphasized that a return value of GrSemaphoresSubmitted::kNo does not mean the flush did not happen. It simply means there were no semaphores submitted to the GPU. A caller should only take this as a failure if they passed in semaphores to be submitted.

    Pending surface commands are flushed regardless of the return result.

    param info

    flush options

    param newState

    optional state change request after flush

Surface.flushAndSubmit(self: skia.Surface) None

Call to ensure all reads/writes of the surface have been issued to the underlying 3D API.

Skia will correctly order its own draws and pixel operations. This must to be used to ensure correct ordering when the surface backing store is accessed outside Skia (e.g. direct use of the 3D API or a windowing system). GrContext has additional flush and submit methods that apply to all surfaces and images created from a GrContext. This is equivalent to calling flush() with a default GrFlushInfo followed by GrContext.submit().

Surface.generationID(self: skia.Surface) int

Returns unique value identifying the content of Surface.

Returned value changes each time the content changes. Content is changed by drawing, or by calling notifyContentWillChange().

Returns

unique content identifier

Surface.getBackendRenderTarget(self: skia.Surface, backendHandleAccess: skia.Surface.BackendHandleAccess) skia.GrBackendRenderTarget

Retrieves the back-end render target.

If Surface 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 Surface is drawn to or deleted.

Returns

GPU render target reference; invalid on failure

Surface.getBackendTexture(self: skia.Surface, backendHandleAccess: skia.Surface.BackendHandleAccess) skia.GrBackendTexture

Retrieves the back-end texture.

If Surface 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 Surface is drawn to or deleted.

Returns

GPU texture reference; invalid on failure

Surface.getCanvas(self: skia.Surface) skia.Canvas

Returns Canvas that draws into Surface.

Subsequent calls return the same Canvas. Canvas returned is managed and owned by Surface, and is deleted when Surface is deleted.

Returns

drawing Canvas for Surface

Surface.height(self: skia.Surface) int

Returns pixel row count; may be zero or greater.

Returns

number of pixel rows

Surface.imageInfo(self: skia.Surface) skia.ImageInfo

Returns an ImageInfo describing the surface.

Surface.isCompatible(self: skia.Surface, characterization: skia.SurfaceCharacterization) bool

Is this surface compatible with the provided characterization?

This method can be used to determine if an existing Surface is a viable destination for an DeferredDisplayList.

Parameters

characterization (skia.SurfaceCharacterization) – The characterization for which a compatibility check is desired

Returns

true if this surface is compatible with the characterization; false otherwise

Surface.makeImageSnapshot(*args, **kwargs)

Overloaded function.

  1. makeImageSnapshot(self: skia.Surface) -> skia.Image

    Returns Image capturing Surface contents.

    Subsequent drawing to Surface contents are not captured. Image allocation is accounted for if Surface was created with Budgeted.kYes.

    return

    Image initialized with Surface contents

  2. makeImageSnapshot(self: skia.Surface, bounds: skia.IRect) -> skia.Image

    Like the no-parameter version, this returns an image of the current surface contents.

    This variant takes a rectangle specifying the subset of the surface that is of interest. These bounds will be sanitized before being used.

    • If bounds extends beyond the surface, it will be trimmed to just the

      intersection of it and the surface.

    • If bounds does not intersect the surface, then this returns nullptr.

    • If bounds == the surface, then this is the same as calling the

      no-parameter variant.

Surface.makeSurface(*args, **kwargs)

Overloaded function.

  1. makeSurface(self: skia.Surface, imageInfo: skia.ImageInfo) -> skia.Surface

    Returns a compatible Surface, or nullptr.

    Returned Surface contains the same raster, GPU, or null properties as the original. Returned Surface does not share the same pixels.

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

    param skia.ImageInfo imageInfo

    width, height, ColorType, AlphaType, ColorSpace, of Surface; width and height must be greater than zero

    return

    compatible Surface or nullptr

  2. makeSurface(self: skia.Surface, width: int, height: int) -> skia.Surface

    Calls makeSurface() with the same ImageInfo as this surface, but with the specified width and height.

Surface.notifyContentWillChange(self: skia.Surface, mode: skia.Surface.ContentChangeMode) None

Notifies that Surface contents will be changed by code outside of Skia.

Subsequent calls to generationID() return a different value.

Surface.peekPixels(self: skia.Surface, pixmap: skia.Pixmap) bool

Copies Surface pixel address, row bytes, and ImageInfo to Pixmap, if address is available, and returns true.

If pixel address is not available, return false and leave Pixmap unchanged.

pixmap contents become invalid on any future change to Surface.

Parameters

pixmap (skia.Pixmap) – storage for pixel state if pixels are readable; otherwise, ignored

Returns

true if Surface has direct access to pixels

Surface.props(self: skia.Surface) skia.SurfaceProps

Returns SurfaceProps for surface.

Returns

LCD striping orientation and setting for device independent fonts

Surface.readPixels(*args, **kwargs)

Overloaded function.

  1. readPixels(self: skia.Surface, dst: skia.Pixmap, srcX: int = 0, srcY: int = 0) -> bool

    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 ColorType and AlphaType 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.

    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

    true if pixels were copied

  2. readPixels(self: skia.Surface, dstInfo: skia.ImageInfo, dstPixels: buffer, dstRowBytes: int = 0, srcX: int = 0, srcY: int = 0) -> bool

    Copies Rect of pixels from Canvas into array.

    Matrix and clip are ignored.

    Source Rect corners are (srcX, srcY) and Surface (width(), height()). Destination Rect corners are (0, 0) and (array.shape[1], array.shape[0]). Copies each readable pixel intersecting both rectangles, without scaling, converting to ColorType.kN32 and AlphaType.kPremul if required.

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

    The destination pixel storage must be allocated by the caller.

    Pixel values are converted only if ColorType and AlphaType do not match. Only pixels within both source and destination rectangles are copied. array 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

      ColorType.kN32 or AlphaType.kPremul.

    dstInfo

    width, height, ColorType, and AlphaType 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. Ignored when dstPixels has more than one-dimension.

    srcX

    offset into readable pixels on x-axis; may be negative

    srcY

    offset into readable pixels on y-axis; may be negative

    return

    true if pixels were copied

  3. readPixels(self: skia.Surface, dst: skia.Bitmap, srcX: int, srcY: int) -> bool

    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 ColorType and AlphaType 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.

    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

    true if pixels were copied

Surface.recordingContext(self: skia.Surface) skia.GrRecordingContext

Returns the recording context being used by the Surface.

Returns

GPU context, if available; nullptr otherwise

Surface.ref(self: skia.RefCntBase) None

Increment the reference count.

Must be balanced by a call to unref().

Surface.replaceBackendTexture(self: skia.Surface, backendTexture: skia.GrBackendTexture, origin: skia.GrSurfaceOrigin, mode: skia.Surface.ContentChangeMode = <ContentChangeMode.kRetain_ContentChangeMode: 1>) bool

If the surface was made via MakeFromBackendTexture() then it’s backing texture may be substituted with a different texture.

The contents of the previous backing texture are copied into the new texture. Canvas state is preserved. The original sample count is used. The GrBackendFormat and dimensions of replacement texture must match that of the original.

Parameters
  • backendTexture – the new backing texture for the surface

  • mode – Retain or discard current Content

Surface.toarray(self: skia.Surface, srcX: int = 0, srcY: int = 0, colorType: skia.ColorType = <ColorType.kUnknown_ColorType: 0>, alphaType: skia.AlphaType = <AlphaType.kUnpremul_AlphaType: 3>, colorSpace: skia.ColorSpace = None) numpy.ndarray

Exports a numpy.ndarray.

Parameters
  • srcX – offset into readable pixels on x-axis; may be negative

  • srcY – offset into readable pixels on y-axis; may be negative

  • colorType – target ColorType

  • alphaType – target AlphaType

  • colorSpace – target ColorSpace

Returns

numpy.ndarray

Surface.unique(self: skia.RefCntBase) bool

May return true if the caller is the only owner.

Ensures that all previous owner’s actions are complete.

Surface.unref(self: skia.RefCntBase) None

Decrement the reference count.

If the reference count is 1 before the decrement, then delete the object. Note that if this is the case, then the object needs to have been allocated via new, and not on the stack.

Surface.width(self: skia.Surface) int

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

Returns

number of pixel columns

Surface.writePixels(*args, **kwargs)

Overloaded function.

  1. writePixels(self: skia.Surface, src: skia.Pixmap, dstX: int = 0, dstY: int = 0) -> None

    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 (dstX + Surface width(), dstY + Surface height()).

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

    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

  2. writePixels(self: skia.Surface, src: skia.Bitmap, dstX: int = 0, dstY: int = 0) -> None

    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 (dstX + Surface width(), dstY + Surface height()).

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

    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

Attributes

Surface.kDiscardWrite_BackendHandleAccess = <BackendHandleAccess.kDiscardWrite_BackendHandleAccess: 2>
Surface.kDiscard_ContentChangeMode = <ContentChangeMode.kDiscard_ContentChangeMode: 0>
Surface.kFlushRead_BackendHandleAccess = <BackendHandleAccess.kFlushRead_BackendHandleAccess: 0>
Surface.kFlushWrite_BackendHandleAccess = <BackendHandleAccess.kFlushWrite_BackendHandleAccess: 1>
Surface.kLinear = <RescaleGamma.kLinear: 1>
Surface.kNoAccess = <BackendSurfaceAccess.kNoAccess: 0>
Surface.kPresent = <BackendSurfaceAccess.kPresent: 1>
Surface.kRetain_ContentChangeMode = <ContentChangeMode.kRetain_ContentChangeMode: 1>
Surface.kSrc = <RescaleGamma.kSrc: 0>