forked from androidx/androidx
-
Notifications
You must be signed in to change notification settings - Fork 144
Introduces several caching of objects across graphics Skiko code #3117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ApoloApps
wants to merge
9
commits into
JetBrains:jb-main
Choose a base branch
from
ApoloApps:followupMatrixSkiko
base: jb-main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
de99b4e
Cache Matrix class in SkiaBackedPath to mimic Android behaviour
ApoloApps 250ac4d
Cache RoundedRect FloatArray object to avoid allocating it each time.…
ApoloApps 78e6ee1
Add toFloatArray method that avoids iterator allocation (compiles to …
ApoloApps 329ba77
Adds CanvasHolder class to avoid creating a new SkiaBackedCanvas obje…
ApoloApps ebc198a
Merge branch 'jb-main' into followupMatrixSkiko
ApoloApps eddb40b
Fix after rebase
ApoloApps fd6b122
Avoid Rect allocations on extremely hot paths. (each frame, single or…
ApoloApps 211369e
Reduce number of copies in SyntheticEventSender and use Primitive Col…
ApoloApps 9db20d7
Change comment mentioning Android Canvas
ApoloApps File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ import org.jetbrains.skia.Matrix44 | |
| import org.jetbrains.skia.MipmapMode | ||
| import org.jetbrains.skia.Paint as SkPaint | ||
| import org.jetbrains.skia.SamplingMode | ||
| import org.jetbrains.skia.Surface | ||
| import org.jetbrains.skia.impl.use | ||
|
|
||
| @Deprecated( | ||
|
|
@@ -46,13 +47,15 @@ internal actual fun ActualCanvas(image: ImageBitmap): Canvas { | |
| require(!skiaBitmap.isImmutable) { | ||
| "Cannot draw on immutable ImageBitmap" | ||
| } | ||
| return SkiaBackedCanvas(SkCanvas(skiaBitmap)) | ||
| return SkiaBackedCanvas().apply { | ||
| internalSkiaCanvas = SkCanvas(skiaBitmap) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Convert the [org.jetbrains.skia.Canvas] instance into a Compose-compatible Canvas | ||
| */ | ||
| fun SkCanvas.asComposeCanvas(): Canvas = SkiaBackedCanvas(this) | ||
| fun SkCanvas.asComposeCanvas(): Canvas = SkiaBackedCanvas().apply { internalSkiaCanvas = this@asComposeCanvas } | ||
|
|
||
| /** | ||
| * Provides access to the underlying [org.jetbrains.skia.Canvas] instance. | ||
|
|
@@ -75,6 +78,26 @@ val Canvas.skiaCanvas: SkCanvas | |
| val Canvas.nativeCanvas: NativeCanvas | ||
| get() = skiaCanvas | ||
|
|
||
|
|
||
| // Stub canvas instance used to keep the internal canvas parameter non-null during its | ||
| // scoped usage and prevent unnecessary byte code null checks from being generated | ||
| private val EmptyCanvas = Surface.makeNull(1,1).canvas | ||
|
|
||
| /** | ||
| * Holder class that is used to issue scoped calls to a [Canvas] from the framework equivalent | ||
| * canvas without having to allocate an object on each draw call | ||
| */ | ||
| class CanvasHolder { | ||
| @PublishedApi internal val skiaBackedCanvas = SkiaBackedCanvas() | ||
|
|
||
| inline fun drawInto(targetCanvas: SkCanvas, block: Canvas.() -> Unit) { | ||
| val previousCanvas = skiaBackedCanvas.internalSkiaCanvas | ||
| skiaBackedCanvas.internalSkiaCanvas = targetCanvas | ||
| skiaBackedCanvas.block() | ||
| skiaBackedCanvas.internalSkiaCanvas = previousCanvas | ||
| } | ||
| } | ||
|
|
||
| // This was added for internal usage from old render layers (another submodule), | ||
| // but wasn't properly marked as internal. Keep it as deprecated for some time to be safe. | ||
| @InternalComposeApi | ||
|
|
@@ -86,9 +109,12 @@ var Canvas.alphaMultiplier: Float | |
| get() = (this as SkiaBackedCanvas).alphaMultiplier | ||
| set(value) { (this as SkiaBackedCanvas).alphaMultiplier = value } | ||
|
|
||
| internal class SkiaBackedCanvas( | ||
| internal val internalSkiaCanvas: SkCanvas, | ||
| ) : Canvas { | ||
| @PublishedApi | ||
| internal class SkiaBackedCanvas : Canvas { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It wasn't supposed to be exposed. Again, requirement for binary compatibility in a place where we're not ready to do it |
||
|
|
||
| // Keep the internal canvas as a var prevent having to allocate an AndroidCanvas | ||
| // instance on each draw call | ||
| @PublishedApi internal var internalSkiaCanvas: SkCanvas = EmptyCanvas | ||
| internal var alphaMultiplier: Float = 1.0f | ||
|
|
||
| private fun Paint.asSkiaPaintWithAppliedAlphaMultiplier(): SkPaint { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: a new public-stable API that we'll have to maintain forever