Class Surface
- All Implemented Interfaces:
RefCounted
- Direct Known Subclasses:
GraniteSurface
Canvas that will draw into the surface.
Call getCanvas() to use that canvas (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 null will be returned.-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final intprotected static final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidOverride this method to invoke de-allocation of the underlying resource.Returns Canvas that draws into Surface.Returns the GPU context being used by the Surface.final ObjectReturns unique value identifying the content of Surface.final intReturns pixel row count; may be zero or greater.abstract @NonNull ImageInfoReturns an ImageInfo describing the Surface.final intgetWidth()Returns pixel count in each row; may be zero or greater.final booleanstatic @Nullable SurfacemakeFromBackendTexture(RecordingContext context, BackendImage backendImage, int origin, int sampleCount, int colorType, Runnable releaseCallback) Wraps a GPU-backed texture into Surface.final @Nullable @SharedPtr ImageReturns Image capturing Surface contents.final @Nullable @SharedPtr ImagemakeImageSnapshot(@NonNull Rect2ic subset) Like the no-parameter version, this returns an image of the current surface contents.static @Nullable SurfacemakeRenderTarget(RecordingContext context, ImageInfo imageInfo, int origin, int sampleCount, boolean mipmapped, boolean budgeted) Returns Surface on GPU indicated by context.final voidNotifies that Surface contents will be changed externally.protected abstract booleanonCopyOnWrite(int changeMode) If the surface is about to change, we call this so that our subclass can optionally fork their backend (copy-on-write) in case it was being shared with the cachedImage.protected voidCalled as a performance hint when the Surface is allowed to make its contents undefined.Allocate a canvas that will draw into this surface.protected abstract @Nullable @SharedPtr ImageonNewImageSnapshot(@Nullable Rect2ic subset) Allocate an Image that represents the current contents of the surface.protected voidSignal the surface to remind its backing store that it's mutable again.
-
Field Details
-
kPreserve_ContentChangeMode
protected static final int kPreserve_ContentChangeMode- See Also:
-
kDiscard_ContentChangeMode
protected static final int kDiscard_ContentChangeMode- See Also:
-
-
Constructor Details
-
Surface
protected Surface(int width, int height)
-
-
Method Details
-
makeFromBackendTexture
public static @Nullable Surface makeFromBackendTexture(RecordingContext context, BackendImage backendImage, int origin, int sampleCount, int colorType, Runnable releaseCallback) Wraps a GPU-backed texture into Surface. Caller must ensure the texture is valid for the lifetime of returned Surface. IfsampleCountgreater than one, creates an intermediate MSAA Surface which is used for drawingbackendTexture.Surface is returned if all parameters are valid.
backendTextureis valid if its pixel configuration agrees withcontext; for instance, ifbackendTexturehas an sRGB configuration, thencontextmust support sRGB. Further,backendTexturewidth and height must not exceedcontextcapabilities, and thecontextmust be able to support back-end textures.Upon success
releaseCallbackis 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 failsreleaseCallbackis called before this method returns.- Parameters:
context- GPU contextbackendImage- texture residing on GPUsampleCount- samples per pixel, or 1 to disable full scene anti-aliasingreleaseCallback- function called when texture can be released, may be null- Returns:
- Surface if all parameters are valid; otherwise, null
-
makeRenderTarget
public static @Nullable Surface makeRenderTarget(RecordingContext context, ImageInfo imageInfo, int origin, int sampleCount, boolean mipmapped, boolean budgeted) Returns Surface on GPU indicated by context. Allocates memory for pixels, based on the width, height, and ColorType in ColorInfo.budgetedselects whether allocation for pixels is tracked by context.imageInfodescribes the pixel format in ColorType, and transparency in AlphaType.sampleCountrequests the number of samples per pixel. Pass one 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.originpins either the top-left or the bottom-left corner to the origin.mipmappedhints that Image returned by makeImageSnapshot() has mipmaps.- Parameters:
context- GPU contextimageInfo- width, height, ColorType, AlphaType; width, or height, or both, may be zerosampleCount- samples per pixel, or 1 to disable full scene anti-aliasingmipmapped- hint that Surface will host mipmap images- Returns:
- Surface if all parameters are valid; otherwise, null
-
deallocate
protected void deallocate()Description copied from class:RefCntOverride this method to invoke de-allocation of the underlying resource.- Specified by:
deallocatein classRefCnt
-
getWidth
public final int getWidth()Returns pixel count in each row; may be zero or greater.- Returns:
- number of pixel columns
-
getHeight
public final int getHeight()Returns pixel row count; may be zero or greater.- Returns:
- number of pixel rows
-
getImageInfo
Returns an ImageInfo describing the Surface. -
getGenerationID
Returns unique value identifying the content of Surface. Returned value changes each time the content changes. Content is changed by drawing, or by callingnotifyWillChange().- Returns:
- unique content identifier
-
notifyWillChange
public final void notifyWillChange()Notifies that Surface contents will be changed externally. Subsequent calls togetGenerationID()return a different value. -
getCommandContext
Returns the GPU context being used by the Surface.- Returns:
- the GPU context, if available; null otherwise
-
getCanvas
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
-
makeImageSnapshot
Returns Image capturing Surface contents. Subsequent drawing to Surface contents are not captured. Image allocation is accounted for if Surface was created with Budgeted flag.- Returns:
- Image initialized with Surface contents
-
makeImageSnapshot
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. -
getCachedCanvas
-
getCachedImage
-
hasCachedImage
@Internal public final boolean hasCachedImage() -
onGetCommandContext
-
onNewCanvas
Allocate a canvas that will draw into this surface. We will cache this canvas, to return the same object to the caller multiple times. We take ownership, and will call unref() on the canvas when we go out of scope. -
onNewImageSnapshot
@Internal @SharedPtr protected abstract @Nullable @SharedPtr Image onNewImageSnapshot(@Nullable Rect2ic subset) Allocate an Image that represents the current contents of the surface. This needs to be able to outlive the surface itself (if need be), and must faithfully represent the current contents, even if the surface is changed after this called (e.g. it is drawn to via its canvas).If a subset is specified, the impl must make a copy, rather than try to wait on copy-on-write.
-
onDiscard
@Internal protected void onDiscard()Called as a performance hint when the Surface is allowed to make its contents undefined. -
onCopyOnWrite
@Internal protected abstract boolean onCopyOnWrite(int changeMode) If the surface is about to change, we call this so that our subclass can optionally fork their backend (copy-on-write) in case it was being shared with the cachedImage.Returns false if the backing cannot be un-shared.
-
onRestoreBackingMutability
@Internal protected void onRestoreBackingMutability()Signal the surface to remind its backing store that it's mutable again. Called only when we _didn't_ copy-on-write; we assume the copies start mutable.
-