The WebView, which is used directly in Cantk-runtime, directs the canvas's 2d context API to JNI via the Java extension interface, and the graphics acceleration through OpenGL, rendering speed is greatly improved. Later, the test found that most of the phones are normal, but in some of the old mobile phone speed is not stable, there is a sudden lag. The study found that the reason is that the old version of the WebKit is not requestanimationframe this interface (or similar interface), with settimeout to simulate the requestanimationframe is very unstable.
To solve this problem, we decided to integrate the Google V8 JS engine like any other runtime to simulate a webview. The architecture is quite simple, the above with Glsurfaceview, in the JNI use Google V8 js engine to JS code, in Ondrawframe to perform requestanimationframe registration function, after testing performance is quite stable.
Google V8 JS engine is very handy by default, its own examples can be used, such as:
int main (int argc, char* argv[]) {V8:: V8:: Initializeicu();V8::P latform*Platform =V8::p Latform:: Createdefaultplatform();V8:: V8:: Initializeplatform(platform);V8:: V8:: Initialize();V8:: V8:: Setflagsfromcommandline(&ARGC, argv,true);ShellarraybufferallocatorArray_buffer_allocator;V8:: V8:: Setarraybufferallocator(&array_buffer_allocator);V8:: isolate*Isolate =V8:: Isolate:: New(); Run_shell = (ARGC = =1); int result; {V8:: Isolate:: ScopeIsolate_scope (isolate);V8:: HandlescopeHandle_scope (isolate);V8:: Handle<v8:: Context> context =Createshellcontext(isolate);if(Context.IsEmpty()) {fprintf (stderr,"Error creating context\n");return 1; }V8:: Context:: ScopeContext_scope (context); result =Runmain(Isolate, argc, argv);if(Run_shell)Runshell(context); }V8:: V8::D ispose();V8:: V8:: Shutdownplatform(); Delete platform;returnResult;}
But I need to do initialization work in onsurfacecreated, to execute JS in Ondrawframe. A seemingly simple change to the East has encountered trouble: Isolate::getcurrent () The return value is empty. The first contact V8, the concept of the inside understanding is not deep, think Isolate::scope and handlescope like, in the scope of the end of the release of isolate, I hope the onsurfacecreated after the call is complete isolate object is still alive, The ondrawframe can also be used, so the V8::isolate::scope Isolate_scope (Isolate) is removed, but the result isolate::getcurrent () return value is still empty.
No similar application was found on the internet, so I went to see the V8 code. Discovering Isolate::scope's function is not what it's about. In the constructor of Isolate::scope, call Isolate::enter to set the isolate specified by the parameter to the current isolate (placed in the thread local store), Isolate::scope in the destructor call isolate:: Exit restores the previous isolate. Only isolate::getcurrent () is valid within this range.
Once the principle is known, the modification is simple: call Isolate::enter directly.
Cantk-runtime V8 version is not perfect at present, but welcome to add Star:)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Using the Google V8 JS engine on Android