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; private boolean hasAlpha = true;
long bytes = 0; // used by native function AndroidBitmap_lockPixels() long bytes = 0; // used by native function AndroidBitmap_lockPixels()
private boolean recycled = false; private boolean recycled = false;
boolean mutable = true;
Bitmap(long texture) { Bitmap(long texture) {
this(native_get_width(texture), native_get_height(texture), Config.ARGB_8888); this(native_get_width(texture), native_get_height(texture), Config.ARGB_8888);
@@ -194,7 +195,7 @@ public final class Bitmap {
} }
public boolean isMutable() { public boolean isMutable() {
return true; return mutable;
} }
public boolean compress(Bitmap.CompressFormat format, int quality, OutputStream stream) throws IOException { 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) * size be returned (in opts.outWidth and opts.outHeight)
*/ */
public static Bitmap decodeResource(Resources res, int id, Options opts) throws NotFoundException { 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) { public void setBitmap(Bitmap bitmap) {
if (!bitmap.isMutable()) {
throw new IllegalStateException("Bitmap must be mutable");
}
this.bitmap = bitmap; this.bitmap = bitmap;
gsk_canvas.snapshot = bitmap == null ? 0 : bitmap.getSnapshot(); gsk_canvas.snapshot = bitmap == null ? 0 : bitmap.getSnapshot();
} }
@@ -393,7 +396,10 @@ public class Canvas {
return false; 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) { public void drawRoundRect(RectF rect, float rx, float ry, Paint paint) {
if (paint.getShader() instanceof BitmapShader) { if (paint.getShader() instanceof BitmapShader) {
@@ -425,11 +431,11 @@ public class Canvas {
} }
public int getWidth() { public int getWidth() {
return 10; //FIXME return (bitmap == null) ? 0 : bitmap.getWidth();
} }
public int getHeight() { public int getHeight() {
return 10; //FIXME return (bitmap == null) ? 0 : bitmap.getHeight();
} }
public void drawColor(int dummy) {} public void drawColor(int dummy) {}

View File

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