Renderer.CanvasRenderer2


public abstract class Renderer.CanvasRenderer2<SharedAssetsT extends Renderer.SharedAssets> extends Renderer.CanvasRenderer

Known direct subclasses
ListenableCanvasRenderer2

This class is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.


Watch faces that require Canvas rendering and are able to take advantage of SharedAssets to save memory (there can be more than once instance when editing), should extend their Renderer from this class.

A CanvasRenderer is expected to be constructed on the background thread associated with WatchFaceService.getBackgroundThreadHandler inside a call to WatchFaceService.createWatchFace. All rendering is be done on the UiThread. There is a memory barrier between construction and rendering so no special threading primitives are required.

In Java it may be easier to extend androidx.wear.watchface.ListenableCanvasRenderer2 instead.

use Watch Face Format instead

Parameters
<SharedAssetsT extends Renderer.SharedAssets>

The type extending SharedAssets returned by createSharedAssets and passed into render and renderHighlightLayer.

Summary

Public constructors

@WorkerThread
<SharedAssetsT extends Renderer.SharedAssets> CanvasRenderer2(
    @NonNull SurfaceHolder surfaceHolder,
    @NonNull CurrentUserStyleRepository currentUserStyleRepository,
    @NonNull WatchState watchState,
    int canvasType,
    @IntRange(from = 0, to = 60000) long interactiveDrawModeUpdateDelayMillis,
    boolean clearWithBackgroundTintBeforeRenderingHighlightLayer
)

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

Public methods

final void
render(
    @NonNull Canvas canvas,
    @NonNull Rect bounds,
    @NonNull ZonedDateTime zonedDateTime
)

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

abstract void
@UiThread
render(
    @NonNull Canvas canvas,
    @NonNull Rect bounds,
    @NonNull ZonedDateTime zonedDateTime,
    @NonNull SharedAssetsT sharedAssets
)

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

final void
renderHighlightLayer(
    @NonNull Canvas canvas,
    @NonNull Rect bounds,
    @NonNull ZonedDateTime zonedDateTime
)

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

abstract void
@UiThread
renderHighlightLayer(
    @NonNull Canvas canvas,
    @NonNull Rect bounds,
    @NonNull ZonedDateTime zonedDateTime,
    @NonNull SharedAssetsT sharedAssets
)

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

Protected methods

abstract @NonNull SharedAssetsT

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

Inherited methods

From androidx.wear.watchface.Renderer
final @NonNull Collection<@NonNull Pair<@NonNull Integer, @NonNull ContentDescriptionLabel>>

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

final float

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

final float

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

final long

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

@NonNull Rect

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

final @NonNull RenderParameters

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

final @NonNull Rect

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

final @NonNull SurfaceHolder

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

final WatchFaceColors

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

final void

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

void

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

void

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

final void

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

final void

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

final void

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

final void
setInteractiveDrawModeUpdateDelayMillis(
    long interactiveDrawModeUpdateDelayMillis
)

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

final void

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

final void

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

boolean

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

From androidx.wear.watchface.Renderer.CanvasRenderer
final boolean

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

void

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

void

This method is deprecated. AndroidX watchface libraries are deprecated, use Watch Face Format instead.

Public constructors

CanvasRenderer2

@WorkerThread
public <SharedAssetsT extends Renderer.SharedAssets> CanvasRenderer2(
    @NonNull SurfaceHolder surfaceHolder,
    @NonNull CurrentUserStyleRepository currentUserStyleRepository,
    @NonNull WatchState watchState,
    int canvasType,
    @IntRange(from = 0, to = 60000) long interactiveDrawModeUpdateDelayMillis,
    boolean clearWithBackgroundTintBeforeRenderingHighlightLayer
)
Parameters
<SharedAssetsT extends Renderer.SharedAssets>

The type extending SharedAssets returned by createSharedAssets and passed into render and renderHighlightLayer.

@NonNull SurfaceHolder surfaceHolder

The SurfaceHolder from which a Canvas to will be obtained and passed into render.

@NonNull CurrentUserStyleRepository currentUserStyleRepository

The watch face's associated CurrentUserStyleRepository.

@NonNull WatchState watchState

The watch face's associated WatchState.

int canvasType

The CanvasTypeIntDef to request. Note even if CanvasType.HARDWARE is used, screenshots will taken using the software rendering pipeline, as such Bitmaps with Bitmap.Config.HARDWARE must be avoided.

@IntRange(from = 0, to = 60000) long interactiveDrawModeUpdateDelayMillis

The interval in milliseconds between frames in interactive DrawModes. To render at 60hz set to 16. Note when battery is low, the frame rate will be clamped to 10fps. Watch faces are recommended to use lower frame rates if possible for better battery life. Variable frame rates can also help preserve battery life, e.g. if a watch face has a short animation once per second it can adjust the framerate inorder to sleep when not animating.

boolean clearWithBackgroundTintBeforeRenderingHighlightLayer

Whether the Canvas is cleared with RenderParameters.HighlightLayer.backgroundTint before renderHighlightLayer is called. Defaults to false.

Public methods

render

public final void render(
    @NonNull Canvas canvas,
    @NonNull Rect bounds,
    @NonNull ZonedDateTime zonedDateTime
)

Sub-classes should override this to implement their watch face rendering logic which should respect the current renderParameters. Please note WatchState.isAmbient may not match the RenderParameters.drawMode and should not be used to decide what to render. E.g. when editing from the companion phone while the watch is ambient, renders may be requested with DrawMode.INTERACTIVE.

Any highlights due to RenderParameters.highlightLayer should be rendered by renderHighlightLayer instead where possible. For correct behavior this function must use the supplied ZonedDateTime in favor of any other ways of getting the time.

Before any calls to render, init will be called once.

Parameters
@NonNull Canvas canvas

The Canvas to render into. Don't assume this is always the canvas from the SurfaceHolder backing the display

@NonNull Rect bounds

A Rect describing the bonds of the canvas to draw into

@NonNull ZonedDateTime zonedDateTime

The ZonedDateTime to render with

render

Added in 1.1.0
Deprecated in 1.3.0-alpha06
@UiThread
public abstract void render(
    @NonNull Canvas canvas,
    @NonNull Rect bounds,
    @NonNull ZonedDateTime zonedDateTime,
    @NonNull SharedAssetsT sharedAssets
)

Sub-classes should override this to implement their watch face rendering logic which should respect the current renderParameters. Please note WatchState.isAmbient may not match the RenderParameters.drawMode and should not be used to decide what to render. E.g. when editing from the companion phone while the watch is ambient, renders may be requested with DrawMode.INTERACTIVE.

Any highlights due to RenderParameters.highlightLayer should be rendered by renderHighlightLayer instead where possible. For correct behavior this function must use the supplied ZonedDateTime in favor of any other ways of getting the time.

Before any calls to render, init will be called once.

Parameters
@NonNull Canvas canvas

The Canvas to render into. Don't assume this is always the canvas from the SurfaceHolder backing the display

@NonNull Rect bounds

A Rect describing the bonds of the canvas to draw into

@NonNull ZonedDateTime zonedDateTime

The ZonedDateTime to render with

@NonNull SharedAssetsT sharedAssets

The SharedAssetsT returned by createSharedAssets

renderHighlightLayer

public final void renderHighlightLayer(
    @NonNull Canvas canvas,
    @NonNull Rect bounds,
    @NonNull ZonedDateTime zonedDateTime
)

Sub-classes should override this to implement their watch face highlight layer rendering logic for the RenderParameters.highlightLayer aspect of renderParameters. Typically the implementation will clear canvas to RenderParameters.HighlightLayer.backgroundTint before rendering a transparent highlight or a solid outline around the RenderParameters.HighlightLayer.highlightedElement. This will be composited as needed on top of the results of render. For correct behavior this function must use the supplied ZonedDateTime in favor of any other ways of getting the time.

Note if clearWithBackgroundTintBeforeRenderingHighlightLayer is true then canvas will cleared with RenderParameters.HighlightLayer.backgroundTint before renderHighlightLayer is called. Otherwise it is up to the overridden function to clear the Canvas if necessary.

Parameters
@NonNull Canvas canvas

The Canvas to render into. Don't assume this is always the canvas from the SurfaceHolder backing the display

@NonNull Rect bounds

A Rect describing the bonds of the canvas to draw into

@NonNull ZonedDateTime zonedDateTime

the ZonedDateTime to render with

renderHighlightLayer

Added in 1.1.0
Deprecated in 1.3.0-alpha06
@UiThread
public abstract void renderHighlightLayer(
    @NonNull Canvas canvas,
    @NonNull Rect bounds,
    @NonNull ZonedDateTime zonedDateTime,
    @NonNull SharedAssetsT sharedAssets
)

Sub-classes should override this to implement their watch face highlight layer rendering logic for the RenderParameters.highlightLayer aspect of renderParameters. Typically the implementation will clear canvas to RenderParameters.HighlightLayer.backgroundTint before rendering a transparent highlight or a solid outline around the RenderParameters.HighlightLayer.highlightedElement. This will be composited as needed on top of the results of render. For correct behavior this function must use the supplied ZonedDateTime in favor of any other ways of getting the time.

Note if clearWithBackgroundTintBeforeRenderingHighlightLayer is true then canvas will cleared with RenderParameters.HighlightLayer.backgroundTint before renderHighlightLayer is called. Otherwise it is up to the overridden function to clear the Canvas if necessary.

Parameters
@NonNull Canvas canvas

The Canvas to render into. Don't assume this is always the canvas from the SurfaceHolder backing the display

@NonNull Rect bounds

A Rect describing the bonds of the canvas to draw into

@NonNull ZonedDateTime zonedDateTime

the ZonedDateTime to render with

@NonNull SharedAssetsT sharedAssets

The SharedAssetsT returned by createSharedAssets

Protected methods

createSharedAssets

@WorkerThread
protected abstract @NonNull SharedAssetsT createSharedAssets()

When editing multiple WatchFaceService instances and hence Renderers can exist concurrently (e.g. a headless instance and an interactive instance) and using SharedAssets allows memory to be saved by sharing immutable data (e.g. Bitmaps, shaders, etc...) between them.

To take advantage of SharedAssets, override this method. The constructed SharedAssets are passed into the render as an argument (NB you'll have to cast this to your type).

When all instances using SharedAssets have been closed, SharedAssets.onDestroy will be called.

Note that while SharedAssets are constructed on a background thread, they'll typically be used on the main thread and subsequently destroyed there.

Returns
@NonNull SharedAssetsT

The SharedAssetsT that will be passed into render and renderHighlightLayer.