Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Derek Sollenberger | 5368eda | 2019-10-25 11:20:03 -0400 | [diff] [blame] | 17 | #undef LOG_TAG |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 18 | #define LOG_TAG "BitmapRegionDecoder" |
| 19 | |
Ben Wagner | 60126ef | 2015-08-07 12:13:48 -0400 | [diff] [blame] | 20 | #include "BitmapFactory.h" |
| 21 | #include "CreateJavaOutputStreamAdaptor.h" |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 22 | #include "GraphicsJNI.h" |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 23 | #include "Utils.h" |
| 24 | |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 25 | #include "BitmapRegionDecoder.h" |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 26 | #include "SkBitmap.h" |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 27 | #include "SkCodec.h" |
| 28 | #include "SkData.h" |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 29 | #include "SkStream.h" |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 30 | |
Leon Scroggins III | ee3bfe7 | 2019-01-31 08:42:23 -0500 | [diff] [blame] | 31 | #include <HardwareBitmapUploader.h> |
Ben Wagner | 60126ef | 2015-08-07 12:13:48 -0400 | [diff] [blame] | 32 | #include <androidfw/Asset.h> |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 33 | #include <sys/stat.h> |
| 34 | |
Ben Wagner | 18bd885 | 2016-10-24 14:50:10 -0400 | [diff] [blame] | 35 | #include <memory> |
| 36 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 37 | using namespace android; |
| 38 | |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 39 | static jobject createBitmapRegionDecoder(JNIEnv* env, sk_sp<SkData> data) { |
| 40 | auto brd = skia::BitmapRegionDecoder::Make(std::move(data)); |
Ben Wagner | 18bd885 | 2016-10-24 14:50:10 -0400 | [diff] [blame] | 41 | if (!brd) { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 42 | doThrowIOE(env, "Image format not supported"); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 43 | return nullObjectReturn("CreateBitmapRegionDecoder returned null"); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 44 | } |
| 45 | |
Ben Wagner | 18bd885 | 2016-10-24 14:50:10 -0400 | [diff] [blame] | 46 | return GraphicsJNI::createBitmapRegionDecoder(env, brd.release()); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static jobject nativeNewInstanceFromByteArray(JNIEnv* env, jobject, jbyteArray byteArray, |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 50 | jint offset, jint length) { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 51 | AutoJavaByteArray ar(env, byteArray); |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 52 | return createBitmapRegionDecoder(env, SkData::MakeWithCopy(ar.ptr() + offset, length)); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static jobject nativeNewInstanceFromFileDescriptor(JNIEnv* env, jobject clazz, |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 56 | jobject fileDescriptor) { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 57 | NPE_CHECK_RETURN_ZERO(env, fileDescriptor); |
| 58 | |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 59 | jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor); |
Derek Sollenberger | 5827cb5 | 2013-07-26 14:58:06 -0400 | [diff] [blame] | 60 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 61 | struct stat fdStat; |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 62 | if (fstat(descriptor, &fdStat) == -1) { |
| 63 | doThrowIOE(env, "broken file descriptor"); |
| 64 | return nullObjectReturn("fstat return -1"); |
| 65 | } |
| 66 | |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 67 | return createBitmapRegionDecoder(env, SkData::MakeFromFD(descriptor)); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 68 | } |
| 69 | |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 70 | static jobject nativeNewInstanceFromStream(JNIEnv* env, jobject clazz, jobject is, // InputStream |
| 71 | jbyteArray storage) { // byte[] |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 72 | jobject brd = nullptr; |
| 73 | sk_sp<SkData> data = CopyJavaInputStream(env, is, storage); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 74 | |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 75 | if (data) { |
| 76 | brd = createBitmapRegionDecoder(env, std::move(data)); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 77 | } |
Derek Sollenberger | 5827cb5 | 2013-07-26 14:58:06 -0400 | [diff] [blame] | 78 | return brd; |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 79 | } |
| 80 | |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 81 | static jobject nativeNewInstanceFromAsset(JNIEnv* env, jobject clazz, jlong native_asset) { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 82 | Asset* asset = reinterpret_cast<Asset*>(native_asset); |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 83 | sk_sp<SkData> data = CopyAssetToData(asset); |
| 84 | if (!data) { |
| 85 | return nullptr; |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 86 | } |
Derek Sollenberger | 5827cb5 | 2013-07-26 14:58:06 -0400 | [diff] [blame] | 87 | |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 88 | return createBitmapRegionDecoder(env, data); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
| 92 | * nine patch not supported |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 93 | * purgeable not supported |
| 94 | * reportSizeToVM not supported |
| 95 | */ |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 96 | static jobject nativeDecodeRegion(JNIEnv* env, jobject, jlong brdHandle, jint inputX, |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 97 | jint inputY, jint inputWidth, jint inputHeight, jobject options, jlong inBitmapHandle, |
| 98 | jlong colorSpaceHandle) { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 99 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 100 | // Set default options. |
| 101 | int sampleSize = 1; |
| 102 | SkColorType colorType = kN32_SkColorType; |
| 103 | bool requireUnpremul = false; |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 104 | jobject javaBitmap = nullptr; |
sergeyv | da6c8ffc | 2016-11-22 18:28:54 -0800 | [diff] [blame] | 105 | bool isHardware = false; |
Leon Scroggins III | 0e443d1 | 2018-12-19 11:38:35 -0500 | [diff] [blame] | 106 | sk_sp<SkColorSpace> colorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 107 | // Update the default options with any options supplied by the client. |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 108 | if (NULL != options) { |
| 109 | sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 110 | jobject jconfig = env->GetObjectField(options, gOptions_configFieldID); |
| 111 | colorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig); |
sergeyv | da6c8ffc | 2016-11-22 18:28:54 -0800 | [diff] [blame] | 112 | isHardware = GraphicsJNI::isHardwareConfig(env, jconfig); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 113 | requireUnpremul = !env->GetBooleanField(options, gOptions_premultipliedFieldID); |
| 114 | javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID); |
| 115 | // The Java options of ditherMode and preferQualityOverSpeed are deprecated. We will |
| 116 | // ignore the values of these fields. |
| 117 | |
| 118 | // Initialize these fields to indicate a failure. If the decode succeeds, we |
| 119 | // will update them later on. |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 120 | env->SetIntField(options, gOptions_widthFieldID, -1); |
| 121 | env->SetIntField(options, gOptions_heightFieldID, -1); |
| 122 | env->SetObjectField(options, gOptions_mimeFieldID, 0); |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 123 | env->SetObjectField(options, gOptions_outConfigFieldID, 0); |
| 124 | env->SetObjectField(options, gOptions_outColorSpaceFieldID, 0); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 125 | } |
| 126 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 127 | // Recycle a bitmap if possible. |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 128 | android::Bitmap* recycledBitmap = nullptr; |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 129 | size_t recycledBytes = 0; |
| 130 | if (javaBitmap) { |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 131 | recycledBitmap = &bitmap::toBitmap(inBitmapHandle); |
sergeyv | c69853c | 2016-10-07 14:14:09 -0700 | [diff] [blame] | 132 | if (recycledBitmap->isImmutable()) { |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 133 | ALOGW("Warning: Reusing an immutable bitmap as an image decoder target."); |
| 134 | } |
Leon Scroggins III | ca8aef6 | 2019-03-26 12:11:27 -0400 | [diff] [blame] | 135 | recycledBytes = recycledBitmap->getAllocationByteCount(); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 136 | } |
| 137 | |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 138 | auto* brd = reinterpret_cast<skia::BitmapRegionDecoder*>(brdHandle); |
Leon Scroggins III | 8d592f9 | 2018-03-12 15:44:03 -0400 | [diff] [blame] | 139 | SkColorType decodeColorType = brd->computeOutputColorType(colorType); |
Alec Mouri | 1efd0a5 | 2022-01-20 13:58:23 -0800 | [diff] [blame^] | 140 | |
| 141 | if (isHardware) { |
| 142 | if (decodeColorType == kRGBA_F16_SkColorType && |
Leon Scroggins III | ee3bfe7 | 2019-01-31 08:42:23 -0500 | [diff] [blame] | 143 | !uirenderer::HardwareBitmapUploader::hasFP16Support()) { |
Alec Mouri | 1efd0a5 | 2022-01-20 13:58:23 -0800 | [diff] [blame^] | 144 | decodeColorType = kN32_SkColorType; |
| 145 | } |
| 146 | if (decodeColorType == kRGBA_1010102_SkColorType && |
| 147 | !uirenderer::HardwareBitmapUploader::has1010102Support()) { |
| 148 | decodeColorType = kN32_SkColorType; |
| 149 | } |
Leon Scroggins III | ee3bfe7 | 2019-01-31 08:42:23 -0500 | [diff] [blame] | 150 | } |
Leon Scroggins III | 8d592f9 | 2018-03-12 15:44:03 -0400 | [diff] [blame] | 151 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 152 | // Set up the pixel allocator |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 153 | skia::BRDAllocator* allocator = nullptr; |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 154 | RecyclingClippingPixelAllocator recycleAlloc(recycledBitmap, recycledBytes); |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame] | 155 | HeapAllocator heapAlloc; |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 156 | if (javaBitmap) { |
| 157 | allocator = &recycleAlloc; |
| 158 | // We are required to match the color type of the recycled bitmap. |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 159 | decodeColorType = recycledBitmap->info().colorType(); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 160 | } else { |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame] | 161 | allocator = &heapAlloc; |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 162 | } |
| 163 | |
Leon Scroggins III | 8d592f9 | 2018-03-12 15:44:03 -0400 | [diff] [blame] | 164 | sk_sp<SkColorSpace> decodeColorSpace = brd->computeOutputColorSpace( |
| 165 | decodeColorType, colorSpace); |
| 166 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 167 | // Decode the region. |
| 168 | SkIRect subset = SkIRect::MakeXYWH(inputX, inputY, inputWidth, inputHeight); |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 169 | SkBitmap bitmap; |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 170 | if (!brd->decodeRegion(&bitmap, allocator, subset, sampleSize, |
| 171 | decodeColorType, requireUnpremul, decodeColorSpace)) { |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 172 | return nullObjectReturn("Failed to decode region."); |
Owen Lin | f970c2e | 2012-04-25 18:49:09 +0800 | [diff] [blame] | 173 | } |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 174 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 175 | // If the client provided options, indicate that the decode was successful. |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 176 | if (NULL != options) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 177 | env->SetIntField(options, gOptions_widthFieldID, bitmap.width()); |
| 178 | env->SetIntField(options, gOptions_heightFieldID, bitmap.height()); |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 179 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 180 | env->SetObjectField(options, gOptions_mimeFieldID, |
Leon Scroggins III | 407b544 | 2019-11-22 17:10:20 -0500 | [diff] [blame] | 181 | getMimeTypeAsJavaString(env, brd->getEncodedFormat())); |
Matt Sarett | d31512b | 2015-12-09 15:16:31 -0500 | [diff] [blame] | 182 | if (env->ExceptionCheck()) { |
| 183 | return nullObjectReturn("OOM in encodedFormatToString()"); |
| 184 | } |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 185 | |
| 186 | jint configID = GraphicsJNI::colorTypeToLegacyBitmapConfig(decodeColorType); |
| 187 | if (isHardware) { |
| 188 | configID = GraphicsJNI::kHardware_LegacyBitmapConfig; |
| 189 | } |
| 190 | jobject config = env->CallStaticObjectMethod(gBitmapConfig_class, |
| 191 | gBitmapConfig_nativeToConfigMethodID, configID); |
| 192 | env->SetObjectField(options, gOptions_outConfigFieldID, config); |
| 193 | |
| 194 | env->SetObjectField(options, gOptions_outColorSpaceFieldID, |
Derek Sollenberger | bf3e464 | 2019-01-30 11:28:27 -0500 | [diff] [blame] | 195 | GraphicsJNI::getColorSpace(env, decodeColorSpace.get(), decodeColorType)); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 196 | } |
| 197 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 198 | // If we may have reused a bitmap, we need to indicate that the pixels have changed. |
| 199 | if (javaBitmap) { |
| 200 | recycleAlloc.copyIfNecessary(); |
Romain Guy | 5545518 | 2017-04-15 21:41:22 -0700 | [diff] [blame] | 201 | bitmap::reinitBitmap(env, javaBitmap, recycledBitmap->info(), !requireUnpremul); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 202 | return javaBitmap; |
Owen Lin | f970c2e | 2012-04-25 18:49:09 +0800 | [diff] [blame] | 203 | } |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 204 | |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 205 | int bitmapCreateFlags = 0; |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 206 | if (!requireUnpremul) { |
sergeyv | c69853c | 2016-10-07 14:14:09 -0700 | [diff] [blame] | 207 | bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Premultiplied; |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 208 | } |
sergeyv | da6c8ffc | 2016-11-22 18:28:54 -0800 | [diff] [blame] | 209 | if (isHardware) { |
| 210 | sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(bitmap); |
| 211 | return bitmap::createBitmap(env, hardwareBitmap.release(), bitmapCreateFlags); |
| 212 | } |
sergeyv | c69853c | 2016-10-07 14:14:09 -0700 | [diff] [blame] | 213 | return android::bitmap::createBitmap(env, heapAlloc.getStorageObjAndReset(), bitmapCreateFlags); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 214 | } |
| 215 | |
Ashok Bhat | b091d47 | 2014-01-08 14:32:49 +0000 | [diff] [blame] | 216 | static jint nativeGetHeight(JNIEnv* env, jobject, jlong brdHandle) { |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 217 | auto* brd = reinterpret_cast<skia::BitmapRegionDecoder*>(brdHandle); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 218 | return static_cast<jint>(brd->height()); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 219 | } |
| 220 | |
Ashok Bhat | b091d47 | 2014-01-08 14:32:49 +0000 | [diff] [blame] | 221 | static jint nativeGetWidth(JNIEnv* env, jobject, jlong brdHandle) { |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 222 | auto* brd = reinterpret_cast<skia::BitmapRegionDecoder*>(brdHandle); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 223 | return static_cast<jint>(brd->width()); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 224 | } |
| 225 | |
Ashok Bhat | b091d47 | 2014-01-08 14:32:49 +0000 | [diff] [blame] | 226 | static void nativeClean(JNIEnv* env, jobject, jlong brdHandle) { |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 227 | auto* brd = reinterpret_cast<skia::BitmapRegionDecoder*>(brdHandle); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 228 | delete brd; |
| 229 | } |
| 230 | |
| 231 | /////////////////////////////////////////////////////////////////////////////// |
| 232 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 233 | static const JNINativeMethod gBitmapRegionDecoderMethods[] = { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 234 | { "nativeDecodeRegion", |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 235 | "(JIIIILandroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;", |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 236 | (void*)nativeDecodeRegion}, |
| 237 | |
Ashok Bhat | b091d47 | 2014-01-08 14:32:49 +0000 | [diff] [blame] | 238 | { "nativeGetHeight", "(J)I", (void*)nativeGetHeight}, |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 239 | |
Ashok Bhat | b091d47 | 2014-01-08 14:32:49 +0000 | [diff] [blame] | 240 | { "nativeGetWidth", "(J)I", (void*)nativeGetWidth}, |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 241 | |
Ashok Bhat | b091d47 | 2014-01-08 14:32:49 +0000 | [diff] [blame] | 242 | { "nativeClean", "(J)V", (void*)nativeClean}, |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 243 | |
| 244 | { "nativeNewInstance", |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 245 | "([BII)Landroid/graphics/BitmapRegionDecoder;", |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 246 | (void*)nativeNewInstanceFromByteArray |
| 247 | }, |
| 248 | |
| 249 | { "nativeNewInstance", |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 250 | "(Ljava/io/InputStream;[B)Landroid/graphics/BitmapRegionDecoder;", |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 251 | (void*)nativeNewInstanceFromStream |
| 252 | }, |
| 253 | |
| 254 | { "nativeNewInstance", |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 255 | "(Ljava/io/FileDescriptor;)Landroid/graphics/BitmapRegionDecoder;", |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 256 | (void*)nativeNewInstanceFromFileDescriptor |
| 257 | }, |
| 258 | |
| 259 | { "nativeNewInstance", |
Leon Scroggins III | 96a1388 | 2020-03-04 10:29:04 -0500 | [diff] [blame] | 260 | "(J)Landroid/graphics/BitmapRegionDecoder;", |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 261 | (void*)nativeNewInstanceFromAsset |
| 262 | }, |
| 263 | }; |
| 264 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 265 | int register_android_graphics_BitmapRegionDecoder(JNIEnv* env) |
| 266 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 267 | return android::RegisterMethodsOrDie(env, "android/graphics/BitmapRegionDecoder", |
| 268 | gBitmapRegionDecoderMethods, NELEM(gBitmapRegionDecoderMethods)); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 269 | } |