blob: 90b1da8462058cda9994f90be0ed65f8259ef7f1 [file] [log] [blame]
Leon Scroggins III671cce22018-01-14 16:52:17 -05001/*
2 * Copyright (C) 2018 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
Leon Scroggins III671cce22018-01-14 16:52:17 -050017#include <SkAndroidCodec.h>
18#include <SkAnimatedImage.h>
19#include <SkColorFilter.h>
Kevin Lubick1175dc02022-02-28 12:41:27 -050020#include <SkEncodedImageFormat.h>
Leon Scroggins III671cce22018-01-14 16:52:17 -050021#include <SkPicture.h>
22#include <SkPictureRecorder.h>
Kevin Lubick1175dc02022-02-28 12:41:27 -050023#include <SkRect.h>
24#include <SkRefCnt.h>
Leon Scroggins III5b7f4262018-01-26 11:03:54 -050025#include <hwui/AnimatedImageDrawable.h>
26#include <hwui/Canvas.h>
Nader Jawadf0ea9e12023-05-02 15:18:00 -070027#include <hwui/ImageDecoder.h>
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -050028#include <utils/Looper.h>
Leon Scroggins III671cce22018-01-14 16:52:17 -050029
Nader Jawadf0ea9e12023-05-02 15:18:00 -070030#include "ColorFilter.h"
31#include "GraphicsJNI.h"
32#include "ImageDecoder.h"
33#include "Utils.h"
34
Leon Scroggins III671cce22018-01-14 16:52:17 -050035using namespace android;
36
John Reck3998a012021-07-28 11:04:30 -040037static jclass gAnimatedImageDrawableClass;
38static jmethodID gAnimatedImageDrawable_callOnAnimationEndMethodID;
Leon Scroggins III671cce22018-01-14 16:52:17 -050039
40// Note: jpostProcess holds a handle to the ImageDecoder.
41static jlong AnimatedImageDrawable_nCreate(JNIEnv* env, jobject /*clazz*/,
42 jlong nativeImageDecoder, jobject jpostProcess,
Leon Scroggins IIIeac14232019-02-04 10:30:22 -050043 jint width, jint height, jlong colorSpaceHandle,
44 jboolean extended, jobject jsubset) {
Leon Scroggins III671cce22018-01-14 16:52:17 -050045 if (nativeImageDecoder == 0) {
46 doThrowIOE(env, "Cannot create AnimatedImageDrawable from null!");
47 return 0;
48 }
49
50 auto* imageDecoder = reinterpret_cast<ImageDecoder*>(nativeImageDecoder);
Leon Scroggins III671cce22018-01-14 16:52:17 -050051 SkIRect subset;
52 if (jsubset) {
53 GraphicsJNI::jrect_to_irect(env, jsubset, &subset);
54 } else {
55 subset = SkIRect::MakeWH(width, height);
56 }
57
Leon Scroggins IIIeb3b38e2018-03-20 11:11:13 -040058 bool hasRestoreFrame = false;
Leon Scroggins IIIeac14232019-02-04 10:30:22 -050059 if (imageDecoder->mCodec->getEncodedFormat() != SkEncodedImageFormat::kWEBP) {
Leon Scroggins IIIeb3b38e2018-03-20 11:11:13 -040060 const int frameCount = imageDecoder->mCodec->codec()->getFrameCount();
61 for (int i = 0; i < frameCount; ++i) {
62 SkCodec::FrameInfo frameInfo;
63 if (!imageDecoder->mCodec->codec()->getFrameInfo(i, &frameInfo)) {
64 doThrowIOE(env, "Failed to read frame info!");
65 return 0;
66 }
67 if (frameInfo.fDisposalMethod == SkCodecAnimation::DisposalMethod::kRestorePrevious) {
68 hasRestoreFrame = true;
69 break;
70 }
71 }
72 }
73
Leon Scroggins IIIeac14232019-02-04 10:30:22 -050074 auto info = imageDecoder->mCodec->getInfo().makeWH(width, height)
75 .makeColorSpace(GraphicsJNI::getNativeColorSpace(colorSpaceHandle));
76 if (extended) {
77 info = info.makeColorType(kRGBA_F16_SkColorType);
78 }
79
Leon Scroggins IIIeb3b38e2018-03-20 11:11:13 -040080 size_t bytesUsed = info.computeMinByteSize();
81 // SkAnimatedImage has one SkBitmap for decoding, plus an extra one if there is a
82 // kRestorePrevious frame. AnimatedImageDrawable has two SkPictures storing the current
83 // frame and the next frame. (The former assumes that the image is animated, and the
84 // latter assumes that it is drawn to a hardware canvas.)
85 bytesUsed *= hasRestoreFrame ? 4 : 3;
Leon Scroggins III671cce22018-01-14 16:52:17 -050086 sk_sp<SkPicture> picture;
87 if (jpostProcess) {
88 SkRect bounds = SkRect::MakeWH(subset.width(), subset.height());
89
90 SkPictureRecorder recorder;
91 SkCanvas* skcanvas = recorder.beginRecording(bounds);
92 std::unique_ptr<Canvas> canvas(Canvas::create_canvas(skcanvas));
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -050093 postProcessAndRelease(env, jpostProcess, std::move(canvas));
Leon Scroggins III671cce22018-01-14 16:52:17 -050094 if (env->ExceptionCheck()) {
95 return 0;
96 }
97 picture = recorder.finishRecordingAsPicture();
Leon Scroggins IIIeb3b38e2018-03-20 11:11:13 -040098 bytesUsed += picture->approximateBytesUsed();
Leon Scroggins III671cce22018-01-14 16:52:17 -050099 }
100
John Reck2be87bb2023-04-05 17:33:00 -0400101 SkEncodedImageFormat format = imageDecoder->mCodec->getEncodedFormat();
Derek Sollenberger2d142132018-01-22 10:25:26 -0500102 sk_sp<SkAnimatedImage> animatedImg = SkAnimatedImage::Make(std::move(imageDecoder->mCodec),
Leon Scroggins IIIeac14232019-02-04 10:30:22 -0500103 info, subset,
Derek Sollenberger2d142132018-01-22 10:25:26 -0500104 std::move(picture));
105 if (!animatedImg) {
Leon Scroggins III671cce22018-01-14 16:52:17 -0500106 doThrowIOE(env, "Failed to create drawable");
107 return 0;
108 }
Leon Scroggins III671cce22018-01-14 16:52:17 -0500109
Leon Scroggins IIIeb3b38e2018-03-20 11:11:13 -0400110 bytesUsed += sizeof(animatedImg.get());
111
John Reck2be87bb2023-04-05 17:33:00 -0400112 sk_sp<AnimatedImageDrawable> drawable(
113 new AnimatedImageDrawable(std::move(animatedImg), bytesUsed, format));
Leon Scroggins III671cce22018-01-14 16:52:17 -0500114 return reinterpret_cast<jlong>(drawable.release());
115}
116
117static void AnimatedImageDrawable_destruct(AnimatedImageDrawable* drawable) {
Derek Sollenberger2d142132018-01-22 10:25:26 -0500118 SkSafeUnref(drawable);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500119}
120
121static jlong AnimatedImageDrawable_nGetNativeFinalizer(JNIEnv* /*env*/, jobject /*clazz*/) {
122 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&AnimatedImageDrawable_destruct));
123}
124
Leon Scroggins III5b7f4262018-01-26 11:03:54 -0500125// Java's FINISHED relies on this being -1
126static_assert(SkAnimatedImage::kFinished == -1);
127
Leon Scroggins III671cce22018-01-14 16:52:17 -0500128static jlong AnimatedImageDrawable_nDraw(JNIEnv* env, jobject /*clazz*/, jlong nativePtr,
Derek Sollenberger2d142132018-01-22 10:25:26 -0500129 jlong canvasPtr) {
Leon Scroggins III671cce22018-01-14 16:52:17 -0500130 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500131 auto* canvas = reinterpret_cast<Canvas*>(canvasPtr);
Derek Sollenberger2d142132018-01-22 10:25:26 -0500132 return (jlong) canvas->drawAnimatedImage(drawable);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500133}
134
135static void AnimatedImageDrawable_nSetAlpha(JNIEnv* env, jobject /*clazz*/, jlong nativePtr,
136 jint alpha) {
137 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
Derek Sollenberger2d142132018-01-22 10:25:26 -0500138 drawable->setStagingAlpha(alpha);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500139}
140
141static jlong AnimatedImageDrawable_nGetAlpha(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) {
142 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
Derek Sollenberger2d142132018-01-22 10:25:26 -0500143 return drawable->getStagingAlpha();
Leon Scroggins III671cce22018-01-14 16:52:17 -0500144}
145
146static void AnimatedImageDrawable_nSetColorFilter(JNIEnv* env, jobject /*clazz*/, jlong nativePtr,
147 jlong nativeFilter) {
148 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
Nader Jawadf0ea9e12023-05-02 15:18:00 -0700149 auto filter = uirenderer::ColorFilter::fromJava(nativeFilter);
150 auto skColorFilter = filter != nullptr ? filter->getInstance() : sk_sp<SkColorFilter>();
151 drawable->setStagingColorFilter(skColorFilter);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500152}
153
154static jboolean AnimatedImageDrawable_nIsRunning(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) {
155 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
Derek Sollenberger2d142132018-01-22 10:25:26 -0500156 return drawable->isRunning();
Leon Scroggins III671cce22018-01-14 16:52:17 -0500157}
158
Leon Scroggins III057c91a2018-01-24 13:02:16 -0500159static jboolean AnimatedImageDrawable_nStart(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) {
Leon Scroggins III671cce22018-01-14 16:52:17 -0500160 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
Leon Scroggins III057c91a2018-01-24 13:02:16 -0500161 return drawable->start();
Leon Scroggins III671cce22018-01-14 16:52:17 -0500162}
163
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500164static jboolean AnimatedImageDrawable_nStop(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) {
Leon Scroggins III671cce22018-01-14 16:52:17 -0500165 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500166 return drawable->stop();
Leon Scroggins III671cce22018-01-14 16:52:17 -0500167}
168
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500169// Java's LOOP_INFINITE relies on this being the same.
170static_assert(SkCodec::kRepetitionCountInfinite == -1);
171
Leon Scroggins III6de55b82018-02-23 16:11:37 -0500172static jint AnimatedImageDrawable_nGetRepeatCount(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) {
Leon Scroggins III1474b782018-02-23 09:38:12 -0500173 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
174 return drawable->getRepetitionCount();
175}
176
Leon Scroggins III6de55b82018-02-23 16:11:37 -0500177static void AnimatedImageDrawable_nSetRepeatCount(JNIEnv* env, jobject /*clazz*/, jlong nativePtr,
178 jint loopCount) {
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500179 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
180 drawable->setRepetitionCount(loopCount);
181}
182
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500183class InvokeListener : public MessageHandler {
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500184public:
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500185 InvokeListener(JNIEnv* env, jobject javaObject) {
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500186 LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&mJvm) != JNI_OK);
John Reck3998a012021-07-28 11:04:30 -0400187 mCallbackRef = env->NewGlobalRef(javaObject);
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500188 }
189
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500190 ~InvokeListener() override {
Leon Scroggins IIIf97b29d22020-04-06 12:01:01 -0400191 auto* env = requireEnv(mJvm);
John Reck3998a012021-07-28 11:04:30 -0400192 env->DeleteGlobalRef(mCallbackRef);
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500193 }
194
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500195 virtual void handleMessage(const Message&) override {
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500196 auto* env = get_env_or_die(mJvm);
John Reck3998a012021-07-28 11:04:30 -0400197 env->CallStaticVoidMethod(gAnimatedImageDrawableClass,
198 gAnimatedImageDrawable_callOnAnimationEndMethodID, mCallbackRef);
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500199 }
200
201private:
202 JavaVM* mJvm;
John Reck3998a012021-07-28 11:04:30 -0400203 jobject mCallbackRef;
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500204};
205
206class JniAnimationEndListener : public OnAnimationEndListener {
207public:
208 JniAnimationEndListener(sp<Looper>&& looper, JNIEnv* env, jobject javaObject) {
209 mListener = new InvokeListener(env, javaObject);
210 mLooper = std::move(looper);
211 }
212
213 void onAnimationEnd() override { mLooper->sendMessage(mListener, 0); }
214
215private:
216 sp<InvokeListener> mListener;
217 sp<Looper> mLooper;
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500218};
219
220static void AnimatedImageDrawable_nSetOnAnimationEndListener(JNIEnv* env, jobject /*clazz*/,
221 jlong nativePtr, jobject jdrawable) {
222 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500223 if (!jdrawable) {
224 drawable->setOnAnimationEndListener(nullptr);
225 } else {
226 sp<Looper> looper = Looper::getForThread();
227 if (!looper.get()) {
228 doThrowISE(env,
229 "Must set AnimatedImageDrawable's AnimationCallback on a thread with a "
230 "looper!");
231 return;
232 }
233
234 drawable->setOnAnimationEndListener(
235 std::make_unique<JniAnimationEndListener>(std::move(looper), env, jdrawable));
236 }
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500237}
238
Leon Scroggins IIIeb3b38e2018-03-20 11:11:13 -0400239static jlong AnimatedImageDrawable_nNativeByteSize(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) {
Leon Scroggins III671cce22018-01-14 16:52:17 -0500240 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
Leon Scroggins IIIeb3b38e2018-03-20 11:11:13 -0400241 return drawable->byteSize();
Leon Scroggins III671cce22018-01-14 16:52:17 -0500242}
243
Leon Scroggins III5e8447f2018-03-05 14:22:51 -0500244static void AnimatedImageDrawable_nSetMirrored(JNIEnv* env, jobject /*clazz*/, jlong nativePtr,
245 jboolean mirrored) {
246 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
247 drawable->setStagingMirrored(mirrored);
248}
249
Leon Scroggins IIIbe969ef2021-01-20 15:28:39 -0500250static void AnimatedImageDrawable_nSetBounds(JNIEnv* env, jobject /*clazz*/, jlong nativePtr,
251 jobject jrect) {
252 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
253 SkRect rect;
254 GraphicsJNI::jrect_to_rect(env, jrect, &rect);
255 drawable->setStagingBounds(rect);
256}
257
Leon Scroggins III671cce22018-01-14 16:52:17 -0500258static const JNINativeMethod gAnimatedImageDrawableMethods[] = {
John Reck3998a012021-07-28 11:04:30 -0400259 {"nCreate", "(JLandroid/graphics/ImageDecoder;IIJZLandroid/graphics/Rect;)J",
260 (void*)AnimatedImageDrawable_nCreate},
261 {"nGetNativeFinalizer", "()J", (void*)AnimatedImageDrawable_nGetNativeFinalizer},
262 {"nDraw", "(JJ)J", (void*)AnimatedImageDrawable_nDraw},
263 {"nSetAlpha", "(JI)V", (void*)AnimatedImageDrawable_nSetAlpha},
264 {"nGetAlpha", "(J)I", (void*)AnimatedImageDrawable_nGetAlpha},
265 {"nSetColorFilter", "(JJ)V", (void*)AnimatedImageDrawable_nSetColorFilter},
266 {"nIsRunning", "(J)Z", (void*)AnimatedImageDrawable_nIsRunning},
267 {"nStart", "(J)Z", (void*)AnimatedImageDrawable_nStart},
268 {"nStop", "(J)Z", (void*)AnimatedImageDrawable_nStop},
269 {"nGetRepeatCount", "(J)I", (void*)AnimatedImageDrawable_nGetRepeatCount},
270 {"nSetRepeatCount", "(JI)V", (void*)AnimatedImageDrawable_nSetRepeatCount},
271 {"nSetOnAnimationEndListener", "(JLjava/lang/ref/WeakReference;)V",
272 (void*)AnimatedImageDrawable_nSetOnAnimationEndListener},
273 {"nNativeByteSize", "(J)J", (void*)AnimatedImageDrawable_nNativeByteSize},
274 {"nSetMirrored", "(JZ)V", (void*)AnimatedImageDrawable_nSetMirrored},
275 {"nSetBounds", "(JLandroid/graphics/Rect;)V", (void*)AnimatedImageDrawable_nSetBounds},
Leon Scroggins III671cce22018-01-14 16:52:17 -0500276};
277
278int register_android_graphics_drawable_AnimatedImageDrawable(JNIEnv* env) {
John Reck3998a012021-07-28 11:04:30 -0400279 gAnimatedImageDrawableClass = reinterpret_cast<jclass>(env->NewGlobalRef(
280 FindClassOrDie(env, "android/graphics/drawable/AnimatedImageDrawable")));
281 gAnimatedImageDrawable_callOnAnimationEndMethodID =
282 GetStaticMethodIDOrDie(env, gAnimatedImageDrawableClass, "callOnAnimationEnd",
283 "(Ljava/lang/ref/WeakReference;)V");
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500284
Leon Scroggins III671cce22018-01-14 16:52:17 -0500285 return android::RegisterMethodsOrDie(env, "android/graphics/drawable/AnimatedImageDrawable",
286 gAnimatedImageDrawableMethods, NELEM(gAnimatedImageDrawableMethods));
287}
288