Canvas: fix some issues spotted by the CTS

This commit is contained in:
Julian Winkler 2025-02-11 19:40:09 +01:00
parent fb2cba8572
commit 829df70a9f
4 changed files with 20 additions and 6 deletions

View file

@ -43,6 +43,7 @@ public final class Bitmap {
private boolean hasAlpha = true;
long bytes = 0; // used by native function AndroidBitmap_lockPixels()
private boolean recycled = false;
boolean mutable = true;
Bitmap(long texture) {
this(native_get_width(texture), native_get_height(texture), Config.ARGB_8888);
@ -194,7 +195,7 @@ public final class Bitmap {
}
public boolean isMutable() {
return true;
return mutable;
}
public boolean compress(Bitmap.CompressFormat format, int quality, OutputStream stream) throws IOException {

View file

@ -443,7 +443,11 @@ public class BitmapFactory {
* size be returned (in opts.outWidth and opts.outHeight)
*/
public static Bitmap decodeResource(Resources res, int id, Options opts) throws NotFoundException {
return decodeStreamInternal(res.openRawResource(id), null, opts);
Bitmap bitmap = decodeStreamInternal(res.openRawResource(id), null, opts);
if (opts != null && opts.inScaled == false) {
bitmap.mutable = false;
}
return bitmap;
}
/**

View file

@ -380,6 +380,9 @@ public class Canvas {
}
public void setBitmap(Bitmap bitmap) {
if (!bitmap.isMutable()) {
throw new IllegalStateException("Bitmap must be mutable");
}
this.bitmap = bitmap;
gsk_canvas.snapshot = bitmap == null ? 0 : bitmap.getSnapshot();
}
@ -393,7 +396,10 @@ public class Canvas {
return false;
}
public void restoreToCount(int count) {}
public void restoreToCount(int count) {
gsk_canvas.snapshot = bitmap.getSnapshot();
gsk_canvas.restoreToCount(count);
}
public void drawRoundRect(RectF rect, float rx, float ry, Paint paint) {
if (paint.getShader() instanceof BitmapShader) {
@ -425,11 +431,11 @@ public class Canvas {
}
public int getWidth() {
return 10; //FIXME
return (bitmap == null) ? 0 : bitmap.getWidth();
}
public int getHeight() {
return 10; //FIXME
return (bitmap == null) ? 0 : bitmap.getHeight();
}
public void drawColor(int dummy) {}

View file

@ -6,7 +6,7 @@ package android.graphics;
*/
public class GskCanvas extends Canvas {
public long snapshot;
private int save_count = 0;
private int save_count = 1;
private static Paint default_paint = new Paint();
@ -28,6 +28,9 @@ public class GskCanvas extends Canvas {
@Override
public void restoreToCount(int count) {
if (count < 1) {
throw new IllegalArgumentException("count must be >= 1");
}
while (save_count > count) {
restore();
}