blob: 8dd5f5f0e561bf5cbc249ecf763ae95fca11a58e [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
17#include "GraphicsJNI.h"
18#include "ImageDecoder.h"
Leon Scroggins III127d31a2018-01-19 12:29:47 -050019#include "Utils.h"
Leon Scroggins III5b7f4262018-01-26 11:03:54 -050020#include "core_jni_helpers.h"
Leon Scroggins III671cce22018-01-14 16:52:17 -050021
Leon Scroggins III671cce22018-01-14 16:52:17 -050022#include <SkAndroidCodec.h>
23#include <SkAnimatedImage.h>
24#include <SkColorFilter.h>
25#include <SkPicture.h>
26#include <SkPictureRecorder.h>
Leon Scroggins III5b7f4262018-01-26 11:03:54 -050027#include <hwui/AnimatedImageDrawable.h>
28#include <hwui/Canvas.h>
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -050029#include <utils/Looper.h>
Leon Scroggins III671cce22018-01-14 16:52:17 -050030
31using namespace android;
32
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -050033static jmethodID gAnimatedImageDrawable_onAnimationEndMethodID;
Leon Scroggins III671cce22018-01-14 16:52:17 -050034
35// Note: jpostProcess holds a handle to the ImageDecoder.
36static jlong AnimatedImageDrawable_nCreate(JNIEnv* env, jobject /*clazz*/,
37 jlong nativeImageDecoder, jobject jpostProcess,
38 jint width, jint height, jobject jsubset) {
39 if (nativeImageDecoder == 0) {
40 doThrowIOE(env, "Cannot create AnimatedImageDrawable from null!");
41 return 0;
42 }
43
44 auto* imageDecoder = reinterpret_cast<ImageDecoder*>(nativeImageDecoder);
Leon Scroggins III671cce22018-01-14 16:52:17 -050045 const SkISize scaledSize = SkISize::Make(width, height);
46 SkIRect subset;
47 if (jsubset) {
48 GraphicsJNI::jrect_to_irect(env, jsubset, &subset);
49 } else {
50 subset = SkIRect::MakeWH(width, height);
51 }
52
Leon Scroggins IIIeb3b38e2018-03-20 11:11:13 -040053 auto info = imageDecoder->mCodec->getInfo();
54 bool hasRestoreFrame = false;
55 if (imageDecoder->mCodec->getEncodedFormat() == SkEncodedImageFormat::kWEBP) {
56 if (width < info.width() && height < info.height()) {
57 // WebP will scale its SkBitmap to the scaled size.
58 // FIXME: b/73529447 GIF should do the same.
59 info = info.makeWH(width, height);
60 }
61 } else {
62 const int frameCount = imageDecoder->mCodec->codec()->getFrameCount();
63 for (int i = 0; i < frameCount; ++i) {
64 SkCodec::FrameInfo frameInfo;
65 if (!imageDecoder->mCodec->codec()->getFrameInfo(i, &frameInfo)) {
66 doThrowIOE(env, "Failed to read frame info!");
67 return 0;
68 }
69 if (frameInfo.fDisposalMethod == SkCodecAnimation::DisposalMethod::kRestorePrevious) {
70 hasRestoreFrame = true;
71 break;
72 }
73 }
74 }
75
76 size_t bytesUsed = info.computeMinByteSize();
77 // SkAnimatedImage has one SkBitmap for decoding, plus an extra one if there is a
78 // kRestorePrevious frame. AnimatedImageDrawable has two SkPictures storing the current
79 // frame and the next frame. (The former assumes that the image is animated, and the
80 // latter assumes that it is drawn to a hardware canvas.)
81 bytesUsed *= hasRestoreFrame ? 4 : 3;
Leon Scroggins III671cce22018-01-14 16:52:17 -050082 sk_sp<SkPicture> picture;
83 if (jpostProcess) {
84 SkRect bounds = SkRect::MakeWH(subset.width(), subset.height());
85
86 SkPictureRecorder recorder;
87 SkCanvas* skcanvas = recorder.beginRecording(bounds);
88 std::unique_ptr<Canvas> canvas(Canvas::create_canvas(skcanvas));
Leon Scroggins IIIe5de9aa2018-01-10 20:56:51 -050089 postProcessAndRelease(env, jpostProcess, std::move(canvas));
Leon Scroggins III671cce22018-01-14 16:52:17 -050090 if (env->ExceptionCheck()) {
91 return 0;
92 }
93 picture = recorder.finishRecordingAsPicture();
Leon Scroggins IIIeb3b38e2018-03-20 11:11:13 -040094 bytesUsed += picture->approximateBytesUsed();
Leon Scroggins III671cce22018-01-14 16:52:17 -050095 }
96
Derek Sollenberger2d142132018-01-22 10:25:26 -050097
98 sk_sp<SkAnimatedImage> animatedImg = SkAnimatedImage::Make(std::move(imageDecoder->mCodec),
99 scaledSize, subset,
100 std::move(picture));
101 if (!animatedImg) {
Leon Scroggins III671cce22018-01-14 16:52:17 -0500102 doThrowIOE(env, "Failed to create drawable");
103 return 0;
104 }
Leon Scroggins III671cce22018-01-14 16:52:17 -0500105
Leon Scroggins IIIeb3b38e2018-03-20 11:11:13 -0400106 bytesUsed += sizeof(animatedImg.get());
107
108 sk_sp<AnimatedImageDrawable> drawable(new AnimatedImageDrawable(std::move(animatedImg),
109 bytesUsed));
Leon Scroggins III671cce22018-01-14 16:52:17 -0500110 return reinterpret_cast<jlong>(drawable.release());
111}
112
113static void AnimatedImageDrawable_destruct(AnimatedImageDrawable* drawable) {
Derek Sollenberger2d142132018-01-22 10:25:26 -0500114 SkSafeUnref(drawable);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500115}
116
117static jlong AnimatedImageDrawable_nGetNativeFinalizer(JNIEnv* /*env*/, jobject /*clazz*/) {
118 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&AnimatedImageDrawable_destruct));
119}
120
Leon Scroggins III5b7f4262018-01-26 11:03:54 -0500121// Java's FINISHED relies on this being -1
122static_assert(SkAnimatedImage::kFinished == -1);
123
Leon Scroggins III671cce22018-01-14 16:52:17 -0500124static jlong AnimatedImageDrawable_nDraw(JNIEnv* env, jobject /*clazz*/, jlong nativePtr,
Derek Sollenberger2d142132018-01-22 10:25:26 -0500125 jlong canvasPtr) {
Leon Scroggins III671cce22018-01-14 16:52:17 -0500126 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500127 auto* canvas = reinterpret_cast<Canvas*>(canvasPtr);
Derek Sollenberger2d142132018-01-22 10:25:26 -0500128 return (jlong) canvas->drawAnimatedImage(drawable);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500129}
130
131static void AnimatedImageDrawable_nSetAlpha(JNIEnv* env, jobject /*clazz*/, jlong nativePtr,
132 jint alpha) {
133 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
Derek Sollenberger2d142132018-01-22 10:25:26 -0500134 drawable->setStagingAlpha(alpha);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500135}
136
137static jlong AnimatedImageDrawable_nGetAlpha(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) {
138 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
Derek Sollenberger2d142132018-01-22 10:25:26 -0500139 return drawable->getStagingAlpha();
Leon Scroggins III671cce22018-01-14 16:52:17 -0500140}
141
142static void AnimatedImageDrawable_nSetColorFilter(JNIEnv* env, jobject /*clazz*/, jlong nativePtr,
143 jlong nativeFilter) {
144 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
145 auto* filter = reinterpret_cast<SkColorFilter*>(nativeFilter);
Derek Sollenberger2d142132018-01-22 10:25:26 -0500146 drawable->setStagingColorFilter(sk_ref_sp(filter));
Leon Scroggins III671cce22018-01-14 16:52:17 -0500147}
148
149static jboolean AnimatedImageDrawable_nIsRunning(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) {
150 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
Derek Sollenberger2d142132018-01-22 10:25:26 -0500151 return drawable->isRunning();
Leon Scroggins III671cce22018-01-14 16:52:17 -0500152}
153
Leon Scroggins III057c91a2018-01-24 13:02:16 -0500154static jboolean AnimatedImageDrawable_nStart(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) {
Leon Scroggins III671cce22018-01-14 16:52:17 -0500155 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
Leon Scroggins III057c91a2018-01-24 13:02:16 -0500156 return drawable->start();
Leon Scroggins III671cce22018-01-14 16:52:17 -0500157}
158
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500159static jboolean AnimatedImageDrawable_nStop(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) {
Leon Scroggins III671cce22018-01-14 16:52:17 -0500160 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500161 return drawable->stop();
Leon Scroggins III671cce22018-01-14 16:52:17 -0500162}
163
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500164// Java's LOOP_INFINITE relies on this being the same.
165static_assert(SkCodec::kRepetitionCountInfinite == -1);
166
Leon Scroggins III6de55b82018-02-23 16:11:37 -0500167static jint AnimatedImageDrawable_nGetRepeatCount(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) {
Leon Scroggins III1474b782018-02-23 09:38:12 -0500168 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
169 return drawable->getRepetitionCount();
170}
171
Leon Scroggins III6de55b82018-02-23 16:11:37 -0500172static void AnimatedImageDrawable_nSetRepeatCount(JNIEnv* env, jobject /*clazz*/, jlong nativePtr,
173 jint loopCount) {
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500174 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
175 drawable->setRepetitionCount(loopCount);
176}
177
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500178class InvokeListener : public MessageHandler {
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500179public:
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500180 InvokeListener(JNIEnv* env, jobject javaObject) {
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500181 LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&mJvm) != JNI_OK);
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500182 // Hold a weak reference to break a cycle that would prevent GC.
183 mWeakRef = env->NewWeakGlobalRef(javaObject);
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500184 }
185
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500186 ~InvokeListener() override {
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500187 auto* env = get_env_or_die(mJvm);
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500188 env->DeleteWeakGlobalRef(mWeakRef);
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500189 }
190
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500191 virtual void handleMessage(const Message&) override {
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500192 auto* env = get_env_or_die(mJvm);
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500193 jobject localRef = env->NewLocalRef(mWeakRef);
194 if (localRef) {
195 env->CallVoidMethod(localRef, gAnimatedImageDrawable_onAnimationEndMethodID);
196 }
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500197 }
198
199private:
200 JavaVM* mJvm;
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500201 jweak mWeakRef;
202};
203
204class JniAnimationEndListener : public OnAnimationEndListener {
205public:
206 JniAnimationEndListener(sp<Looper>&& looper, JNIEnv* env, jobject javaObject) {
207 mListener = new InvokeListener(env, javaObject);
208 mLooper = std::move(looper);
209 }
210
211 void onAnimationEnd() override { mLooper->sendMessage(mListener, 0); }
212
213private:
214 sp<InvokeListener> mListener;
215 sp<Looper> mLooper;
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500216};
217
218static void AnimatedImageDrawable_nSetOnAnimationEndListener(JNIEnv* env, jobject /*clazz*/,
219 jlong nativePtr, jobject jdrawable) {
220 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500221 if (!jdrawable) {
222 drawable->setOnAnimationEndListener(nullptr);
223 } else {
224 sp<Looper> looper = Looper::getForThread();
225 if (!looper.get()) {
226 doThrowISE(env,
227 "Must set AnimatedImageDrawable's AnimationCallback on a thread with a "
228 "looper!");
229 return;
230 }
231
232 drawable->setOnAnimationEndListener(
233 std::make_unique<JniAnimationEndListener>(std::move(looper), env, jdrawable));
234 }
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500235}
236
Leon Scroggins IIIeb3b38e2018-03-20 11:11:13 -0400237static jlong AnimatedImageDrawable_nNativeByteSize(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) {
Leon Scroggins III671cce22018-01-14 16:52:17 -0500238 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
Leon Scroggins IIIeb3b38e2018-03-20 11:11:13 -0400239 return drawable->byteSize();
Leon Scroggins III671cce22018-01-14 16:52:17 -0500240}
241
Leon Scroggins III5e8447f2018-03-05 14:22:51 -0500242static void AnimatedImageDrawable_nSetMirrored(JNIEnv* env, jobject /*clazz*/, jlong nativePtr,
243 jboolean mirrored) {
244 auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
245 drawable->setStagingMirrored(mirrored);
246}
247
Leon Scroggins III671cce22018-01-14 16:52:17 -0500248static const JNINativeMethod gAnimatedImageDrawableMethods[] = {
249 { "nCreate", "(JLandroid/graphics/ImageDecoder;IILandroid/graphics/Rect;)J", (void*) AnimatedImageDrawable_nCreate },
250 { "nGetNativeFinalizer", "()J", (void*) AnimatedImageDrawable_nGetNativeFinalizer },
Derek Sollenberger2d142132018-01-22 10:25:26 -0500251 { "nDraw", "(JJ)J", (void*) AnimatedImageDrawable_nDraw },
Leon Scroggins III671cce22018-01-14 16:52:17 -0500252 { "nSetAlpha", "(JI)V", (void*) AnimatedImageDrawable_nSetAlpha },
253 { "nGetAlpha", "(J)I", (void*) AnimatedImageDrawable_nGetAlpha },
254 { "nSetColorFilter", "(JJ)V", (void*) AnimatedImageDrawable_nSetColorFilter },
255 { "nIsRunning", "(J)Z", (void*) AnimatedImageDrawable_nIsRunning },
Leon Scroggins III057c91a2018-01-24 13:02:16 -0500256 { "nStart", "(J)Z", (void*) AnimatedImageDrawable_nStart },
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500257 { "nStop", "(J)Z", (void*) AnimatedImageDrawable_nStop },
Leon Scroggins III6de55b82018-02-23 16:11:37 -0500258 { "nGetRepeatCount", "(J)I", (void*) AnimatedImageDrawable_nGetRepeatCount },
259 { "nSetRepeatCount", "(JI)V", (void*) AnimatedImageDrawable_nSetRepeatCount },
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500260 { "nSetOnAnimationEndListener", "(JLandroid/graphics/drawable/AnimatedImageDrawable;)V", (void*) AnimatedImageDrawable_nSetOnAnimationEndListener },
Leon Scroggins III671cce22018-01-14 16:52:17 -0500261 { "nNativeByteSize", "(J)J", (void*) AnimatedImageDrawable_nNativeByteSize },
Leon Scroggins III5e8447f2018-03-05 14:22:51 -0500262 { "nSetMirrored", "(JZ)V", (void*) AnimatedImageDrawable_nSetMirrored },
Leon Scroggins III671cce22018-01-14 16:52:17 -0500263};
264
265int register_android_graphics_drawable_AnimatedImageDrawable(JNIEnv* env) {
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500266 jclass animatedImageDrawable_class = FindClassOrDie(env, "android/graphics/drawable/AnimatedImageDrawable");
Leon Scroggins IIIbeaf5d92018-01-26 11:03:54 -0500267 gAnimatedImageDrawable_onAnimationEndMethodID = GetMethodIDOrDie(env, animatedImageDrawable_class, "onAnimationEnd", "()V");
Leon Scroggins III127d31a2018-01-19 12:29:47 -0500268
Leon Scroggins III671cce22018-01-14 16:52:17 -0500269 return android::RegisterMethodsOrDie(env, "android/graphics/drawable/AnimatedImageDrawable",
270 gAnimatedImageDrawableMethods, NELEM(gAnimatedImageDrawableMethods));
271}
272