Surface

public final 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 surface->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 nil will be returned.

Use one of the static make methods to create instances of 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 nil; 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, call flush() or peekPixels().

    Declaration

    Swift

    public static func make(_ info: ImageInfo, _ pixels: UnsafeMutableRawPointer, _ rowBytes: Int, _ surfaceProps: SurfaceProperties? = nil) -> Surface?

    Parameters

    imageInfo

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

    pixels

    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 nil

    Return Value

    Surface if all parameters are valid; otherwise, nil

  • 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 nil; rowBytes is large enough to contain info width pixels of ColorType. To access pixels after drawing, call flush() or peekPixels().

    Declaration

    Swift

    public static func make(_ info: ImageInfo, _ rowBytes: Int? = nil, _ surfaceProps: SurfaceProperties? = nil) -> Surface?

    Return Value

    Surface if all parameters are valid; otherwise, nil

  • Allocates raster Surface. Canvas returned by Surface draws directly into the pixmap’s 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 nil;

    Declaration

    Swift

    public static func make(pixmap: Pixmap, surfaceProps: SurfaceProperties? = nil) -> Surface?

    Parameters

    pixmap

    The pixmap to use as the backing store for this surface

    surfaceProps

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

    Return Value

    Surface if all parameters are valid; otherwise, nil

  • Returns Surface without backing pixels. Drawing to the Canvas returned from this surface has no effect. Calling makeImageSnapshot' on returned SkSurface returnsnil`.

    Declaration

    Swift

    public static func makeNull(width: Int32, height: Int32) -> Surface?

    Parameters

    width

    one or greater

    height

    one or greater

    Return Value

    Surface if width and height are positive; otherwise, nil

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

    Declaration

    Swift

    public var canvas: Canvas { get }

    Return Value

    drawing Canvas for Surface

  • Returns SurfaceProps for the surface which contains the LCD striping orientation and setting for device independent fonts

    Declaration

    Swift

    public var surfaceProperties: SurfaceProperties { get }
  • Returns Image capturing Surface contents. Subsequent drawing to Surface contents are not captured. Image allocation is accounted for if Surface was created with Budgeted = .yes

    Declaration

    Swift

    public func snapshot() -> Image?

    Return Value

    Image initialized with Surface contents

  • Draws Surface contents to canvas, with its top-left corner at (x, y). If Paint paint is not nil, apply ColorFilter, alpha, ImageFilter, BlendMode, and DrawLooper.

    Declaration

    Swift

    public func draw(canvas: Canvas, x: Float, y: Float, paint: Paint? = nil)

    Parameters

    canvas

    Canvas drawn into

    x

    horizontal offset in Canvas

    y

    vertical offset in Canvas

    paint

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

  • Copies Surface pixel address, row bytes, and ImageInfo to a pixmap and returns it, if address is available, and returns true. If pixel address is not available, returns nil.

    pixmap contents become invalid on any future change to Surface.

    Declaration

    Swift

    public func peekPixels() -> Pixmap?

    Return Value

    The pixmap with the contents, or nil.

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

    Declaration

    Swift

    public func readPixels(info: ImageInfo, dstPixels: UnsafeMutableRawPointer!, dstRowBytes: Int, srcX: Int32, srcY: Int32) -> ImageInfo?

    Parameters

    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

    srcX

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

    srcY

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

    Return Value

    the updated ImageInfo, or nil on error