Merge changes I68d56d76,Iad432577

* changes:
  Hidden method to get a graphic buffer from a hardware bitmap
  Move GraphicBuffer to graphics package
This commit is contained in:
Sergei Vasilinetc
2017-01-06 20:36:38 +00:00
committed by Android (Google) Code Review
9 changed files with 92 additions and 60 deletions

View File

@@ -60,7 +60,6 @@ LOCAL_SRC_FILES:= \
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 @@ LOCAL_SRC_FILES:= \
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 \

View File

@@ -57,6 +57,7 @@ extern int register_android_graphics_BitmapFactory(JNIEnv*);
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_PdfEditor(JNIEnv* env);
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 @@ static const RegJNIRec gRegJNI[] = {
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 @@ static const RegJNIRec gRegJNI[] = {
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),

View File

@@ -1,6 +1,7 @@
#define LOG_TAG "Bitmap"
#include "Bitmap.h"
#include "GraphicBuffer.h"
#include "SkBitmap.h"
#include "SkPixelRef.h"
#include "SkImageEncoder.h"
@@ -1291,7 +1292,7 @@ static jint Bitmap_getAllocationByteCount(JNIEnv* env, jobject, jlong bitmapPtr)
return static_cast<jint>(bitmapHandle->getAllocationByteCount());
}
static jobject Bitmap_nativeCopyPreserveInternalConfig(JNIEnv* env, jobject, jlong bitmapPtr) {
static jobject Bitmap_copyPreserveInternalConfig(JNIEnv* env, jobject, jlong bitmapPtr) {
LocalScopedBitmap bitmapHandle(bitmapPtr);
LOG_ALWAYS_FATAL_IF(!bitmapHandle->isHardware(),
"Hardware config is only supported config in Bitmap_nativeCopyPreserveInternalConfig");
@@ -1308,6 +1309,26 @@ static jobject Bitmap_nativeCopyPreserveInternalConfig(JNIEnv* env, jobject, jlo
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 jobject Bitmap_createGraphicBufferHandle(JNIEnv* env, jobject, jlong bitmapPtr) {
LocalScopedBitmap bitmapHandle(bitmapPtr);
LOG_ALWAYS_FATAL_IF(!bitmapHandle->isHardware(),
"Hardware config is only supported config in Bitmap_getGraphicBuffer");
Bitmap& hwuiBitmap = bitmapHandle->bitmap();
sp<GraphicBuffer> buffer(hwuiBitmap.graphicBuffer());
return createJavaGraphicBuffer(env, buffer);
}
///////////////////////////////////////////////////////////////////////////////
static jclass make_globalref(JNIEnv* env, const char classname[])
{
@@ -1367,7 +1388,11 @@ static const JNINativeMethod gBitmapMethods[] = {
{ "nativePrepareToDraw", "(J)V", (void*)Bitmap_prepareToDraw },
{ "nativeGetAllocationByteCount", "(J)I", (void*)Bitmap_getAllocationByteCount },
{ "nativeCopyPreserveInternalConfig", "(J)Landroid/graphics/Bitmap;",
(void*)Bitmap_nativeCopyPreserveInternalConfig },
(void*)Bitmap_copyPreserveInternalConfig },
{ "nativeCreateHardwareBitmap", "(Landroid/graphics/GraphicBuffer;)Landroid/graphics/Bitmap;",
(void*) Bitmap_createHardwareBitmap },
{ "nativeCreateGraphicBufferHandle", "(J)Landroid/graphics/GraphicBuffer;",
(void*) Bitmap_createGraphicBufferHandle }
};
int register_android_graphics_Bitmap(JNIEnv* env)

View File

@@ -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>
@@ -59,6 +58,8 @@ static const bool kDebugGraphicBuffer = false;
static struct {
jfieldID mNativeObject;
jclass mClass;
jmethodID mConstructorMethodID;
} gGraphicBufferClassInfo;
static struct {
@@ -100,7 +101,7 @@ public:
// 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 +126,7 @@ static jlong android_view_GraphiceBuffer_create(JNIEnv* env, jobject clazz,
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 +152,7 @@ static inline SkColorType convertPixelFormat(int32_t format) {
}
}
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 +210,7 @@ static jboolean android_view_GraphicBuffer_lockCanvas(JNIEnv* env, jobject,
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 +230,7 @@ static jboolean android_view_GraphicBuffer_unlockCanvasAndPost(JNIEnv* env, jobj
// 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 +240,7 @@ static void android_view_GraphiceBuffer_write(JNIEnv* env, jobject clazz,
}
}
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 +253,6 @@ static jlong android_view_GraphiceBuffer_read(JNIEnv* env, jobject clazz,
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
// ----------------------------------------------------------------------------
@@ -279,35 +269,46 @@ sp<GraphicBuffer> graphicBufferForJavaObject(JNIEnv* env, jobject obj) {
return NULL;
}
jobject createJavaGraphicBuffer(JNIEnv* env, const sp<GraphicBuffer>& buffer) {
GraphicBufferWrapper* wrapper = new GraphicBufferWrapper(buffer);
jobject obj = env->NewObject(gGraphicBufferClassInfo.mClass,
gGraphicBufferClassInfo.mConstructorMethodID, buffer->getWidth(), buffer->getHeight(),
buffer->getPixelFormat(), buffer->getUsage(), reinterpret_cast<jlong>(wrapper));
return obj;
}
};
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");
gGraphicBufferClassInfo.mNativeObject = GetFieldIDOrDie(env, clazz, "mNativeObject", "J");
int register_android_graphics_GraphicBuffer(JNIEnv* env) {
gGraphicBufferClassInfo.mClass = MakeGlobalRefOrDie(env, FindClassOrDie(env, kClassPathName));
gGraphicBufferClassInfo.mNativeObject = GetFieldIDOrDie(env, gGraphicBufferClassInfo.mClass,
"mNativeObject", "J");
gGraphicBufferClassInfo.mConstructorMethodID = env->GetMethodID(gGraphicBufferClassInfo.mClass,
"<init>", "(IIIIJ)V");
clazz = FindClassOrDie(env, "android/graphics/Rect");
jclass clazz = FindClassOrDie(env, "android/graphics/Rect");
gRectClassInfo.set = GetMethodIDOrDie(env, clazz, "set", "(IIII)V");
gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I");
gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I");
@@ -315,6 +316,4 @@ int register_android_view_GraphicBuffer(JNIEnv* env) {
gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
}
};
}

View File

@@ -24,4 +24,6 @@ namespace android {
// object must be an instance of android.view.GraphicBuffer
extern sp<GraphicBuffer> graphicBufferForJavaObject(JNIEnv* env, jobject obj);
jobject createJavaGraphicBuffer(JNIEnv* env, const sp<GraphicBuffer>& buffer);
}

View File

@@ -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>

View File

@@ -654,6 +654,17 @@ public final class Bitmap implements Parcelable {
return b;
}
/**
* Create hardware bitmap backed GraphicBuffer.
*
* @return Bitmap or null if this GraphicBuffer has unsupported PixelFormat.
* currently PIXEL_FORMAT_RGBA_8888 is the only supported format
* @hide
*/
public static Bitmap createHardwareBitmap(GraphicBuffer graphicBuffer) {
return nativeCreateHardwareBitmap(graphicBuffer);
}
/**
* Creates a new bitmap, scaled from an existing bitmap, when possible. If the
* specified width and height are the same as the current width and height of
@@ -1735,6 +1746,15 @@ public final class Bitmap implements Parcelable {
nativePrepareToDraw(mNativePtr);
}
/**
*
* @return {@link GraphicBuffer} which is internally used by hardware bitmap
* @hide
*/
public GraphicBuffer createGraphicBufferHandle() {
return nativeCreateGraphicBufferHandle(mNativePtr);
}
//////////// native methods
private static native Bitmap nativeCreate(int[] colors, int offset,
@@ -1794,4 +1814,6 @@ public final class Bitmap implements Parcelable {
private static native void nativePrepareToDraw(long nativeBitmap);
private static native int nativeGetAllocationByteCount(long nativeBitmap);
private static native Bitmap nativeCopyPreserveInternalConfig(long nativeBitmap);
private static native Bitmap nativeCreateHardwareBitmap(GraphicBuffer buffer);
private static native GraphicBuffer nativeCreateGraphicBufferHandle(long nativeBitmap);
}

View File

@@ -14,6 +14,6 @@
* limitations under the License.
*/
package android.view;
package android.graphics;
parcelable GraphicBuffer;

View File

@@ -14,12 +14,8 @@
* limitations under the License.
*/
package android.view;
package android.graphics;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.os.Parcel;
import android.os.Parcelable;
@@ -265,16 +261,6 @@ public class GraphicBuffer implements Parcelable {
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) {
@@ -300,5 +286,4 @@ public class GraphicBuffer implements Parcelable {
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);
}