blob: 258bf91f212425840f250201e60120714a5aa97c [file] [log] [blame]
Derek Sollenbergerc5882c42019-10-25 11:11:32 -04001#include <assert.h>
Alec Mouri7dcb7d22024-07-26 13:41:04 +00002#include <cutils/ashmem.h>
3#include <hwui/Canvas.h>
4#include <log/log.h>
5#include <nativehelper/JNIHelp.h>
Riley Andrews39d7f302014-11-13 17:43:25 -08006#include <unistd.h>
Riley Andrews39d7f302014-11-13 17:43:25 -08007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008#include "GraphicsJNI.h"
Kevin Lubick1175dc02022-02-28 12:41:27 -05009#include "SkBitmap.h"
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080010#include "SkCanvas.h"
Alec Mouri7dcb7d22024-07-26 13:41:04 +000011#include "SkColor.h"
Kevin Lubick1175dc02022-02-28 12:41:27 -050012#include "SkColorSpace.h"
Seigo Nonaka1ed4f642020-09-10 17:19:34 -070013#include "SkFontMetrics.h"
Kevin Lubick1175dc02022-02-28 12:41:27 -050014#include "SkImageInfo.h"
Kevin Lubick1175dc02022-02-28 12:41:27 -050015#include "SkPixelRef.h"
16#include "SkPoint.h"
17#include "SkRect.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018#include "SkRegion.h"
Alec Mouri7dcb7d22024-07-26 13:41:04 +000019#include "SkSamplingOptions.h"
Kevin Lubick1175dc02022-02-28 12:41:27 -050020#include "SkTypes.h"
Alec Mouri7dcb7d22024-07-26 13:41:04 +000021#include "jni.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022
Romain Guy95648b82017-04-13 18:43:42 -070023using namespace android;
24
Derek Sollenbergerc5882c42019-10-25 11:11:32 -040025/*static*/ JavaVM* GraphicsJNI::mJavaVM = nullptr;
26
27void GraphicsJNI::setJavaVM(JavaVM* javaVM) {
28 mJavaVM = javaVM;
29}
30
31/** return a pointer to the JNIEnv for this thread */
32JNIEnv* GraphicsJNI::getJNIEnv() {
33 assert(mJavaVM != nullptr);
34 JNIEnv* env;
35 if (mJavaVM->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
36 return nullptr;
37 }
38 return env;
39}
40
41/** create a JNIEnv* for this thread or assert if one already exists */
42JNIEnv* GraphicsJNI::attachJNIEnv(const char* envName) {
43 assert(getJNIEnv() == nullptr);
44 JNIEnv* env = nullptr;
45 JavaVMAttachArgs args = { JNI_VERSION_1_4, envName, NULL };
46 int result = mJavaVM->AttachCurrentThread(&env, (void*) &args);
47 if (result != JNI_OK) {
48 ALOGE("thread attach failed: %#x", result);
49 }
50 return env;
51}
52
53/** detach the current thread from the JavaVM */
54void GraphicsJNI::detachJNIEnv() {
55 assert(mJavaVM != nullptr);
56 mJavaVM->DetachCurrentThread();
57}
58
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059void doThrowNPE(JNIEnv* env) {
Elliott Hughes69a017b2011-04-08 14:10:28 -070060 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061}
62
63void doThrowAIOOBE(JNIEnv* env) {
Elliott Hughes8451b252011-04-07 19:17:57 -070064 jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065}
66
67void doThrowRE(JNIEnv* env, const char* msg) {
Elliott Hughes69a017b2011-04-08 14:10:28 -070068 jniThrowRuntimeException(env, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069}
70
71void doThrowIAE(JNIEnv* env, const char* msg) {
Elliott Hughes8451b252011-04-07 19:17:57 -070072 jniThrowException(env, "java/lang/IllegalArgumentException", msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073}
74
75void doThrowISE(JNIEnv* env, const char* msg) {
Elliott Hughes8451b252011-04-07 19:17:57 -070076 jniThrowException(env, "java/lang/IllegalStateException", msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077}
78
79void doThrowOOME(JNIEnv* env, const char* msg) {
Elliott Hughes8451b252011-04-07 19:17:57 -070080 jniThrowException(env, "java/lang/OutOfMemoryError", msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081}
82
Joseph Wenf1f48bc2010-07-19 16:59:51 +080083void doThrowIOE(JNIEnv* env, const char* msg) {
Elliott Hughes8451b252011-04-07 19:17:57 -070084 jniThrowException(env, "java/io/IOException", msg);
Joseph Wenf1f48bc2010-07-19 16:59:51 +080085}
86
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087bool GraphicsJNI::hasException(JNIEnv *env) {
88 if (env->ExceptionCheck() != 0) {
Steve Block3762c312012-01-06 19:20:56 +000089 ALOGE("*** Uncaught exception returned from Java call!\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 env->ExceptionDescribe();
91 return true;
92 }
93 return false;
94}
95
96///////////////////////////////////////////////////////////////////////////////
97
98AutoJavaFloatArray::AutoJavaFloatArray(JNIEnv* env, jfloatArray array,
Mike Reedc04851f2009-10-28 15:09:45 -040099 int minLength, JNIAccess access)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100: fEnv(env), fArray(array), fPtr(NULL), fLen(0) {
Ben Wagner1c32772f2017-08-16 14:23:24 -0400101 ALOG_ASSERT(env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 if (array) {
103 fLen = env->GetArrayLength(array);
104 if (fLen < minLength) {
Ben Wagner1c32772f2017-08-16 14:23:24 -0400105 LOG_ALWAYS_FATAL("bad length");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 }
107 fPtr = env->GetFloatArrayElements(array, NULL);
108 }
Mike Reedc04851f2009-10-28 15:09:45 -0400109 fReleaseMode = (access == kRO_JNIAccess) ? JNI_ABORT : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110}
111
112AutoJavaFloatArray::~AutoJavaFloatArray() {
113 if (fPtr) {
Mike Reedc04851f2009-10-28 15:09:45 -0400114 fEnv->ReleaseFloatArrayElements(fArray, fPtr, fReleaseMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 }
116}
117
118AutoJavaIntArray::AutoJavaIntArray(JNIEnv* env, jintArray array,
119 int minLength)
120: fEnv(env), fArray(array), fPtr(NULL), fLen(0) {
Ben Wagner1c32772f2017-08-16 14:23:24 -0400121 ALOG_ASSERT(env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 if (array) {
123 fLen = env->GetArrayLength(array);
124 if (fLen < minLength) {
Ben Wagner1c32772f2017-08-16 14:23:24 -0400125 LOG_ALWAYS_FATAL("bad length");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 }
127 fPtr = env->GetIntArrayElements(array, NULL);
128 }
129}
130
131AutoJavaIntArray::~AutoJavaIntArray() {
132 if (fPtr) {
133 fEnv->ReleaseIntArrayElements(fArray, fPtr, 0);
134 }
135}
136
137AutoJavaShortArray::AutoJavaShortArray(JNIEnv* env, jshortArray array,
Mike Reedc04851f2009-10-28 15:09:45 -0400138 int minLength, JNIAccess access)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139: fEnv(env), fArray(array), fPtr(NULL), fLen(0) {
Ben Wagner1c32772f2017-08-16 14:23:24 -0400140 ALOG_ASSERT(env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 if (array) {
142 fLen = env->GetArrayLength(array);
143 if (fLen < minLength) {
Ben Wagner1c32772f2017-08-16 14:23:24 -0400144 LOG_ALWAYS_FATAL("bad length");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 }
146 fPtr = env->GetShortArrayElements(array, NULL);
147 }
Mike Reedc04851f2009-10-28 15:09:45 -0400148 fReleaseMode = (access == kRO_JNIAccess) ? JNI_ABORT : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149}
150
151AutoJavaShortArray::~AutoJavaShortArray() {
152 if (fPtr) {
Mike Reedc04851f2009-10-28 15:09:45 -0400153 fEnv->ReleaseShortArrayElements(fArray, fPtr, fReleaseMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 }
155}
156
157AutoJavaByteArray::AutoJavaByteArray(JNIEnv* env, jbyteArray array,
158 int minLength)
159: fEnv(env), fArray(array), fPtr(NULL), fLen(0) {
Ben Wagner1c32772f2017-08-16 14:23:24 -0400160 ALOG_ASSERT(env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 if (array) {
162 fLen = env->GetArrayLength(array);
163 if (fLen < minLength) {
Ben Wagner1c32772f2017-08-16 14:23:24 -0400164 LOG_ALWAYS_FATAL("bad length");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 }
166 fPtr = env->GetByteArrayElements(array, NULL);
167 }
168}
169
170AutoJavaByteArray::~AutoJavaByteArray() {
171 if (fPtr) {
172 fEnv->ReleaseByteArrayElements(fArray, fPtr, 0);
173 }
174}
175
176///////////////////////////////////////////////////////////////////////////////
177
178static jclass gRect_class;
179static jfieldID gRect_leftFieldID;
180static jfieldID gRect_topFieldID;
181static jfieldID gRect_rightFieldID;
182static jfieldID gRect_bottomFieldID;
183
184static jclass gRectF_class;
185static jfieldID gRectF_leftFieldID;
186static jfieldID gRectF_topFieldID;
187static jfieldID gRectF_rightFieldID;
188static jfieldID gRectF_bottomFieldID;
189
190static jclass gPoint_class;
191static jfieldID gPoint_xFieldID;
192static jfieldID gPoint_yFieldID;
193
194static jclass gPointF_class;
195static jfieldID gPointF_xFieldID;
196static jfieldID gPointF_yFieldID;
197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198static jclass gBitmapConfig_class;
199static jfieldID gBitmapConfig_nativeInstanceID;
Derek Sollenberger213daca2019-10-25 14:17:32 -0400200static jmethodID gBitmapConfig_nativeToConfigMethodID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800202static jclass gBitmapRegionDecoder_class;
203static jmethodID gBitmapRegionDecoder_constructorMethodID;
Joseph Wenf1f48bc2010-07-19 16:59:51 +0800204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205static jclass gCanvas_class;
206static jfieldID gCanvas_nativeInstanceID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208static jclass gPicture_class;
209static jfieldID gPicture_nativeInstanceID;
210
211static jclass gRegion_class;
212static jfieldID gRegion_nativeInstanceID;
213static jmethodID gRegion_constructorMethodID;
214
Makoto Onukie4a54a82024-05-02 08:43:05 -0700215static jclass gByte_class;
Mathieu Chartier7384b422013-10-17 18:16:42 -0700216
Romain Guy95648b82017-04-13 18:43:42 -0700217static jclass gColorSpace_class;
Romain Guy95648b82017-04-13 18:43:42 -0700218static jmethodID gColorSpace_getMethodID;
219static jmethodID gColorSpace_matchMethodID;
220
221static jclass gColorSpaceRGB_class;
Romain Guy95648b82017-04-13 18:43:42 -0700222static jmethodID gColorSpaceRGB_constructorMethodID;
223
224static jclass gColorSpace_Named_class;
225static jfieldID gColorSpace_Named_sRGBFieldID;
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500226static jfieldID gColorSpace_Named_ExtendedSRGBFieldID;
227static jfieldID gColorSpace_Named_LinearSRGBFieldID;
Romain Guy95648b82017-04-13 18:43:42 -0700228static jfieldID gColorSpace_Named_LinearExtendedSRGBFieldID;
229
230static jclass gTransferParameters_class;
231static jmethodID gTransferParameters_constructorMethodID;
232
Seigo Nonaka1ed4f642020-09-10 17:19:34 -0700233static jclass gFontMetrics_class;
234static jfieldID gFontMetrics_top;
235static jfieldID gFontMetrics_ascent;
236static jfieldID gFontMetrics_descent;
237static jfieldID gFontMetrics_bottom;
238static jfieldID gFontMetrics_leading;
239
240static jclass gFontMetricsInt_class;
241static jfieldID gFontMetricsInt_top;
242static jfieldID gFontMetricsInt_ascent;
243static jfieldID gFontMetricsInt_descent;
244static jfieldID gFontMetricsInt_bottom;
245static jfieldID gFontMetricsInt_leading;
246
Seigo Nonakadd617182023-12-07 08:56:31 +0900247static jclass gRunInfo_class;
248static jfieldID gRunInfo_clusterCount;
249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250///////////////////////////////////////////////////////////////////////////////
251
252void GraphicsJNI::get_jrect(JNIEnv* env, jobject obj, int* L, int* T, int* R, int* B)
253{
Ben Wagner1c32772f2017-08-16 14:23:24 -0400254 ALOG_ASSERT(env->IsInstanceOf(obj, gRect_class));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255
256 *L = env->GetIntField(obj, gRect_leftFieldID);
257 *T = env->GetIntField(obj, gRect_topFieldID);
258 *R = env->GetIntField(obj, gRect_rightFieldID);
259 *B = env->GetIntField(obj, gRect_bottomFieldID);
260}
261
262void GraphicsJNI::set_jrect(JNIEnv* env, jobject obj, int L, int T, int R, int B)
263{
Ben Wagner1c32772f2017-08-16 14:23:24 -0400264 ALOG_ASSERT(env->IsInstanceOf(obj, gRect_class));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265
266 env->SetIntField(obj, gRect_leftFieldID, L);
267 env->SetIntField(obj, gRect_topFieldID, T);
268 env->SetIntField(obj, gRect_rightFieldID, R);
269 env->SetIntField(obj, gRect_bottomFieldID, B);
270}
271
272SkIRect* GraphicsJNI::jrect_to_irect(JNIEnv* env, jobject obj, SkIRect* ir)
273{
Ben Wagner1c32772f2017-08-16 14:23:24 -0400274 ALOG_ASSERT(env->IsInstanceOf(obj, gRect_class));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275
Mike Reed39adc882019-08-22 11:53:05 -0400276 ir->setLTRB(env->GetIntField(obj, gRect_leftFieldID),
277 env->GetIntField(obj, gRect_topFieldID),
278 env->GetIntField(obj, gRect_rightFieldID),
279 env->GetIntField(obj, gRect_bottomFieldID));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 return ir;
281}
282
283void GraphicsJNI::irect_to_jrect(const SkIRect& ir, JNIEnv* env, jobject obj)
284{
Ben Wagner1c32772f2017-08-16 14:23:24 -0400285 ALOG_ASSERT(env->IsInstanceOf(obj, gRect_class));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286
287 env->SetIntField(obj, gRect_leftFieldID, ir.fLeft);
288 env->SetIntField(obj, gRect_topFieldID, ir.fTop);
289 env->SetIntField(obj, gRect_rightFieldID, ir.fRight);
290 env->SetIntField(obj, gRect_bottomFieldID, ir.fBottom);
291}
292
293SkRect* GraphicsJNI::jrectf_to_rect(JNIEnv* env, jobject obj, SkRect* r)
294{
Ben Wagner1c32772f2017-08-16 14:23:24 -0400295 ALOG_ASSERT(env->IsInstanceOf(obj, gRectF_class));
Elliott Hughes8451b252011-04-07 19:17:57 -0700296
Mike Reed39adc882019-08-22 11:53:05 -0400297 r->setLTRB(env->GetFloatField(obj, gRectF_leftFieldID),
298 env->GetFloatField(obj, gRectF_topFieldID),
299 env->GetFloatField(obj, gRectF_rightFieldID),
300 env->GetFloatField(obj, gRectF_bottomFieldID));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 return r;
302}
303
304SkRect* GraphicsJNI::jrect_to_rect(JNIEnv* env, jobject obj, SkRect* r)
305{
Ben Wagner1c32772f2017-08-16 14:23:24 -0400306 ALOG_ASSERT(env->IsInstanceOf(obj, gRect_class));
Elliott Hughes8451b252011-04-07 19:17:57 -0700307
Mike Reed39adc882019-08-22 11:53:05 -0400308 r->setLTRB(SkIntToScalar(env->GetIntField(obj, gRect_leftFieldID)),
309 SkIntToScalar(env->GetIntField(obj, gRect_topFieldID)),
310 SkIntToScalar(env->GetIntField(obj, gRect_rightFieldID)),
311 SkIntToScalar(env->GetIntField(obj, gRect_bottomFieldID)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 return r;
313}
314
315void GraphicsJNI::rect_to_jrectf(const SkRect& r, JNIEnv* env, jobject obj)
316{
Ben Wagner1c32772f2017-08-16 14:23:24 -0400317 ALOG_ASSERT(env->IsInstanceOf(obj, gRectF_class));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318
319 env->SetFloatField(obj, gRectF_leftFieldID, SkScalarToFloat(r.fLeft));
320 env->SetFloatField(obj, gRectF_topFieldID, SkScalarToFloat(r.fTop));
321 env->SetFloatField(obj, gRectF_rightFieldID, SkScalarToFloat(r.fRight));
322 env->SetFloatField(obj, gRectF_bottomFieldID, SkScalarToFloat(r.fBottom));
323}
324
325SkIPoint* GraphicsJNI::jpoint_to_ipoint(JNIEnv* env, jobject obj, SkIPoint* point)
326{
Ben Wagner1c32772f2017-08-16 14:23:24 -0400327 ALOG_ASSERT(env->IsInstanceOf(obj, gPoint_class));
Elliott Hughes8451b252011-04-07 19:17:57 -0700328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 point->set(env->GetIntField(obj, gPoint_xFieldID),
330 env->GetIntField(obj, gPoint_yFieldID));
331 return point;
332}
333
334void GraphicsJNI::ipoint_to_jpoint(const SkIPoint& ir, JNIEnv* env, jobject obj)
335{
Ben Wagner1c32772f2017-08-16 14:23:24 -0400336 ALOG_ASSERT(env->IsInstanceOf(obj, gPoint_class));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337
338 env->SetIntField(obj, gPoint_xFieldID, ir.fX);
339 env->SetIntField(obj, gPoint_yFieldID, ir.fY);
340}
341
342SkPoint* GraphicsJNI::jpointf_to_point(JNIEnv* env, jobject obj, SkPoint* point)
343{
Ben Wagner1c32772f2017-08-16 14:23:24 -0400344 ALOG_ASSERT(env->IsInstanceOf(obj, gPointF_class));
Elliott Hughes8451b252011-04-07 19:17:57 -0700345
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400346 point->set(env->GetIntField(obj, gPointF_xFieldID),
347 env->GetIntField(obj, gPointF_yFieldID));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 return point;
349}
350
351void GraphicsJNI::point_to_jpointf(const SkPoint& r, JNIEnv* env, jobject obj)
352{
Ben Wagner1c32772f2017-08-16 14:23:24 -0400353 ALOG_ASSERT(env->IsInstanceOf(obj, gPointF_class));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354
355 env->SetFloatField(obj, gPointF_xFieldID, SkScalarToFloat(r.fX));
356 env->SetFloatField(obj, gPointF_yFieldID, SkScalarToFloat(r.fY));
357}
358
Romain Guye8d2ebb2017-02-09 18:38:47 -0800359// See enum values in GraphicsJNI.h
Mike Reed1103b322014-07-08 12:36:44 -0400360jint GraphicsJNI::colorTypeToLegacyBitmapConfig(SkColorType colorType) {
361 switch (colorType) {
Romain Guy9505a652016-12-14 09:43:50 -0800362 case kRGBA_F16_SkColorType:
363 return kRGBA_16F_LegacyBitmapConfig;
Mike Reed1103b322014-07-08 12:36:44 -0400364 case kN32_SkColorType:
365 return kARGB_8888_LegacyBitmapConfig;
366 case kARGB_4444_SkColorType:
367 return kARGB_4444_LegacyBitmapConfig;
368 case kRGB_565_SkColorType:
369 return kRGB_565_LegacyBitmapConfig;
Mike Reed1103b322014-07-08 12:36:44 -0400370 case kAlpha_8_SkColorType:
371 return kA8_LegacyBitmapConfig;
Alec Mouri1efd0a52022-01-20 13:58:23 -0800372 case kRGBA_1010102_SkColorType:
373 return kRGBA_1010102_LegacyBitmapConfig;
Mike Reed1103b322014-07-08 12:36:44 -0400374 case kUnknown_SkColorType:
375 default:
376 break;
377 }
378 return kNo_LegacyBitmapConfig;
379}
380
381SkColorType GraphicsJNI::legacyBitmapConfigToColorType(jint legacyConfig) {
382 const uint8_t gConfig2ColorType[] = {
Alec Mouri1efd0a52022-01-20 13:58:23 -0800383 kUnknown_SkColorType, kAlpha_8_SkColorType,
384 kUnknown_SkColorType, // Previously kIndex_8_SkColorType,
385 kRGB_565_SkColorType, kARGB_4444_SkColorType, kN32_SkColorType,
386 kRGBA_F16_SkColorType, kN32_SkColorType, kRGBA_1010102_SkColorType,
Mike Reed1103b322014-07-08 12:36:44 -0400387 };
388
389 if (legacyConfig < 0 || legacyConfig > kLastEnum_LegacyBitmapConfig) {
390 legacyConfig = kNo_LegacyBitmapConfig;
391 }
392 return static_cast<SkColorType>(gConfig2ColorType[legacyConfig]);
393}
394
Derek Sollenberger213daca2019-10-25 14:17:32 -0400395AndroidBitmapFormat GraphicsJNI::getFormatFromConfig(JNIEnv* env, jobject jconfig) {
396 ALOG_ASSERT(env);
397 if (NULL == jconfig) {
398 return ANDROID_BITMAP_FORMAT_NONE;
399 }
400 ALOG_ASSERT(env->IsInstanceOf(jconfig, gBitmapConfig_class));
401 jint javaConfigId = env->GetIntField(jconfig, gBitmapConfig_nativeInstanceID);
402
403 const AndroidBitmapFormat config2BitmapFormat[] = {
Alec Mouri1efd0a52022-01-20 13:58:23 -0800404 ANDROID_BITMAP_FORMAT_NONE, ANDROID_BITMAP_FORMAT_A_8,
405 ANDROID_BITMAP_FORMAT_NONE, // Previously Config.Index_8
406 ANDROID_BITMAP_FORMAT_RGB_565, ANDROID_BITMAP_FORMAT_RGBA_4444,
407 ANDROID_BITMAP_FORMAT_RGBA_8888, ANDROID_BITMAP_FORMAT_RGBA_F16,
408 ANDROID_BITMAP_FORMAT_NONE, // Congfig.HARDWARE
409 ANDROID_BITMAP_FORMAT_RGBA_1010102};
Derek Sollenberger213daca2019-10-25 14:17:32 -0400410 return config2BitmapFormat[javaConfigId];
411}
412
413jobject GraphicsJNI::getConfigFromFormat(JNIEnv* env, AndroidBitmapFormat format) {
414 ALOG_ASSERT(env);
415 jint configId = kNo_LegacyBitmapConfig;
416 switch (format) {
417 case ANDROID_BITMAP_FORMAT_A_8:
418 configId = kA8_LegacyBitmapConfig;
419 break;
420 case ANDROID_BITMAP_FORMAT_RGB_565:
421 configId = kRGB_565_LegacyBitmapConfig;
422 break;
423 case ANDROID_BITMAP_FORMAT_RGBA_4444:
424 configId = kARGB_4444_LegacyBitmapConfig;
425 break;
426 case ANDROID_BITMAP_FORMAT_RGBA_8888:
427 configId = kARGB_8888_LegacyBitmapConfig;
428 break;
429 case ANDROID_BITMAP_FORMAT_RGBA_F16:
430 configId = kRGBA_16F_LegacyBitmapConfig;
431 break;
Alec Mouri1efd0a52022-01-20 13:58:23 -0800432 case ANDROID_BITMAP_FORMAT_RGBA_1010102:
433 configId = kRGBA_1010102_LegacyBitmapConfig;
434 break;
Derek Sollenberger213daca2019-10-25 14:17:32 -0400435 default:
436 break;
437 }
438
439 return env->CallStaticObjectMethod(gBitmapConfig_class,
440 gBitmapConfig_nativeToConfigMethodID, configId);
441}
442
Mike Reed42a1d082014-07-07 18:06:18 -0400443SkColorType GraphicsJNI::getNativeBitmapColorType(JNIEnv* env, jobject jconfig) {
Ben Wagner1c32772f2017-08-16 14:23:24 -0400444 ALOG_ASSERT(env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 if (NULL == jconfig) {
Mike Reed42a1d082014-07-07 18:06:18 -0400446 return kUnknown_SkColorType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 }
Ben Wagner1c32772f2017-08-16 14:23:24 -0400448 ALOG_ASSERT(env->IsInstanceOf(jconfig, gBitmapConfig_class));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 int c = env->GetIntField(jconfig, gBitmapConfig_nativeInstanceID);
Mike Reed1103b322014-07-08 12:36:44 -0400450 return legacyBitmapConfigToColorType(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451}
452
sergeyvda6c8ffc2016-11-22 18:28:54 -0800453bool GraphicsJNI::isHardwareConfig(JNIEnv* env, jobject jconfig) {
Ben Wagner1c32772f2017-08-16 14:23:24 -0400454 ALOG_ASSERT(env);
sergeyvda6c8ffc2016-11-22 18:28:54 -0800455 if (NULL == jconfig) {
456 return false;
457 }
458 int c = env->GetIntField(jconfig, gBitmapConfig_nativeInstanceID);
459 return c == kHardware_LegacyBitmapConfig;
460}
461
sergeyv19b4b012016-12-13 16:06:00 -0800462jint GraphicsJNI::hardwareLegacyBitmapConfig() {
463 return kHardware_LegacyBitmapConfig;
464}
465
John Reckc1b33d62015-04-22 09:04:45 -0700466android::Canvas* GraphicsJNI::getNativeCanvas(JNIEnv* env, jobject canvas) {
Ben Wagner1c32772f2017-08-16 14:23:24 -0400467 ALOG_ASSERT(env);
468 ALOG_ASSERT(canvas);
469 ALOG_ASSERT(env->IsInstanceOf(canvas, gCanvas_class));
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000470 jlong canvasHandle = env->GetLongField(canvas, gCanvas_nativeInstanceID);
Bo Liude92f4c2014-11-24 10:53:52 -0800471 if (!canvasHandle) {
472 return NULL;
473 }
John Reckc1b33d62015-04-22 09:04:45 -0700474 return reinterpret_cast<android::Canvas*>(canvasHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475}
476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477SkRegion* GraphicsJNI::getNativeRegion(JNIEnv* env, jobject region)
478{
Ben Wagner1c32772f2017-08-16 14:23:24 -0400479 ALOG_ASSERT(env);
480 ALOG_ASSERT(region);
481 ALOG_ASSERT(env->IsInstanceOf(region, gRegion_class));
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000482 jlong regionHandle = env->GetLongField(region, gRegion_nativeInstanceID);
483 SkRegion* r = reinterpret_cast<SkRegion*>(regionHandle);
Ben Wagner1c32772f2017-08-16 14:23:24 -0400484 ALOG_ASSERT(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 return r;
486}
487
Seigo Nonaka1ed4f642020-09-10 17:19:34 -0700488void GraphicsJNI::set_metrics(JNIEnv* env, jobject metrics, const SkFontMetrics& skmetrics) {
489 if (metrics == nullptr) return;
Kevin Lubick40ba5e52023-01-18 15:37:14 +0000490 LOG_FATAL_IF(!env->IsInstanceOf(metrics, gFontMetrics_class));
Seigo Nonaka1ed4f642020-09-10 17:19:34 -0700491 env->SetFloatField(metrics, gFontMetrics_top, SkScalarToFloat(skmetrics.fTop));
492 env->SetFloatField(metrics, gFontMetrics_ascent, SkScalarToFloat(skmetrics.fAscent));
493 env->SetFloatField(metrics, gFontMetrics_descent, SkScalarToFloat(skmetrics.fDescent));
494 env->SetFloatField(metrics, gFontMetrics_bottom, SkScalarToFloat(skmetrics.fBottom));
495 env->SetFloatField(metrics, gFontMetrics_leading, SkScalarToFloat(skmetrics.fLeading));
496}
497
498int GraphicsJNI::set_metrics_int(JNIEnv* env, jobject metrics, const SkFontMetrics& skmetrics) {
499 int ascent = SkScalarRoundToInt(skmetrics.fAscent);
500 int descent = SkScalarRoundToInt(skmetrics.fDescent);
501 int leading = SkScalarRoundToInt(skmetrics.fLeading);
502
503 if (metrics) {
Kevin Lubick40ba5e52023-01-18 15:37:14 +0000504 LOG_FATAL_IF(!env->IsInstanceOf(metrics, gFontMetricsInt_class));
Seigo Nonaka1ed4f642020-09-10 17:19:34 -0700505 env->SetIntField(metrics, gFontMetricsInt_top, SkScalarFloorToInt(skmetrics.fTop));
506 env->SetIntField(metrics, gFontMetricsInt_ascent, ascent);
507 env->SetIntField(metrics, gFontMetricsInt_descent, descent);
508 env->SetIntField(metrics, gFontMetricsInt_bottom, SkScalarCeilToInt(skmetrics.fBottom));
509 env->SetIntField(metrics, gFontMetricsInt_leading, leading);
510 }
511 return descent - ascent + leading;
512}
513
Seigo Nonakadd617182023-12-07 08:56:31 +0900514void GraphicsJNI::set_cluster_count_to_run_info(JNIEnv* env, jobject runInfo, jint clusterCount) {
515 env->SetIntField(runInfo, gRunInfo_clusterCount, clusterCount);
516}
517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518///////////////////////////////////////////////////////////////////////////////////////////
519
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000520jobject GraphicsJNI::createBitmapRegionDecoder(JNIEnv* env, BitmapRegionDecoderWrapper* bitmap) {
Ben Wagner1c32772f2017-08-16 14:23:24 -0400521 ALOG_ASSERT(bitmap != NULL);
Joseph Wenf1f48bc2010-07-19 16:59:51 +0800522
Elliott Hughescf6f7a02011-04-12 17:50:45 -0700523 jobject obj = env->NewObject(gBitmapRegionDecoder_class,
524 gBitmapRegionDecoder_constructorMethodID,
Ashok Bhatb091d472014-01-08 14:32:49 +0000525 reinterpret_cast<jlong>(bitmap));
Elliott Hughescf6f7a02011-04-12 17:50:45 -0700526 hasException(env); // For the side effect of logging.
Joseph Wenf1f48bc2010-07-19 16:59:51 +0800527 return obj;
528}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529
530jobject GraphicsJNI::createRegion(JNIEnv* env, SkRegion* region)
531{
Ben Wagner1c32772f2017-08-16 14:23:24 -0400532 ALOG_ASSERT(region != NULL);
Elliott Hughescf6f7a02011-04-12 17:50:45 -0700533 jobject obj = env->NewObject(gRegion_class, gRegion_constructorMethodID,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000534 reinterpret_cast<jlong>(region), 0);
Elliott Hughescf6f7a02011-04-12 17:50:45 -0700535 hasException(env); // For the side effect of logging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 return obj;
537}
538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539///////////////////////////////////////////////////////////////////////////////
540
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500541jobject GraphicsJNI::getColorSpace(JNIEnv* env, SkColorSpace* decodeColorSpace,
Romain Guy95648b82017-04-13 18:43:42 -0700542 SkColorType decodeColorType) {
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500543 if (!decodeColorSpace || decodeColorType == kAlpha_8_SkColorType) {
544 return nullptr;
Romain Guy95648b82017-04-13 18:43:42 -0700545 }
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500546
547 // Special checks for the common sRGB cases and their extended variants.
548 jobject namedCS = nullptr;
549 sk_sp<SkColorSpace> srgbLinear = SkColorSpace::MakeSRGBLinear();
550 if (decodeColorType == kRGBA_F16_SkColorType) {
551 // An F16 Bitmap will always report that it is EXTENDED if
552 // it matches a ColorSpace that has an EXTENDED variant.
553 if (decodeColorSpace->isSRGB()) {
554 namedCS = env->GetStaticObjectField(gColorSpace_Named_class,
555 gColorSpace_Named_ExtendedSRGBFieldID);
556 } else if (decodeColorSpace == srgbLinear.get()) {
557 namedCS = env->GetStaticObjectField(gColorSpace_Named_class,
558 gColorSpace_Named_LinearExtendedSRGBFieldID);
559 }
560 } else if (decodeColorSpace->isSRGB()) {
561 namedCS = env->GetStaticObjectField(gColorSpace_Named_class,
562 gColorSpace_Named_sRGBFieldID);
563 } else if (decodeColorSpace == srgbLinear.get()) {
564 namedCS = env->GetStaticObjectField(gColorSpace_Named_class,
565 gColorSpace_Named_LinearSRGBFieldID);
566 }
567
568 if (namedCS) {
569 return env->CallStaticObjectMethod(gColorSpace_class, gColorSpace_getMethodID, namedCS);
570 }
571
572 // Try to match against known RGB color spaces using the CIE XYZ D50
573 // conversion matrix and numerical transfer function parameters
574 skcms_Matrix3x3 xyzMatrix;
575 LOG_ALWAYS_FATAL_IF(!decodeColorSpace->toXYZD50(&xyzMatrix));
576
577 skcms_TransferFunction transferParams;
Sally Qi12d371a2022-10-26 17:30:26 -0700578 decodeColorSpace->transferFn(&transferParams);
579 auto res = skcms_TransferFunction_getType(&transferParams);
580 LOG_ALWAYS_FATAL_IF(res == skcms_TFType_HLGinvish || res == skcms_TFType_Invalid);
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500581
Sally Qi12d371a2022-10-26 17:30:26 -0700582 jobject params;
John Recke4a3d632023-03-15 23:16:21 -0400583 params = env->NewObject(gTransferParameters_class, gTransferParameters_constructorMethodID,
584 transferParams.a, transferParams.b, transferParams.c, transferParams.d,
585 transferParams.e, transferParams.f, transferParams.g);
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500586
Leon Scroggins III3eb6c882021-10-01 16:35:46 -0400587 // Some transfer functions that are considered valid by Skia are not
588 // accepted by android.graphics.
589 if (hasException(env)) {
590 // Callers (e.g. Bitmap#getColorSpace) are not expected to throw an
591 // Exception, so clear it and return null, which is a documented
592 // possibility.
593 env->ExceptionClear();
594 return nullptr;
595 }
596
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500597 jfloatArray xyzArray = env->NewFloatArray(9);
598 jfloat xyz[9] = {
599 xyzMatrix.vals[0][0],
600 xyzMatrix.vals[1][0],
601 xyzMatrix.vals[2][0],
602 xyzMatrix.vals[0][1],
603 xyzMatrix.vals[1][1],
604 xyzMatrix.vals[2][1],
605 xyzMatrix.vals[0][2],
606 xyzMatrix.vals[1][2],
607 xyzMatrix.vals[2][2]
608 };
609 env->SetFloatArrayRegion(xyzArray, 0, 9, xyz);
610
611 jobject colorSpace = env->CallStaticObjectMethod(gColorSpace_class,
612 gColorSpace_matchMethodID, xyzArray, params);
613
614 if (colorSpace == nullptr) {
615 // We couldn't find an exact match, let's create a new color space
616 // instance with the 3x3 conversion matrix and transfer function
617 colorSpace = env->NewObject(gColorSpaceRGB_class,
618 gColorSpaceRGB_constructorMethodID,
619 env->NewStringUTF("Unknown"), xyzArray, params);
620 }
621
622 env->DeleteLocalRef(xyzArray);
Romain Guy95648b82017-04-13 18:43:42 -0700623 return colorSpace;
624}
625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626///////////////////////////////////////////////////////////////////////////////
Mike Reed81397c42017-07-18 17:04:16 -0400627bool HeapAllocator::allocPixelRef(SkBitmap* bitmap) {
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -0400628 mStorage = android::Bitmap::allocateHeapBitmap(bitmap);
sergeyvc69853c2016-10-07 14:14:09 -0700629 return !!mStorage;
Joseph Wenf1f48bc2010-07-19 16:59:51 +0800630}
631
632////////////////////////////////////////////////////////////////////////////////
633
Alec Mouri7dcb7d22024-07-26 13:41:04 +0000634RecyclingClippingPixelAllocator::RecyclingClippingPixelAllocator(
635 android::Bitmap* recycledBitmap, bool mustMatchColorType,
636 std::optional<SkRect> desiredSubset)
John Reck40ffc1d2023-06-20 17:26:09 -0400637 : mRecycledBitmap(recycledBitmap)
638 , mRecycledBytes(recycledBitmap ? recycledBitmap->getAllocationByteCount() : 0)
639 , mSkiaBitmap(nullptr)
640 , mNeedsCopy(false)
Alec Mouri7dcb7d22024-07-26 13:41:04 +0000641 , mMustMatchColorType(mustMatchColorType)
642 , mDesiredSubset(getSourceBoundsForUpsample(desiredSubset)) {}
Matt Sarett1f979632015-10-27 10:33:20 -0400643
644RecyclingClippingPixelAllocator::~RecyclingClippingPixelAllocator() {}
645
Mike Reed81397c42017-07-18 17:04:16 -0400646bool RecyclingClippingPixelAllocator::allocPixelRef(SkBitmap* bitmap) {
Matt Sarett1f979632015-10-27 10:33:20 -0400647 // Ensure that the caller did not pass in a NULL bitmap to the constructor or this
648 // function.
649 LOG_ALWAYS_FATAL_IF(!mRecycledBitmap);
650 LOG_ALWAYS_FATAL_IF(!bitmap);
651 mSkiaBitmap = bitmap;
652
John Reck40ffc1d2023-06-20 17:26:09 -0400653 if (mMustMatchColorType) {
654 // This behaves differently than the RecyclingPixelAllocator. For backwards
655 // compatibility, the original color type of the recycled bitmap must be maintained.
656 if (mRecycledBitmap->info().colorType() != bitmap->colorType()) {
657 ALOGW("recycled color type %d != bitmap color type %d",
658 mRecycledBitmap->info().colorType(), bitmap->colorType());
659 return false;
660 }
661 } else {
662 mRecycledBitmap->reconfigure(mRecycledBitmap->info().makeColorType(bitmap->colorType()));
Matt Sarett1f979632015-10-27 10:33:20 -0400663 }
664
665 // The Skia bitmap specifies the width and height needed by the decoder.
666 // mRecycledBitmap specifies the width and height of the bitmap that we
667 // want to reuse. Neither can be changed. We will try to find a way
668 // to reuse the memory.
Brian Osman070199c2020-02-06 15:33:54 -0500669 const int maxWidth = std::max(bitmap->width(), mRecycledBitmap->info().width());
670 const int maxHeight = std::max(bitmap->height(), mRecycledBitmap->info().height());
Matt Sarett1f979632015-10-27 10:33:20 -0400671 const SkImageInfo maxInfo = bitmap->info().makeWH(maxWidth, maxHeight);
672 const size_t rowBytes = maxInfo.minRowBytes();
Mike Reed7569de02017-10-06 16:25:49 -0400673 const size_t bytesNeeded = maxInfo.computeByteSize(rowBytes);
Alec Mouri7dcb7d22024-07-26 13:41:04 +0000674
675 if (!mDesiredSubset && bytesNeeded <= mRecycledBytes) {
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -0400676 // Here we take advantage of reconfigure() to reset the rowBytes
Matt Sarett1f979632015-10-27 10:33:20 -0400677 // of mRecycledBitmap. It is very important that we pass in
678 // mRecycledBitmap->info() for the SkImageInfo. According to the
679 // specification for BitmapRegionDecoder, we are not allowed to change
680 // the SkImageInfo.
Romain Guy55455182017-04-15 21:41:22 -0700681 // We can (must) preserve the color space since it doesn't affect the
682 // storage needs
683 mRecycledBitmap->reconfigure(
684 mRecycledBitmap->info().makeColorSpace(bitmap->refColorSpace()),
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -0400685 rowBytes);
Matt Sarett1f979632015-10-27 10:33:20 -0400686
Matt Sarettdb4773f2016-05-19 09:23:41 -0400687 // Give the bitmap the same pixelRef as mRecycledBitmap.
688 // skbug.com/4538: We also need to make sure that the rowBytes on the pixel ref
689 // match the rowBytes on the bitmap.
690 bitmap->setInfo(bitmap->info(), rowBytes);
Mike Reed826deef2017-04-04 15:32:04 -0400691 bitmap->setPixelRef(sk_ref_sp(mRecycledBitmap), 0, 0);
Matt Sarett1f979632015-10-27 10:33:20 -0400692
693 // Make sure that the recycled bitmap has the correct alpha type.
694 mRecycledBitmap->setAlphaType(bitmap->alphaType());
695
Derek Sollenbergera9471b12016-03-25 12:19:22 -0400696 bitmap->notifyPixelsChanged();
Matt Sarett1f979632015-10-27 10:33:20 -0400697 mNeedsCopy = false;
698
699 // TODO: If the dimensions of the SkBitmap are smaller than those of
700 // mRecycledBitmap, should we zero the memory in mRecycledBitmap?
701 return true;
702 }
703
704 // In the event that mRecycledBitmap is not large enough, allocate new memory
705 // on the heap.
706 SkBitmap::HeapAllocator heapAllocator;
707
708 // We will need to copy from heap memory to mRecycledBitmap's memory after the
709 // decode is complete.
710 mNeedsCopy = true;
711
Mike Reed81397c42017-07-18 17:04:16 -0400712 return heapAllocator.allocPixelRef(bitmap);
Matt Sarett1f979632015-10-27 10:33:20 -0400713}
714
715void RecyclingClippingPixelAllocator::copyIfNecessary() {
716 if (mNeedsCopy) {
sergeyvc69853c2016-10-07 14:14:09 -0700717 mRecycledBitmap->ref();
John Reck40ffc1d2023-06-20 17:26:09 -0400718 android::Bitmap* recycledPixels = mRecycledBitmap;
Alec Mouri7dcb7d22024-07-26 13:41:04 +0000719 if (mDesiredSubset) {
720 recycledPixels->setAlphaType(mSkiaBitmap->alphaType());
721 recycledPixels->setColorSpace(mSkiaBitmap->refColorSpace());
722
723 auto canvas = SkCanvas(recycledPixels->getSkBitmap());
724 SkRect destination = SkRect::Make(recycledPixels->info().bounds());
725 destination.intersect(SkRect::Make(mSkiaBitmap->info().bounds()));
726 canvas.drawImageRect(mSkiaBitmap->asImage(), *mDesiredSubset, destination,
727 SkSamplingOptions(SkFilterMode::kLinear), nullptr,
728 SkCanvas::kFast_SrcRectConstraint);
729 } else {
730 void* dst = recycledPixels->pixels();
731 const size_t dstRowBytes = mRecycledBitmap->rowBytes();
732 const size_t bytesToCopy = std::min(mRecycledBitmap->info().minRowBytes(),
733 mSkiaBitmap->info().minRowBytes());
734 const int rowsToCopy =
735 std::min(mRecycledBitmap->info().height(), mSkiaBitmap->info().height());
736 for (int y = 0; y < rowsToCopy; y++) {
737 memcpy(dst, mSkiaBitmap->getAddr(0, y), bytesToCopy);
738 // Cast to bytes in order to apply the dstRowBytes offset correctly.
739 dst = reinterpret_cast<void*>(reinterpret_cast<uint8_t*>(dst) + dstRowBytes);
740 }
741 recycledPixels->setAlphaType(mSkiaBitmap->alphaType());
742 recycledPixels->setColorSpace(mSkiaBitmap->refColorSpace());
Matt Sarett1f979632015-10-27 10:33:20 -0400743 }
744 recycledPixels->notifyPixelsChanged();
745 recycledPixels->unref();
746 }
747 mRecycledBitmap = nullptr;
748 mSkiaBitmap = nullptr;
749}
750
Alec Mouri7dcb7d22024-07-26 13:41:04 +0000751std::optional<SkRect> RecyclingClippingPixelAllocator::getSourceBoundsForUpsample(
752 std::optional<SkRect> subset) {
753 if (!uirenderer::Properties::resampleGainmapRegions || !subset || subset->isEmpty()) {
754 return std::nullopt;
755 }
756
757 if (subset->left() == floor(subset->left()) && subset->top() == floor(subset->top()) &&
758 subset->right() == floor(subset->right()) && subset->bottom() == floor(subset->bottom())) {
759 return std::nullopt;
760 }
761
762 return subset;
763}
764
Matt Sarett1f979632015-10-27 10:33:20 -0400765////////////////////////////////////////////////////////////////////////////////
766
Riley Andrews721ae5f2015-05-11 16:08:22 -0700767AshmemPixelAllocator::AshmemPixelAllocator(JNIEnv *env) {
768 LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&mJavaVM) != JNI_OK,
769 "env->GetJavaVM failed");
770}
771
Mike Reed81397c42017-07-18 17:04:16 -0400772bool AshmemPixelAllocator::allocPixelRef(SkBitmap* bitmap) {
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -0400773 mStorage = android::Bitmap::allocateAshmemBitmap(bitmap);
sergeyvc69853c2016-10-07 14:14:09 -0700774 return !!mStorage;
Riley Andrews721ae5f2015-05-11 16:08:22 -0700775}
776
777////////////////////////////////////////////////////////////////////////////////
778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779int register_android_graphics_Graphics(JNIEnv* env)
780{
781 jmethodID m;
782 jclass c;
783
Romain Guy95648b82017-04-13 18:43:42 -0700784 gRect_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Rect"));
785 gRect_leftFieldID = GetFieldIDOrDie(env, gRect_class, "left", "I");
786 gRect_topFieldID = GetFieldIDOrDie(env, gRect_class, "top", "I");
787 gRect_rightFieldID = GetFieldIDOrDie(env, gRect_class, "right", "I");
788 gRect_bottomFieldID = GetFieldIDOrDie(env, gRect_class, "bottom", "I");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789
Romain Guy95648b82017-04-13 18:43:42 -0700790 gRectF_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/RectF"));
791 gRectF_leftFieldID = GetFieldIDOrDie(env, gRectF_class, "left", "F");
792 gRectF_topFieldID = GetFieldIDOrDie(env, gRectF_class, "top", "F");
793 gRectF_rightFieldID = GetFieldIDOrDie(env, gRectF_class, "right", "F");
794 gRectF_bottomFieldID = GetFieldIDOrDie(env, gRectF_class, "bottom", "F");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795
Romain Guy95648b82017-04-13 18:43:42 -0700796 gPoint_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Point"));
797 gPoint_xFieldID = GetFieldIDOrDie(env, gPoint_class, "x", "I");
798 gPoint_yFieldID = GetFieldIDOrDie(env, gPoint_class, "y", "I");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799
Romain Guy95648b82017-04-13 18:43:42 -0700800 gPointF_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/PointF"));
801 gPointF_xFieldID = GetFieldIDOrDie(env, gPointF_class, "x", "F");
802 gPointF_yFieldID = GetFieldIDOrDie(env, gPointF_class, "y", "F");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803
Romain Guy95648b82017-04-13 18:43:42 -0700804 gBitmapRegionDecoder_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/BitmapRegionDecoder"));
805 gBitmapRegionDecoder_constructorMethodID = GetMethodIDOrDie(env, gBitmapRegionDecoder_class, "<init>", "(J)V");
Joseph Wenf1f48bc2010-07-19 16:59:51 +0800806
Romain Guy95648b82017-04-13 18:43:42 -0700807 gBitmapConfig_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Bitmap$Config"));
808 gBitmapConfig_nativeInstanceID = GetFieldIDOrDie(env, gBitmapConfig_class, "nativeInt", "I");
Derek Sollenberger213daca2019-10-25 14:17:32 -0400809 gBitmapConfig_nativeToConfigMethodID = GetStaticMethodIDOrDie(env, gBitmapConfig_class,
810 "nativeToConfig",
811 "(I)Landroid/graphics/Bitmap$Config;");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812
Romain Guy95648b82017-04-13 18:43:42 -0700813 gCanvas_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Canvas"));
814 gCanvas_nativeInstanceID = GetFieldIDOrDie(env, gCanvas_class, "mNativeCanvasWrapper", "J");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815
Romain Guy95648b82017-04-13 18:43:42 -0700816 gPicture_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Picture"));
817 gPicture_nativeInstanceID = GetFieldIDOrDie(env, gPicture_class, "mNativePicture", "J");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818
Romain Guy95648b82017-04-13 18:43:42 -0700819 gRegion_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Region"));
820 gRegion_nativeInstanceID = GetFieldIDOrDie(env, gRegion_class, "mNativeRegion", "J");
821 gRegion_constructorMethodID = GetMethodIDOrDie(env, gRegion_class, "<init>", "(JI)V");
Elliott Hughes8451b252011-04-07 19:17:57 -0700822
Mathieu Chartier7384b422013-10-17 18:16:42 -0700823 c = env->FindClass("java/lang/Byte");
Mathieu Chartier6ecb7a92013-10-18 11:04:11 -0700824 gByte_class = (jclass) env->NewGlobalRef(
Mathieu Chartier7384b422013-10-17 18:16:42 -0700825 env->GetStaticObjectField(c, env->GetStaticFieldID(c, "TYPE", "Ljava/lang/Class;")));
826
Romain Guy95648b82017-04-13 18:43:42 -0700827 gColorSpace_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/ColorSpace"));
Romain Guy95648b82017-04-13 18:43:42 -0700828 gColorSpace_getMethodID = GetStaticMethodIDOrDie(env, gColorSpace_class,
829 "get", "(Landroid/graphics/ColorSpace$Named;)Landroid/graphics/ColorSpace;");
830 gColorSpace_matchMethodID = GetStaticMethodIDOrDie(env, gColorSpace_class, "match",
831 "([FLandroid/graphics/ColorSpace$Rgb$TransferParameters;)Landroid/graphics/ColorSpace;");
832
833 gColorSpaceRGB_class = MakeGlobalRefOrDie(env,
834 FindClassOrDie(env, "android/graphics/ColorSpace$Rgb"));
835 gColorSpaceRGB_constructorMethodID = GetMethodIDOrDie(env, gColorSpaceRGB_class,
836 "<init>", "(Ljava/lang/String;[FLandroid/graphics/ColorSpace$Rgb$TransferParameters;)V");
Romain Guy95648b82017-04-13 18:43:42 -0700837
838 gColorSpace_Named_class = MakeGlobalRefOrDie(env,
839 FindClassOrDie(env, "android/graphics/ColorSpace$Named"));
840 gColorSpace_Named_sRGBFieldID = GetStaticFieldIDOrDie(env,
841 gColorSpace_Named_class, "SRGB", "Landroid/graphics/ColorSpace$Named;");
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500842 gColorSpace_Named_ExtendedSRGBFieldID = GetStaticFieldIDOrDie(env,
843 gColorSpace_Named_class, "EXTENDED_SRGB", "Landroid/graphics/ColorSpace$Named;");
844 gColorSpace_Named_LinearSRGBFieldID = GetStaticFieldIDOrDie(env,
845 gColorSpace_Named_class, "LINEAR_SRGB", "Landroid/graphics/ColorSpace$Named;");
Romain Guy95648b82017-04-13 18:43:42 -0700846 gColorSpace_Named_LinearExtendedSRGBFieldID = GetStaticFieldIDOrDie(env,
847 gColorSpace_Named_class, "LINEAR_EXTENDED_SRGB", "Landroid/graphics/ColorSpace$Named;");
848
849 gTransferParameters_class = MakeGlobalRefOrDie(env, FindClassOrDie(env,
850 "android/graphics/ColorSpace$Rgb$TransferParameters"));
Sally Qi12d371a2022-10-26 17:30:26 -0700851 gTransferParameters_constructorMethodID =
John Recke4a3d632023-03-15 23:16:21 -0400852 GetMethodIDOrDie(env, gTransferParameters_class, "<init>", "(DDDDDDD)V");
Mathieu Chartier7384b422013-10-17 18:16:42 -0700853
Seigo Nonaka1ed4f642020-09-10 17:19:34 -0700854 gFontMetrics_class = FindClassOrDie(env, "android/graphics/Paint$FontMetrics");
855 gFontMetrics_class = MakeGlobalRefOrDie(env, gFontMetrics_class);
856
857 gFontMetrics_top = GetFieldIDOrDie(env, gFontMetrics_class, "top", "F");
858 gFontMetrics_ascent = GetFieldIDOrDie(env, gFontMetrics_class, "ascent", "F");
859 gFontMetrics_descent = GetFieldIDOrDie(env, gFontMetrics_class, "descent", "F");
860 gFontMetrics_bottom = GetFieldIDOrDie(env, gFontMetrics_class, "bottom", "F");
861 gFontMetrics_leading = GetFieldIDOrDie(env, gFontMetrics_class, "leading", "F");
862
863 gFontMetricsInt_class = FindClassOrDie(env, "android/graphics/Paint$FontMetricsInt");
864 gFontMetricsInt_class = MakeGlobalRefOrDie(env, gFontMetricsInt_class);
865
866 gFontMetricsInt_top = GetFieldIDOrDie(env, gFontMetricsInt_class, "top", "I");
867 gFontMetricsInt_ascent = GetFieldIDOrDie(env, gFontMetricsInt_class, "ascent", "I");
868 gFontMetricsInt_descent = GetFieldIDOrDie(env, gFontMetricsInt_class, "descent", "I");
869 gFontMetricsInt_bottom = GetFieldIDOrDie(env, gFontMetricsInt_class, "bottom", "I");
870 gFontMetricsInt_leading = GetFieldIDOrDie(env, gFontMetricsInt_class, "leading", "I");
871
Seigo Nonakadd617182023-12-07 08:56:31 +0900872 gRunInfo_class = FindClassOrDie(env, "android/graphics/Paint$RunInfo");
873 gRunInfo_class = MakeGlobalRefOrDie(env, gRunInfo_class);
874
875 gRunInfo_clusterCount = GetFieldIDOrDie(env, gRunInfo_class, "mClusterCount", "I");
876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 return 0;
878}