Flutter Engine
The Flutter Engine
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
73};
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
95 options.fDisableDistanceFieldPaths = true;
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
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.
188 .setResourceProvider(skresources::DataURIResourceProviderProxy::Make(nullptr,
190 .setTextShapingFactory(sk_make_sp<SkShapers::HarfbuzzFactory>())
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 }
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
void clear(SkColor color)
Definition: SkCanvas.h:1199
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
SkCanvas * getCanvas()
Definition: SkSurface.cpp:82
T * get() const
Definition: SkRefCnt.h:303
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)
SK_API GrBackendRenderTarget MakeGL(int width, int height, int sampleCnt, int stencilBits, const GrGLFramebufferInfo &glInfo)
SK_API sk_sp< GrDirectContext > MakeGL(sk_sp< const GrGLInterface >, const GrContextOptions &)
SK_API sk_sp< const GrGLInterface > MakeEGL()
void SK_API Register(Decoder d)
Definition: SkCodec.cpp:135
constexpr SkCodecs::Decoder Decoder()
Definition: SkGifDecoder.h:38
constexpr SkCodecs::Decoder Decoder()
Definition: SkJpegDecoder.h:38
constexpr SkCodecs::Decoder Decoder()
Definition: SkPngDecoder.h:38
Optional< SkRect > bounds
Definition: SkRecords.h:189
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
DlVertices::Builder Builder
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
static void release_global_jni_ref(const void *, void *context)
Definition: native-lib.cpp:77
JNIEXPORT void JNICALL Java_org_skia_skottie_SkottieAnimation_nDeleteProxy(JNIEnv *env, jclass clazz, jlong nativeProxy)
Definition: native-lib.cpp:206
JNIEXPORT jlong JNICALL Java_org_skia_skottie_SkottieRunner_nCreateProxy(JNIEnv *env, jclass clazz)
Definition: native-lib.cpp:88
JNIEXPORT jlong JNICALL Java_org_skia_skottie_SkottieAnimation_nGetDuration(JNIEnv *env, jclass clazz, jlong nativeProxy)
Definition: native-lib.cpp:276
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)
Definition: native-lib.cpp:217
JNIEXPORT void JNICALL Java_org_skia_skottie_SkottieRunner_nDeleteProxy(JNIEnv *env, jclass clazz, jlong nativeProxy)
Definition: native-lib.cpp:109
#define STENCIL_BUFFER_SIZE
Definition: native-lib.cpp:47
JNIEXPORT void JNICALL Java_org_skia_skottie_SkottieRunner_nSetMaxCacheSize(JNIEnv *env, jclass clazz, jint maxCacheSize, jlong nativeProxy)
Definition: native-lib.cpp:124
#define ATRACE_NAME(name)
Definition: native-lib.cpp:68
static JavaVM * sJVM
Definition: native-lib.cpp:75
JNIEXPORT jlong JNICALL Java_org_skia_skottie_SkottieAnimation_nCreateProxy(JNIEnv *env, jobject clazz, jlong runner, jobject bufferObj)
Definition: native-lib.cpp:145
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
Definition: native-lib.cpp:137
SkottieRunner * mRunner
Definition: native-lib.cpp:136
sk_sp< skottie::Animation > mAnimation
Definition: native-lib.cpp:138
sk_sp< GrDirectContext > mDContext
Definition: native-lib.cpp:72
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63