Bitmap.cpp revision 0781a2f116be045ff1a3aca721c47f9fef980bea
1f29ed28c7b878ef28058bc730715d0d32445bc57John Reck#define LOG_TAG "Bitmap"
2f29ed28c7b878ef28058bc730715d0d32445bc57John Reck#include "Bitmap.h"
3f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
46ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod#include "Paint.h"
532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik#include "SkBitmap.h"
632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik#include "SkPixelRef.h"
732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik#include "SkImageEncoder.h"
857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III#include "SkImageInfo.h"
932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik#include "SkColorPriv.h"
1032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik#include "GraphicsJNI.h"
1132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik#include "SkDither.h"
1232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik#include "SkUnPreMultiply.h"
1332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik#include "SkStream.h"
1432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
1532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik#include <binder/Parcel.h>
1632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik#include "android_os_Parcel.h"
1732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik#include "android_util_Binder.h"
1832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik#include "android_nio_utils.h"
1932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik#include "CreateJavaOutputStreamAdaptor.h"
20f29ed28c7b878ef28058bc730715d0d32445bc57John Reck#include <Caches.h>
2132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
22ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe#include "core_jni_helpers.h"
23ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe
2432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik#include <jni.h>
2539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews#include <memory>
2639d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews#include <string>
2739d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews#include <sys/mman.h>
2839d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews#include <cutils/ashmem.h>
2932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
30f29ed28c7b878ef28058bc730715d0d32445bc57John Recknamespace android {
31f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
32f29ed28c7b878ef28058bc730715d0d32445bc57John Reckclass WrappedPixelRef : public SkPixelRef {
33f29ed28c7b878ef28058bc730715d0d32445bc57John Reckpublic:
34f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    WrappedPixelRef(Bitmap* wrapper, void* storage,
35f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
36f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            : SkPixelRef(info)
37f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            , mBitmap(*wrapper)
38f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            , mStorage(storage) {
39f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        reconfigure(info, rowBytes, ctable);
40f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
41f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
42f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    ~WrappedPixelRef() {
43f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        // Tell SkRefCnt that everything is as it expects by forcing
44f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        // the refcnt to 1
45f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        internal_dispose_restore_refcnt_to_1();
46f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        SkSafeUnref(mColorTable);
47f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
48f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
490781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck    void reconfigure(const SkImageInfo& newInfo, size_t rowBytes, SkColorTable* ctable) {
500781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck        if (kIndex_8_SkColorType != newInfo.colorType()) {
51f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            ctable = nullptr;
52f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        }
53f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        mRowBytes = rowBytes;
54f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        if (mColorTable != ctable) {
55f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            SkSafeUnref(mColorTable);
56f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            mColorTable = ctable;
57f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            SkSafeRef(mColorTable);
58f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        }
590781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck
600781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck        // Need to validate the alpha type to filter against the color type
610781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck        // to prevent things like a non-opaque RGB565 bitmap
620781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck        SkAlphaType alphaType;
630781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck        LOG_ALWAYS_FATAL_IF(!SkColorTypeValidateAlphaType(
640781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck                newInfo.colorType(), newInfo.alphaType(), &alphaType),
650781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck                "Failed to validate alpha type!");
660781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck
67f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        // Dirty hack is dirty
68f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        // TODO: Figure something out here, Skia's current design makes this
69f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        // really hard to work with. Skia really, really wants immutable objects,
70f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        // but with the nested-ref-count hackery going on that's just not
71f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        // feasible without going insane trying to figure it out
72f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        SkImageInfo* myInfo = const_cast<SkImageInfo*>(&this->info());
730781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck        *myInfo = newInfo;
740781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck        changeAlphaType(alphaType);
75f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
76f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        // Docs say to only call this in the ctor, but we're going to call
77f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        // it anyway even if this isn't always the ctor.
78f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        // TODO: Fix this too as part of the above TODO
79f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        setPreLocked(mStorage, mRowBytes, mColorTable);
80f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
81f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
82f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    // Can't mark as override since SkPixelRef::rowBytes isn't virtual
83f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    // but that's OK since we just want BitmapWrapper to be able to rely
84f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    // on calling rowBytes() on an unlocked pixelref, which it will be
85f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    // doing on a WrappedPixelRef type, not a SkPixelRef, so static
86f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    // dispatching will do what we want.
87f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    size_t rowBytes() const { return mRowBytes; }
88f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkColorTable* colorTable() const { return mColorTable; }
89f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
90f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    bool hasHardwareMipMap() const {
91f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        return mHasHardwareMipMap;
92f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
93f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
94f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    void setHasHardwareMipMap(bool hasMipMap) {
95f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        mHasHardwareMipMap = hasMipMap;
96f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
97f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
98f29ed28c7b878ef28058bc730715d0d32445bc57John Reckprotected:
99f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    virtual bool onNewLockPixels(LockRec* rec) override {
100f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        rec->fPixels = mStorage;
101f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        rec->fRowBytes = mRowBytes;
102f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        rec->fColorTable = mColorTable;
103f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        return true;
104f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
105f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
106f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    virtual void onUnlockPixels() override {
107f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        // nothing
108f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
109f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
110f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    virtual size_t getAllocatedSizeInBytes() const override {
111f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        return info().getSafeSize(mRowBytes);
112f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
113f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
114f29ed28c7b878ef28058bc730715d0d32445bc57John Reckprivate:
115f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    Bitmap& mBitmap;
116f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    void* mStorage;
117f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    size_t mRowBytes = 0;
118f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkColorTable* mColorTable = nullptr;
119f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    bool mHasHardwareMipMap = false;
120f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
121f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    virtual void internal_dispose() const override {
122f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        mBitmap.onStrongRefDestroyed();
123f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
124f29ed28c7b878ef28058bc730715d0d32445bc57John Reck};
125f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
126f29ed28c7b878ef28058bc730715d0d32445bc57John ReckBitmap::Bitmap(JNIEnv* env, jbyteArray storageObj, void* address,
127f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
128f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        : mPixelStorageType(PixelStorageType::Java) {
129f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    env->GetJavaVM(&mPixelStorage.java.jvm);
130f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    mPixelStorage.java.jweakRef = env->NewWeakGlobalRef(storageObj);
131f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    mPixelStorage.java.jstrongRef = nullptr;
132f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable));
133f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    // Note: this will trigger a call to onStrongRefDestroyed(), but
134f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    // we want the pixel ref to have a ref count of 0 at this point
135f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    mPixelRef->unref();
136f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
137f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
138f29ed28c7b878ef28058bc730715d0d32445bc57John ReckBitmap::Bitmap(void* address, void* context, FreeFunc freeFunc,
139f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
140f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        : mPixelStorageType(PixelStorageType::External) {
141f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    mPixelStorage.external.address = address;
142f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    mPixelStorage.external.context = context;
143f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    mPixelStorage.external.freeFunc = freeFunc;
144f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable));
145f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    // Note: this will trigger a call to onStrongRefDestroyed(), but
146f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    // we want the pixel ref to have a ref count of 0 at this point
147f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    mPixelRef->unref();
148f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
149f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
15039d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley AndrewsBitmap::Bitmap(void* address, int fd,
15139d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews            const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
15239d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        : mPixelStorageType(PixelStorageType::Ashmem) {
15339d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    mPixelStorage.ashmem.address = address;
15439d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    mPixelStorage.ashmem.fd = fd;
15539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    mPixelStorage.ashmem.size = ashmem_get_size_region(fd);
15639d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable));
15739d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    // Note: this will trigger a call to onStrongRefDestroyed(), but
15839d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    // we want the pixel ref to have a ref count of 0 at this point
15939d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    mPixelRef->unref();
16039d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews}
161f29ed28c7b878ef28058bc730715d0d32445bc57John ReckBitmap::~Bitmap() {
162f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    doFreePixels();
163f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
164f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
165f29ed28c7b878ef28058bc730715d0d32445bc57John Reckvoid Bitmap::freePixels() {
166f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    AutoMutex _lock(mLock);
167f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    if (mPinnedRefCount == 0) {
168f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        doFreePixels();
169f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        mPixelStorageType = PixelStorageType::Invalid;
170f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
171f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
172f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
173f29ed28c7b878ef28058bc730715d0d32445bc57John Reckvoid Bitmap::doFreePixels() {
174f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    switch (mPixelStorageType) {
175f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    case PixelStorageType::Invalid:
176f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        // already free'd, nothing to do
177f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        break;
178f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    case PixelStorageType::External:
179f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        mPixelStorage.external.freeFunc(mPixelStorage.external.address,
180f29ed28c7b878ef28058bc730715d0d32445bc57John Reck                mPixelStorage.external.context);
181f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        break;
18239d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    case PixelStorageType::Ashmem:
18339d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        munmap(mPixelStorage.ashmem.address, mPixelStorage.ashmem.size);
18439d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        close(mPixelStorage.ashmem.fd);
18539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        break;
186f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    case PixelStorageType::Java:
187f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        JNIEnv* env = jniEnv();
188f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        LOG_ALWAYS_FATAL_IF(mPixelStorage.java.jstrongRef,
189f29ed28c7b878ef28058bc730715d0d32445bc57John Reck                "Deleting a bitmap wrapper while there are outstanding strong "
190f29ed28c7b878ef28058bc730715d0d32445bc57John Reck                "references! mPinnedRefCount = %d", mPinnedRefCount);
191f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        env->DeleteWeakGlobalRef(mPixelStorage.java.jweakRef);
192f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        break;
193f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
194f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
195f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    if (android::uirenderer::Caches::hasInstance()) {
196f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        android::uirenderer::Caches::getInstance().textureCache.releaseTexture(
197f29ed28c7b878ef28058bc730715d0d32445bc57John Reck                mPixelRef->getStableID());
198f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
199f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
200f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
201f29ed28c7b878ef28058bc730715d0d32445bc57John Reckbool Bitmap::hasHardwareMipMap() {
202f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    return mPixelRef->hasHardwareMipMap();
203f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
204f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
205f29ed28c7b878ef28058bc730715d0d32445bc57John Reckvoid Bitmap::setHasHardwareMipMap(bool hasMipMap) {
206f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    mPixelRef->setHasHardwareMipMap(hasMipMap);
207f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
208f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
20939d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrewsint Bitmap::getAshmemFd() const {
21039d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    switch (mPixelStorageType) {
21139d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    case PixelStorageType::Ashmem:
21239d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        return mPixelStorage.ashmem.fd;
21339d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    default:
21439d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        return -1;
21539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    }
21639d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews}
21739d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews
218f29ed28c7b878ef28058bc730715d0d32445bc57John Reckconst SkImageInfo& Bitmap::info() const {
219f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    assertValid();
220f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    return mPixelRef->info();
221f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
222f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
223f29ed28c7b878ef28058bc730715d0d32445bc57John Recksize_t Bitmap::rowBytes() const {
224f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    return mPixelRef->rowBytes();
225f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
226f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
227ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn ReckSkPixelRef* Bitmap::peekAtPixelRef() const {
228f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    assertValid();
229f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    return mPixelRef.get();
230f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
231f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
232ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn ReckSkPixelRef* Bitmap::refPixelRef() {
233ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck    assertValid();
234ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck    android::AutoMutex _lock(mLock);
235ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck    return refPixelRefLocked();
236ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck}
237ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck
238ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn ReckSkPixelRef* Bitmap::refPixelRefLocked() {
239ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck    mPixelRef->ref();
240ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck    if (mPixelRef->unique()) {
241ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck        // We just restored this from 0, pin the pixels and inc the strong count
242ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck        // Note that there *might be* an incoming onStrongRefDestroyed from whatever
243ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck        // last unref'd
244ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck        pinPixelsLocked();
245ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck        mPinnedRefCount++;
246ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck    }
247ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck    return mPixelRef.get();
248ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck}
249ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck
250f29ed28c7b878ef28058bc730715d0d32445bc57John Reckvoid Bitmap::reconfigure(const SkImageInfo& info, size_t rowBytes,
251f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        SkColorTable* ctable) {
252ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck    {
253ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck        android::AutoMutex _lock(mLock);
254ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck        if (mPinnedRefCount) {
255ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck            ALOGW("Called reconfigure on a bitmap that is in use! This may"
256ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck                    " cause graphical corruption!");
257ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck        }
258ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck    }
259f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    mPixelRef->reconfigure(info, rowBytes, ctable);
260f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
261f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
262f29ed28c7b878ef28058bc730715d0d32445bc57John Reckvoid Bitmap::reconfigure(const SkImageInfo& info) {
2632a94a10bec186d832c2b95675cb6dc27b012c2d0Derek Sollenberger    reconfigure(info, info.minRowBytes(), nullptr);
264f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
265f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
2660781a2f116be045ff1a3aca721c47f9fef980beaJohn Reckvoid Bitmap::setAlphaType(SkAlphaType alphaType) {
2670781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck    if (!SkColorTypeValidateAlphaType(info().colorType(), alphaType, &alphaType)) {
2680781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck        return;
2690781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck    }
2700781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck
2710781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck    mPixelRef->changeAlphaType(alphaType);
2720781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck}
2730781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck
274f29ed28c7b878ef28058bc730715d0d32445bc57John Reckvoid Bitmap::detachFromJava() {
275f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    bool disposeSelf;
276f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    {
277f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        android::AutoMutex _lock(mLock);
278f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        mAttachedToJava = false;
279f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        disposeSelf = shouldDisposeSelfLocked();
280f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
281f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    if (disposeSelf) {
282f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        delete this;
283f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
284f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
285f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
286f29ed28c7b878ef28058bc730715d0d32445bc57John Reckbool Bitmap::shouldDisposeSelfLocked() {
287f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    return mPinnedRefCount == 0 && !mAttachedToJava;
288f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
289f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
290f29ed28c7b878ef28058bc730715d0d32445bc57John ReckJNIEnv* Bitmap::jniEnv() {
291f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    JNIEnv* env;
292f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    auto success = mPixelStorage.java.jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
293f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    LOG_ALWAYS_FATAL_IF(success != JNI_OK,
294f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        "Failed to get JNIEnv* from JVM: %p", mPixelStorage.java.jvm);
295f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    return env;
296f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
297f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
298f29ed28c7b878ef28058bc730715d0d32445bc57John Reckvoid Bitmap::onStrongRefDestroyed() {
299f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    bool disposeSelf = false;
300f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    {
301f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        android::AutoMutex _lock(mLock);
302f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        if (mPinnedRefCount > 0) {
303f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            mPinnedRefCount--;
304f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            if (mPinnedRefCount == 0) {
305f29ed28c7b878ef28058bc730715d0d32445bc57John Reck                unpinPixelsLocked();
306f29ed28c7b878ef28058bc730715d0d32445bc57John Reck                disposeSelf = shouldDisposeSelfLocked();
307f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            }
308f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        }
309f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
310f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    if (disposeSelf) {
311f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        delete this;
312f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
313f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
314f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
315f29ed28c7b878ef28058bc730715d0d32445bc57John Reckvoid Bitmap::pinPixelsLocked() {
316f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    switch (mPixelStorageType) {
317f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    case PixelStorageType::Invalid:
318f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        LOG_ALWAYS_FATAL("Cannot pin invalid pixels!");
319f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        break;
320f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    case PixelStorageType::External:
32139d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    case PixelStorageType::Ashmem:
322f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        // Nothing to do
323f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        break;
324f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    case PixelStorageType::Java: {
325f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        JNIEnv* env = jniEnv();
326f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        if (!mPixelStorage.java.jstrongRef) {
327f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            mPixelStorage.java.jstrongRef = reinterpret_cast<jbyteArray>(
328f29ed28c7b878ef28058bc730715d0d32445bc57John Reck                    env->NewGlobalRef(mPixelStorage.java.jweakRef));
329f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            if (!mPixelStorage.java.jstrongRef) {
330f29ed28c7b878ef28058bc730715d0d32445bc57John Reck                LOG_ALWAYS_FATAL("Failed to acquire strong reference to pixels");
331f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            }
332f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        }
333f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        break;
334f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
335f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
336f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
337f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
338f29ed28c7b878ef28058bc730715d0d32445bc57John Reckvoid Bitmap::unpinPixelsLocked() {
339f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    switch (mPixelStorageType) {
340f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    case PixelStorageType::Invalid:
341f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        LOG_ALWAYS_FATAL("Cannot unpin invalid pixels!");
342f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        break;
343f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    case PixelStorageType::External:
34439d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    case PixelStorageType::Ashmem:
345f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        // Don't need to do anything
346f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        break;
347f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    case PixelStorageType::Java: {
348f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        JNIEnv* env = jniEnv();
349f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        if (mPixelStorage.java.jstrongRef) {
350f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            env->DeleteGlobalRef(mPixelStorage.java.jstrongRef);
351f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            mPixelStorage.java.jstrongRef = nullptr;
352f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        }
353f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        break;
354f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
355f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
356f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
357f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
358f29ed28c7b878ef28058bc730715d0d32445bc57John Reckvoid Bitmap::getSkBitmap(SkBitmap* outBitmap) {
359f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    assertValid();
360f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    android::AutoMutex _lock(mLock);
361f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    // Safe because mPixelRef is a WrappedPixelRef type, otherwise rowBytes()
362f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    // would require locking the pixels first.
363f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    outBitmap->setInfo(mPixelRef->info(), mPixelRef->rowBytes());
364ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck    outBitmap->setPixelRef(refPixelRefLocked())->unref();
365f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    outBitmap->setHasHardwareMipMap(hasHardwareMipMap());
366f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
367f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
368f29ed28c7b878ef28058bc730715d0d32445bc57John Reckvoid Bitmap::assertValid() const {
369f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    LOG_ALWAYS_FATAL_IF(mPixelStorageType == PixelStorageType::Invalid,
370f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            "Error, cannot access an invalid/free'd bitmap here!");
371f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
372f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
373f29ed28c7b878ef28058bc730715d0d32445bc57John Reck} // namespace android
374f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
375f29ed28c7b878ef28058bc730715d0d32445bc57John Reckusing namespace android;
376f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
377f29ed28c7b878ef28058bc730715d0d32445bc57John Reck// Convenience class that does not take a global ref on the pixels, relying
378f29ed28c7b878ef28058bc730715d0d32445bc57John Reck// on the caller already having a local JNI ref
379f29ed28c7b878ef28058bc730715d0d32445bc57John Reckclass LocalScopedBitmap {
380f29ed28c7b878ef28058bc730715d0d32445bc57John Reckpublic:
381f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    LocalScopedBitmap(jlong bitmapHandle)
382f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            : mBitmap(reinterpret_cast<Bitmap*>(bitmapHandle)) {}
383f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
384f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    Bitmap* operator->() {
385f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        return mBitmap;
386f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
387f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
388f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    void* pixels() {
389ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck        return mBitmap->peekAtPixelRef()->pixels();
390f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
391f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
392f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    bool valid() {
393f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        return mBitmap && mBitmap->valid();
394f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
395f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
396f29ed28c7b878ef28058bc730715d0d32445bc57John Reckprivate:
397f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    Bitmap* mBitmap;
398f29ed28c7b878ef28058bc730715d0d32445bc57John Reck};
399f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
40032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik///////////////////////////////////////////////////////////////////////////////
40132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik// Conversions to/from SkColor, for get/setPixels, and the create method, which
40232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik// is basically like setPixels
40332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
40432054b0b3edb350a5444c47753b2982312dd7ffdChris Craiktypedef void (*FromColorProc)(void* dst, const SkColor src[], int width,
40532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                              int x, int y);
40632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
40732054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void FromColor_D32(void* dst, const SkColor src[], int width,
40832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                          int, int) {
40932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkPMColor* d = (SkPMColor*)dst;
41032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
41132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    for (int i = 0; i < width; i++) {
41232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        *d++ = SkPreMultiplyColor(*src++);
41332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
41432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
41532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
41632054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void FromColor_D32_Raw(void* dst, const SkColor src[], int width,
41732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                          int, int) {
41846d8444631b4b1253a76bfcc78a29d26014d022fDan Albert    // Needed to thwart the unreachable code detection from clang.
41946d8444631b4b1253a76bfcc78a29d26014d022fDan Albert    static const bool sk_color_ne_zero = SK_COLOR_MATCHES_PMCOLOR_BYTE_ORDER;
42046d8444631b4b1253a76bfcc78a29d26014d022fDan Albert
42132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    // SkColor's ordering may be different from SkPMColor
42246d8444631b4b1253a76bfcc78a29d26014d022fDan Albert    if (sk_color_ne_zero) {
42332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        memcpy(dst, src, width * sizeof(SkColor));
42432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        return;
42532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
42632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
42732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    // order isn't same, repack each pixel manually
42832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkPMColor* d = (SkPMColor*)dst;
42932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    for (int i = 0; i < width; i++) {
43032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        SkColor c = *src++;
43132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        *d++ = SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c),
43232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                   SkColorGetG(c), SkColorGetB(c));
43332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
43432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
43532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
43632054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void FromColor_D565(void* dst, const SkColor src[], int width,
43732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                           int x, int y) {
43832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    uint16_t* d = (uint16_t*)dst;
43932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
44032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    DITHER_565_SCAN(y);
44132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    for (int stop = x + width; x < stop; x++) {
44232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        SkColor c = *src++;
44332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        *d++ = SkDitherRGBTo565(SkColorGetR(c), SkColorGetG(c), SkColorGetB(c),
44432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                DITHER_VALUE(x));
44532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
44632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
44732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
44832054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void FromColor_D4444(void* dst, const SkColor src[], int width,
44932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                            int x, int y) {
45032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkPMColor16* d = (SkPMColor16*)dst;
45132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
45232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    DITHER_4444_SCAN(y);
45332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    for (int stop = x + width; x < stop; x++) {
45432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        SkPMColor pmc = SkPreMultiplyColor(*src++);
45532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        *d++ = SkDitherARGB32To4444(pmc, DITHER_VALUE(x));
45632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik//        *d++ = SkPixel32ToPixel4444(pmc);
45732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
45832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
45932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
46032054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void FromColor_D4444_Raw(void* dst, const SkColor src[], int width,
46132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                            int x, int y) {
46232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkPMColor16* d = (SkPMColor16*)dst;
46332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
46432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    DITHER_4444_SCAN(y);
46532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    for (int stop = x + width; x < stop; x++) {
46632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        SkColor c = *src++;
46732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
46832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        // SkPMColor is used because the ordering is ARGB32, even though the target actually premultiplied
46932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        SkPMColor pmc = SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c),
47032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                            SkColorGetG(c), SkColorGetB(c));
47132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        *d++ = SkDitherARGB32To4444(pmc, DITHER_VALUE(x));
47232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik//        *d++ = SkPixel32ToPixel4444(pmc);
47332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
47432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
47532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
47632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik// can return NULL
47757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins IIIstatic FromColorProc ChooseFromColorProc(const SkBitmap& bitmap) {
47857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    switch (bitmap.colorType()) {
47957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        case kN32_SkColorType:
48057ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III            return bitmap.alphaType() == kPremul_SkAlphaType ? FromColor_D32 : FromColor_D32_Raw;
48157ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        case kARGB_4444_SkColorType:
48257ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III            return bitmap.alphaType() == kPremul_SkAlphaType ? FromColor_D4444 :
48357ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                    FromColor_D4444_Raw;
48457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        case kRGB_565_SkColorType:
48532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik            return FromColor_D565;
48632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        default:
48732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik            break;
48832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
48932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    return NULL;
49032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
49132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
49232054b0b3edb350a5444c47753b2982312dd7ffdChris Craikbool GraphicsJNI::SetPixels(JNIEnv* env, jintArray srcColors, int srcOffset, int srcStride,
49357ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        int x, int y, int width, int height, const SkBitmap& dstBitmap) {
49432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkAutoLockPixels alp(dstBitmap);
49532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    void* dst = dstBitmap.getPixels();
49657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    FromColorProc proc = ChooseFromColorProc(dstBitmap);
49732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
49832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    if (NULL == dst || NULL == proc) {
49932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        return false;
50032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
50132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
50232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    const jint* array = env->GetIntArrayElements(srcColors, NULL);
50332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    const SkColor* src = (const SkColor*)array + srcOffset;
50432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
50532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    // reset to to actual choice from caller
50632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    dst = dstBitmap.getAddr(x, y);
50732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    // now copy/convert each scanline
50832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    for (int y = 0; y < height; y++) {
50932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        proc(dst, src, width, x, y);
51032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        src += srcStride;
51132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        dst = (char*)dst + dstBitmap.rowBytes();
51232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
51332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
51432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    dstBitmap.notifyPixelsChanged();
51532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
51632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    env->ReleaseIntArrayElements(srcColors, const_cast<jint*>(array),
51732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                 JNI_ABORT);
51832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    return true;
51932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
52032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
52132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik//////////////////// ToColor procs
52232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
52332054b0b3edb350a5444c47753b2982312dd7ffdChris Craiktypedef void (*ToColorProc)(SkColor dst[], const void* src, int width,
52432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                            SkColorTable*);
52532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
52632054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void ToColor_S32_Alpha(SkColor dst[], const void* src, int width,
52732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                              SkColorTable*) {
52832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkASSERT(width > 0);
52932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    const SkPMColor* s = (const SkPMColor*)src;
53032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    do {
53132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        *dst++ = SkUnPreMultiply::PMColorToColor(*s++);
53232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    } while (--width != 0);
53332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
53432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
53532054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void ToColor_S32_Raw(SkColor dst[], const void* src, int width,
53632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                              SkColorTable*) {
53732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkASSERT(width > 0);
53832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    const SkPMColor* s = (const SkPMColor*)src;
53932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    do {
54032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        SkPMColor c = *s++;
54132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
54232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                SkGetPackedG32(c), SkGetPackedB32(c));
54332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    } while (--width != 0);
54432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
54532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
54632054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void ToColor_S32_Opaque(SkColor dst[], const void* src, int width,
54732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                               SkColorTable*) {
54832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkASSERT(width > 0);
54932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    const SkPMColor* s = (const SkPMColor*)src;
55032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    do {
55132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        SkPMColor c = *s++;
55232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
55332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                               SkGetPackedB32(c));
55432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    } while (--width != 0);
55532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
55632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
55732054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void ToColor_S4444_Alpha(SkColor dst[], const void* src, int width,
55832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                SkColorTable*) {
55932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkASSERT(width > 0);
56032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    const SkPMColor16* s = (const SkPMColor16*)src;
56132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    do {
56232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        *dst++ = SkUnPreMultiply::PMColorToColor(SkPixel4444ToPixel32(*s++));
56332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    } while (--width != 0);
56432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
56532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
56632054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void ToColor_S4444_Raw(SkColor dst[], const void* src, int width,
56732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                SkColorTable*) {
56832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkASSERT(width > 0);
56932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    const SkPMColor16* s = (const SkPMColor16*)src;
57032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    do {
57132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        SkPMColor c = SkPixel4444ToPixel32(*s++);
57232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
57332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                SkGetPackedG32(c), SkGetPackedB32(c));
57432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    } while (--width != 0);
57532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
57632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
57732054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void ToColor_S4444_Opaque(SkColor dst[], const void* src, int width,
57832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                 SkColorTable*) {
57932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkASSERT(width > 0);
58032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    const SkPMColor16* s = (const SkPMColor16*)src;
58132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    do {
58232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        SkPMColor c = SkPixel4444ToPixel32(*s++);
58332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
58432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                               SkGetPackedB32(c));
58532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    } while (--width != 0);
58632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
58732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
58832054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void ToColor_S565(SkColor dst[], const void* src, int width,
58932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                         SkColorTable*) {
59032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkASSERT(width > 0);
59132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    const uint16_t* s = (const uint16_t*)src;
59232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    do {
59332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        uint16_t c = *s++;
59432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        *dst++ =  SkColorSetRGB(SkPacked16ToR32(c), SkPacked16ToG32(c),
59532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                SkPacked16ToB32(c));
59632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    } while (--width != 0);
59732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
59832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
59932054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void ToColor_SI8_Alpha(SkColor dst[], const void* src, int width,
60032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                              SkColorTable* ctable) {
60132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkASSERT(width > 0);
60232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    const uint8_t* s = (const uint8_t*)src;
60371487eb0ceb2b7dea02649e78d99bb5952f5eaefMike Reed    const SkPMColor* colors = ctable->readColors();
60432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    do {
60532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        *dst++ = SkUnPreMultiply::PMColorToColor(colors[*s++]);
60632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    } while (--width != 0);
60732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
60832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
60932054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void ToColor_SI8_Raw(SkColor dst[], const void* src, int width,
61032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                              SkColorTable* ctable) {
61132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkASSERT(width > 0);
61232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    const uint8_t* s = (const uint8_t*)src;
61371487eb0ceb2b7dea02649e78d99bb5952f5eaefMike Reed    const SkPMColor* colors = ctable->readColors();
61432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    do {
61532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        SkPMColor c = colors[*s++];
61632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
61732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                SkGetPackedG32(c), SkGetPackedB32(c));
61832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    } while (--width != 0);
61932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
62032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
62132054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void ToColor_SI8_Opaque(SkColor dst[], const void* src, int width,
62232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                               SkColorTable* ctable) {
62332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkASSERT(width > 0);
62432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    const uint8_t* s = (const uint8_t*)src;
62571487eb0ceb2b7dea02649e78d99bb5952f5eaefMike Reed    const SkPMColor* colors = ctable->readColors();
62632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    do {
62732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        SkPMColor c = colors[*s++];
62832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
62932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                               SkGetPackedB32(c));
63032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    } while (--width != 0);
63132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
63232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
63332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik// can return NULL
63457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins IIIstatic ToColorProc ChooseToColorProc(const SkBitmap& src) {
635b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed    switch (src.colorType()) {
636b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed        case kN32_SkColorType:
63757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III            switch (src.alphaType()) {
63857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                case kOpaque_SkAlphaType:
63957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                    return ToColor_S32_Opaque;
64057ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                case kPremul_SkAlphaType:
64157ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                    return ToColor_S32_Alpha;
64257ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                case kUnpremul_SkAlphaType:
64357ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                    return ToColor_S32_Raw;
64457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                default:
64557ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                    return NULL;
64657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III            }
647b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed        case kARGB_4444_SkColorType:
64857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III            switch (src.alphaType()) {
64957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                case kOpaque_SkAlphaType:
65057ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                    return ToColor_S4444_Opaque;
65157ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                case kPremul_SkAlphaType:
65257ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                    return ToColor_S4444_Alpha;
65357ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                case kUnpremul_SkAlphaType:
65457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                    return ToColor_S4444_Raw;
65557ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                default:
65657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                    return NULL;
65757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III            }
658b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed        case kRGB_565_SkColorType:
65932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik            return ToColor_S565;
660b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed        case kIndex_8_SkColorType:
66132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik            if (src.getColorTable() == NULL) {
66232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                return NULL;
66332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik            }
66457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III            switch (src.alphaType()) {
66557ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                case kOpaque_SkAlphaType:
66657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                    return ToColor_SI8_Opaque;
66757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                case kPremul_SkAlphaType:
66857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                    return ToColor_SI8_Alpha;
66957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                case kUnpremul_SkAlphaType:
67057ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                    return ToColor_SI8_Raw;
67157ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                default:
67257ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                    return NULL;
67357ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III            }
67432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        default:
67532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik            break;
67632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
67732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    return NULL;
67832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
67932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
68032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik///////////////////////////////////////////////////////////////////////////////
68132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik///////////////////////////////////////////////////////////////////////////////
68232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
68332054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic int getPremulBitmapCreateFlags(bool isMutable) {
68432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    int flags = GraphicsJNI::kBitmapCreateFlag_Premultiplied;
68532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    if (isMutable) flags |= GraphicsJNI::kBitmapCreateFlag_Mutable;
68632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    return flags;
68732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
68832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
68932054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic jobject Bitmap_creator(JNIEnv* env, jobject, jintArray jColors,
69032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                              jint offset, jint stride, jint width, jint height,
69132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                              jint configHandle, jboolean isMutable) {
6921103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed    SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle);
69332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    if (NULL != jColors) {
69432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        size_t n = env->GetArrayLength(jColors);
69532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        if (n < SkAbs32(stride) * (size_t)height) {
69632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik            doThrowAIOOBE(env);
69732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik            return NULL;
69832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        }
69932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
70032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
70132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    // ARGB_4444 is a deprecated format, convert automatically to 8888
702b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed    if (colorType == kARGB_4444_SkColorType) {
703b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed        colorType = kN32_SkColorType;
70432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
70532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
70632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkBitmap bitmap;
707b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed    bitmap.setInfo(SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType));
70832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
709f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    Bitmap* nativeBitmap = GraphicsJNI::allocateJavaPixelRef(env, &bitmap, NULL);
710f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    if (!nativeBitmap) {
71132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        return NULL;
71232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
71332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
71432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    if (jColors != NULL) {
71532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        GraphicsJNI::SetPixels(env, jColors, offset, stride,
71657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                0, 0, width, height, bitmap);
71732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
71832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
719f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    return GraphicsJNI::createBitmap(env, nativeBitmap,
720f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            getPremulBitmapCreateFlags(isMutable));
72132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
72232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
72332054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic jobject Bitmap_copy(JNIEnv* env, jobject, jlong srcHandle,
72432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                           jint dstConfigHandle, jboolean isMutable) {
725f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkBitmap src;
726f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
7271103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed    SkColorType dstCT = GraphicsJNI::legacyBitmapConfigToColorType(dstConfigHandle);
72832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkBitmap            result;
72932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    JavaPixelAllocator  allocator(env);
73032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
731f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    if (!src.copyTo(&result, dstCT, &allocator)) {
73232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        return NULL;
73332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
734f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    Bitmap* bitmap = allocator.getStorageObjAndReset();
735f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    return GraphicsJNI::createBitmap(env, bitmap,
736f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            getPremulBitmapCreateFlags(isMutable));
73732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
73832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
739721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrewsstatic jobject Bitmap_copyAshmem(JNIEnv* env, jobject, jlong srcHandle) {
740721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    SkBitmap src;
741721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
742721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    SkBitmap result;
743721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews
744721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    AshmemPixelAllocator allocator(env);
745721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    if (!src.copyTo(&result, &allocator)) {
746721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews        return NULL;
747721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    }
748721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    Bitmap* bitmap = allocator.getStorageObjAndReset();
749721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    bitmap->peekAtPixelRef()->setImmutable();
750721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    jobject ret = GraphicsJNI::createBitmap(env, bitmap, getPremulBitmapCreateFlags(false));
751721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    return ret;
752721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews}
753721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews
75432054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void Bitmap_destructor(JNIEnv* env, jobject, jlong bitmapHandle) {
755f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    LocalScopedBitmap bitmap(bitmapHandle);
756f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    bitmap->detachFromJava();
75732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
75832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
75932054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic jboolean Bitmap_recycle(JNIEnv* env, jobject, jlong bitmapHandle) {
760f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    LocalScopedBitmap bitmap(bitmapHandle);
761f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    bitmap->freePixels();
76232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    return JNI_TRUE;
76332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
76432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
76532054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void Bitmap_reconfigure(JNIEnv* env, jobject clazz, jlong bitmapHandle,
76617a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III        jint width, jint height, jint configHandle, jint allocSize,
76717a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III        jboolean requestPremul) {
768f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    LocalScopedBitmap bitmap(bitmapHandle);
7691103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed    SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle);
77017a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III
77117a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III    // ARGB_4444 is a deprecated format, convert automatically to 8888
77217a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III    if (colorType == kARGB_4444_SkColorType) {
77317a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III        colorType = kN32_SkColorType;
77417a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III    }
77517a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III
77617a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III    if (width * height * SkColorTypeBytesPerPixel(colorType) > allocSize) {
77732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        // done in native as there's no way to get BytesPerPixel in Java
77832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        doThrowIAE(env, "Bitmap not large enough to support new configuration");
77932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        return;
78032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
78117a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III    SkAlphaType alphaType;
782f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    if (bitmap->info().colorType() != kRGB_565_SkColorType
783f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            && bitmap->info().alphaType() == kOpaque_SkAlphaType) {
78417a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III        // If the original bitmap was set to opaque, keep that setting, unless it
78517a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III        // was 565, which is required to be opaque.
78617a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III        alphaType = kOpaque_SkAlphaType;
78717a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III    } else {
78817a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III        // Otherwise respect the premultiplied request.
78917a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III        alphaType = requestPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
79017a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III    }
791f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    bitmap->reconfigure(SkImageInfo::Make(width, height, colorType, alphaType));
79232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
79332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
79432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik// These must match the int values in Bitmap.java
79532054b0b3edb350a5444c47753b2982312dd7ffdChris Craikenum JavaEncodeFormat {
79632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    kJPEG_JavaEncodeFormat = 0,
79732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    kPNG_JavaEncodeFormat = 1,
79832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    kWEBP_JavaEncodeFormat = 2
79932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik};
80032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
80132054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic jboolean Bitmap_compress(JNIEnv* env, jobject clazz, jlong bitmapHandle,
80232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                jint format, jint quality,
80332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                jobject jstream, jbyteArray jstorage) {
804f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
805f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    LocalScopedBitmap bitmap(bitmapHandle);
80632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkImageEncoder::Type fm;
80732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
80832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    switch (format) {
80932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    case kJPEG_JavaEncodeFormat:
81032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        fm = SkImageEncoder::kJPEG_Type;
81132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        break;
81232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    case kPNG_JavaEncodeFormat:
81332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        fm = SkImageEncoder::kPNG_Type;
81432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        break;
81532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    case kWEBP_JavaEncodeFormat:
81632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        fm = SkImageEncoder::kWEBP_Type;
81732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        break;
81832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    default:
81932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        return JNI_FALSE;
82032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
82132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
822f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    if (!bitmap.valid()) {
823f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        return JNI_FALSE;
824f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
82532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
826f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    bool success = false;
82732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
828f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    std::unique_ptr<SkWStream> strm(CreateJavaOutputStreamAdaptor(env, jstream, jstorage));
829f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    if (!strm.get()) {
830f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        return JNI_FALSE;
831f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    }
83232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
833f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    std::unique_ptr<SkImageEncoder> encoder(SkImageEncoder::Create(fm));
834f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    if (encoder.get()) {
835f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        SkBitmap skbitmap;
836f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        bitmap->getSkBitmap(&skbitmap);
837f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        success = encoder->encodeStream(strm.get(), skbitmap, quality);
83832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
83932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    return success ? JNI_TRUE : JNI_FALSE;
84032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
84132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
84232054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void Bitmap_erase(JNIEnv* env, jobject, jlong bitmapHandle, jint color) {
843f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    LocalScopedBitmap bitmap(bitmapHandle);
844f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkBitmap skBitmap;
845f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    bitmap->getSkBitmap(&skBitmap);
846f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    skBitmap.eraseColor(color);
84732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
84832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
84932054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic jint Bitmap_rowBytes(JNIEnv* env, jobject, jlong bitmapHandle) {
850f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    LocalScopedBitmap bitmap(bitmapHandle);
85132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    return static_cast<jint>(bitmap->rowBytes());
85232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
85332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
85432054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic jint Bitmap_config(JNIEnv* env, jobject, jlong bitmapHandle) {
855f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    LocalScopedBitmap bitmap(bitmapHandle);
856f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    return GraphicsJNI::colorTypeToLegacyBitmapConfig(bitmap->info().colorType());
85732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
85832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
85932054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic jint Bitmap_getGenerationId(JNIEnv* env, jobject, jlong bitmapHandle) {
860f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    LocalScopedBitmap bitmap(bitmapHandle);
861ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck    return static_cast<jint>(bitmap->peekAtPixelRef()->getGenerationID());
86232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
86332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
86457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins IIIstatic jboolean Bitmap_isPremultiplied(JNIEnv* env, jobject, jlong bitmapHandle) {
865f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    LocalScopedBitmap bitmap(bitmapHandle);
866f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    if (bitmap->info().alphaType() == kPremul_SkAlphaType) {
86757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        return JNI_TRUE;
86857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    }
86957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    return JNI_FALSE;
87057ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III}
87157ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III
87232054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic jboolean Bitmap_hasAlpha(JNIEnv* env, jobject, jlong bitmapHandle) {
873f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    LocalScopedBitmap bitmap(bitmapHandle);
874f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    return !bitmap->info().isOpaque() ? JNI_TRUE : JNI_FALSE;
87532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
87632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
87757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins IIIstatic void Bitmap_setHasAlpha(JNIEnv* env, jobject, jlong bitmapHandle,
87857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        jboolean hasAlpha, jboolean requestPremul) {
879f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    LocalScopedBitmap bitmap(bitmapHandle);
88057ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    if (hasAlpha) {
8810781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck        bitmap->setAlphaType(
882f29ed28c7b878ef28058bc730715d0d32445bc57John Reck                requestPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType);
88332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    } else {
8840781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck        bitmap->setAlphaType(kOpaque_SkAlphaType);
88557ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    }
88657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III}
88757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III
88857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins IIIstatic void Bitmap_setPremultiplied(JNIEnv* env, jobject, jlong bitmapHandle,
88957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        jboolean isPremul) {
890f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    LocalScopedBitmap bitmap(bitmapHandle);
891f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    if (!bitmap->info().isOpaque()) {
89257ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        if (isPremul) {
8930781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck            bitmap->setAlphaType(kPremul_SkAlphaType);
89457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        } else {
8950781a2f116be045ff1a3aca721c47f9fef980beaJohn Reck            bitmap->setAlphaType(kUnpremul_SkAlphaType);
89657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        }
89732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
89832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
89932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
90032054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic jboolean Bitmap_hasMipMap(JNIEnv* env, jobject, jlong bitmapHandle) {
901f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    LocalScopedBitmap bitmap(bitmapHandle);
90232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    return bitmap->hasHardwareMipMap() ? JNI_TRUE : JNI_FALSE;
90332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
90432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
90532054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void Bitmap_setHasMipMap(JNIEnv* env, jobject, jlong bitmapHandle,
90632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                jboolean hasMipMap) {
907f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    LocalScopedBitmap bitmap(bitmapHandle);
90832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    bitmap->setHasHardwareMipMap(hasMipMap);
90932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
91032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
91132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik///////////////////////////////////////////////////////////////////////////////
91232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
91332054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic jobject Bitmap_createFromParcel(JNIEnv* env, jobject, jobject parcel) {
91432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    if (parcel == NULL) {
91532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        SkDebugf("-------- unparcel parcel is NULL\n");
91632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        return NULL;
91732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
91832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
91932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    android::Parcel* p = android::parcelForJavaObject(env, parcel);
92032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
921b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed    const bool        isMutable = p->readInt32() != 0;
922b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed    const SkColorType colorType = (SkColorType)p->readInt32();
923b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed    const SkAlphaType alphaType = (SkAlphaType)p->readInt32();
924b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed    const int         width = p->readInt32();
925b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed    const int         height = p->readInt32();
926b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed    const int         rowBytes = p->readInt32();
927b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed    const int         density = p->readInt32();
928b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed
929b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed    if (kN32_SkColorType != colorType &&
930b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed            kRGB_565_SkColorType != colorType &&
931b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed            kARGB_4444_SkColorType != colorType &&
932b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed            kIndex_8_SkColorType != colorType &&
933b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed            kAlpha_8_SkColorType != colorType) {
934b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed        SkDebugf("Bitmap_createFromParcel unknown colortype: %d\n", colorType);
93532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        return NULL;
93632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
93732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
938ec419e0b731d2aa32f7f570e4021fe18b8be228dLeon Scroggins III    std::unique_ptr<SkBitmap> bitmap(new SkBitmap);
93932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
940ec419e0b731d2aa32f7f570e4021fe18b8be228dLeon Scroggins III    if (!bitmap->setInfo(SkImageInfo::Make(width, height, colorType, alphaType), rowBytes)) {
941ec419e0b731d2aa32f7f570e4021fe18b8be228dLeon Scroggins III        return NULL;
942ec419e0b731d2aa32f7f570e4021fe18b8be228dLeon Scroggins III    }
94332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
94432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkColorTable* ctable = NULL;
945b933055cf3f7f8ea89bfd3bc9c37a3891ff7310aMike Reed    if (colorType == kIndex_8_SkColorType) {
94632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        int count = p->readInt32();
947ec419e0b731d2aa32f7f570e4021fe18b8be228dLeon Scroggins III        if (count < 0 || count > 256) {
948ec419e0b731d2aa32f7f570e4021fe18b8be228dLeon Scroggins III            // The data is corrupt, since SkColorTable enforces a value between 0 and 256,
949ec419e0b731d2aa32f7f570e4021fe18b8be228dLeon Scroggins III            // inclusive.
950ec419e0b731d2aa32f7f570e4021fe18b8be228dLeon Scroggins III            return NULL;
951ec419e0b731d2aa32f7f570e4021fe18b8be228dLeon Scroggins III        }
95232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        if (count > 0) {
95332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik            size_t size = count * sizeof(SkPMColor);
95432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik            const SkPMColor* src = (const SkPMColor*)p->readInplace(size);
955ec419e0b731d2aa32f7f570e4021fe18b8be228dLeon Scroggins III            if (src == NULL) {
956ec419e0b731d2aa32f7f570e4021fe18b8be228dLeon Scroggins III                return NULL;
957ec419e0b731d2aa32f7f570e4021fe18b8be228dLeon Scroggins III            }
95832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik            ctable = new SkColorTable(src, count);
95932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        }
96032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
96132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
96239d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    int fd = p->readFileDescriptor();
96339d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    int dupFd = dup(fd);
96439d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    if (dupFd < 0) {
96532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        SkSafeUnref(ctable);
96639d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        doThrowRE(env, "Could not dup parcel fd.");
96732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        return NULL;
96832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
96932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
97039d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    bool readOnlyMapping = !isMutable;
97139d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    Bitmap* nativeBitmap = GraphicsJNI::mapAshmemPixelRef(env, bitmap.get(),
97239d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        ctable, dupFd, readOnlyMapping);
97332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkSafeUnref(ctable);
97439d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    if (!nativeBitmap) {
97539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        close(dupFd);
97639d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        doThrowRE(env, "Could not allocate ashmem pixel ref.");
97732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        return NULL;
97832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
97939d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    bitmap->pixelRef()->setImmutable();
98032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
981f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    return GraphicsJNI::createBitmap(env, nativeBitmap,
982ec419e0b731d2aa32f7f570e4021fe18b8be228dLeon Scroggins III            getPremulBitmapCreateFlags(isMutable), NULL, NULL, density);
98332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
98432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
98539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrewsclass Ashmem {
98639d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrewspublic:
98739d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    Ashmem(size_t sz, bool removeWritePerm) : mSize(sz) {
98839d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        int fd = -1;
98939d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        void *addr = nullptr;
99039d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews
99139d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        // Create new ashmem region with read/write priv
99239d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        fd = ashmem_create_region("bitmap", sz);
99339d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        if (fd < 0) {
99439d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews            goto error;
99539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        }
99639d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        addr = mmap(nullptr, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
99739d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        if (addr == MAP_FAILED) {
99839d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews            goto error;
99939d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        }
100039d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        // If requested, remove the ability to make additional writeable to
100139d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        // this memory.
100239d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        if (removeWritePerm) {
100339d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews            if (ashmem_set_prot_region(fd, PROT_READ) < 0) {
100439d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews                goto error;
100539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews            }
100639d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        }
100739d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        mFd = fd;
100839d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        mPtr = addr;
100939d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        return;
101039d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrewserror:
101139d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        if (fd >= 0) {
101239d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews            close(fd);
101339d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        }
101439d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        if (addr) {
101539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews            munmap(addr, sz);
101639d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        }
101739d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    }
101839d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    ~Ashmem() {
101939d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        if (mPtr) {
102039d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews            close(mFd);
102139d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews            munmap(mPtr, mSize);
102239d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        }
102339d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    }
102439d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    void *getPtr() const { return mPtr; }
102539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    int getFd() const { return mFd; }
102639d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrewsprivate:
102739d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    int mFd = -1;
102839d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    int mSize;
102939d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    void* mPtr = nullptr;
103039d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews};
103139d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews
103232054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic jboolean Bitmap_writeToParcel(JNIEnv* env, jobject,
103332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                     jlong bitmapHandle,
103432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                     jboolean isMutable, jint density,
103532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                     jobject parcel) {
103632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    if (parcel == NULL) {
103732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        SkDebugf("------- writeToParcel null parcel\n");
103832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        return JNI_FALSE;
103932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
104032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
104132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    android::Parcel* p = android::parcelForJavaObject(env, parcel);
1042f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkBitmap bitmap;
104339d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews
104439d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    android::Bitmap* androidBitmap = reinterpret_cast<Bitmap*>(bitmapHandle);
104539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    androidBitmap->getSkBitmap(&bitmap);
104632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
104732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    p->writeInt32(isMutable);
1048f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    p->writeInt32(bitmap.colorType());
1049f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    p->writeInt32(bitmap.alphaType());
1050f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    p->writeInt32(bitmap.width());
1051f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    p->writeInt32(bitmap.height());
1052f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    p->writeInt32(bitmap.rowBytes());
105332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    p->writeInt32(density);
105432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
1055f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    if (bitmap.colorType() == kIndex_8_SkColorType) {
1056f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        SkColorTable* ctable = bitmap.getColorTable();
105732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        if (ctable != NULL) {
105832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik            int count = ctable->count();
105932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik            p->writeInt32(count);
106032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik            memcpy(p->writeInplace(count * sizeof(SkPMColor)),
106171487eb0ceb2b7dea02649e78d99bb5952f5eaefMike Reed                   ctable->readColors(), count * sizeof(SkPMColor));
106232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        } else {
106332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik            p->writeInt32(0);   // indicate no ctable
106432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        }
106532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
106632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
106739d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    bool ashmemSrc = androidBitmap->getAshmemFd() >= 0;
106839d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    if (ashmemSrc && !isMutable) {
106939d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        p->writeDupFileDescriptor(androidBitmap->getAshmemFd());
107032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    } else {
107139d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        Ashmem dstAshmem(bitmap.getSize(), !isMutable);
107239d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        if (!dstAshmem.getPtr()) {
107339d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews            doThrowRE(env, "Could not allocate ashmem for new bitmap.");
107439d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews            return JNI_FALSE;
107539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        }
107632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
107739d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        bitmap.lockPixels();
107839d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        const void* pSrc = bitmap.getPixels();
107939d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        if (pSrc == NULL) {
108039d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews            memset(dstAshmem.getPtr(), 0, bitmap.getSize());
108139d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        } else {
108239d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews            memcpy(dstAshmem.getPtr(), pSrc, bitmap.getSize());
108339d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        }
108439d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        bitmap.unlockPixels();
108539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        p->writeDupFileDescriptor(dstAshmem.getFd());
108639d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    }
108732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    return JNI_TRUE;
108832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
108932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
109032054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic jobject Bitmap_extractAlpha(JNIEnv* env, jobject clazz,
109132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                   jlong srcHandle, jlong paintHandle,
109232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                   jintArray offsetXY) {
1093f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkBitmap src;
1094f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
10956ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    const android::Paint* paint = reinterpret_cast<android::Paint*>(paintHandle);
109632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkIPoint  offset;
1097f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkBitmap dst;
109832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    JavaPixelAllocator allocator(env);
109932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
1100f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    src.extractAlpha(&dst, paint, &allocator, &offset);
110132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    // If Skia can't allocate pixels for destination bitmap, it resets
110232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    // it, that is set its pixels buffer to NULL, and zero width and height.
1103f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    if (dst.getPixels() == NULL && src.getPixels() != NULL) {
110432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        doThrowOOME(env, "failed to allocate pixels for alpha");
110532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        return NULL;
110632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
110732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    if (offsetXY != 0 && env->GetArrayLength(offsetXY) >= 2) {
110832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        int* array = env->GetIntArrayElements(offsetXY, NULL);
110932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        array[0] = offset.fX;
111032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        array[1] = offset.fY;
111132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        env->ReleaseIntArrayElements(offsetXY, array, 0);
111232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
111332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
1114f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    return GraphicsJNI::createBitmap(env, allocator.getStorageObjAndReset(),
1115f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            getPremulBitmapCreateFlags(true));
111632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
111732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
111832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik///////////////////////////////////////////////////////////////////////////////
111932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
112032054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic jint Bitmap_getPixel(JNIEnv* env, jobject, jlong bitmapHandle,
112157ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        jint x, jint y) {
1122f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkBitmap bitmap;
1123f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1124f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkAutoLockPixels alp(bitmap);
112532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
1126f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    ToColorProc proc = ChooseToColorProc(bitmap);
112732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    if (NULL == proc) {
112832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        return 0;
112932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
1130f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    const void* src = bitmap.getAddr(x, y);
113132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    if (NULL == src) {
113232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        return 0;
113332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
113432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
113532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkColor dst[1];
1136f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    proc(dst, src, 1, bitmap.getColorTable());
113732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    return static_cast<jint>(dst[0]);
113832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
113932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
114032054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void Bitmap_getPixels(JNIEnv* env, jobject, jlong bitmapHandle,
114132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        jintArray pixelArray, jint offset, jint stride,
114257ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        jint x, jint y, jint width, jint height) {
1143f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkBitmap bitmap;
1144f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1145f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkAutoLockPixels alp(bitmap);
114632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
1147f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    ToColorProc proc = ChooseToColorProc(bitmap);
114832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    if (NULL == proc) {
114932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        return;
115032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
1151f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    const void* src = bitmap.getAddr(x, y);
115232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    if (NULL == src) {
115332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        return;
115432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
115532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
1156f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkColorTable* ctable = bitmap.getColorTable();
115732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    jint* dst = env->GetIntArrayElements(pixelArray, NULL);
115832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkColor* d = (SkColor*)dst + offset;
115932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    while (--height >= 0) {
116032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        proc(d, src, width, ctable);
116132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        d += stride;
1162f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        src = (void*)((const char*)src + bitmap.rowBytes());
116332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
116432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    env->ReleaseIntArrayElements(pixelArray, dst, 0);
116532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
116632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
116732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik///////////////////////////////////////////////////////////////////////////////
116832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
116932054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void Bitmap_setPixel(JNIEnv* env, jobject, jlong bitmapHandle,
117057ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        jint x, jint y, jint colorHandle) {
1171f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkBitmap bitmap;
1172f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
117332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    SkColor color = static_cast<SkColor>(colorHandle);
1174f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkAutoLockPixels alp(bitmap);
1175f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    if (NULL == bitmap.getPixels()) {
117632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        return;
117732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
117832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
1179f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    FromColorProc proc = ChooseFromColorProc(bitmap);
118032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    if (NULL == proc) {
118132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        return;
118232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
118332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
1184f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    proc(bitmap.getAddr(x, y), &color, 1, x, y);
1185f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    bitmap.notifyPixelsChanged();
118632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
118732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
118832054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void Bitmap_setPixels(JNIEnv* env, jobject, jlong bitmapHandle,
118932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        jintArray pixelArray, jint offset, jint stride,
119057ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        jint x, jint y, jint width, jint height) {
1191f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkBitmap bitmap;
1192f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
119332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    GraphicsJNI::SetPixels(env, pixelArray, offset, stride,
1194f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            x, y, width, height, bitmap);
119532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
119632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
119732054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void Bitmap_copyPixelsToBuffer(JNIEnv* env, jobject,
119832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                      jlong bitmapHandle, jobject jbuffer) {
1199f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkBitmap bitmap;
1200f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1201f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkAutoLockPixels alp(bitmap);
1202f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    const void* src = bitmap.getPixels();
120332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
120432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    if (NULL != src) {
120532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        android::AutoBufferPointer abp(env, jbuffer, JNI_TRUE);
120632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
120732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        // the java side has already checked that buffer is large enough
1208f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        memcpy(abp.pointer(), src, bitmap.getSize());
120932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
121032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
121132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
121232054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic void Bitmap_copyPixelsFromBuffer(JNIEnv* env, jobject,
121332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                        jlong bitmapHandle, jobject jbuffer) {
1214f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkBitmap bitmap;
1215f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1216f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkAutoLockPixels alp(bitmap);
1217f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    void* dst = bitmap.getPixels();
121832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
121932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    if (NULL != dst) {
122032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        android::AutoBufferPointer abp(env, jbuffer, JNI_FALSE);
122132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        // the java side has already checked that buffer is large enough
1222f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        memcpy(dst, abp.pointer(), bitmap.getSize());
1223f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        bitmap.notifyPixelsChanged();
122432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
122532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
122632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
122732054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic jboolean Bitmap_sameAs(JNIEnv* env, jobject, jlong bm0Handle,
122832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                              jlong bm1Handle) {
1229f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkBitmap bm0;
1230f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkBitmap bm1;
1231f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    reinterpret_cast<Bitmap*>(bm0Handle)->getSkBitmap(&bm0);
1232f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    reinterpret_cast<Bitmap*>(bm1Handle)->getSkBitmap(&bm1);
1233f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    if (bm0.width() != bm1.width() ||
1234f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        bm0.height() != bm1.height() ||
1235f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        bm0.colorType() != bm1.colorType()) {
123632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        return JNI_FALSE;
123732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
123832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
1239f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkAutoLockPixels alp0(bm0);
1240f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    SkAutoLockPixels alp1(bm1);
124132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
124232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    // if we can't load the pixels, return false
1243f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    if (NULL == bm0.getPixels() || NULL == bm1.getPixels()) {
124432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        return JNI_FALSE;
124532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
124632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
1247f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    if (bm0.colorType() == kIndex_8_SkColorType) {
1248f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        SkColorTable* ct0 = bm0.getColorTable();
1249f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        SkColorTable* ct1 = bm1.getColorTable();
125032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        if (NULL == ct0 || NULL == ct1) {
125132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik            return JNI_FALSE;
125232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        }
125332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        if (ct0->count() != ct1->count()) {
125432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik            return JNI_FALSE;
125532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        }
125632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
125732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        const size_t size = ct0->count() * sizeof(SkPMColor);
125871487eb0ceb2b7dea02649e78d99bb5952f5eaefMike Reed        if (memcmp(ct0->readColors(), ct1->readColors(), size) != 0) {
125932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik            return JNI_FALSE;
126032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        }
126132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
126232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
126332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    // now compare each scanline. We can't do the entire buffer at once,
126432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    // since we don't care about the pixel values that might extend beyond
126532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    // the width (since the scanline might be larger than the logical width)
1266f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    const int h = bm0.height();
1267f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    const size_t size = bm0.width() * bm0.bytesPerPixel();
126832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    for (int y = 0; y < h; y++) {
126953001ca3a629c0069f4fdac44f7711ad4a0c395ahenry.uh_chen        // SkBitmap::getAddr(int, int) may return NULL due to unrecognized config
127053001ca3a629c0069f4fdac44f7711ad4a0c395ahenry.uh_chen        // (ex: kRLE_Index8_Config). This will cause memcmp method to crash. Since bm0
127153001ca3a629c0069f4fdac44f7711ad4a0c395ahenry.uh_chen        // and bm1 both have pixel data() (have passed NULL == getPixels() check),
127253001ca3a629c0069f4fdac44f7711ad4a0c395ahenry.uh_chen        // those 2 bitmaps should be valid (only unrecognized), we return JNI_FALSE
127353001ca3a629c0069f4fdac44f7711ad4a0c395ahenry.uh_chen        // to warn user those 2 unrecognized config bitmaps may be different.
1274f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        void *bm0Addr = bm0.getAddr(0, y);
1275f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        void *bm1Addr = bm1.getAddr(0, y);
127653001ca3a629c0069f4fdac44f7711ad4a0c395ahenry.uh_chen
127753001ca3a629c0069f4fdac44f7711ad4a0c395ahenry.uh_chen        if(bm0Addr == NULL || bm1Addr == NULL) {
127853001ca3a629c0069f4fdac44f7711ad4a0c395ahenry.uh_chen            return JNI_FALSE;
127953001ca3a629c0069f4fdac44f7711ad4a0c395ahenry.uh_chen        }
128053001ca3a629c0069f4fdac44f7711ad4a0c395ahenry.uh_chen
128153001ca3a629c0069f4fdac44f7711ad4a0c395ahenry.uh_chen        if (memcmp(bm0Addr, bm1Addr, size) != 0) {
128232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik            return JNI_FALSE;
128332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        }
128432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    }
128532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    return JNI_TRUE;
128632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
128732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
1288c6e2e8ff474ae44bab5b9eb665851118abd27b68John Reckstatic jlong Bitmap_refPixelRef(JNIEnv* env, jobject, jlong bitmapHandle) {
1289f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    LocalScopedBitmap bitmap(bitmapHandle);
1290ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck    SkPixelRef* pixelRef = bitmap.valid() ? bitmap->peekAtPixelRef() : nullptr;
1291c6e2e8ff474ae44bab5b9eb665851118abd27b68John Reck    SkSafeRef(pixelRef);
1292c6e2e8ff474ae44bab5b9eb665851118abd27b68John Reck    return reinterpret_cast<jlong>(pixelRef);
1293c6e2e8ff474ae44bab5b9eb665851118abd27b68John Reck}
1294c6e2e8ff474ae44bab5b9eb665851118abd27b68John Reck
129532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik///////////////////////////////////////////////////////////////////////////////
129632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
129732054b0b3edb350a5444c47753b2982312dd7ffdChris Craikstatic JNINativeMethod gBitmapMethods[] = {
129832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    {   "nativeCreate",             "([IIIIIIZ)Landroid/graphics/Bitmap;",
129932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        (void*)Bitmap_creator },
130032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    {   "nativeCopy",               "(JIZ)Landroid/graphics/Bitmap;",
130132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        (void*)Bitmap_copy },
1302721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    {   "nativeCopyAshmem",         "(J)Landroid/graphics/Bitmap;",
1303721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews        (void*)Bitmap_copyAshmem },
130432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    {   "nativeDestructor",         "(J)V", (void*)Bitmap_destructor },
130532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    {   "nativeRecycle",            "(J)Z", (void*)Bitmap_recycle },
130617a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III    {   "nativeReconfigure",        "(JIIIIZ)V", (void*)Bitmap_reconfigure },
130732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    {   "nativeCompress",           "(JIILjava/io/OutputStream;[B)Z",
130832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        (void*)Bitmap_compress },
130932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    {   "nativeErase",              "(JI)V", (void*)Bitmap_erase },
131032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    {   "nativeRowBytes",           "(J)I", (void*)Bitmap_rowBytes },
131132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    {   "nativeConfig",             "(J)I", (void*)Bitmap_config },
131232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    {   "nativeHasAlpha",           "(J)Z", (void*)Bitmap_hasAlpha },
131357ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    {   "nativeIsPremultiplied",    "(J)Z", (void*)Bitmap_isPremultiplied},
131457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    {   "nativeSetHasAlpha",        "(JZZ)V", (void*)Bitmap_setHasAlpha},
131557ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    {   "nativeSetPremultiplied",   "(JZ)V", (void*)Bitmap_setPremultiplied},
131632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    {   "nativeHasMipMap",          "(J)Z", (void*)Bitmap_hasMipMap },
131732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    {   "nativeSetHasMipMap",       "(JZ)V", (void*)Bitmap_setHasMipMap },
131832054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    {   "nativeCreateFromParcel",
131932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        "(Landroid/os/Parcel;)Landroid/graphics/Bitmap;",
132032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        (void*)Bitmap_createFromParcel },
132132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    {   "nativeWriteToParcel",      "(JZILandroid/os/Parcel;)Z",
132232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        (void*)Bitmap_writeToParcel },
132332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    {   "nativeExtractAlpha",       "(JJ[I)Landroid/graphics/Bitmap;",
132432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik        (void*)Bitmap_extractAlpha },
132532054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    {   "nativeGenerationId",       "(J)I", (void*)Bitmap_getGenerationId },
132657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    {   "nativeGetPixel",           "(JII)I", (void*)Bitmap_getPixel },
132757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    {   "nativeGetPixels",          "(J[IIIIIII)V", (void*)Bitmap_getPixels },
132857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    {   "nativeSetPixel",           "(JIII)V", (void*)Bitmap_setPixel },
132957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    {   "nativeSetPixels",          "(J[IIIIIII)V", (void*)Bitmap_setPixels },
133032054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    {   "nativeCopyPixelsToBuffer", "(JLjava/nio/Buffer;)V",
133132054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                            (void*)Bitmap_copyPixelsToBuffer },
133232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    {   "nativeCopyPixelsFromBuffer", "(JLjava/nio/Buffer;)V",
133332054b0b3edb350a5444c47753b2982312dd7ffdChris Craik                                            (void*)Bitmap_copyPixelsFromBuffer },
133432054b0b3edb350a5444c47753b2982312dd7ffdChris Craik    {   "nativeSameAs",             "(JJ)Z", (void*)Bitmap_sameAs },
1335c6e2e8ff474ae44bab5b9eb665851118abd27b68John Reck    {   "nativeRefPixelRef",        "(J)J", (void*)Bitmap_refPixelRef },
133632054b0b3edb350a5444c47753b2982312dd7ffdChris Craik};
133732054b0b3edb350a5444c47753b2982312dd7ffdChris Craik
133832054b0b3edb350a5444c47753b2982312dd7ffdChris Craikint register_android_graphics_Bitmap(JNIEnv* env)
133932054b0b3edb350a5444c47753b2982312dd7ffdChris Craik{
1340ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    return android::RegisterMethodsOrDie(env, "android/graphics/Bitmap", gBitmapMethods,
1341ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe                                         NELEM(gBitmapMethods));
134232054b0b3edb350a5444c47753b2982312dd7ffdChris Craik}
1343