John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1 | #define LOG_TAG "Bitmap" |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 2 | #include "Bitmap.h" |
| 3 | |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 4 | #include "SkBitmap.h" |
| 5 | #include "SkPixelRef.h" |
| 6 | #include "SkImageEncoder.h" |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 7 | #include "SkImageInfo.h" |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 8 | #include "SkColorPriv.h" |
| 9 | #include "GraphicsJNI.h" |
| 10 | #include "SkDither.h" |
| 11 | #include "SkUnPreMultiply.h" |
| 12 | #include "SkStream.h" |
| 13 | |
| 14 | #include <binder/Parcel.h> |
| 15 | #include "android_os_Parcel.h" |
| 16 | #include "android_util_Binder.h" |
| 17 | #include "android_nio_utils.h" |
| 18 | #include "CreateJavaOutputStreamAdaptor.h" |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 19 | #include <Caches.h> |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 20 | #include <hwui/Paint.h> |
John Reck | 4387190 | 2016-08-01 14:39:24 -0700 | [diff] [blame] | 21 | #include <renderthread/RenderProxy.h> |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 22 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 23 | #include "core_jni_helpers.h" |
| 24 | |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 25 | #include <jni.h> |
Riley Andrews | 39d7f30 | 2014-11-13 17:43:25 -0800 | [diff] [blame] | 26 | #include <memory> |
| 27 | #include <string> |
| 28 | #include <sys/mman.h> |
| 29 | #include <cutils/ashmem.h> |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 30 | |
Jeff Brown | a316c5d | 2015-06-05 15:14:06 -0700 | [diff] [blame] | 31 | #define DEBUG_PARCEL 0 |
Riley Andrews | 8cee7c1 | 2015-11-01 23:36:04 -0800 | [diff] [blame] | 32 | #define ASHMEM_BITMAP_MIN_SIZE (128 * (1 << 10)) |
Jeff Brown | a316c5d | 2015-06-05 15:14:06 -0700 | [diff] [blame] | 33 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 34 | namespace android { |
| 35 | |
| 36 | class WrappedPixelRef : public SkPixelRef { |
| 37 | public: |
| 38 | WrappedPixelRef(Bitmap* wrapper, void* storage, |
| 39 | const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable) |
| 40 | : SkPixelRef(info) |
| 41 | , mBitmap(*wrapper) |
| 42 | , mStorage(storage) { |
| 43 | reconfigure(info, rowBytes, ctable); |
| 44 | } |
| 45 | |
| 46 | ~WrappedPixelRef() { |
| 47 | // Tell SkRefCnt that everything is as it expects by forcing |
| 48 | // the refcnt to 1 |
| 49 | internal_dispose_restore_refcnt_to_1(); |
| 50 | SkSafeUnref(mColorTable); |
| 51 | } |
| 52 | |
John Reck | 0781a2f | 2015-05-27 16:29:17 -0700 | [diff] [blame] | 53 | void reconfigure(const SkImageInfo& newInfo, size_t rowBytes, SkColorTable* ctable) { |
| 54 | if (kIndex_8_SkColorType != newInfo.colorType()) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 55 | ctable = nullptr; |
| 56 | } |
| 57 | mRowBytes = rowBytes; |
| 58 | if (mColorTable != ctable) { |
| 59 | SkSafeUnref(mColorTable); |
| 60 | mColorTable = ctable; |
| 61 | SkSafeRef(mColorTable); |
| 62 | } |
John Reck | 0781a2f | 2015-05-27 16:29:17 -0700 | [diff] [blame] | 63 | |
| 64 | // Need to validate the alpha type to filter against the color type |
| 65 | // to prevent things like a non-opaque RGB565 bitmap |
| 66 | SkAlphaType alphaType; |
| 67 | LOG_ALWAYS_FATAL_IF(!SkColorTypeValidateAlphaType( |
| 68 | newInfo.colorType(), newInfo.alphaType(), &alphaType), |
| 69 | "Failed to validate alpha type!"); |
| 70 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 71 | // Dirty hack is dirty |
| 72 | // TODO: Figure something out here, Skia's current design makes this |
| 73 | // really hard to work with. Skia really, really wants immutable objects, |
| 74 | // but with the nested-ref-count hackery going on that's just not |
| 75 | // feasible without going insane trying to figure it out |
| 76 | SkImageInfo* myInfo = const_cast<SkImageInfo*>(&this->info()); |
John Reck | 0781a2f | 2015-05-27 16:29:17 -0700 | [diff] [blame] | 77 | *myInfo = newInfo; |
| 78 | changeAlphaType(alphaType); |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 79 | |
| 80 | // Docs say to only call this in the ctor, but we're going to call |
| 81 | // it anyway even if this isn't always the ctor. |
| 82 | // TODO: Fix this too as part of the above TODO |
| 83 | setPreLocked(mStorage, mRowBytes, mColorTable); |
| 84 | } |
| 85 | |
| 86 | // Can't mark as override since SkPixelRef::rowBytes isn't virtual |
| 87 | // but that's OK since we just want BitmapWrapper to be able to rely |
| 88 | // on calling rowBytes() on an unlocked pixelref, which it will be |
| 89 | // doing on a WrappedPixelRef type, not a SkPixelRef, so static |
| 90 | // dispatching will do what we want. |
| 91 | size_t rowBytes() const { return mRowBytes; } |
| 92 | SkColorTable* colorTable() const { return mColorTable; } |
| 93 | |
| 94 | bool hasHardwareMipMap() const { |
| 95 | return mHasHardwareMipMap; |
| 96 | } |
| 97 | |
| 98 | void setHasHardwareMipMap(bool hasMipMap) { |
| 99 | mHasHardwareMipMap = hasMipMap; |
| 100 | } |
| 101 | |
| 102 | protected: |
| 103 | virtual bool onNewLockPixels(LockRec* rec) override { |
| 104 | rec->fPixels = mStorage; |
| 105 | rec->fRowBytes = mRowBytes; |
| 106 | rec->fColorTable = mColorTable; |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | virtual void onUnlockPixels() override { |
| 111 | // nothing |
| 112 | } |
| 113 | |
| 114 | virtual size_t getAllocatedSizeInBytes() const override { |
| 115 | return info().getSafeSize(mRowBytes); |
| 116 | } |
| 117 | |
| 118 | private: |
| 119 | Bitmap& mBitmap; |
| 120 | void* mStorage; |
| 121 | size_t mRowBytes = 0; |
| 122 | SkColorTable* mColorTable = nullptr; |
| 123 | bool mHasHardwareMipMap = false; |
| 124 | |
| 125 | virtual void internal_dispose() const override { |
| 126 | mBitmap.onStrongRefDestroyed(); |
| 127 | } |
| 128 | }; |
| 129 | |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame^] | 130 | Bitmap::Bitmap(void* address, size_t size, const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable) |
| 131 | : mPixelStorageType(PixelStorageType::Heap) { |
| 132 | mPixelStorage.heap.address = address; |
| 133 | mPixelStorage.heap.size = size; |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 134 | mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable)); |
| 135 | // Note: this will trigger a call to onStrongRefDestroyed(), but |
| 136 | // we want the pixel ref to have a ref count of 0 at this point |
| 137 | mPixelRef->unref(); |
| 138 | } |
| 139 | |
| 140 | Bitmap::Bitmap(void* address, void* context, FreeFunc freeFunc, |
| 141 | const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable) |
| 142 | : mPixelStorageType(PixelStorageType::External) { |
| 143 | mPixelStorage.external.address = address; |
| 144 | mPixelStorage.external.context = context; |
| 145 | mPixelStorage.external.freeFunc = freeFunc; |
| 146 | mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable)); |
| 147 | // Note: this will trigger a call to onStrongRefDestroyed(), but |
| 148 | // we want the pixel ref to have a ref count of 0 at this point |
| 149 | mPixelRef->unref(); |
| 150 | } |
| 151 | |
John Reck | 003bdee | 2016-09-12 10:43:35 -0700 | [diff] [blame] | 152 | Bitmap::Bitmap(void* address, int fd, size_t mappedSize, |
Riley Andrews | 39d7f30 | 2014-11-13 17:43:25 -0800 | [diff] [blame] | 153 | const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable) |
| 154 | : mPixelStorageType(PixelStorageType::Ashmem) { |
| 155 | mPixelStorage.ashmem.address = address; |
| 156 | mPixelStorage.ashmem.fd = fd; |
John Reck | 003bdee | 2016-09-12 10:43:35 -0700 | [diff] [blame] | 157 | mPixelStorage.ashmem.size = mappedSize; |
Riley Andrews | 39d7f30 | 2014-11-13 17:43:25 -0800 | [diff] [blame] | 158 | mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable)); |
| 159 | // Note: this will trigger a call to onStrongRefDestroyed(), but |
| 160 | // we want the pixel ref to have a ref count of 0 at this point |
| 161 | mPixelRef->unref(); |
| 162 | } |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 163 | Bitmap::~Bitmap() { |
| 164 | doFreePixels(); |
| 165 | } |
| 166 | |
| 167 | void Bitmap::freePixels() { |
| 168 | AutoMutex _lock(mLock); |
| 169 | if (mPinnedRefCount == 0) { |
| 170 | doFreePixels(); |
| 171 | mPixelStorageType = PixelStorageType::Invalid; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void Bitmap::doFreePixels() { |
| 176 | switch (mPixelStorageType) { |
| 177 | case PixelStorageType::Invalid: |
| 178 | // already free'd, nothing to do |
| 179 | break; |
| 180 | case PixelStorageType::External: |
| 181 | mPixelStorage.external.freeFunc(mPixelStorage.external.address, |
| 182 | mPixelStorage.external.context); |
| 183 | break; |
Riley Andrews | 39d7f30 | 2014-11-13 17:43:25 -0800 | [diff] [blame] | 184 | case PixelStorageType::Ashmem: |
| 185 | munmap(mPixelStorage.ashmem.address, mPixelStorage.ashmem.size); |
| 186 | close(mPixelStorage.ashmem.fd); |
| 187 | break; |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame^] | 188 | case PixelStorageType::Heap: |
| 189 | free(mPixelStorage.heap.address); |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 190 | break; |
| 191 | } |
| 192 | |
| 193 | if (android::uirenderer::Caches::hasInstance()) { |
| 194 | android::uirenderer::Caches::getInstance().textureCache.releaseTexture( |
| 195 | mPixelRef->getStableID()); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | bool Bitmap::hasHardwareMipMap() { |
| 200 | return mPixelRef->hasHardwareMipMap(); |
| 201 | } |
| 202 | |
| 203 | void Bitmap::setHasHardwareMipMap(bool hasMipMap) { |
| 204 | mPixelRef->setHasHardwareMipMap(hasMipMap); |
| 205 | } |
| 206 | |
Riley Andrews | 39d7f30 | 2014-11-13 17:43:25 -0800 | [diff] [blame] | 207 | int Bitmap::getAshmemFd() const { |
| 208 | switch (mPixelStorageType) { |
| 209 | case PixelStorageType::Ashmem: |
| 210 | return mPixelStorage.ashmem.fd; |
| 211 | default: |
| 212 | return -1; |
| 213 | } |
| 214 | } |
| 215 | |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame^] | 216 | size_t Bitmap::getAllocationByteCount() const { |
| 217 | switch (mPixelStorageType) { |
| 218 | case PixelStorageType::Heap: |
| 219 | return mPixelStorage.heap.size; |
| 220 | default: |
| 221 | return rowBytes() * height(); |
| 222 | } |
| 223 | } |
| 224 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 225 | const SkImageInfo& Bitmap::info() const { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 226 | return mPixelRef->info(); |
| 227 | } |
| 228 | |
| 229 | size_t Bitmap::rowBytes() const { |
| 230 | return mPixelRef->rowBytes(); |
| 231 | } |
| 232 | |
John Reck | ae2e8b4 | 2015-05-06 14:55:05 -0700 | [diff] [blame] | 233 | SkPixelRef* Bitmap::peekAtPixelRef() const { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 234 | assertValid(); |
| 235 | return mPixelRef.get(); |
| 236 | } |
| 237 | |
John Reck | ae2e8b4 | 2015-05-06 14:55:05 -0700 | [diff] [blame] | 238 | SkPixelRef* Bitmap::refPixelRef() { |
| 239 | assertValid(); |
| 240 | android::AutoMutex _lock(mLock); |
| 241 | return refPixelRefLocked(); |
| 242 | } |
| 243 | |
| 244 | SkPixelRef* Bitmap::refPixelRefLocked() { |
| 245 | mPixelRef->ref(); |
| 246 | if (mPixelRef->unique()) { |
| 247 | // We just restored this from 0, pin the pixels and inc the strong count |
| 248 | // Note that there *might be* an incoming onStrongRefDestroyed from whatever |
| 249 | // last unref'd |
John Reck | ae2e8b4 | 2015-05-06 14:55:05 -0700 | [diff] [blame] | 250 | mPinnedRefCount++; |
| 251 | } |
| 252 | return mPixelRef.get(); |
| 253 | } |
| 254 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 255 | void Bitmap::reconfigure(const SkImageInfo& info, size_t rowBytes, |
| 256 | SkColorTable* ctable) { |
| 257 | mPixelRef->reconfigure(info, rowBytes, ctable); |
| 258 | } |
| 259 | |
| 260 | void Bitmap::reconfigure(const SkImageInfo& info) { |
Derek Sollenberger | 2a94a10 | 2015-05-07 13:56:14 -0400 | [diff] [blame] | 261 | reconfigure(info, info.minRowBytes(), nullptr); |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 262 | } |
| 263 | |
John Reck | 0781a2f | 2015-05-27 16:29:17 -0700 | [diff] [blame] | 264 | void Bitmap::setAlphaType(SkAlphaType alphaType) { |
| 265 | if (!SkColorTypeValidateAlphaType(info().colorType(), alphaType, &alphaType)) { |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | mPixelRef->changeAlphaType(alphaType); |
| 270 | } |
| 271 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 272 | void Bitmap::detachFromJava() { |
| 273 | bool disposeSelf; |
| 274 | { |
| 275 | android::AutoMutex _lock(mLock); |
| 276 | mAttachedToJava = false; |
| 277 | disposeSelf = shouldDisposeSelfLocked(); |
| 278 | } |
| 279 | if (disposeSelf) { |
| 280 | delete this; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | bool Bitmap::shouldDisposeSelfLocked() { |
| 285 | return mPinnedRefCount == 0 && !mAttachedToJava; |
| 286 | } |
| 287 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 288 | |
| 289 | void Bitmap::onStrongRefDestroyed() { |
| 290 | bool disposeSelf = false; |
| 291 | { |
| 292 | android::AutoMutex _lock(mLock); |
| 293 | if (mPinnedRefCount > 0) { |
| 294 | mPinnedRefCount--; |
| 295 | if (mPinnedRefCount == 0) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 296 | disposeSelf = shouldDisposeSelfLocked(); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | if (disposeSelf) { |
| 301 | delete this; |
| 302 | } |
| 303 | } |
| 304 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 305 | void Bitmap::getSkBitmap(SkBitmap* outBitmap) { |
| 306 | assertValid(); |
| 307 | android::AutoMutex _lock(mLock); |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 308 | // Safe because mPixelRef is a WrappedPixelRef type, otherwise rowBytes() |
| 309 | // would require locking the pixels first. |
| 310 | outBitmap->setInfo(mPixelRef->info(), mPixelRef->rowBytes()); |
John Reck | ae2e8b4 | 2015-05-06 14:55:05 -0700 | [diff] [blame] | 311 | outBitmap->setPixelRef(refPixelRefLocked())->unref(); |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 312 | outBitmap->setHasHardwareMipMap(hasHardwareMipMap()); |
| 313 | } |
| 314 | |
| 315 | void Bitmap::assertValid() const { |
| 316 | LOG_ALWAYS_FATAL_IF(mPixelStorageType == PixelStorageType::Invalid, |
| 317 | "Error, cannot access an invalid/free'd bitmap here!"); |
| 318 | } |
| 319 | |
| 320 | } // namespace android |
| 321 | |
| 322 | using namespace android; |
| 323 | |
| 324 | // Convenience class that does not take a global ref on the pixels, relying |
| 325 | // on the caller already having a local JNI ref |
| 326 | class LocalScopedBitmap { |
| 327 | public: |
Chih-Hung Hsieh | c6baf56 | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 328 | explicit LocalScopedBitmap(jlong bitmapHandle) |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 329 | : mBitmap(reinterpret_cast<Bitmap*>(bitmapHandle)) {} |
| 330 | |
| 331 | Bitmap* operator->() { |
| 332 | return mBitmap; |
| 333 | } |
| 334 | |
| 335 | void* pixels() { |
John Reck | ae2e8b4 | 2015-05-06 14:55:05 -0700 | [diff] [blame] | 336 | return mBitmap->peekAtPixelRef()->pixels(); |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | bool valid() { |
| 340 | return mBitmap && mBitmap->valid(); |
| 341 | } |
| 342 | |
| 343 | private: |
| 344 | Bitmap* mBitmap; |
| 345 | }; |
| 346 | |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 347 | /////////////////////////////////////////////////////////////////////////////// |
| 348 | // Conversions to/from SkColor, for get/setPixels, and the create method, which |
| 349 | // is basically like setPixels |
| 350 | |
| 351 | typedef void (*FromColorProc)(void* dst, const SkColor src[], int width, |
| 352 | int x, int y); |
| 353 | |
| 354 | static void FromColor_D32(void* dst, const SkColor src[], int width, |
| 355 | int, int) { |
| 356 | SkPMColor* d = (SkPMColor*)dst; |
| 357 | |
| 358 | for (int i = 0; i < width; i++) { |
| 359 | *d++ = SkPreMultiplyColor(*src++); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | static void FromColor_D32_Raw(void* dst, const SkColor src[], int width, |
| 364 | int, int) { |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 365 | // Needed to thwart the unreachable code detection from clang. |
| 366 | static const bool sk_color_ne_zero = SK_COLOR_MATCHES_PMCOLOR_BYTE_ORDER; |
| 367 | |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 368 | // SkColor's ordering may be different from SkPMColor |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 369 | if (sk_color_ne_zero) { |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 370 | memcpy(dst, src, width * sizeof(SkColor)); |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | // order isn't same, repack each pixel manually |
| 375 | SkPMColor* d = (SkPMColor*)dst; |
| 376 | for (int i = 0; i < width; i++) { |
| 377 | SkColor c = *src++; |
| 378 | *d++ = SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c), |
| 379 | SkColorGetG(c), SkColorGetB(c)); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | static void FromColor_D565(void* dst, const SkColor src[], int width, |
| 384 | int x, int y) { |
| 385 | uint16_t* d = (uint16_t*)dst; |
| 386 | |
| 387 | DITHER_565_SCAN(y); |
| 388 | for (int stop = x + width; x < stop; x++) { |
| 389 | SkColor c = *src++; |
| 390 | *d++ = SkDitherRGBTo565(SkColorGetR(c), SkColorGetG(c), SkColorGetB(c), |
| 391 | DITHER_VALUE(x)); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | static void FromColor_D4444(void* dst, const SkColor src[], int width, |
| 396 | int x, int y) { |
| 397 | SkPMColor16* d = (SkPMColor16*)dst; |
| 398 | |
| 399 | DITHER_4444_SCAN(y); |
| 400 | for (int stop = x + width; x < stop; x++) { |
| 401 | SkPMColor pmc = SkPreMultiplyColor(*src++); |
| 402 | *d++ = SkDitherARGB32To4444(pmc, DITHER_VALUE(x)); |
| 403 | // *d++ = SkPixel32ToPixel4444(pmc); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | static void FromColor_D4444_Raw(void* dst, const SkColor src[], int width, |
| 408 | int x, int y) { |
| 409 | SkPMColor16* d = (SkPMColor16*)dst; |
| 410 | |
| 411 | DITHER_4444_SCAN(y); |
| 412 | for (int stop = x + width; x < stop; x++) { |
| 413 | SkColor c = *src++; |
| 414 | |
| 415 | // SkPMColor is used because the ordering is ARGB32, even though the target actually premultiplied |
| 416 | SkPMColor pmc = SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c), |
| 417 | SkColorGetG(c), SkColorGetB(c)); |
| 418 | *d++ = SkDitherARGB32To4444(pmc, DITHER_VALUE(x)); |
| 419 | // *d++ = SkPixel32ToPixel4444(pmc); |
| 420 | } |
| 421 | } |
| 422 | |
Chris Craik | 6260b22 | 2015-07-24 15:17:29 -0700 | [diff] [blame] | 423 | static void FromColor_DA8(void* dst, const SkColor src[], int width, int x, int y) { |
| 424 | uint8_t* d = (uint8_t*)dst; |
| 425 | |
| 426 | for (int stop = x + width; x < stop; x++) { |
| 427 | *d++ = SkColorGetA(*src++); |
| 428 | } |
| 429 | } |
| 430 | |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 431 | // can return NULL |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 432 | static FromColorProc ChooseFromColorProc(const SkBitmap& bitmap) { |
| 433 | switch (bitmap.colorType()) { |
| 434 | case kN32_SkColorType: |
| 435 | return bitmap.alphaType() == kPremul_SkAlphaType ? FromColor_D32 : FromColor_D32_Raw; |
| 436 | case kARGB_4444_SkColorType: |
| 437 | return bitmap.alphaType() == kPremul_SkAlphaType ? FromColor_D4444 : |
| 438 | FromColor_D4444_Raw; |
| 439 | case kRGB_565_SkColorType: |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 440 | return FromColor_D565; |
Chris Craik | 6260b22 | 2015-07-24 15:17:29 -0700 | [diff] [blame] | 441 | case kAlpha_8_SkColorType: |
| 442 | return FromColor_DA8; |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 443 | default: |
| 444 | break; |
| 445 | } |
| 446 | return NULL; |
| 447 | } |
| 448 | |
| 449 | bool GraphicsJNI::SetPixels(JNIEnv* env, jintArray srcColors, int srcOffset, int srcStride, |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 450 | int x, int y, int width, int height, const SkBitmap& dstBitmap) { |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 451 | SkAutoLockPixels alp(dstBitmap); |
| 452 | void* dst = dstBitmap.getPixels(); |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 453 | FromColorProc proc = ChooseFromColorProc(dstBitmap); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 454 | |
| 455 | if (NULL == dst || NULL == proc) { |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | const jint* array = env->GetIntArrayElements(srcColors, NULL); |
| 460 | const SkColor* src = (const SkColor*)array + srcOffset; |
| 461 | |
| 462 | // reset to to actual choice from caller |
| 463 | dst = dstBitmap.getAddr(x, y); |
| 464 | // now copy/convert each scanline |
| 465 | for (int y = 0; y < height; y++) { |
| 466 | proc(dst, src, width, x, y); |
| 467 | src += srcStride; |
| 468 | dst = (char*)dst + dstBitmap.rowBytes(); |
| 469 | } |
| 470 | |
| 471 | dstBitmap.notifyPixelsChanged(); |
| 472 | |
| 473 | env->ReleaseIntArrayElements(srcColors, const_cast<jint*>(array), |
| 474 | JNI_ABORT); |
| 475 | return true; |
| 476 | } |
| 477 | |
| 478 | //////////////////// ToColor procs |
| 479 | |
| 480 | typedef void (*ToColorProc)(SkColor dst[], const void* src, int width, |
| 481 | SkColorTable*); |
| 482 | |
| 483 | static void ToColor_S32_Alpha(SkColor dst[], const void* src, int width, |
| 484 | SkColorTable*) { |
| 485 | SkASSERT(width > 0); |
| 486 | const SkPMColor* s = (const SkPMColor*)src; |
| 487 | do { |
| 488 | *dst++ = SkUnPreMultiply::PMColorToColor(*s++); |
| 489 | } while (--width != 0); |
| 490 | } |
| 491 | |
| 492 | static void ToColor_S32_Raw(SkColor dst[], const void* src, int width, |
| 493 | SkColorTable*) { |
| 494 | SkASSERT(width > 0); |
| 495 | const SkPMColor* s = (const SkPMColor*)src; |
| 496 | do { |
| 497 | SkPMColor c = *s++; |
| 498 | *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c), |
| 499 | SkGetPackedG32(c), SkGetPackedB32(c)); |
| 500 | } while (--width != 0); |
| 501 | } |
| 502 | |
| 503 | static void ToColor_S32_Opaque(SkColor dst[], const void* src, int width, |
| 504 | SkColorTable*) { |
| 505 | SkASSERT(width > 0); |
| 506 | const SkPMColor* s = (const SkPMColor*)src; |
| 507 | do { |
| 508 | SkPMColor c = *s++; |
| 509 | *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c), |
| 510 | SkGetPackedB32(c)); |
| 511 | } while (--width != 0); |
| 512 | } |
| 513 | |
| 514 | static void ToColor_S4444_Alpha(SkColor dst[], const void* src, int width, |
| 515 | SkColorTable*) { |
| 516 | SkASSERT(width > 0); |
| 517 | const SkPMColor16* s = (const SkPMColor16*)src; |
| 518 | do { |
| 519 | *dst++ = SkUnPreMultiply::PMColorToColor(SkPixel4444ToPixel32(*s++)); |
| 520 | } while (--width != 0); |
| 521 | } |
| 522 | |
| 523 | static void ToColor_S4444_Raw(SkColor dst[], const void* src, int width, |
| 524 | SkColorTable*) { |
| 525 | SkASSERT(width > 0); |
| 526 | const SkPMColor16* s = (const SkPMColor16*)src; |
| 527 | do { |
| 528 | SkPMColor c = SkPixel4444ToPixel32(*s++); |
| 529 | *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c), |
| 530 | SkGetPackedG32(c), SkGetPackedB32(c)); |
| 531 | } while (--width != 0); |
| 532 | } |
| 533 | |
| 534 | static void ToColor_S4444_Opaque(SkColor dst[], const void* src, int width, |
| 535 | SkColorTable*) { |
| 536 | SkASSERT(width > 0); |
| 537 | const SkPMColor16* s = (const SkPMColor16*)src; |
| 538 | do { |
| 539 | SkPMColor c = SkPixel4444ToPixel32(*s++); |
| 540 | *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c), |
| 541 | SkGetPackedB32(c)); |
| 542 | } while (--width != 0); |
| 543 | } |
| 544 | |
| 545 | static void ToColor_S565(SkColor dst[], const void* src, int width, |
| 546 | SkColorTable*) { |
| 547 | SkASSERT(width > 0); |
| 548 | const uint16_t* s = (const uint16_t*)src; |
| 549 | do { |
| 550 | uint16_t c = *s++; |
| 551 | *dst++ = SkColorSetRGB(SkPacked16ToR32(c), SkPacked16ToG32(c), |
| 552 | SkPacked16ToB32(c)); |
| 553 | } while (--width != 0); |
| 554 | } |
| 555 | |
| 556 | static void ToColor_SI8_Alpha(SkColor dst[], const void* src, int width, |
| 557 | SkColorTable* ctable) { |
| 558 | SkASSERT(width > 0); |
| 559 | const uint8_t* s = (const uint8_t*)src; |
Mike Reed | 71487eb | 2014-11-19 16:13:20 -0500 | [diff] [blame] | 560 | const SkPMColor* colors = ctable->readColors(); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 561 | do { |
| 562 | *dst++ = SkUnPreMultiply::PMColorToColor(colors[*s++]); |
| 563 | } while (--width != 0); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | static void ToColor_SI8_Raw(SkColor dst[], const void* src, int width, |
| 567 | SkColorTable* ctable) { |
| 568 | SkASSERT(width > 0); |
| 569 | const uint8_t* s = (const uint8_t*)src; |
Mike Reed | 71487eb | 2014-11-19 16:13:20 -0500 | [diff] [blame] | 570 | const SkPMColor* colors = ctable->readColors(); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 571 | do { |
| 572 | SkPMColor c = colors[*s++]; |
| 573 | *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c), |
| 574 | SkGetPackedG32(c), SkGetPackedB32(c)); |
| 575 | } while (--width != 0); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | static void ToColor_SI8_Opaque(SkColor dst[], const void* src, int width, |
| 579 | SkColorTable* ctable) { |
| 580 | SkASSERT(width > 0); |
| 581 | const uint8_t* s = (const uint8_t*)src; |
Mike Reed | 71487eb | 2014-11-19 16:13:20 -0500 | [diff] [blame] | 582 | const SkPMColor* colors = ctable->readColors(); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 583 | do { |
| 584 | SkPMColor c = colors[*s++]; |
| 585 | *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c), |
| 586 | SkGetPackedB32(c)); |
| 587 | } while (--width != 0); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Chris Craik | 6260b22 | 2015-07-24 15:17:29 -0700 | [diff] [blame] | 590 | static void ToColor_SA8(SkColor dst[], const void* src, int width, SkColorTable*) { |
| 591 | SkASSERT(width > 0); |
| 592 | const uint8_t* s = (const uint8_t*)src; |
| 593 | do { |
| 594 | uint8_t c = *s++; |
| 595 | *dst++ = SkColorSetARGB(c, c, c, c); |
| 596 | } while (--width != 0); |
| 597 | } |
| 598 | |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 599 | // can return NULL |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 600 | static ToColorProc ChooseToColorProc(const SkBitmap& src) { |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 601 | switch (src.colorType()) { |
| 602 | case kN32_SkColorType: |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 603 | switch (src.alphaType()) { |
| 604 | case kOpaque_SkAlphaType: |
| 605 | return ToColor_S32_Opaque; |
| 606 | case kPremul_SkAlphaType: |
| 607 | return ToColor_S32_Alpha; |
| 608 | case kUnpremul_SkAlphaType: |
| 609 | return ToColor_S32_Raw; |
| 610 | default: |
| 611 | return NULL; |
| 612 | } |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 613 | case kARGB_4444_SkColorType: |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 614 | switch (src.alphaType()) { |
| 615 | case kOpaque_SkAlphaType: |
| 616 | return ToColor_S4444_Opaque; |
| 617 | case kPremul_SkAlphaType: |
| 618 | return ToColor_S4444_Alpha; |
| 619 | case kUnpremul_SkAlphaType: |
| 620 | return ToColor_S4444_Raw; |
| 621 | default: |
| 622 | return NULL; |
| 623 | } |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 624 | case kRGB_565_SkColorType: |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 625 | return ToColor_S565; |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 626 | case kIndex_8_SkColorType: |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 627 | if (src.getColorTable() == NULL) { |
| 628 | return NULL; |
| 629 | } |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 630 | switch (src.alphaType()) { |
| 631 | case kOpaque_SkAlphaType: |
| 632 | return ToColor_SI8_Opaque; |
| 633 | case kPremul_SkAlphaType: |
| 634 | return ToColor_SI8_Alpha; |
| 635 | case kUnpremul_SkAlphaType: |
| 636 | return ToColor_SI8_Raw; |
| 637 | default: |
| 638 | return NULL; |
| 639 | } |
Chris Craik | 6260b22 | 2015-07-24 15:17:29 -0700 | [diff] [blame] | 640 | case kAlpha_8_SkColorType: |
| 641 | return ToColor_SA8; |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 642 | default: |
| 643 | break; |
| 644 | } |
| 645 | return NULL; |
| 646 | } |
| 647 | |
| 648 | /////////////////////////////////////////////////////////////////////////////// |
| 649 | /////////////////////////////////////////////////////////////////////////////// |
| 650 | |
| 651 | static int getPremulBitmapCreateFlags(bool isMutable) { |
| 652 | int flags = GraphicsJNI::kBitmapCreateFlag_Premultiplied; |
| 653 | if (isMutable) flags |= GraphicsJNI::kBitmapCreateFlag_Mutable; |
| 654 | return flags; |
| 655 | } |
| 656 | |
| 657 | static jobject Bitmap_creator(JNIEnv* env, jobject, jintArray jColors, |
| 658 | jint offset, jint stride, jint width, jint height, |
| 659 | jint configHandle, jboolean isMutable) { |
Mike Reed | 1103b32 | 2014-07-08 12:36:44 -0400 | [diff] [blame] | 660 | SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 661 | if (NULL != jColors) { |
| 662 | size_t n = env->GetArrayLength(jColors); |
| 663 | if (n < SkAbs32(stride) * (size_t)height) { |
| 664 | doThrowAIOOBE(env); |
| 665 | return NULL; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | // ARGB_4444 is a deprecated format, convert automatically to 8888 |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 670 | if (colorType == kARGB_4444_SkColorType) { |
| 671 | colorType = kN32_SkColorType; |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | SkBitmap bitmap; |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 675 | bitmap.setInfo(SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType)); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 676 | |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame^] | 677 | Bitmap* nativeBitmap = GraphicsJNI::allocateHeapPixelRef(&bitmap, NULL); |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 678 | if (!nativeBitmap) { |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 679 | return NULL; |
| 680 | } |
| 681 | |
| 682 | if (jColors != NULL) { |
| 683 | GraphicsJNI::SetPixels(env, jColors, offset, stride, |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 684 | 0, 0, width, height, bitmap); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 685 | } |
| 686 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 687 | return GraphicsJNI::createBitmap(env, nativeBitmap, |
| 688 | getPremulBitmapCreateFlags(isMutable)); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | static jobject Bitmap_copy(JNIEnv* env, jobject, jlong srcHandle, |
| 692 | jint dstConfigHandle, jboolean isMutable) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 693 | SkBitmap src; |
| 694 | reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src); |
Mike Reed | 1103b32 | 2014-07-08 12:36:44 -0400 | [diff] [blame] | 695 | SkColorType dstCT = GraphicsJNI::legacyBitmapConfigToColorType(dstConfigHandle); |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame^] | 696 | SkBitmap result; |
| 697 | HeapAllocator allocator; |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 698 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 699 | if (!src.copyTo(&result, dstCT, &allocator)) { |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 700 | return NULL; |
| 701 | } |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 702 | Bitmap* bitmap = allocator.getStorageObjAndReset(); |
| 703 | return GraphicsJNI::createBitmap(env, bitmap, |
| 704 | getPremulBitmapCreateFlags(isMutable)); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Winson | a5fdde9 | 2016-04-14 15:27:15 -0700 | [diff] [blame] | 707 | static Bitmap* Bitmap_copyAshmemImpl(JNIEnv* env, SkBitmap& src, SkColorType& dstCT) { |
Riley Andrews | 721ae5f | 2015-05-11 16:08:22 -0700 | [diff] [blame] | 708 | SkBitmap result; |
| 709 | |
| 710 | AshmemPixelAllocator allocator(env); |
Winson | a5fdde9 | 2016-04-14 15:27:15 -0700 | [diff] [blame] | 711 | if (!src.copyTo(&result, dstCT, &allocator)) { |
Riley Andrews | 721ae5f | 2015-05-11 16:08:22 -0700 | [diff] [blame] | 712 | return NULL; |
| 713 | } |
| 714 | Bitmap* bitmap = allocator.getStorageObjAndReset(); |
| 715 | bitmap->peekAtPixelRef()->setImmutable(); |
Winson | a5fdde9 | 2016-04-14 15:27:15 -0700 | [diff] [blame] | 716 | return bitmap; |
| 717 | } |
| 718 | |
| 719 | static jobject Bitmap_copyAshmem(JNIEnv* env, jobject, jlong srcHandle) { |
| 720 | SkBitmap src; |
| 721 | reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src); |
| 722 | SkColorType dstCT = src.colorType(); |
| 723 | Bitmap* bitmap = Bitmap_copyAshmemImpl(env, src, dstCT); |
| 724 | jobject ret = GraphicsJNI::createBitmap(env, bitmap, getPremulBitmapCreateFlags(false)); |
| 725 | return ret; |
| 726 | } |
| 727 | |
| 728 | static jobject Bitmap_copyAshmemConfig(JNIEnv* env, jobject, jlong srcHandle, jint dstConfigHandle) { |
| 729 | SkBitmap src; |
| 730 | reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src); |
| 731 | SkColorType dstCT = GraphicsJNI::legacyBitmapConfigToColorType(dstConfigHandle); |
| 732 | Bitmap* bitmap = Bitmap_copyAshmemImpl(env, src, dstCT); |
Riley Andrews | 721ae5f | 2015-05-11 16:08:22 -0700 | [diff] [blame] | 733 | jobject ret = GraphicsJNI::createBitmap(env, bitmap, getPremulBitmapCreateFlags(false)); |
| 734 | return ret; |
| 735 | } |
| 736 | |
Richard Uhler | 775873a | 2015-12-29 12:37:39 -0800 | [diff] [blame] | 737 | static void Bitmap_destruct(Bitmap* bitmap) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 738 | bitmap->detachFromJava(); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Richard Uhler | 775873a | 2015-12-29 12:37:39 -0800 | [diff] [blame] | 741 | static jlong Bitmap_getNativeFinalizer(JNIEnv*, jobject) { |
| 742 | return static_cast<jlong>(reinterpret_cast<uintptr_t>(&Bitmap_destruct)); |
| 743 | } |
| 744 | |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 745 | static jboolean Bitmap_recycle(JNIEnv* env, jobject, jlong bitmapHandle) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 746 | LocalScopedBitmap bitmap(bitmapHandle); |
| 747 | bitmap->freePixels(); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 748 | return JNI_TRUE; |
| 749 | } |
| 750 | |
| 751 | static void Bitmap_reconfigure(JNIEnv* env, jobject clazz, jlong bitmapHandle, |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame^] | 752 | jint width, jint height, jint configHandle, jboolean requestPremul) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 753 | LocalScopedBitmap bitmap(bitmapHandle); |
Mike Reed | 1103b32 | 2014-07-08 12:36:44 -0400 | [diff] [blame] | 754 | SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle); |
Leon Scroggins III | 17a8bfc | 2014-06-03 16:15:15 -0400 | [diff] [blame] | 755 | |
| 756 | // ARGB_4444 is a deprecated format, convert automatically to 8888 |
| 757 | if (colorType == kARGB_4444_SkColorType) { |
| 758 | colorType = kN32_SkColorType; |
| 759 | } |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame^] | 760 | size_t requestedSize = width * height * SkColorTypeBytesPerPixel(colorType); |
| 761 | if (requestedSize > bitmap->getAllocationByteCount()) { |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 762 | // done in native as there's no way to get BytesPerPixel in Java |
| 763 | doThrowIAE(env, "Bitmap not large enough to support new configuration"); |
| 764 | return; |
| 765 | } |
Leon Scroggins III | 17a8bfc | 2014-06-03 16:15:15 -0400 | [diff] [blame] | 766 | SkAlphaType alphaType; |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 767 | if (bitmap->info().colorType() != kRGB_565_SkColorType |
| 768 | && bitmap->info().alphaType() == kOpaque_SkAlphaType) { |
Leon Scroggins III | 17a8bfc | 2014-06-03 16:15:15 -0400 | [diff] [blame] | 769 | // If the original bitmap was set to opaque, keep that setting, unless it |
| 770 | // was 565, which is required to be opaque. |
| 771 | alphaType = kOpaque_SkAlphaType; |
| 772 | } else { |
| 773 | // Otherwise respect the premultiplied request. |
| 774 | alphaType = requestPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType; |
| 775 | } |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 776 | bitmap->reconfigure(SkImageInfo::Make(width, height, colorType, alphaType)); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | // These must match the int values in Bitmap.java |
| 780 | enum JavaEncodeFormat { |
| 781 | kJPEG_JavaEncodeFormat = 0, |
| 782 | kPNG_JavaEncodeFormat = 1, |
| 783 | kWEBP_JavaEncodeFormat = 2 |
| 784 | }; |
| 785 | |
| 786 | static jboolean Bitmap_compress(JNIEnv* env, jobject clazz, jlong bitmapHandle, |
| 787 | jint format, jint quality, |
| 788 | jobject jstream, jbyteArray jstorage) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 789 | |
| 790 | LocalScopedBitmap bitmap(bitmapHandle); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 791 | SkImageEncoder::Type fm; |
| 792 | |
| 793 | switch (format) { |
| 794 | case kJPEG_JavaEncodeFormat: |
| 795 | fm = SkImageEncoder::kJPEG_Type; |
| 796 | break; |
| 797 | case kPNG_JavaEncodeFormat: |
| 798 | fm = SkImageEncoder::kPNG_Type; |
| 799 | break; |
| 800 | case kWEBP_JavaEncodeFormat: |
| 801 | fm = SkImageEncoder::kWEBP_Type; |
| 802 | break; |
| 803 | default: |
| 804 | return JNI_FALSE; |
| 805 | } |
| 806 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 807 | if (!bitmap.valid()) { |
| 808 | return JNI_FALSE; |
| 809 | } |
| 810 | |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 811 | bool success = false; |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 812 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 813 | std::unique_ptr<SkWStream> strm(CreateJavaOutputStreamAdaptor(env, jstream, jstorage)); |
| 814 | if (!strm.get()) { |
| 815 | return JNI_FALSE; |
| 816 | } |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 817 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 818 | std::unique_ptr<SkImageEncoder> encoder(SkImageEncoder::Create(fm)); |
| 819 | if (encoder.get()) { |
| 820 | SkBitmap skbitmap; |
| 821 | bitmap->getSkBitmap(&skbitmap); |
| 822 | success = encoder->encodeStream(strm.get(), skbitmap, quality); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 823 | } |
| 824 | return success ? JNI_TRUE : JNI_FALSE; |
| 825 | } |
| 826 | |
| 827 | static void Bitmap_erase(JNIEnv* env, jobject, jlong bitmapHandle, jint color) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 828 | LocalScopedBitmap bitmap(bitmapHandle); |
| 829 | SkBitmap skBitmap; |
| 830 | bitmap->getSkBitmap(&skBitmap); |
| 831 | skBitmap.eraseColor(color); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | static jint Bitmap_rowBytes(JNIEnv* env, jobject, jlong bitmapHandle) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 835 | LocalScopedBitmap bitmap(bitmapHandle); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 836 | return static_cast<jint>(bitmap->rowBytes()); |
| 837 | } |
| 838 | |
| 839 | static jint Bitmap_config(JNIEnv* env, jobject, jlong bitmapHandle) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 840 | LocalScopedBitmap bitmap(bitmapHandle); |
| 841 | return GraphicsJNI::colorTypeToLegacyBitmapConfig(bitmap->info().colorType()); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | static jint Bitmap_getGenerationId(JNIEnv* env, jobject, jlong bitmapHandle) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 845 | LocalScopedBitmap bitmap(bitmapHandle); |
John Reck | ae2e8b4 | 2015-05-06 14:55:05 -0700 | [diff] [blame] | 846 | return static_cast<jint>(bitmap->peekAtPixelRef()->getGenerationID()); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 849 | static jboolean Bitmap_isPremultiplied(JNIEnv* env, jobject, jlong bitmapHandle) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 850 | LocalScopedBitmap bitmap(bitmapHandle); |
| 851 | if (bitmap->info().alphaType() == kPremul_SkAlphaType) { |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 852 | return JNI_TRUE; |
| 853 | } |
| 854 | return JNI_FALSE; |
| 855 | } |
| 856 | |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 857 | static jboolean Bitmap_hasAlpha(JNIEnv* env, jobject, jlong bitmapHandle) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 858 | LocalScopedBitmap bitmap(bitmapHandle); |
| 859 | return !bitmap->info().isOpaque() ? JNI_TRUE : JNI_FALSE; |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 862 | static void Bitmap_setHasAlpha(JNIEnv* env, jobject, jlong bitmapHandle, |
| 863 | jboolean hasAlpha, jboolean requestPremul) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 864 | LocalScopedBitmap bitmap(bitmapHandle); |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 865 | if (hasAlpha) { |
John Reck | 0781a2f | 2015-05-27 16:29:17 -0700 | [diff] [blame] | 866 | bitmap->setAlphaType( |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 867 | requestPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 868 | } else { |
John Reck | 0781a2f | 2015-05-27 16:29:17 -0700 | [diff] [blame] | 869 | bitmap->setAlphaType(kOpaque_SkAlphaType); |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 870 | } |
| 871 | } |
| 872 | |
| 873 | static void Bitmap_setPremultiplied(JNIEnv* env, jobject, jlong bitmapHandle, |
| 874 | jboolean isPremul) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 875 | LocalScopedBitmap bitmap(bitmapHandle); |
| 876 | if (!bitmap->info().isOpaque()) { |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 877 | if (isPremul) { |
John Reck | 0781a2f | 2015-05-27 16:29:17 -0700 | [diff] [blame] | 878 | bitmap->setAlphaType(kPremul_SkAlphaType); |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 879 | } else { |
John Reck | 0781a2f | 2015-05-27 16:29:17 -0700 | [diff] [blame] | 880 | bitmap->setAlphaType(kUnpremul_SkAlphaType); |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 881 | } |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 882 | } |
| 883 | } |
| 884 | |
| 885 | static jboolean Bitmap_hasMipMap(JNIEnv* env, jobject, jlong bitmapHandle) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 886 | LocalScopedBitmap bitmap(bitmapHandle); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 887 | return bitmap->hasHardwareMipMap() ? JNI_TRUE : JNI_FALSE; |
| 888 | } |
| 889 | |
| 890 | static void Bitmap_setHasMipMap(JNIEnv* env, jobject, jlong bitmapHandle, |
| 891 | jboolean hasMipMap) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 892 | LocalScopedBitmap bitmap(bitmapHandle); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 893 | bitmap->setHasHardwareMipMap(hasMipMap); |
| 894 | } |
| 895 | |
| 896 | /////////////////////////////////////////////////////////////////////////////// |
| 897 | |
| 898 | static jobject Bitmap_createFromParcel(JNIEnv* env, jobject, jobject parcel) { |
| 899 | if (parcel == NULL) { |
| 900 | SkDebugf("-------- unparcel parcel is NULL\n"); |
| 901 | return NULL; |
| 902 | } |
| 903 | |
| 904 | android::Parcel* p = android::parcelForJavaObject(env, parcel); |
| 905 | |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 906 | const bool isMutable = p->readInt32() != 0; |
| 907 | const SkColorType colorType = (SkColorType)p->readInt32(); |
| 908 | const SkAlphaType alphaType = (SkAlphaType)p->readInt32(); |
| 909 | const int width = p->readInt32(); |
| 910 | const int height = p->readInt32(); |
| 911 | const int rowBytes = p->readInt32(); |
| 912 | const int density = p->readInt32(); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 913 | |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 914 | if (kN32_SkColorType != colorType && |
| 915 | kRGB_565_SkColorType != colorType && |
| 916 | kARGB_4444_SkColorType != colorType && |
| 917 | kIndex_8_SkColorType != colorType && |
| 918 | kAlpha_8_SkColorType != colorType) { |
| 919 | SkDebugf("Bitmap_createFromParcel unknown colortype: %d\n", colorType); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 920 | return NULL; |
| 921 | } |
| 922 | |
Leon Scroggins III | ec419e0 | 2015-03-11 13:12:06 -0400 | [diff] [blame] | 923 | std::unique_ptr<SkBitmap> bitmap(new SkBitmap); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 924 | |
Leon Scroggins III | ec419e0 | 2015-03-11 13:12:06 -0400 | [diff] [blame] | 925 | if (!bitmap->setInfo(SkImageInfo::Make(width, height, colorType, alphaType), rowBytes)) { |
| 926 | return NULL; |
| 927 | } |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 928 | |
| 929 | SkColorTable* ctable = NULL; |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 930 | if (colorType == kIndex_8_SkColorType) { |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 931 | int count = p->readInt32(); |
Leon Scroggins III | ec419e0 | 2015-03-11 13:12:06 -0400 | [diff] [blame] | 932 | if (count < 0 || count > 256) { |
| 933 | // The data is corrupt, since SkColorTable enforces a value between 0 and 256, |
| 934 | // inclusive. |
| 935 | return NULL; |
| 936 | } |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 937 | if (count > 0) { |
| 938 | size_t size = count * sizeof(SkPMColor); |
| 939 | const SkPMColor* src = (const SkPMColor*)p->readInplace(size); |
Leon Scroggins III | ec419e0 | 2015-03-11 13:12:06 -0400 | [diff] [blame] | 940 | if (src == NULL) { |
| 941 | return NULL; |
| 942 | } |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 943 | ctable = new SkColorTable(src, count); |
| 944 | } |
| 945 | } |
| 946 | |
Jeff Brown | a316c5d | 2015-06-05 15:14:06 -0700 | [diff] [blame] | 947 | // Read the bitmap blob. |
| 948 | size_t size = bitmap->getSize(); |
| 949 | android::Parcel::ReadableBlob blob; |
| 950 | android::status_t status = p->readBlob(size, &blob); |
| 951 | if (status) { |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 952 | SkSafeUnref(ctable); |
Jeff Brown | a316c5d | 2015-06-05 15:14:06 -0700 | [diff] [blame] | 953 | doThrowRE(env, "Could not read bitmap blob."); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 954 | return NULL; |
| 955 | } |
| 956 | |
Jeff Brown | a316c5d | 2015-06-05 15:14:06 -0700 | [diff] [blame] | 957 | // Map the bitmap in place from the ashmem region if possible otherwise copy. |
| 958 | Bitmap* nativeBitmap; |
Riley Andrews | 8cee7c1 | 2015-11-01 23:36:04 -0800 | [diff] [blame] | 959 | if (blob.fd() >= 0 && (blob.isMutable() || !isMutable) && (size >= ASHMEM_BITMAP_MIN_SIZE)) { |
Jeff Brown | a316c5d | 2015-06-05 15:14:06 -0700 | [diff] [blame] | 960 | #if DEBUG_PARCEL |
| 961 | ALOGD("Bitmap.createFromParcel: mapped contents of %s bitmap from %s blob " |
| 962 | "(fds %s)", |
| 963 | isMutable ? "mutable" : "immutable", |
| 964 | blob.isMutable() ? "mutable" : "immutable", |
| 965 | p->allowFds() ? "allowed" : "forbidden"); |
| 966 | #endif |
| 967 | // Dup the file descriptor so we can keep a reference to it after the Parcel |
| 968 | // is disposed. |
| 969 | int dupFd = dup(blob.fd()); |
| 970 | if (dupFd < 0) { |
Erik Wolsheimer | 211abad | 2015-11-13 11:54:47 -0800 | [diff] [blame] | 971 | ALOGE("Error allocating dup fd. Error:%d", errno); |
Jeff Brown | a316c5d | 2015-06-05 15:14:06 -0700 | [diff] [blame] | 972 | blob.release(); |
| 973 | SkSafeUnref(ctable); |
| 974 | doThrowRE(env, "Could not allocate dup blob fd."); |
| 975 | return NULL; |
| 976 | } |
| 977 | |
| 978 | // Map the pixels in place and take ownership of the ashmem region. |
| 979 | nativeBitmap = GraphicsJNI::mapAshmemPixelRef(env, bitmap.get(), |
John Reck | 003bdee | 2016-09-12 10:43:35 -0700 | [diff] [blame] | 980 | ctable, dupFd, const_cast<void*>(blob.data()), size, !isMutable); |
Jeff Brown | a316c5d | 2015-06-05 15:14:06 -0700 | [diff] [blame] | 981 | SkSafeUnref(ctable); |
| 982 | if (!nativeBitmap) { |
| 983 | close(dupFd); |
| 984 | blob.release(); |
| 985 | doThrowRE(env, "Could not allocate ashmem pixel ref."); |
| 986 | return NULL; |
| 987 | } |
| 988 | |
| 989 | // Clear the blob handle, don't release it. |
| 990 | blob.clear(); |
| 991 | } else { |
| 992 | #if DEBUG_PARCEL |
| 993 | if (blob.fd() >= 0) { |
| 994 | ALOGD("Bitmap.createFromParcel: copied contents of mutable bitmap " |
| 995 | "from immutable blob (fds %s)", |
| 996 | p->allowFds() ? "allowed" : "forbidden"); |
| 997 | } else { |
| 998 | ALOGD("Bitmap.createFromParcel: copied contents from %s blob " |
| 999 | "(fds %s)", |
| 1000 | blob.isMutable() ? "mutable" : "immutable", |
| 1001 | p->allowFds() ? "allowed" : "forbidden"); |
| 1002 | } |
| 1003 | #endif |
| 1004 | |
| 1005 | // Copy the pixels into a new buffer. |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame^] | 1006 | nativeBitmap = GraphicsJNI::allocateHeapPixelRef(bitmap.get(), ctable); |
Jeff Brown | a316c5d | 2015-06-05 15:14:06 -0700 | [diff] [blame] | 1007 | SkSafeUnref(ctable); |
| 1008 | if (!nativeBitmap) { |
| 1009 | blob.release(); |
| 1010 | doThrowRE(env, "Could not allocate java pixel ref."); |
| 1011 | return NULL; |
| 1012 | } |
| 1013 | bitmap->lockPixels(); |
| 1014 | memcpy(bitmap->getPixels(), blob.data(), size); |
| 1015 | bitmap->unlockPixels(); |
| 1016 | |
| 1017 | // Release the blob handle. |
| 1018 | blob.release(); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1019 | } |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1020 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1021 | return GraphicsJNI::createBitmap(env, nativeBitmap, |
Leon Scroggins III | ec419e0 | 2015-03-11 13:12:06 -0400 | [diff] [blame] | 1022 | getPremulBitmapCreateFlags(isMutable), NULL, NULL, density); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | static jboolean Bitmap_writeToParcel(JNIEnv* env, jobject, |
| 1026 | jlong bitmapHandle, |
| 1027 | jboolean isMutable, jint density, |
| 1028 | jobject parcel) { |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1029 | if (parcel == NULL) { |
| 1030 | SkDebugf("------- writeToParcel null parcel\n"); |
| 1031 | return JNI_FALSE; |
| 1032 | } |
| 1033 | |
| 1034 | android::Parcel* p = android::parcelForJavaObject(env, parcel); |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1035 | SkBitmap bitmap; |
Riley Andrews | 39d7f30 | 2014-11-13 17:43:25 -0800 | [diff] [blame] | 1036 | |
| 1037 | android::Bitmap* androidBitmap = reinterpret_cast<Bitmap*>(bitmapHandle); |
| 1038 | androidBitmap->getSkBitmap(&bitmap); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1039 | |
| 1040 | p->writeInt32(isMutable); |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1041 | p->writeInt32(bitmap.colorType()); |
| 1042 | p->writeInt32(bitmap.alphaType()); |
| 1043 | p->writeInt32(bitmap.width()); |
| 1044 | p->writeInt32(bitmap.height()); |
| 1045 | p->writeInt32(bitmap.rowBytes()); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1046 | p->writeInt32(density); |
| 1047 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1048 | if (bitmap.colorType() == kIndex_8_SkColorType) { |
Leon Scroggins III | 66ce1c3 | 2016-02-02 11:11:55 -0500 | [diff] [blame] | 1049 | // The bitmap needs to be locked to access its color table. |
| 1050 | SkAutoLockPixels alp(bitmap); |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1051 | SkColorTable* ctable = bitmap.getColorTable(); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1052 | if (ctable != NULL) { |
| 1053 | int count = ctable->count(); |
| 1054 | p->writeInt32(count); |
| 1055 | memcpy(p->writeInplace(count * sizeof(SkPMColor)), |
Mike Reed | 71487eb | 2014-11-19 16:13:20 -0500 | [diff] [blame] | 1056 | ctable->readColors(), count * sizeof(SkPMColor)); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1057 | } else { |
| 1058 | p->writeInt32(0); // indicate no ctable |
| 1059 | } |
| 1060 | } |
| 1061 | |
Jeff Brown | a316c5d | 2015-06-05 15:14:06 -0700 | [diff] [blame] | 1062 | // Transfer the underlying ashmem region if we have one and it's immutable. |
| 1063 | android::status_t status; |
| 1064 | int fd = androidBitmap->getAshmemFd(); |
| 1065 | if (fd >= 0 && !isMutable && p->allowFds()) { |
| 1066 | #if DEBUG_PARCEL |
| 1067 | ALOGD("Bitmap.writeToParcel: transferring immutable bitmap's ashmem fd as " |
| 1068 | "immutable blob (fds %s)", |
| 1069 | p->allowFds() ? "allowed" : "forbidden"); |
| 1070 | #endif |
| 1071 | |
| 1072 | status = p->writeDupImmutableBlobFileDescriptor(fd); |
| 1073 | if (status) { |
| 1074 | doThrowRE(env, "Could not write bitmap blob file descriptor."); |
Riley Andrews | 39d7f30 | 2014-11-13 17:43:25 -0800 | [diff] [blame] | 1075 | return JNI_FALSE; |
| 1076 | } |
Jeff Brown | a316c5d | 2015-06-05 15:14:06 -0700 | [diff] [blame] | 1077 | return JNI_TRUE; |
Riley Andrews | 39d7f30 | 2014-11-13 17:43:25 -0800 | [diff] [blame] | 1078 | } |
Jeff Brown | a316c5d | 2015-06-05 15:14:06 -0700 | [diff] [blame] | 1079 | |
| 1080 | // Copy the bitmap to a new blob. |
| 1081 | bool mutableCopy = isMutable; |
| 1082 | #if DEBUG_PARCEL |
| 1083 | ALOGD("Bitmap.writeToParcel: copying %s bitmap into new %s blob (fds %s)", |
| 1084 | isMutable ? "mutable" : "immutable", |
| 1085 | mutableCopy ? "mutable" : "immutable", |
| 1086 | p->allowFds() ? "allowed" : "forbidden"); |
| 1087 | #endif |
| 1088 | |
| 1089 | size_t size = bitmap.getSize(); |
| 1090 | android::Parcel::WritableBlob blob; |
| 1091 | status = p->writeBlob(size, mutableCopy, &blob); |
| 1092 | if (status) { |
| 1093 | doThrowRE(env, "Could not copy bitmap to parcel blob."); |
| 1094 | return JNI_FALSE; |
| 1095 | } |
| 1096 | |
| 1097 | bitmap.lockPixels(); |
| 1098 | const void* pSrc = bitmap.getPixels(); |
| 1099 | if (pSrc == NULL) { |
| 1100 | memset(blob.data(), 0, size); |
| 1101 | } else { |
| 1102 | memcpy(blob.data(), pSrc, size); |
| 1103 | } |
| 1104 | bitmap.unlockPixels(); |
| 1105 | |
| 1106 | blob.release(); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1107 | return JNI_TRUE; |
| 1108 | } |
| 1109 | |
| 1110 | static jobject Bitmap_extractAlpha(JNIEnv* env, jobject clazz, |
| 1111 | jlong srcHandle, jlong paintHandle, |
| 1112 | jintArray offsetXY) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1113 | SkBitmap src; |
| 1114 | reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 1115 | const android::Paint* paint = reinterpret_cast<android::Paint*>(paintHandle); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1116 | SkIPoint offset; |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1117 | SkBitmap dst; |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame^] | 1118 | HeapAllocator allocator; |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1119 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1120 | src.extractAlpha(&dst, paint, &allocator, &offset); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1121 | // If Skia can't allocate pixels for destination bitmap, it resets |
| 1122 | // it, that is set its pixels buffer to NULL, and zero width and height. |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1123 | if (dst.getPixels() == NULL && src.getPixels() != NULL) { |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1124 | doThrowOOME(env, "failed to allocate pixels for alpha"); |
| 1125 | return NULL; |
| 1126 | } |
| 1127 | if (offsetXY != 0 && env->GetArrayLength(offsetXY) >= 2) { |
| 1128 | int* array = env->GetIntArrayElements(offsetXY, NULL); |
| 1129 | array[0] = offset.fX; |
| 1130 | array[1] = offset.fY; |
| 1131 | env->ReleaseIntArrayElements(offsetXY, array, 0); |
| 1132 | } |
| 1133 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1134 | return GraphicsJNI::createBitmap(env, allocator.getStorageObjAndReset(), |
| 1135 | getPremulBitmapCreateFlags(true)); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | /////////////////////////////////////////////////////////////////////////////// |
| 1139 | |
| 1140 | static jint Bitmap_getPixel(JNIEnv* env, jobject, jlong bitmapHandle, |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 1141 | jint x, jint y) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1142 | SkBitmap bitmap; |
| 1143 | reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap); |
| 1144 | SkAutoLockPixels alp(bitmap); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1145 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1146 | ToColorProc proc = ChooseToColorProc(bitmap); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1147 | if (NULL == proc) { |
| 1148 | return 0; |
| 1149 | } |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1150 | const void* src = bitmap.getAddr(x, y); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1151 | if (NULL == src) { |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
| 1155 | SkColor dst[1]; |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1156 | proc(dst, src, 1, bitmap.getColorTable()); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1157 | return static_cast<jint>(dst[0]); |
| 1158 | } |
| 1159 | |
| 1160 | static void Bitmap_getPixels(JNIEnv* env, jobject, jlong bitmapHandle, |
| 1161 | jintArray pixelArray, jint offset, jint stride, |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 1162 | jint x, jint y, jint width, jint height) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1163 | SkBitmap bitmap; |
| 1164 | reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap); |
| 1165 | SkAutoLockPixels alp(bitmap); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1166 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1167 | ToColorProc proc = ChooseToColorProc(bitmap); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1168 | if (NULL == proc) { |
| 1169 | return; |
| 1170 | } |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1171 | const void* src = bitmap.getAddr(x, y); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1172 | if (NULL == src) { |
| 1173 | return; |
| 1174 | } |
| 1175 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1176 | SkColorTable* ctable = bitmap.getColorTable(); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1177 | jint* dst = env->GetIntArrayElements(pixelArray, NULL); |
| 1178 | SkColor* d = (SkColor*)dst + offset; |
| 1179 | while (--height >= 0) { |
| 1180 | proc(d, src, width, ctable); |
| 1181 | d += stride; |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1182 | src = (void*)((const char*)src + bitmap.rowBytes()); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1183 | } |
| 1184 | env->ReleaseIntArrayElements(pixelArray, dst, 0); |
| 1185 | } |
| 1186 | |
| 1187 | /////////////////////////////////////////////////////////////////////////////// |
| 1188 | |
| 1189 | static void Bitmap_setPixel(JNIEnv* env, jobject, jlong bitmapHandle, |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 1190 | jint x, jint y, jint colorHandle) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1191 | SkBitmap bitmap; |
| 1192 | reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1193 | SkColor color = static_cast<SkColor>(colorHandle); |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1194 | SkAutoLockPixels alp(bitmap); |
| 1195 | if (NULL == bitmap.getPixels()) { |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1196 | return; |
| 1197 | } |
| 1198 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1199 | FromColorProc proc = ChooseFromColorProc(bitmap); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1200 | if (NULL == proc) { |
| 1201 | return; |
| 1202 | } |
| 1203 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1204 | proc(bitmap.getAddr(x, y), &color, 1, x, y); |
| 1205 | bitmap.notifyPixelsChanged(); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | static void Bitmap_setPixels(JNIEnv* env, jobject, jlong bitmapHandle, |
| 1209 | jintArray pixelArray, jint offset, jint stride, |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 1210 | jint x, jint y, jint width, jint height) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1211 | SkBitmap bitmap; |
| 1212 | reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1213 | GraphicsJNI::SetPixels(env, pixelArray, offset, stride, |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1214 | x, y, width, height, bitmap); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | static void Bitmap_copyPixelsToBuffer(JNIEnv* env, jobject, |
| 1218 | jlong bitmapHandle, jobject jbuffer) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1219 | SkBitmap bitmap; |
| 1220 | reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap); |
| 1221 | SkAutoLockPixels alp(bitmap); |
| 1222 | const void* src = bitmap.getPixels(); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1223 | |
| 1224 | if (NULL != src) { |
| 1225 | android::AutoBufferPointer abp(env, jbuffer, JNI_TRUE); |
| 1226 | |
| 1227 | // the java side has already checked that buffer is large enough |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1228 | memcpy(abp.pointer(), src, bitmap.getSize()); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | static void Bitmap_copyPixelsFromBuffer(JNIEnv* env, jobject, |
| 1233 | jlong bitmapHandle, jobject jbuffer) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1234 | SkBitmap bitmap; |
| 1235 | reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap); |
| 1236 | SkAutoLockPixels alp(bitmap); |
| 1237 | void* dst = bitmap.getPixels(); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1238 | |
| 1239 | if (NULL != dst) { |
| 1240 | android::AutoBufferPointer abp(env, jbuffer, JNI_FALSE); |
| 1241 | // the java side has already checked that buffer is large enough |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1242 | memcpy(dst, abp.pointer(), bitmap.getSize()); |
| 1243 | bitmap.notifyPixelsChanged(); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | static jboolean Bitmap_sameAs(JNIEnv* env, jobject, jlong bm0Handle, |
| 1248 | jlong bm1Handle) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1249 | SkBitmap bm0; |
| 1250 | SkBitmap bm1; |
| 1251 | reinterpret_cast<Bitmap*>(bm0Handle)->getSkBitmap(&bm0); |
| 1252 | reinterpret_cast<Bitmap*>(bm1Handle)->getSkBitmap(&bm1); |
| 1253 | if (bm0.width() != bm1.width() || |
| 1254 | bm0.height() != bm1.height() || |
| 1255 | bm0.colorType() != bm1.colorType()) { |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1256 | return JNI_FALSE; |
| 1257 | } |
| 1258 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1259 | SkAutoLockPixels alp0(bm0); |
| 1260 | SkAutoLockPixels alp1(bm1); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1261 | |
| 1262 | // if we can't load the pixels, return false |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1263 | if (NULL == bm0.getPixels() || NULL == bm1.getPixels()) { |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1264 | return JNI_FALSE; |
| 1265 | } |
| 1266 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1267 | if (bm0.colorType() == kIndex_8_SkColorType) { |
| 1268 | SkColorTable* ct0 = bm0.getColorTable(); |
| 1269 | SkColorTable* ct1 = bm1.getColorTable(); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1270 | if (NULL == ct0 || NULL == ct1) { |
| 1271 | return JNI_FALSE; |
| 1272 | } |
| 1273 | if (ct0->count() != ct1->count()) { |
| 1274 | return JNI_FALSE; |
| 1275 | } |
| 1276 | |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1277 | const size_t size = ct0->count() * sizeof(SkPMColor); |
Mike Reed | 71487eb | 2014-11-19 16:13:20 -0500 | [diff] [blame] | 1278 | if (memcmp(ct0->readColors(), ct1->readColors(), size) != 0) { |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1279 | return JNI_FALSE; |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | // now compare each scanline. We can't do the entire buffer at once, |
| 1284 | // since we don't care about the pixel values that might extend beyond |
| 1285 | // the width (since the scanline might be larger than the logical width) |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1286 | const int h = bm0.height(); |
| 1287 | const size_t size = bm0.width() * bm0.bytesPerPixel(); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1288 | for (int y = 0; y < h; y++) { |
henry.uh_chen | 53001ca | 2014-07-03 20:40:22 +0800 | [diff] [blame] | 1289 | // SkBitmap::getAddr(int, int) may return NULL due to unrecognized config |
| 1290 | // (ex: kRLE_Index8_Config). This will cause memcmp method to crash. Since bm0 |
| 1291 | // and bm1 both have pixel data() (have passed NULL == getPixels() check), |
| 1292 | // those 2 bitmaps should be valid (only unrecognized), we return JNI_FALSE |
| 1293 | // to warn user those 2 unrecognized config bitmaps may be different. |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1294 | void *bm0Addr = bm0.getAddr(0, y); |
| 1295 | void *bm1Addr = bm1.getAddr(0, y); |
henry.uh_chen | 53001ca | 2014-07-03 20:40:22 +0800 | [diff] [blame] | 1296 | |
| 1297 | if(bm0Addr == NULL || bm1Addr == NULL) { |
| 1298 | return JNI_FALSE; |
| 1299 | } |
| 1300 | |
| 1301 | if (memcmp(bm0Addr, bm1Addr, size) != 0) { |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1302 | return JNI_FALSE; |
| 1303 | } |
| 1304 | } |
| 1305 | return JNI_TRUE; |
| 1306 | } |
| 1307 | |
John Reck | c6e2e8f | 2015-04-15 13:24:47 -0700 | [diff] [blame] | 1308 | static jlong Bitmap_refPixelRef(JNIEnv* env, jobject, jlong bitmapHandle) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 1309 | LocalScopedBitmap bitmap(bitmapHandle); |
John Reck | ae2e8b4 | 2015-05-06 14:55:05 -0700 | [diff] [blame] | 1310 | SkPixelRef* pixelRef = bitmap.valid() ? bitmap->peekAtPixelRef() : nullptr; |
John Reck | c6e2e8f | 2015-04-15 13:24:47 -0700 | [diff] [blame] | 1311 | SkSafeRef(pixelRef); |
| 1312 | return reinterpret_cast<jlong>(pixelRef); |
| 1313 | } |
| 1314 | |
John Reck | 4387190 | 2016-08-01 14:39:24 -0700 | [diff] [blame] | 1315 | static void Bitmap_prepareToDraw(JNIEnv* env, jobject, jlong bitmapPtr) { |
| 1316 | LocalScopedBitmap bitmapHandle(bitmapPtr); |
| 1317 | if (!bitmapHandle.valid()) return; |
| 1318 | SkBitmap bitmap; |
| 1319 | bitmapHandle->getSkBitmap(&bitmap); |
| 1320 | android::uirenderer::renderthread::RenderProxy::prepareToDraw(bitmap); |
| 1321 | } |
| 1322 | |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame^] | 1323 | static jint Bitmap_getAllocationByteCount(JNIEnv* env, jobject, jlong bitmapPtr) { |
| 1324 | LocalScopedBitmap bitmapHandle(bitmapPtr); |
| 1325 | return static_cast<jint>(bitmapHandle->getAllocationByteCount()); |
| 1326 | } |
| 1327 | |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1328 | /////////////////////////////////////////////////////////////////////////////// |
| 1329 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 1330 | static const JNINativeMethod gBitmapMethods[] = { |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1331 | { "nativeCreate", "([IIIIIIZ)Landroid/graphics/Bitmap;", |
| 1332 | (void*)Bitmap_creator }, |
| 1333 | { "nativeCopy", "(JIZ)Landroid/graphics/Bitmap;", |
| 1334 | (void*)Bitmap_copy }, |
Riley Andrews | 721ae5f | 2015-05-11 16:08:22 -0700 | [diff] [blame] | 1335 | { "nativeCopyAshmem", "(J)Landroid/graphics/Bitmap;", |
| 1336 | (void*)Bitmap_copyAshmem }, |
Winson | a5fdde9 | 2016-04-14 15:27:15 -0700 | [diff] [blame] | 1337 | { "nativeCopyAshmemConfig", "(JI)Landroid/graphics/Bitmap;", |
| 1338 | (void*)Bitmap_copyAshmemConfig }, |
Richard Uhler | 775873a | 2015-12-29 12:37:39 -0800 | [diff] [blame] | 1339 | { "nativeGetNativeFinalizer", "()J", (void*)Bitmap_getNativeFinalizer }, |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1340 | { "nativeRecycle", "(J)Z", (void*)Bitmap_recycle }, |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame^] | 1341 | { "nativeReconfigure", "(JIIIZ)V", (void*)Bitmap_reconfigure }, |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1342 | { "nativeCompress", "(JIILjava/io/OutputStream;[B)Z", |
| 1343 | (void*)Bitmap_compress }, |
| 1344 | { "nativeErase", "(JI)V", (void*)Bitmap_erase }, |
| 1345 | { "nativeRowBytes", "(J)I", (void*)Bitmap_rowBytes }, |
| 1346 | { "nativeConfig", "(J)I", (void*)Bitmap_config }, |
| 1347 | { "nativeHasAlpha", "(J)Z", (void*)Bitmap_hasAlpha }, |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 1348 | { "nativeIsPremultiplied", "(J)Z", (void*)Bitmap_isPremultiplied}, |
| 1349 | { "nativeSetHasAlpha", "(JZZ)V", (void*)Bitmap_setHasAlpha}, |
| 1350 | { "nativeSetPremultiplied", "(JZ)V", (void*)Bitmap_setPremultiplied}, |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1351 | { "nativeHasMipMap", "(J)Z", (void*)Bitmap_hasMipMap }, |
| 1352 | { "nativeSetHasMipMap", "(JZ)V", (void*)Bitmap_setHasMipMap }, |
| 1353 | { "nativeCreateFromParcel", |
| 1354 | "(Landroid/os/Parcel;)Landroid/graphics/Bitmap;", |
| 1355 | (void*)Bitmap_createFromParcel }, |
| 1356 | { "nativeWriteToParcel", "(JZILandroid/os/Parcel;)Z", |
| 1357 | (void*)Bitmap_writeToParcel }, |
| 1358 | { "nativeExtractAlpha", "(JJ[I)Landroid/graphics/Bitmap;", |
| 1359 | (void*)Bitmap_extractAlpha }, |
| 1360 | { "nativeGenerationId", "(J)I", (void*)Bitmap_getGenerationId }, |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 1361 | { "nativeGetPixel", "(JII)I", (void*)Bitmap_getPixel }, |
| 1362 | { "nativeGetPixels", "(J[IIIIIII)V", (void*)Bitmap_getPixels }, |
| 1363 | { "nativeSetPixel", "(JIII)V", (void*)Bitmap_setPixel }, |
| 1364 | { "nativeSetPixels", "(J[IIIIIII)V", (void*)Bitmap_setPixels }, |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1365 | { "nativeCopyPixelsToBuffer", "(JLjava/nio/Buffer;)V", |
| 1366 | (void*)Bitmap_copyPixelsToBuffer }, |
| 1367 | { "nativeCopyPixelsFromBuffer", "(JLjava/nio/Buffer;)V", |
| 1368 | (void*)Bitmap_copyPixelsFromBuffer }, |
| 1369 | { "nativeSameAs", "(JJ)Z", (void*)Bitmap_sameAs }, |
John Reck | c6e2e8f | 2015-04-15 13:24:47 -0700 | [diff] [blame] | 1370 | { "nativeRefPixelRef", "(J)J", (void*)Bitmap_refPixelRef }, |
John Reck | 4387190 | 2016-08-01 14:39:24 -0700 | [diff] [blame] | 1371 | { "nativePrepareToDraw", "(J)V", (void*)Bitmap_prepareToDraw }, |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame^] | 1372 | { "nativeGetAllocationByteCount", "(J)I", (void*)Bitmap_getAllocationByteCount }, |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1373 | }; |
| 1374 | |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1375 | int register_android_graphics_Bitmap(JNIEnv* env) |
| 1376 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1377 | return android::RegisterMethodsOrDie(env, "android/graphics/Bitmap", gBitmapMethods, |
| 1378 | NELEM(gBitmapMethods)); |
Chris Craik | 32054b0 | 2014-05-09 13:58:56 -0700 | [diff] [blame] | 1379 | } |