Move GraphicBuffer to graphics package

Test: locally tested Bitmap.createHardwareBitmap method
bug: 30999911
Change-Id: Iad432577f26f2362ede9e77cd8a5425c010692e5
diff --git a/core/java/android/view/GraphicBuffer.aidl b/core/java/android/view/GraphicBuffer.aidl
deleted file mode 100644
index 6dc6bed..0000000
--- a/core/java/android/view/GraphicBuffer.aidl
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * Copyright (C) 2013 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 android.view;
-
-parcelable GraphicBuffer;
diff --git a/core/java/android/view/GraphicBuffer.java b/core/java/android/view/GraphicBuffer.java
deleted file mode 100644
index 64611d0..0000000
--- a/core/java/android/view/GraphicBuffer.java
+++ /dev/null
@@ -1,304 +0,0 @@
-/*
- * Copyright (C) 2013 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 android.view;
-
-import android.graphics.Bitmap;
-import android.graphics.Canvas;
-import android.graphics.PixelFormat;
-import android.graphics.Rect;
-import android.os.Parcel;
-import android.os.Parcelable;
-
-/**
- * Simple wrapper for the native GraphicBuffer class.
- *
- * @hide
- */
-@SuppressWarnings("UnusedDeclaration")
-public class GraphicBuffer implements Parcelable {
-    // Note: keep usage flags in sync with GraphicBuffer.h and gralloc.h
-    public static final int USAGE_SW_READ_NEVER = 0x0;
-    public static final int USAGE_SW_READ_RARELY = 0x2;
-    public static final int USAGE_SW_READ_OFTEN = 0x3;
-    public static final int USAGE_SW_READ_MASK = 0xF;
-
-    public static final int USAGE_SW_WRITE_NEVER = 0x0;
-    public static final int USAGE_SW_WRITE_RARELY = 0x20;
-    public static final int USAGE_SW_WRITE_OFTEN = 0x30;
-    public static final int USAGE_SW_WRITE_MASK = 0xF0;
-
-    public static final int USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK | USAGE_SW_WRITE_MASK;
-
-    public static final int USAGE_PROTECTED = 0x4000;
-
-    public static final int USAGE_HW_TEXTURE = 0x100;
-    public static final int USAGE_HW_RENDER = 0x200;
-    public static final int USAGE_HW_2D = 0x400;
-    public static final int USAGE_HW_COMPOSER = 0x800;
-    public static final int USAGE_HW_VIDEO_ENCODER = 0x10000;
-    public static final int USAGE_HW_MASK = 0x71F00;
-
-    private final int mWidth;
-    private final int mHeight;
-    private final int mFormat;
-    private final int mUsage;
-    // Note: do not rename, this field is used by native code
-    private final long mNativeObject;
-
-    // These two fields are only used by lock/unlockCanvas()
-    private Canvas mCanvas;
-    private int mSaveCount;
-
-    // If set to true, this GraphicBuffer instance cannot be used anymore
-    private boolean mDestroyed;
-
-    /**
-     * Creates new <code>GraphicBuffer</code> instance. This method will return null
-     * if the buffer cannot be created.
-     *
-     * @param width The width in pixels of the buffer
-     * @param height The height in pixels of the buffer
-     * @param format The format of each pixel as specified in {@link PixelFormat}
-     * @param usage Hint indicating how the buffer will be used
-     *
-     * @return A <code>GraphicBuffer</code> instance or null
-     */
-    public static GraphicBuffer create(int width, int height, int format, int usage) {
-        long nativeObject = nCreateGraphicBuffer(width, height, format, usage);
-        if (nativeObject != 0) {
-            return new GraphicBuffer(width, height, format, usage, nativeObject);
-        }
-        return null;
-    }
-
-    /**
-     * Private use only. See {@link #create(int, int, int, int)}.
-     */
-    private GraphicBuffer(int width, int height, int format, int usage, long nativeObject) {
-        mWidth = width;
-        mHeight = height;
-        mFormat = format;
-        mUsage = usage;
-        mNativeObject = nativeObject;
-    }
-
-    /**
-     * Returns the width of this buffer in pixels.
-     */
-    public int getWidth() {
-        return mWidth;
-    }
-
-    /**
-     * Returns the height of this buffer in pixels.
-     */
-    public int getHeight() {
-        return mHeight;
-    }
-
-    /**
-     * Returns the pixel format of this buffer. The pixel format must be one of
-     * the formats defined in {@link PixelFormat}.
-     */
-    public int getFormat() {
-        return mFormat;
-    }
-
-    /**
-     * Returns the usage hint set on this buffer.
-     */
-    public int getUsage() {
-        return mUsage;
-    }
-
-    /**
-     * <p>Start editing the pixels in the buffer. A null is returned if the buffer
-     * cannot be locked for editing.</p>
-     *
-     * <p>The content of the buffer is preserved between unlockCanvas()
-     * and lockCanvas().</p>
-     *
-     * <p>If this method is called after {@link #destroy()}, the return value will
-     * always be null.</p>
-     *
-     * @return A Canvas used to draw into the buffer, or null.
-     *
-     * @see #lockCanvas(android.graphics.Rect)
-     * @see #unlockCanvasAndPost(android.graphics.Canvas)
-     * @see #isDestroyed()
-     */
-    public Canvas lockCanvas() {
-        return lockCanvas(null);
-    }
-
-    /**
-     * Just like {@link #lockCanvas()} but allows specification of a dirty
-     * rectangle.
-     *
-     * <p>If this method is called after {@link #destroy()}, the return value will
-     * always be null.</p>
-     *
-     * @param dirty Area of the buffer that may be modified.
-
-     * @return A Canvas used to draw into the surface, or null.
-     *
-     * @see #lockCanvas()
-     * @see #unlockCanvasAndPost(android.graphics.Canvas)
-     * @see #isDestroyed()
-     */
-    public Canvas lockCanvas(Rect dirty) {
-        if (mDestroyed) {
-            return null;
-        }
-
-        if (mCanvas == null) {
-            mCanvas = new Canvas();
-        }
-
-        if (nLockCanvas(mNativeObject, mCanvas, dirty)) {
-            mSaveCount = mCanvas.save();
-            return mCanvas;
-        }
-
-        return null;
-    }
-
-    /**
-     * Finish editing pixels in the buffer.
-     *
-     * <p>This method doesn't do anything if {@link #destroy()} was
-     * previously called.</p>
-     *
-     * @param canvas The Canvas previously returned by lockCanvas()
-     *
-     * @see #lockCanvas()
-     * @see #lockCanvas(android.graphics.Rect)
-     * @see #isDestroyed()
-     */
-    public void unlockCanvasAndPost(Canvas canvas) {
-        if (!mDestroyed && mCanvas != null && canvas == mCanvas) {
-            canvas.restoreToCount(mSaveCount);
-            mSaveCount = 0;
-
-            nUnlockCanvasAndPost(mNativeObject, mCanvas);
-        }
-    }
-
-    /**
-     * Destroyes this buffer immediately. Calling this method frees up any
-     * underlying native resources. After calling this method, this buffer
-     * must not be used in any way ({@link #lockCanvas()} must not be called,
-     * etc.)
-     *
-     * @see #isDestroyed()
-     */
-    public void destroy() {
-        if (!mDestroyed) {
-            mDestroyed = true;
-            nDestroyGraphicBuffer(mNativeObject);
-        }
-    }
-
-    /**
-     * Indicates whether this buffer has been destroyed. A destroyed buffer
-     * cannot be used in any way: locking a Canvas will return null, the buffer
-     * cannot be written to a parcel, etc.
-     *
-     * @return True if this <code>GraphicBuffer</code> is in a destroyed state,
-     *         false otherwise.
-     *
-     * @see #destroy()
-     */
-    public boolean isDestroyed() {
-        return mDestroyed;
-    }
-
-    @Override
-    protected void finalize() throws Throwable {
-        try {
-            if (!mDestroyed) nDestroyGraphicBuffer(mNativeObject);
-        } finally {
-            super.finalize();
-        }
-    }
-
-    @Override
-    public int describeContents() {
-        return 0;
-    }
-
-    /**
-     * Flatten this object in to a Parcel.
-     *
-     * <p>Calling this method will throw an <code>IllegalStateException</code> if
-     * {@link #destroy()} has been previously called.</p>
-     *
-     * @param dest The Parcel in which the object should be written.
-     * @param flags Additional flags about how the object should be written.
-     *              May be 0 or {@link #PARCELABLE_WRITE_RETURN_VALUE}.
-     */
-    @Override
-    public void writeToParcel(Parcel dest, int flags) {
-        if (mDestroyed) {
-            throw new IllegalStateException("This GraphicBuffer has been destroyed and cannot be "
-                    + "written to a parcel.");
-        }
-
-        dest.writeInt(mWidth);
-        dest.writeInt(mHeight);
-        dest.writeInt(mFormat);
-        dest.writeInt(mUsage);
-        nWriteGraphicBufferToParcel(mNativeObject, dest);
-    }
-
-    /**
-     * Create hardware bitmap backed by this GraphicBuffer.
-     *
-     * @return Bitmap or null if this GraphicBuffer has unsupported PixelFormat.
-     *         currently PIXEL_FORMAT_RGBA_8888 is the only supported format
-     */
-    public Bitmap createHardwareBitmap() {
-        return nCreateHardwareBitmap(mNativeObject);
-    }
-
-    public static final Parcelable.Creator<GraphicBuffer> CREATOR =
-            new Parcelable.Creator<GraphicBuffer>() {
-        public GraphicBuffer createFromParcel(Parcel in) {
-            int width = in.readInt();
-            int height = in.readInt();
-            int format = in.readInt();
-            int usage = in.readInt();
-            long nativeObject = nReadGraphicBufferFromParcel(in);
-            if (nativeObject != 0) {
-                return new GraphicBuffer(width, height, format, usage, nativeObject);
-            }
-            return null;
-        }
-
-        public GraphicBuffer[] newArray(int size) {
-            return new GraphicBuffer[size];
-        }
-    };
-
-    private static native long nCreateGraphicBuffer(int width, int height, int format, int usage);
-    private static native void nDestroyGraphicBuffer(long nativeObject);
-    private static native void nWriteGraphicBufferToParcel(long nativeObject, Parcel dest);
-    private static native long nReadGraphicBufferFromParcel(Parcel in);
-    private static native boolean nLockCanvas(long nativeObject, Canvas canvas, Rect dirty);
-    private static native boolean nUnlockCanvasAndPost(long nativeObject, Canvas canvas);
-    private static native Bitmap nCreateHardwareBitmap(long nativeObject);
-}
diff --git a/core/jni/Android.mk b/core/jni/Android.mk
index a13ebaf..0d1b803 100644
--- a/core/jni/Android.mk
+++ b/core/jni/Android.mk
@@ -60,7 +60,6 @@
     android_graphics_drawable_VectorDrawable.cpp \
     android_view_DisplayEventReceiver.cpp \
     android_view_DisplayListCanvas.cpp \
-    android_view_GraphicBuffer.cpp \
     android_view_HardwareLayer.cpp \
     android_view_InputChannel.cpp \
     android_view_InputDevice.cpp \
@@ -120,6 +119,7 @@
     android/graphics/FontFamily.cpp \
     android/graphics/CreateJavaOutputStreamAdaptor.cpp \
     android/graphics/GIFMovie.cpp \
+    android/graphics/GraphicBuffer.cpp \
     android/graphics/Graphics.cpp \
     android/graphics/HarfBuzzNGFaceSkia.cpp \
     android/graphics/Interpolator.cpp \
diff --git a/core/jni/AndroidRuntime.cpp b/core/jni/AndroidRuntime.cpp
index 6c9a764..c195cfe 100644
--- a/core/jni/AndroidRuntime.cpp
+++ b/core/jni/AndroidRuntime.cpp
@@ -57,6 +57,7 @@
 extern int register_android_graphics_BitmapRegionDecoder(JNIEnv*);
 extern int register_android_graphics_Camera(JNIEnv* env);
 extern int register_android_graphics_CreateJavaOutputStreamAdaptor(JNIEnv* env);
+extern int register_android_graphics_GraphicBuffer(JNIEnv* env);
 extern int register_android_graphics_Graphics(JNIEnv* env);
 extern int register_android_graphics_Interpolator(JNIEnv* env);
 extern int register_android_graphics_MaskFilter(JNIEnv* env);
@@ -134,7 +135,6 @@
 extern int register_android_graphics_pdf_PdfRenderer(JNIEnv* env);
 extern int register_android_view_DisplayEventReceiver(JNIEnv* env);
 extern int register_android_view_DisplayListCanvas(JNIEnv* env);
-extern int register_android_view_GraphicBuffer(JNIEnv* env);
 extern int register_android_view_HardwareLayer(JNIEnv* env);
 extern int register_android_view_RenderNode(JNIEnv* env);
 extern int register_android_view_RenderNodeAnimator(JNIEnv* env);
@@ -1296,7 +1296,6 @@
     REG_JNI(register_android_view_DisplayEventReceiver),
     REG_JNI(register_android_view_RenderNode),
     REG_JNI(register_android_view_RenderNodeAnimator),
-    REG_JNI(register_android_view_GraphicBuffer),
     REG_JNI(register_android_view_DisplayListCanvas),
     REG_JNI(register_android_view_HardwareLayer),
     REG_JNI(register_android_view_ThreadedRenderer),
@@ -1328,6 +1327,7 @@
     REG_JNI(register_android_graphics_ColorFilter),
     REG_JNI(register_android_graphics_DrawFilter),
     REG_JNI(register_android_graphics_FontFamily),
+    REG_JNI(register_android_graphics_GraphicBuffer),
     REG_JNI(register_android_graphics_Interpolator),
     REG_JNI(register_android_graphics_MaskFilter),
     REG_JNI(register_android_graphics_Matrix),
diff --git a/core/jni/android/graphics/Bitmap.cpp b/core/jni/android/graphics/Bitmap.cpp
index 59cbc93..bea2b8c 100755
--- a/core/jni/android/graphics/Bitmap.cpp
+++ b/core/jni/android/graphics/Bitmap.cpp
@@ -1,6 +1,7 @@
 #define LOG_TAG "Bitmap"
 #include "Bitmap.h"
 
+#include "GraphicBuffer.h"
 #include "SkBitmap.h"
 #include "SkPixelRef.h"
 #include "SkImageEncoder.h"
@@ -1308,6 +1309,16 @@
     return createBitmap(env, allocator.getStorageObjAndReset(), kBitmapCreateFlag_None);
 }
 
+static jobject Bitmap_createHardwareBitmap(JNIEnv* env, jobject, jobject graphicBuffer) {
+    sp<GraphicBuffer> buffer(graphicBufferForJavaObject(env, graphicBuffer));
+    sk_sp<Bitmap> bitmap = Bitmap::createFrom(buffer);
+    if (!bitmap.get()) {
+        ALOGW("failed to create hardware bitmap from graphic buffer");
+        return NULL;
+    }
+    return bitmap::createBitmap(env, bitmap.release(), android::bitmap::kBitmapCreateFlag_None);
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 static jclass make_globalref(JNIEnv* env, const char classname[])
 {
@@ -1368,6 +1379,8 @@
     {   "nativeGetAllocationByteCount", "(J)I", (void*)Bitmap_getAllocationByteCount },
     {   "nativeCopyPreserveInternalConfig", "(J)Landroid/graphics/Bitmap;",
         (void*)Bitmap_nativeCopyPreserveInternalConfig },
+    {   "nativeCreateHardwareBitmap", "(Landroid/graphics/GraphicBuffer;)Landroid/graphics/Bitmap;",
+        (void*) Bitmap_createHardwareBitmap }
 };
 
 int register_android_graphics_Bitmap(JNIEnv* env)
diff --git a/core/jni/android_view_GraphicBuffer.cpp b/core/jni/android/graphics/GraphicBuffer.cpp
similarity index 82%
rename from core/jni/android_view_GraphicBuffer.cpp
rename to core/jni/android/graphics/GraphicBuffer.cpp
index f18837f..6cc7109 100644
--- a/core/jni/android_view_GraphicBuffer.cpp
+++ b/core/jni/android/graphics/GraphicBuffer.cpp
@@ -20,9 +20,8 @@
 #include "JNIHelp.h"
 
 #include "android_os_Parcel.h"
-#include "android_view_GraphicBuffer.h"
-#include "android/graphics/GraphicsJNI.h"
-#include "Bitmap.h"
+#include "GraphicBuffer.h"
+#include "GraphicsJNI.h"
 
 #include <android_runtime/AndroidRuntime.h>
 
@@ -100,7 +99,7 @@
 // GraphicBuffer lifecycle
 // ----------------------------------------------------------------------------
 
-static jlong android_view_GraphiceBuffer_create(JNIEnv* env, jobject clazz,
+static jlong android_graphics_GraphicBuffer_create(JNIEnv* env, jobject clazz,
         jint width, jint height, jint format, jint usage) {
 
     sp<ISurfaceComposer> composer(ComposerService::getComposerService());
@@ -125,7 +124,7 @@
     return reinterpret_cast<jlong>(wrapper);
 }
 
-static void android_view_GraphiceBuffer_destroy(JNIEnv* env, jobject clazz,
+static void android_graphics_GraphicBuffer_destroy(JNIEnv* env, jobject clazz,
         jlong wrapperHandle) {
     GraphicBufferWrapper* wrapper =
                 reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle);
@@ -151,7 +150,7 @@
     }
 }
 
-static jboolean android_view_GraphicBuffer_lockCanvas(JNIEnv* env, jobject,
+static jboolean android_graphics_GraphicBuffer_lockCanvas(JNIEnv* env, jobject,
         jlong wrapperHandle, jobject canvas, jobject dirtyRect) {
 
     GraphicBufferWrapper* wrapper =
@@ -209,7 +208,7 @@
     return JNI_TRUE;
 }
 
-static jboolean android_view_GraphicBuffer_unlockCanvasAndPost(JNIEnv* env, jobject,
+static jboolean android_graphics_GraphicBuffer_unlockCanvasAndPost(JNIEnv* env, jobject,
         jlong wrapperHandle, jobject canvas) {
 
     GraphicBufferWrapper* wrapper =
@@ -229,7 +228,7 @@
 // Serialization
 // ----------------------------------------------------------------------------
 
-static void android_view_GraphiceBuffer_write(JNIEnv* env, jobject clazz,
+static void android_graphics_GraphicBuffer_write(JNIEnv* env, jobject clazz,
         jlong wrapperHandle, jobject dest) {
     GraphicBufferWrapper* wrapper =
                 reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle);
@@ -239,7 +238,7 @@
     }
 }
 
-static jlong android_view_GraphiceBuffer_read(JNIEnv* env, jobject clazz,
+static jlong android_graphics_GraphicBuffer_read(JNIEnv* env, jobject clazz,
         jobject in) {
 
     Parcel* parcel = parcelForJavaObject(env, in);
@@ -252,17 +251,6 @@
     return NULL;
 }
 
-static jobject android_view_GraphicBuffer_createHardwareBitmap(JNIEnv* env, jobject,
-        jlong wrapperHandle) {
-    GraphicBufferWrapper* wrapper = reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle);
-    sk_sp<Bitmap> bitmap = Bitmap::createFrom(wrapper->buffer);
-    if (!bitmap.get()) {
-        ALOGW("failed to create hardware bitmap from graphic buffer");
-        return NULL;
-    }
-    return bitmap::createBitmap(env, bitmap.release(), android::bitmap::kBitmapCreateFlag_None);
-}
-
 // ----------------------------------------------------------------------------
 // External helpers
 // ----------------------------------------------------------------------------
@@ -278,33 +266,32 @@
     }
     return NULL;
 }
+};
 
+using namespace android;
 // ----------------------------------------------------------------------------
 // JNI Glue
 // ----------------------------------------------------------------------------
 
-const char* const kClassPathName = "android/view/GraphicBuffer";
+const char* const kClassPathName = "android/graphics/GraphicBuffer";
 
 static const JNINativeMethod gMethods[] = {
-    { "nCreateGraphicBuffer",  "(IIII)J", (void*) android_view_GraphiceBuffer_create },
-    { "nDestroyGraphicBuffer", "(J)V",    (void*) android_view_GraphiceBuffer_destroy },
+    { "nCreateGraphicBuffer",  "(IIII)J", (void*) android_graphics_GraphicBuffer_create },
+    { "nDestroyGraphicBuffer", "(J)V",    (void*) android_graphics_GraphicBuffer_destroy },
 
     { "nWriteGraphicBufferToParcel",  "(JLandroid/os/Parcel;)V",
-            (void*) android_view_GraphiceBuffer_write },
+            (void*) android_graphics_GraphicBuffer_write },
     { "nReadGraphicBufferFromParcel", "(Landroid/os/Parcel;)J",
-            (void*) android_view_GraphiceBuffer_read },
+            (void*) android_graphics_GraphicBuffer_read },
 
     { "nLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)Z",
-            (void*) android_view_GraphicBuffer_lockCanvas },
+            (void*) android_graphics_GraphicBuffer_lockCanvas },
     { "nUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)Z",
-            (void*) android_view_GraphicBuffer_unlockCanvasAndPost },
-    { "nCreateHardwareBitmap", "(J)Landroid/graphics/Bitmap;",
-        (void*) android_view_GraphicBuffer_createHardwareBitmap
-    }
+            (void*) android_graphics_GraphicBuffer_unlockCanvasAndPost }
 };
 
-int register_android_view_GraphicBuffer(JNIEnv* env) {
-    jclass clazz = FindClassOrDie(env, "android/view/GraphicBuffer");
+int register_android_graphics_GraphicBuffer(JNIEnv* env) {
+    jclass clazz = FindClassOrDie(env, "android/graphics/GraphicBuffer");
     gGraphicBufferClassInfo.mNativeObject = GetFieldIDOrDie(env, clazz, "mNativeObject", "J");
 
     clazz = FindClassOrDie(env, "android/graphics/Rect");
@@ -315,6 +302,4 @@
     gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
 
     return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
-}
-
-};
+}
\ No newline at end of file
diff --git a/core/jni/android_view_GraphicBuffer.h b/core/jni/android/graphics/GraphicBuffer.h
similarity index 100%
rename from core/jni/android_view_GraphicBuffer.h
rename to core/jni/android/graphics/GraphicBuffer.h
diff --git a/core/jni/android_view_ThreadedRenderer.cpp b/core/jni/android_view_ThreadedRenderer.cpp
index 14dcb3f..c00bcd4 100644
--- a/core/jni/android_view_ThreadedRenderer.cpp
+++ b/core/jni/android_view_ThreadedRenderer.cpp
@@ -36,7 +36,6 @@
 #include <android_runtime/android_view_Surface.h>
 #include <system/window.h>
 
-#include "android_view_GraphicBuffer.h"
 #include "android_os_MessageQueue.h"
 
 #include <Animator.h>