分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
在cantk-runtime中直接使用的webview,通过JAVA扩展接口把Canvas的2d Context的API定向到JNI,通过OpenGL来图形加速,渲染速度大大提高。后来测试发现在大部分手机上都很正常,但是在有的老手机上速度不稳定,出现突然卡顿的情况。经过研究发现原因是老版本的webkit里没有requestAnimationFrame这个接口(或类似接口),用setTimeout来模拟的requestAnimationFrame非常不稳定。
为了解决这个问题,我们决定像其它Runtime一样集成Google V8 JS引擎,自己模拟一个webview出来。架构也相当简单,上面用GLSurfaceView,在JNI里使用Google V8 JS引擎来JS代码,在onDrawFrame里去执行requestAnimationFrame注册的函数,经过测试性能相当稳定。
Google V8 JS引擎在缺省情况下用起来非常顺手,它自带的例子拿来就可以用,比如:
int main(int argc, char* argv[]) { v8::V8::InitializeICU(); v8::Platform* platform = v8::platform::CreateDefaultPlatform(); v8::V8::InitializePlatform(platform); v8::V8::Initialize(); v8::V8::SetFlagsFromCommandLine(&argc, argv, true); ShellArrayBufferAllocator array_buffer_allocator; v8::V8::SetArrayBufferAllocator(&array_buffer_allocator); v8::Isolate* isolate = v8::Isolate::New(); run_shell = (argc == 1); int result; { v8::Isolate::Scope isolate_scope(isolate); v8::HandleScope handle_scope(isolate); v8::Handle<v8::Context> context = CreateShellContext(isolate); if (context.IsEmpty()) { fprintf(stderr, "Error creating context\n"); return 1; } v8::Context::Scope context_scope(context); result = RunMain(isolate, argc, argv); if (run_shell) RunShell(context); } v8::V8::Dispose(); v8::V8::ShutdownPlatform(); delete platform; return result;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
但是我需要在onSurfaceCreated做初始化的工作,在onDrawFrame里去执行JS。一个看似简单的改东却遇到了麻烦:Isolate::GetCurrent()返回值为空。第一次接触V8,对里面的概念理解不深,以为Isolate::Scope和HandleScope一样,在作用范围结束时释放Isolate,我希望onSurfaceCreated调用完成后Isolate对象还活着,在onDrawFrame还可以使用,所以去掉v8::Isolate::Scope isolate_scope(isolate)这行代码,但是结果Isolate::GetCurrent()返回值还是为空。
网上没有找到类似的应用场景,于是去看V8的代码。发现Isolate::Scope的功能并不是想的那样。在Isolate::Scope的构造函数里调用Isolate::Enter把参数指定的isolate设置成当前的isolate(放在线程局部存储里的),在Isolate::Scope的析构函数调用Isolate::Exit恢复前一个isolate。只有在这个范围内Isolate::GetCurrent()才有效。
知道原理后,修改很简单了:直接调用Isolate::Enter。
cantk-runtime v8版本目前还不完善,不过欢迎大家加星:)
给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/2e19ffb3303a490d20ea5d00dd397156.gif)
新的改变
我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客: