Flutter Engine
The Flutter Engine
Classes | Macros | Functions | Variables
native-lib.cpp File Reference
#include "include/codec/SkCodec.h"
#include "include/codec/SkGifDecoder.h"
#include "include/codec/SkJpegDecoder.h"
#include "include/codec/SkPngDecoder.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkFontMgr.h"
#include "include/core/SkStream.h"
#include "include/core/SkSurface.h"
#include "include/ports/SkFontMgr_empty.h"
#include "include/gpu/GrBackendSurface.h"
#include "include/gpu/GrContextOptions.h"
#include "include/gpu/GrDirectContext.h"
#include "include/gpu/ganesh/SkSurfaceGanesh.h"
#include "include/gpu/ganesh/gl/GrGLBackendSurface.h"
#include "include/gpu/ganesh/gl/GrGLDirectContext.h"
#include "include/gpu/gl/GrGLInterface.h"
#include "include/gpu/gl/GrGLTypes.h"
#include "include/gpu/ganesh/gl/egl/GrGLMakeEGLInterface.h"
#include "modules/skottie/include/Skottie.h"
#include "modules/skresources/include/SkResources.h"
#include "modules/sksg/include/SkSGInvalidationController.h"
#include "modules/skshaper/utils/FactoryHelpers.h"
#include <jni.h>
#include <math.h>
#include <string>
#include <utility>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <GLES3/gl3.h>

Go to the source code of this file.

Classes

struct  SkottieRunner
 
struct  SkottieAnimation
 

Macros

#define STENCIL_BUFFER_SIZE   8
 
#define ATRACE_NAME(name)
 
#define ATRACE_CALL()
 

Functions

static void release_global_jni_ref (const void *, void *context)
 
JNIEXPORT jlong JNICALL Java_org_skia_skottie_SkottieRunner_nCreateProxy (JNIEnv *env, jclass clazz)
 
JNIEXPORT void JNICALL Java_org_skia_skottie_SkottieRunner_nDeleteProxy (JNIEnv *env, jclass clazz, jlong nativeProxy)
 
JNIEXPORT void JNICALL Java_org_skia_skottie_SkottieRunner_nSetMaxCacheSize (JNIEnv *env, jclass clazz, jint maxCacheSize, jlong nativeProxy)
 
JNIEXPORT jlong JNICALL Java_org_skia_skottie_SkottieAnimation_nCreateProxy (JNIEnv *env, jobject clazz, jlong runner, jobject bufferObj)
 
JNIEXPORT void JNICALL Java_org_skia_skottie_SkottieAnimation_nDeleteProxy (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 jlong JNICALL Java_org_skia_skottie_SkottieAnimation_nGetDuration (JNIEnv *env, jclass clazz, jlong nativeProxy)
 

Variables

static JavaVM * sJVM = nullptr
 

Macro Definition Documentation

◆ ATRACE_CALL

#define ATRACE_CALL ( )

Definition at line 69 of file native-lib.cpp.

◆ ATRACE_NAME

#define ATRACE_NAME (   name)

Definition at line 68 of file native-lib.cpp.

◆ STENCIL_BUFFER_SIZE

#define STENCIL_BUFFER_SIZE   8

Definition at line 47 of file native-lib.cpp.

Function Documentation

◆ Java_org_skia_skottie_SkottieAnimation_nCreateProxy()

JNIEXPORT jlong JNICALL Java_org_skia_skottie_SkottieAnimation_nCreateProxy ( JNIEnv *  env,
jobject  clazz,
jlong  runner,
jobject  bufferObj 
)

Definition at line 145 of file native-lib.cpp.

148 {
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}
SK_API sk_sp< SkFontMgr > SkFontMgr_New_Custom_Empty()
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
double duration() const
Definition: Skottie.h:268
static sk_sp< DataURIResourceProviderProxy > Make(sk_sp< ResourceProvider > rp, ImageDecodeStrategy=ImageDecodeStrategy::kLazyDecode, sk_sp< const SkFontMgr > fontMgr=nullptr)
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
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
static JavaVM * sJVM
Definition: native-lib.cpp:75
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
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63

◆ Java_org_skia_skottie_SkottieAnimation_nDeleteProxy()

JNIEXPORT void JNICALL Java_org_skia_skottie_SkottieAnimation_nDeleteProxy ( JNIEnv *  env,
jclass  clazz,
jlong  nativeProxy 
)

Definition at line 206 of file native-lib.cpp.

207 {
208 if (!nativeProxy) {
209 return;
210 }
211 SkottieAnimation* skottieAnimation = reinterpret_cast<SkottieAnimation*>(nativeProxy);
212 delete skottieAnimation;
213}

◆ Java_org_skia_skottie_SkottieAnimation_nDrawFrame()

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 at line 217 of file native-lib.cpp.

223 {
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}
@ kBottomLeft_GrSurfaceOrigin
Definition: GrTypes.h:149
SkColorType
Definition: SkColorType.h:19
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition: SkColorType.h:38
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
@ kUnknown_SkPixelGeometry
T * get() const
Definition: SkRefCnt.h:303
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
SK_API GrBackendRenderTarget MakeGL(int width, int height, int sampleCnt, int stencilBits, const GrGLFramebufferInfo &glInfo)
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)
#define STENCIL_BUFFER_SIZE
Definition: native-lib.cpp:47
#define ATRACE_NAME(name)
Definition: native-lib.cpp:68
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
sk_sp< GrDirectContext > mDContext
Definition: native-lib.cpp:72

◆ Java_org_skia_skottie_SkottieAnimation_nGetDuration()

JNIEXPORT jlong JNICALL Java_org_skia_skottie_SkottieAnimation_nGetDuration ( JNIEnv *  env,
jclass  clazz,
jlong  nativeProxy 
)

Definition at line 276 of file native-lib.cpp.

278 {
279 if (!nativeProxy) {
280 return 0;
281 }
282 SkottieAnimation* skottieAnimation = reinterpret_cast<SkottieAnimation*>(nativeProxy);
283 return (jlong) skottieAnimation->mDuration;
284}

◆ Java_org_skia_skottie_SkottieRunner_nCreateProxy()

JNIEXPORT jlong JNICALL Java_org_skia_skottie_SkottieRunner_nCreateProxy ( JNIEnv *  env,
jclass  clazz 
)

Definition at line 88 of file native-lib.cpp.

88 {
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}
const char * options
SK_API sk_sp< GrDirectContext > MakeGL(sk_sp< const GrGLInterface >, const GrContextOptions &)
SK_API sk_sp< const GrGLInterface > MakeEGL()

◆ Java_org_skia_skottie_SkottieRunner_nDeleteProxy()

JNIEXPORT void JNICALL Java_org_skia_skottie_SkottieRunner_nDeleteProxy ( JNIEnv *  env,
jclass  clazz,
jlong  nativeProxy 
)

Definition at line 109 of file native-lib.cpp.

109 {
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}

◆ Java_org_skia_skottie_SkottieRunner_nSetMaxCacheSize()

JNIEXPORT void JNICALL Java_org_skia_skottie_SkottieRunner_nSetMaxCacheSize ( JNIEnv *  env,
jclass  clazz,
jint  maxCacheSize,
jlong  nativeProxy 
)

Definition at line 124 of file native-lib.cpp.

125 {
126 if (!nativeProxy) {
127 return;
128 }
129 SkottieRunner* skottie = reinterpret_cast<SkottieRunner*>(nativeProxy);
130 if (skottie->mDContext) {
131 skottie->mDContext->setResourceCacheLimit(maxCacheSize);
132 }
133}

◆ release_global_jni_ref()

static void release_global_jni_ref ( const void *  ,
void *  context 
)
static

Definition at line 77 of file native-lib.cpp.

77 {
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}
#define SK_ABORT(message,...)
Definition: SkAssert.h:70

Variable Documentation

◆ sJVM

JavaVM* sJVM = nullptr
static

Definition at line 75 of file native-lib.cpp.