Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
native-lib.cpp
Go to the documentation of this file.
1
2/*
3 * Copyright 2018 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
21
31
36
37#include <jni.h>
38#include <math.h>
39#include <string>
40#include <utility>
41
42#include <GLES2/gl2.h>
43#include <GLES2/gl2ext.h>
44
45#include <GLES3/gl3.h>
46
47#define STENCIL_BUFFER_SIZE 8
48
49/*#define ATRACE_NAME(name) ScopedTrace ___tracer(name)
50
51// ATRACE_CALL is an ATRACE_NAME that uses the current function name.
52#define ATRACE_CALL() ATRACE_NAME(__FUNCTION__)
53namespace {
54 class ScopedTrace {
55 public:
56 inline ScopedTrace(const char *name) {
57 ATrace_beginSection(name);
58 }
59
60 inline ~ScopedTrace() {
61 ATrace_endSection();
62 }
63 };
64
65}*/
66
67//disable atrace
68#define ATRACE_NAME(name)
69#define ATRACE_CALL()
70
74
75static JavaVM* sJVM = nullptr;
76
77static void release_global_jni_ref(const void* /*data*/, void* context) {
78 JNIEnv* env;
79 if (sJVM->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
80 SK_ABORT("Attempting to release a JNI ref on a thread without a JVM attached.");
81 }
82 jobject obj = reinterpret_cast<jobject>(context);
83 env->DeleteGlobalRef(obj);
84}
85
86extern "C" JNIEXPORT jlong
87JNICALL
90 if (!glInterface) {
91 return 0;
92 }
93
96 sk_sp<GrDirectContext> dContext = GrDirectContexts::MakeGL(std::move(glInterface), options);
97 if (!dContext) {
98 return 0;
99 }
100
102 skottie->mDContext = std::move(dContext);
103
104 return (jlong) skottie;
105}
106
107extern "C" JNIEXPORT void
108JNICALL
109Java_org_skia_skottie_SkottieRunner_nDeleteProxy(JNIEnv *env, jclass clazz, jlong nativeProxy) {
110 if (!nativeProxy) {
111 return;
112 }
113 SkottieRunner* skottie = reinterpret_cast<SkottieRunner*>(nativeProxy);
114 if (skottie->mDContext) {
115 skottie->mDContext->releaseResourcesAndAbandonContext();
116 skottie->mDContext.reset();
117 }
118 delete skottie;
119}
120
121
122extern "C" JNIEXPORT void
123JNICALL
124Java_org_skia_skottie_SkottieRunner_nSetMaxCacheSize(JNIEnv *env, jclass clazz, jint maxCacheSize,
125 jlong nativeProxy) {
126 if (!nativeProxy) {
127 return;
128 }
129 SkottieRunner* skottie = reinterpret_cast<SkottieRunner*>(nativeProxy);
130 if (skottie->mDContext) {
131 skottie->mDContext->setResourceCacheLimit(maxCacheSize);
132 }
133}
134
137 std::unique_ptr<SkStream> mStream;
140 float mDuration; //in milliseconds
141};
142
143extern "C" JNIEXPORT jlong
144JNICALL
146 jobject clazz,
147 jlong runner,
148 jobject bufferObj) {
149
150 if (!runner) {
151 return 0;
152 }
153 SkottieRunner *skottieRunner = reinterpret_cast<SkottieRunner*>(runner);
154
155 const void* buffer = env->GetDirectBufferAddress(bufferObj);
156 jlong bufferSize = env->GetDirectBufferCapacity(bufferObj);
157 if (buffer == nullptr || bufferSize <= 0) {
158 return 0;
159 }
160
161 env->GetJavaVM(&sJVM);
162 jobject bufferRef = env->NewGlobalRef(bufferObj);
163 if (bufferRef == nullptr) {
164 return 0;
165 }
166
168 reinterpret_cast<void*>(bufferRef)));
169 std::unique_ptr<SkStream> stream = SkMemoryStream::Make(data);
170 if (!stream) {
171 // Cannot create a stream
172 return 0;
173 }
174
175 SkottieAnimation* skottieAnimation = new SkottieAnimation();
176 skottieAnimation->mRunner = skottieRunner;
177 skottieAnimation->mStream = std::move(stream);
178
180
181 SkCodecs::Register(SkPngDecoder::Decoder());
182 SkCodecs::Register(SkGifDecoder::Decoder());
183 SkCodecs::Register(SkJpegDecoder::Decoder());
184
185 skottieAnimation->mAnimation = skottie::Animation::Builder()
186 // Note, this nullptr ResourceProvider will only be able to decode base64 encoded images
187 // (using the above registered codecs) or base64 encoded FreeType typefaces.
191 .make(skottieAnimation->mStream.get());
192 skottieAnimation->mTimeBase = 0.0f; // force a time reset
193 skottieAnimation->mDuration = 1000 * skottieAnimation->mAnimation->duration();
194
195 if (!skottieAnimation->mAnimation) {
196 //failed to load Bodymovin animation
197 delete skottieAnimation;
198 return 0;
199 }
200
201 return (jlong) skottieAnimation;
202}
203
204extern "C" JNIEXPORT void
205JNICALL
207 jlong nativeProxy) {
208 if (!nativeProxy) {
209 return;
210 }
211 SkottieAnimation* skottieAnimation = reinterpret_cast<SkottieAnimation*>(nativeProxy);
212 delete skottieAnimation;
213}
214
215extern "C" JNIEXPORT bool
216JNICALL
218 jlong nativeProxy, jint width,
219 jint height,
220 jboolean wideColorGamut,
221 jfloat progress,
222 jint backgroundColor,
223 jboolean forceDraw) {
224 ATRACE_NAME("SkottieDrawFrame");
225 if (!nativeProxy) {
226 return false;
227 }
228 SkottieAnimation* skottieAnimation = reinterpret_cast<SkottieAnimation*>(nativeProxy);
229
230 auto dContext = skottieAnimation->mRunner->mDContext.get();
231
232 if (!dContext) {
233 return false;
234 }
235
237
238 if (skottieAnimation->mAnimation) {
239 skottieAnimation->mAnimation->seek(progress, &ic);
240 if (!forceDraw && ic.bounds().isEmpty()) {
241 return false;
242 }
243 }
244
246 // setup surface for fbo0
247 GrGLFramebufferInfo fboInfo;
248 fboInfo.fFBOID = 0;
249 if (wideColorGamut) {
250 fboInfo.fFormat = GL_RGBA16F;
252 } else {
253 fboInfo.fFormat = GL_RGBA8;
254 colorType = kN32_SkColorType;
255 }
256 fboInfo.fProtected = skgpu::Protected::kNo;
257 auto backendRT = GrBackendRenderTargets::MakeGL(width, height, 0, STENCIL_BUFFER_SIZE, fboInfo);
258
260
262 dContext, backendRT, kBottomLeft_GrSurfaceOrigin, colorType, nullptr, &props));
263
264 auto canvas = renderTarget->getCanvas();
265 canvas->clear(backgroundColor);
266
267 SkAutoCanvasRestore acr(canvas, true);
269 skottieAnimation->mAnimation->render(canvas, &bounds);
270 dContext->flushAndSubmit();
271 return true;
272}
273
274extern "C" JNIEXPORT jlong
275JNICALL
277 jclass clazz,
278 jlong nativeProxy) {
279 if (!nativeProxy) {
280 return 0;
281 }
282 SkottieAnimation* skottieAnimation = reinterpret_cast<SkottieAnimation*>(nativeProxy);
283 return (jlong) skottieAnimation->mDuration;
284}
const char * options
@ kBottomLeft_GrSurfaceOrigin
Definition GrTypes.h:149
#define SK_ABORT(message,...)
Definition SkAssert.h:70
SkColorType
Definition SkColorType.h:19
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition SkColorType.h:38
SK_API sk_sp< SkFontMgr > SkFontMgr_New_Custom_Empty()
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
@ kUnknown_SkPixelGeometry
static sk_sp< SkData > MakeWithProc(const void *ptr, size_t length, ReleaseProc proc, void *ctx)
Definition SkData.cpp:128
static std::unique_ptr< SkMemoryStream > Make(sk_sp< SkData > data)
Definition SkStream.cpp:314
T * get() const
Definition SkRefCnt.h:303
Builder & setResourceProvider(sk_sp< ResourceProvider >)
Definition Skottie.cpp:309
sk_sp< Animation > make(SkStream *)
Definition Skottie.cpp:349
Builder & setTextShapingFactory(sk_sp< SkShapers::Factory >)
Definition Skottie.cpp:344
double duration() const
Definition Skottie.h:268
void render(SkCanvas *canvas, const SkRect *dst=nullptr) const
Definition Skottie.cpp:482
void seek(SkScalar t, sksg::InvalidationController *ic=nullptr)
Definition Skottie.h:244
static sk_sp< DataURIResourceProviderProxy > Make(sk_sp< ResourceProvider > rp, ImageDecodeStrategy=ImageDecodeStrategy::kLazyDecode, sk_sp< const SkFontMgr > fontMgr=nullptr)
static const uint8_t buffer[]
SK_API GrBackendRenderTarget MakeGL(int width, int height, int sampleCnt, int stencilBits, const GrGLFramebufferInfo &glInfo)
SK_API sk_sp< GrDirectContext > MakeGL()
SK_API sk_sp< const GrGLInterface > MakeEGL()
constexpr SkCodecs::Decoder Decoder()
constexpr SkCodecs::Decoder Decoder()
constexpr SkCodecs::Decoder Decoder()
sk_sp< Factory > BestAvailable()
SK_API sk_sp< SkSurface > WrapBackendRenderTarget(GrRecordingContext *context, const GrBackendRenderTarget &backendRenderTarget, GrSurfaceOrigin origin, SkColorType colorType, sk_sp< SkColorSpace > colorSpace, const SkSurfaceProps *surfaceProps, RenderTargetReleaseProc releaseProc=nullptr, ReleaseContext releaseContext=nullptr)
Definition __init__.py:1
static void release_global_jni_ref(const void *, void *context)
JNIEXPORT void JNICALL Java_org_skia_skottie_SkottieAnimation_nDeleteProxy(JNIEnv *env, jclass clazz, jlong nativeProxy)
JNIEXPORT jlong JNICALL Java_org_skia_skottie_SkottieRunner_nCreateProxy(JNIEnv *env, jclass clazz)
JNIEXPORT jlong JNICALL Java_org_skia_skottie_SkottieAnimation_nGetDuration(JNIEnv *env, jclass clazz, jlong nativeProxy)
JNIEXPORT bool JNICALL Java_org_skia_skottie_SkottieAnimation_nDrawFrame(JNIEnv *env, jclass clazz, jlong nativeProxy, jint width, jint height, jboolean wideColorGamut, jfloat progress, jint backgroundColor, jboolean forceDraw)
JNIEXPORT void JNICALL Java_org_skia_skottie_SkottieRunner_nDeleteProxy(JNIEnv *env, jclass clazz, jlong nativeProxy)
#define STENCIL_BUFFER_SIZE
JNIEXPORT void JNICALL Java_org_skia_skottie_SkottieRunner_nSetMaxCacheSize(JNIEnv *env, jclass clazz, jint maxCacheSize, jlong nativeProxy)
#define ATRACE_NAME(name)
static JavaVM * sJVM
JNIEXPORT jlong JNICALL Java_org_skia_skottie_SkottieAnimation_nCreateProxy(JNIEnv *env, jobject clazz, jlong runner, jobject bufferObj)
int32_t height
int32_t width
skgpu::Protected fProtected
Definition GrGLTypes.h:199
bool isEmpty() const
Definition SkRect.h:693
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
std::unique_ptr< SkStream > mStream
SkottieRunner * mRunner
sk_sp< skottie::Animation > mAnimation
sk_sp< GrDirectContext > mDContext