From 0bbae0836426ba2704e38e7f90a9d0ca502ab71d Mon Sep 17 00:00:00 2001 From: Romain Guy Date: Tue, 15 Jun 2010 18:03:40 -0700 Subject: 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 --- core/jni/android/graphics/Bitmap.cpp | 5 ++ graphics/java/android/graphics/Bitmap.java | 14 ++++ tests/HwAccelerationTest/Android.mk | 26 +++++++ tests/HwAccelerationTest/AndroidManifest.xml | 30 ++++++++ .../com/google/android/test/hwui/HwUiActivity.java | 81 ++++++++++++++++++++++ 5 files changed, 156 insertions(+) create mode 100644 tests/HwAccelerationTest/Android.mk create mode 100644 tests/HwAccelerationTest/AndroidManifest.xml create mode 100644 tests/HwAccelerationTest/src/com/google/android/test/hwui/HwUiActivity.java diff --git a/core/jni/android/graphics/Bitmap.cpp b/core/jni/android/graphics/Bitmap.cpp index 88bbafd772c9..b06226416621 100644 --- a/core/jni/android/graphics/Bitmap.cpp +++ b/core/jni/android/graphics/Bitmap.cpp @@ -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 }, diff --git a/graphics/java/android/graphics/Bitmap.java b/graphics/java/android/graphics/Bitmap.java index 7ca374164096..e261cfaa7a9a 100644 --- a/graphics/java/android/graphics/Bitmap.java +++ b/graphics/java/android/graphics/Bitmap.java @@ -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 diff --git a/tests/HwAccelerationTest/Android.mk b/tests/HwAccelerationTest/Android.mk new file mode 100644 index 000000000000..d4743f938d4b --- /dev/null +++ b/tests/HwAccelerationTest/Android.mk @@ -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) diff --git a/tests/HwAccelerationTest/AndroidManifest.xml b/tests/HwAccelerationTest/AndroidManifest.xml new file mode 100644 index 000000000000..22feeca4b1a4 --- /dev/null +++ b/tests/HwAccelerationTest/AndroidManifest.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + diff --git a/tests/HwAccelerationTest/src/com/google/android/test/hwui/HwUiActivity.java b/tests/HwAccelerationTest/src/com/google/android/test/hwui/HwUiActivity.java new file mode 100644 index 000000000000..1ee2dc1925c8 --- /dev/null +++ b/tests/HwAccelerationTest/src/com/google/android/test/hwui/HwUiActivity.java @@ -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); + } + } +} -- cgit v1.2.3-59-g8ed1b