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
diff --git a/core/jni/android/graphics/Bitmap.cpp b/core/jni/android/graphics/Bitmap.cpp
index 88bbafd..b062264 100644
--- a/core/jni/android/graphics/Bitmap.cpp
+++ b/core/jni/android/graphics/Bitmap.cpp
@@ -313,6 +313,10 @@
     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 @@
         (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 7ca3741..e261cfa 100644
--- a/graphics/java/android/graphics/Bitmap.java
+++ b/graphics/java/android/graphics/Bitmap.java
@@ -172,6 +172,19 @@
     }
 
     /**
+     * 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 @@
     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 0000000..d4743f9
--- /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 0000000..22feeca
--- /dev/null
+++ b/tests/HwAccelerationTest/AndroidManifest.xml
@@ -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>
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 0000000..1ee2dc1
--- /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);
+        }
+    }
+}