Skip to content
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

Use a large rectangle for the picture bounds in RenderNodeLayer.drawLayer to prevent clipping #1090

Merged
merged 2 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.draw.scale
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Brush
Expand All @@ -40,6 +44,7 @@ import androidx.compose.ui.renderComposeScene
import androidx.compose.ui.test.InternalTestApi
import androidx.compose.ui.test.junit4.DesktopScreenshotTestRule
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import org.junit.Assume.assumeTrue
import org.junit.Rule
Expand Down Expand Up @@ -293,6 +298,42 @@ class GraphicsLayerTest {
screenshotRule.write(snapshot)
}

@Test
fun `box outside bounds should not be clipped`() {
val snapshot = renderComposeScene(width = 40, height = 40) {
Box(modifier = Modifier
.size(40.dp)
.offset {
-IntOffset(41, 41)
}
.drawBehind {
drawRect(
Color.Red,
Offset.Zero,
size * 10f,
)
}
.background(Color.Green)
)
}
screenshotRule.write(snapshot)
}

@Test
fun largeScale() {
val snapshot = renderComposeScene(width = 40, height = 40) {
Box(modifier = Modifier
.size(40.dp)
.scale(1E10.toFloat())
// We need at least two drawing operations because Skia doesn't check the picture
// bounds with just one drawing operation.
.background(Color.Green)
.background(Color.Red.copy(alpha = 0.5f))
)
}
screenshotRule.write(snapshot)
}

@Test
fun alpha() {
val snapshot = renderComposeScene(width = 40, height = 40) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,23 @@ internal class RenderNodeLayer(
override fun drawLayer(canvas: Canvas) {
if (picture == null) {
val bounds = size.toSize().toRect()
val pictureCanvas = pictureRecorder.beginRecording(bounds.toSkiaRect())
val pictureCanvas = pictureRecorder.beginRecording(
// The goal with selecting the size of the rectangle here is to avoid limiting the
// drawable area as much as possible.
// Due to https://partnerissuetracker.corp.google.com/issues/324465764 we have to
// leave room for scale between the values we specify here and Float.MAX_VALUE.
// The maximum possible scale that can be applied to the canvas will be
// Float.MAX_VALUE divided by the largest value below.
// 2^30 was chosen because it's big enough, leaves quite a lot of room between it
// and Float.MAX_VALUE, and also lets the width and height fit into int32 (just in
// case).
org.jetbrains.skia.Rect.makeLTRB(
l = -(1 shl 30).toFloat(),
t = -(1 shl 30).toFloat(),
r = ((1 shl 30)-1).toFloat(),
b = ((1 shl 30)-1).toFloat()
)
)
performDrawLayer(pictureCanvas.asComposeCanvas(), bounds)
picture = pictureRecorder.finishRecordingAsPicture()
}
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.