Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1b214be9 authored by Wei-Ta Chen's avatar Wei-Ta Chen Committed by Android (Google) Code Review
Browse files

Merge "Revert "Rename LargeBitmap to BitmapRegionDecoder for having a better...

Merge "Revert "Rename LargeBitmap to BitmapRegionDecoder for having a better API."" into gingerbread
parents d6e56895 340ce75b
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -102,12 +102,11 @@ LOCAL_SRC_FILES:= \
	android_graphics_PixelFormat.cpp \
	android/graphics/Picture.cpp \
	android/graphics/PorterDuff.cpp \
	android/graphics/BitmapRegionDecoder.cpp \
	android/graphics/LargeBitmap.cpp \
	android/graphics/Rasterizer.cpp \
	android/graphics/Region.cpp \
	android/graphics/Shader.cpp \
	android/graphics/Typeface.cpp \
	android/graphics/Utils.cpp \
	android/graphics/Xfermode.cpp \
	android/graphics/YuvToJpegEncoder.cpp \
	android_media_AudioRecord.cpp \
+2 −2
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ extern int register_android_os_Binder(JNIEnv* env);
extern int register_android_os_Process(JNIEnv* env);
extern int register_android_graphics_Bitmap(JNIEnv*);
extern int register_android_graphics_BitmapFactory(JNIEnv*);
extern int register_android_graphics_BitmapRegionDecoder(JNIEnv*);
extern int register_android_graphics_LargeBitmap(JNIEnv*);
extern int register_android_graphics_Camera(JNIEnv* env);
extern int register_android_graphics_Graphics(JNIEnv* env);
extern int register_android_graphics_Interpolator(JNIEnv* env);
@@ -1244,7 +1244,7 @@ static const RegJNIRec gRegJNI[] = {

    REG_JNI(register_android_graphics_Bitmap),
    REG_JNI(register_android_graphics_BitmapFactory),
    REG_JNI(register_android_graphics_BitmapRegionDecoder),
    REG_JNI(register_android_graphics_LargeBitmap),
    REG_JNI(register_android_graphics_Camera),
    REG_JNI(register_android_graphics_Canvas),
    REG_JNI(register_android_graphics_ColorFilter),
+195 −1
Original line number Diff line number Diff line
@@ -10,7 +10,6 @@
#include "SkUtils.h"
#include "CreateJavaOutputStreamAdaptor.h"
#include "AutoDecodeCancel.h"
#include "Utils.h"

#include <android_runtime/AndroidRuntime.h>
#include <utils/Asset.h>
@@ -99,6 +98,57 @@ public:
    }
};

class AssetStreamAdaptor : public SkStream {
public:
    AssetStreamAdaptor(Asset* a) : fAsset(a) {}
    
    virtual bool rewind() {
        off_t pos = fAsset->seek(0, SEEK_SET);
        if (pos == (off_t)-1) {
            SkDebugf("----- fAsset->seek(rewind) failed\n");
            return false;
        }
        return true;
    }
    
    virtual size_t read(void* buffer, size_t size) {
        ssize_t amount;
        
        if (NULL == buffer) {
            if (0 == size) {  // caller is asking us for our total length
                return fAsset->getLength();
            }
            // asset->seek returns new total offset
            // we want to return amount that was skipped

            off_t oldOffset = fAsset->seek(0, SEEK_CUR);
            if (-1 == oldOffset) {
                SkDebugf("---- fAsset->seek(oldOffset) failed\n");
                return 0;
            }
            off_t newOffset = fAsset->seek(size, SEEK_CUR);
            if (-1 == newOffset) {
                SkDebugf("---- fAsset->seek(%d) failed\n", size);
                return 0;
            }
            amount = newOffset - oldOffset;
        } else {
            amount = fAsset->read(buffer, size);
            if (amount <= 0) {
                SkDebugf("---- fAsset->read(%d) returned %d\n", size, amount);
            }
        }
        
        if (amount < 0) {
            amount = 0;
        }
        return amount;
    }
    
private:
    Asset*  fAsset;
};

///////////////////////////////////////////////////////////////////////////////

static inline int32_t validOrNeg1(bool isValid, int32_t value) {
@@ -150,6 +200,12 @@ static bool optionsReportSizeToVM(JNIEnv* env, jobject options) {
            !env->GetBooleanField(options, gOptions_nativeAllocFieldID);
}

static jobject nullObjectReturn(const char msg[]) {
    if (msg) {
        SkDebugf("--- %s\n", msg);
    }
    return NULL;
}

static SkPixelRef* installPixelRef(SkBitmap* bitmap, SkStream* stream,
                                   int sampleSize, bool ditherImage) {
@@ -320,6 +376,23 @@ static ssize_t getFDSize(int fd) {
    return size;
}

/** Restore the file descriptor's offset in our destructor
 */
class AutoFDSeek {
public:
    AutoFDSeek(int fd) : fFD(fd) {
        fCurr = ::lseek(fd, 0, SEEK_CUR);
    }
    ~AutoFDSeek() {
        if (fCurr >= 0) {
            ::lseek(fFD, fCurr, SEEK_SET);
        }
    }
private:
    int     fFD;
    off_t   fCurr;
};

static jobject nativeDecodeFileDescriptor(JNIEnv* env, jobject clazz,
                                          jobject fileDescriptor,
                                          jobject padding,
@@ -486,6 +559,107 @@ static void nativeSetDefaultConfig(JNIEnv* env, jobject, int nativeConfig) {
    }
}

static jobject doBuildTileIndex(JNIEnv* env, SkStream* stream, bool isShareable) {
    SkImageDecoder* decoder = SkImageDecoder::Factory(stream);
    int width, height;
    if (NULL == decoder) {
        doThrowIOE(env, "Image format not supported");
        return nullObjectReturn("SkImageDecoder::Factory returned null");
    }

    JavaPixelAllocator *javaAllocator = new JavaPixelAllocator(env, true);
    decoder->setAllocator(javaAllocator);
    JavaMemoryUsageReporter *javaMemoryReporter = new JavaMemoryUsageReporter(env);
    decoder->setReporter(javaMemoryReporter);
    javaAllocator->unref();
    javaMemoryReporter->unref();

    if (!decoder->buildTileIndex(stream, &width, &height, isShareable)) {
        char msg[1024];
        snprintf(msg, 1023, "Image failed to decode using %s decoder", decoder->getFormatName());
        doThrowIOE(env, msg);
        return nullObjectReturn("decoder->buildTileIndex returned false");
    }

    SkLargeBitmap *bm = new SkLargeBitmap(decoder, width, height);

    return GraphicsJNI::createLargeBitmap(env, bm);
}

static jobject nativeCreateLargeBitmapFromByteArray(JNIEnv* env, jobject, jbyteArray byteArray,
                                     int offset, int length, jboolean isShareable) {
    AutoJavaByteArray ar(env, byteArray);
    SkStream* stream = new SkMemoryStream(ar.ptr() + offset, length, false);
    SkAutoUnref aur(stream);
    if (isShareable) {
        aur.detach();
    }
    return doBuildTileIndex(env, stream, isShareable);
}

static jobject nativeCreateLargeBitmapFromFileDescriptor(JNIEnv* env, jobject clazz,
                                          jobject fileDescriptor, jboolean isShareable) {
    NPE_CHECK_RETURN_ZERO(env, fileDescriptor);

    jint descriptor = env->GetIntField(fileDescriptor,
                                       gFileDescriptor_descriptor);
    bool weOwnTheFD = false;

    if (isShareable) {
        int newFD = ::dup(descriptor);
        if (-1 != newFD) {
            weOwnTheFD = true;
            descriptor = newFD;
        }
    }

    SkFDStream* stream = new SkFDStream(descriptor, weOwnTheFD);
    SkAutoUnref aur(stream);
    if (!stream->isValid()) {
        return NULL;
    }

    if (isShareable) {
        aur.detach();
    }

    /* Restore our offset when we leave, so we can be called more than once
       with the same descriptor. This is only required if we didn't dup the
       file descriptor, but it is OK to do it all the time.
    */
    AutoFDSeek as(descriptor);

    return doBuildTileIndex(env, stream, isShareable);
}

static jobject nativeCreateLargeBitmapFromStream(JNIEnv* env, jobject clazz,
                                  jobject is,       // InputStream
                                  jbyteArray storage, // byte[]
                                  jboolean isShareable) {
    jobject largeBitmap = NULL;
    SkStream* stream = CreateJavaInputStreamAdaptor(env, is, storage, 1024);

    if (stream) {
        // for now we don't allow shareable with java inputstreams
        largeBitmap = doBuildTileIndex(env, stream, false);
        stream->unref();
    }
    return largeBitmap;
}

static jobject nativeCreateLargeBitmapFromAsset(JNIEnv* env, jobject clazz,
                                 jint native_asset, // Asset
                                 jboolean isShareable) {
    SkStream* stream;
    Asset* asset = reinterpret_cast<Asset*>(native_asset);
    stream = new AssetStreamAdaptor(asset);
    SkAutoUnref aur(stream);
    if (isShareable) {
        aur.detach();
    }
    return doBuildTileIndex(env, stream, isShareable);
}

///////////////////////////////////////////////////////////////////////////////

static JNINativeMethod gMethods[] = {
@@ -515,6 +689,26 @@ static JNINativeMethod gMethods[] = {
    },

    {   "nativeSetDefaultConfig", "(I)V", (void*)nativeSetDefaultConfig },

    {   "nativeCreateLargeBitmap",
        "([BIIZ)Landroid/graphics/LargeBitmap;",
        (void*)nativeCreateLargeBitmapFromByteArray
    },

    {   "nativeCreateLargeBitmap",
        "(Ljava/io/InputStream;[BZ)Landroid/graphics/LargeBitmap;",
        (void*)nativeCreateLargeBitmapFromStream
    },

    {   "nativeCreateLargeBitmap",
        "(Ljava/io/FileDescriptor;Z)Landroid/graphics/LargeBitmap;",
        (void*)nativeCreateLargeBitmapFromFileDescriptor
    },

    {   "nativeCreateLargeBitmap",
        "(IZ)Landroid/graphics/LargeBitmap;",
        (void*)nativeCreateLargeBitmapFromAsset
    },
};

static JNINativeMethod gOptionsMethods[] = {
+8 −8
Original line number Diff line number Diff line
@@ -169,8 +169,8 @@ static jmethodID gBitmap_allocBufferMethodID;
static jclass   gBitmapConfig_class;
static jfieldID gBitmapConfig_nativeInstanceID;

static jclass   gBitmapRegionDecoder_class;
static jmethodID gBitmapRegionDecoder_constructorMethodID;
static jclass   gLargeBitmap_class;
static jmethodID gLargeBitmap_constructorMethodID;

static jclass   gCanvas_class;
static jfieldID gCanvas_nativeInstanceID;
@@ -377,18 +377,17 @@ jobject GraphicsJNI::createBitmap(JNIEnv* env, SkBitmap* bitmap, bool isMutable,
    }
    return obj;
}

jobject GraphicsJNI::createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap)
jobject GraphicsJNI::createLargeBitmap(JNIEnv* env, SkLargeBitmap* bitmap)
{
    SkASSERT(bitmap != NULL);

    jobject obj = env->AllocObject(gBitmapRegionDecoder_class);
    jobject obj = env->AllocObject(gLargeBitmap_class);
    if (hasException(env)) {
        obj = NULL;
        return obj;
    }
    if (obj) {
        env->CallVoidMethod(obj, gBitmapRegionDecoder_constructorMethodID, (jint)bitmap);
        env->CallVoidMethod(obj, gLargeBitmap_constructorMethodID, (jint)bitmap);
        if (hasException(env)) {
            obj = NULL;
        }
@@ -614,8 +613,8 @@ int register_android_graphics_Graphics(JNIEnv* env)
    gBitmap_constructorMethodID = env->GetMethodID(gBitmap_class, "<init>",
                                            "(IZ[BI)V");

    gBitmapRegionDecoder_class = make_globalref(env, "android/graphics/BitmapRegionDecoder");
    gBitmapRegionDecoder_constructorMethodID = env->GetMethodID(gBitmapRegionDecoder_class, "<init>", "(I)V");
    gLargeBitmap_class = make_globalref(env, "android/graphics/LargeBitmap");
    gLargeBitmap_constructorMethodID = env->GetMethodID(gLargeBitmap_class, "<init>", "(I)V");

    gBitmapConfig_class = make_globalref(env, "android/graphics/Bitmap$Config");
    gBitmapConfig_nativeInstanceID = getFieldIDCheck(env, gBitmapConfig_class,
@@ -655,3 +654,4 @@ int register_android_graphics_Graphics(JNIEnv* env)

    return 0;
}
+3 −2
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@
#include "SkPoint.h"
#include "SkRect.h"
#include "SkBitmap.h"
#include "../images/SkBitmapRegionDecoder.h"
#include "../images/SkLargeBitmap.h"
#include "../images/SkImageDecoder.h"
#include <jni.h>

@@ -56,7 +56,7 @@ public:
    
    static jobject createRegion(JNIEnv* env, SkRegion* region);

    static jobject createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap);
    static jobject createLargeBitmap(JNIEnv* env, SkLargeBitmap* bitmap);

    /** Set a pixelref for the bitmap (needs setConfig to already be called)
        Returns true on success. If it returns false, then it failed, and the
@@ -181,3 +181,4 @@ void doThrowIOE(JNIEnv* env, const char* msg = NULL); // IO Exception
    do { if (NULL == (object)) { doThrowNPE(env); return; } } while (0)

#endif
Loading