Add new API to check whether a Bitmap was modified.

Bitmap.getGenerationId() can be used by caches to find out if a Bitmap has been
modified. This simply exposes an existing Skia API.

This change also adds a small test app for Canvas hardware acceleration. The new
Bitmap API is required to implement a texture cache.

Change-Id: I8547b146cd14c8afe1a2327fcd6d71b1b1cb68fc
This commit is contained in:
Romain Guy
2010-06-15 18:03:40 -07:00
parent 7e28c0108b
commit 0bbae08364
5 changed files with 156 additions and 0 deletions

View File

@@ -313,6 +313,10 @@ static int Bitmap_config(JNIEnv* env, jobject, SkBitmap* bitmap) {
return bitmap->config();
}
static int Bitmap_getGenerationId(JNIEnv* env, jobject, SkBitmap* bitmap) {
return bitmap->getGenerationID();
}
static jboolean Bitmap_hasAlpha(JNIEnv* env, jobject, SkBitmap* bitmap) {
return !bitmap->isOpaque();
}
@@ -606,6 +610,7 @@ static JNINativeMethod gBitmapMethods[] = {
(void*)Bitmap_writeToParcel },
{ "nativeExtractAlpha", "(II[I)Landroid/graphics/Bitmap;",
(void*)Bitmap_extractAlpha },
{ "nativeGenerationId", "(I)I", (void*)Bitmap_getGenerationId },
{ "nativeGetPixel", "(III)I", (void*)Bitmap_getPixel },
{ "nativeGetPixels", "(I[IIIIIII)V", (void*)Bitmap_getPixels },
{ "nativeSetPixel", "(IIII)V", (void*)Bitmap_setPixel },

View File

@@ -171,6 +171,19 @@ public final class Bitmap implements Parcelable {
return mRecycled;
}
/**
* Returns the generation ID of this bitmap. The generation ID changes
* whenever the bitmap is modified. This can be used as an efficient way to
* check if a bitmap has changed.
*
* @return The current generation ID for this bitmap.
*
* @hide
*/
public int getGenerationId() {
return nativeGenerationId(mNativeBitmap);
}
/**
* This is called by methods that want to throw an exception if the bitmap
* has already been recycled.
@@ -1041,6 +1054,7 @@ public final class Bitmap implements Parcelable {
private static native void nativeCopyPixelsToBuffer(int nativeBitmap,
Buffer dst);
private static native void nativeCopyPixelsFromBuffer(int nb, Buffer src);
private static native int nativeGenerationId(int nativeBitmap);
private static native Bitmap nativeCreateFromParcel(Parcel p);
// returns true on success

View File

@@ -0,0 +1,26 @@
#
# Copyright (C) 2010 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := HwAccelerationTest
LOCAL_MODULE_TAGS := tests
include $(BUILD_PACKAGE)

View File

@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.test.hwui">
<application android:label="HwUi">
<activity android:name="HwUiActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

View File

@@ -0,0 +1,81 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.test.hwui;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import static android.util.Log.d;
public class HwUiActivity extends Activity {
private static final String LOG_TAG = "HwUi";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new DirtyBitmapView(this));
}
static int dipToPx(Context c, int dip) {
return (int) (c.getResources().getDisplayMetrics().density * dip + 0.5f);
}
static class DirtyBitmapView extends View {
private Bitmap mCache;
DirtyBitmapView(Context c) {
super(c);
final int width = dipToPx(c, 100);
final int height = dipToPx(c, 100);
mCache = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
logGenerationId("Dirty cache created", mCache);
Canvas canvas = new Canvas(mCache);
logGenerationId("Canvas cache created", mCache);
canvas.drawColor(0xffff0000);
logGenerationId("Cache filled", mCache);
Paint p = new Paint();
p.setColor(0xff0000ff);
canvas.drawRect(width / 2.0f, height / 2.0f, width, height, p);
logGenerationId("Cache modified", mCache);
}
private static void logGenerationId(String message, Bitmap b) {
d(LOG_TAG, message);
d(LOG_TAG, " bitmap id=" + b.getGenerationId());
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mCache, 0, 0, null);
logGenerationId("Cache drawn", mCache);
}
}
}