Skia简介以及在Windows下编译操作步骤

Skia是一个C++的开源2D向量图形处理函数库(Cairo是一个矢量库),包括字型、坐标转换、位图等等,相当于轻量级的Cairo,目前主要用于Google的Android和Chrome平台,Skia搭配OpenGL/ES与特定的硬件特征,强化显示的效果。另外,Skia是WebKit支持的众多图形平台之一,在WebKit的GraphicsContext.h/.c中有相关实现。

Android与Chrome的源代码库中都有一份Skia的复制,因需求不同,做了部分的修改。

Skia需要的底层库有:freetype2,expat,tinyxml。

可以用svn从Google下载源码:svncheckout http://skia.googlecode.com/svn 。

Skia的头文件以及库函数接口是以C++实现的。

代码结构,src目录下:

(1)、animator目录:用来实现Skia的动画效果,Android不支持;

(2)、core目录:Skia的核心,基本都是一些图形绘制函数;

(3)、effects目录:实现一些图形图像的特效效果,包括遮罩、浮雕、模糊、滤镜、渐变色、离散、透明等,并且实现了PATH的各种特效;

(4)gl目录:实现了Skia的图形库,Skia调用OpenGL或OpenGL ES来实现一些简单的3D效果;

(5)、images目录:用来处理images的,支持常见图像的解码、部分图像的编码和动画。Skia能处理的图像类型包括:BMP、JPEG/PVJPEG、PNG、ICO、SkMovie处理gif动画;

(6)、ports目录:是Skia的一些接口在不同系统上的实现,平台相关的代码,比如字体、线程、时间等。这些与Skia的接口,需要针对不同的操作系统实现;

(7)、svg目录:实现了Skia对矢量图SVG的支持,Android不支持;

(8)、utils目录:一些辅助工具类;

(9)、views目录:Skia构建了一套界面UI库;

(10)、xml目录:处理xml的数据,Skia在这里只是对xml解析器做了一层包装,具体的xml解析器的实现需要根据不同的操作系统及宿主程序来实现;

(11)、opts目录:性能优化的代码;

(12)、pdf目录:处理PDF文档,用了一个fpdfemb库;

Skia主要使用的第三方库:Zlib、Jpeglib、Pnglib、giflib、fpdfemb(处理pdf文档)。

Skia引擎在android源代码当中的位置:(1)、头文件:android/external/skia/include,其中还包含几个子目录:animotor,core,effects,images,views,…;(2)、源文件位于:android/external/skia/src目录,子目录结构和头文件目录相同;(3)、封装层:android对Skia引擎进行了封装,以便让java代码方便的调用,对Skia封装的代码存在于android/framework/base/core/jni以及android/framework/base/core/jni/android/graphics目录下面。在Android中Skia以扩展库的形式存在,目录为external/skia。

Skia本身是一个opensource的project,集成于android系统中。所以Skia不是android框架的一部分,不需要实现框架的api来支持Skia。不过Skia同样可以挂接其它的第3方编码解码库或者硬件编解码库。

Android版本不同,Skia版本也不太相同,接口有些细微的差别。

Skia主要包含三个库:(1)、libcorecg.so:包含/skia/src/core的部分内容,比如其中的Region,Rect是在SurfaceFlinger里面用来计算可视区域的;(2)、libsgl.so:包含/skia/src/core|effects|images|ports|utils的部分和全部内容,这个实现了skia大部分的图形效果,以及图形格式的编解码;(3)、libskiagl.so:包含/skia/src/gl里面的内容,主要用来调用opengl实现部分效果。

Skia对上层的接口:Skia的源文件及部分头文件都在external/skia/src目录下,导出的头文件在external/skia/include目录下。最主要的是SKCanvas类,几乎整个AndroidGUI系统的底层绘制都是由这个类来完成的。SKCanvas类主要有三种绘制功能:(1)、基本图形绘制(如drawARGB,drawLine函数);(2)、图像文件绘制(如drawBitmap函数);(3)、文本绘制(drawText函数)。

Skia的图像编解码部分:(1)、/include/image/SKImageDecoder.h:把图像文件或者流解码到skia的内部内存SKBitmap中;(2)、/include/image/SKImageEncoder.h :把skia内部内存SKBitmap编码成文件或流的形式。这些接口需要具体的类实现,主要代码在src/image文件中。

Skia引擎重要类:(1)、SkCanvas:Skia引擎的一个核心类,它封装了所有对设备进行的画图操作.

Skia中用到的硬件加速技术:(1)、OpenGL/OpenGLES;(2)、SIMD:SSE2/SSSE3,NEON.

 

Skia在Windows下的编译:

1、   从https://www.python.org/download/releases/2.7.8/下载python-2.7.8.amd64.msi并安装;安装到D:\ProgramFiles\Python27目录下,并将D:\ProgramFiles\Python27添加到环境变量Path中,重启电脑;

2、    从 https://cygwin.com/install.html 下载setup-x86_64.exe 并安装;

3、   使用svn,下载gyp: http://gyp.googlecode.com/svn/trunk, 将整个gyp文件夹存放到/trunk_no_commit/third_party/externals文件夹下;

4、   在其它分支中将jsoncpp、jsoncpp-chromium、libjpeg、libwebp四个文件夹内容全部拷贝到/trunk_no_commit/third_party/externals文件夹下;

5、   打开Cygwin,将其切换到Skia的trunk_no_commit目录下,执行 python gyp_skia,会生成一个out文件夹,里面有生成的skia.sln工程;

6、   如果在执行python gyp_skia命令过程中,提示/third_party/externals/libwebp/src/dec/frame.c 等文件丢失,则从相关网站下载最新代码替换之即可。

7、   (1)、如果出现errorC2220:warning treated as error -no ‘object’ file generated,则将相应工程中的C/C++->General:Treat Warning As Errors,由原来的Level3(/W3)改为No(/WX-),如effects、gm工程;(2)、选中gm/verttext.cpp文件->File->AdvancedSave Options->Encoding:由原来的Unicode(UTF-8 without signature)-Codepage 65001改为ChineseSimplified(GB2312)-Codepage 936;(3)、如果third_party/extternal中的第三方库文件编译时与其它源代码不匹配,可以通过改动gyp文件夹下的相应.gyp文件来做相应的调整;

8、   通过svn http://skia.googlecode.com/svn下载所有相应的Skia源码,包括branches和trunk,下载后发现没有trunk,只有trunk_no_commit;

9、   打开Cygwin,将其定位到trunk_no_commit文件夹,执行 python gyp_skia 命令,如果出现Warning: Missing input files相关信息,说明缺少相关文件,则根据提示查找原因,否则在后期编译时会报错;

10、 正确执行完pythongyp_skia命令后,会在trunk_no_commit目录下生成out文件夹,打开里面的skia.sln,编译SampleApp工程,会生成SampleApp.exe和其它相应静态库。

11、仿照tests工程新创建一个TestSkia工程,工程设置及代码如下:

 (1)、Debug和Release下,Character Set:Use Unicode Character Set;

 (2)、C/C++ ->Additional Include Directories:

 

E:\Skia\code\google\trunk_no_commit\includeE:\Skia\code\google\trunk_no_commit\include\animatorE:\Skia\code\google\trunk_no_commit\include\configE:\Skia\code\google\trunk_no_commit\include\coreE:\Skia\code\google\trunk_no_commit\include\device\xpsE:\Skia\code\google\trunk_no_commit\include\effectsE:\Skia\code\google\trunk_no_commit\include\gpuE:\Skia\code\google\trunk_no_commit\include\gpu\glE:\Skia\code\google\trunk_no_commit\include\imagesE:\Skia\code\google\trunk_no_commit\include\pathopsE:\Skia\code\google\trunk_no_commit\include\pdfE:\Skia\code\google\trunk_no_commit\include\pipeE:\Skia\code\google\trunk_no_commit\include\portsE:\Skia\code\google\trunk_no_commit\include\recordE:\Skia\code\google\trunk_no_commit\include\svgE:\Skia\code\google\trunk_no_commit\include\textE:\Skia\code\google\trunk_no_commit\include\utilsE:\Skia\code\google\trunk_no_commit\include\utils\winE:\Skia\code\google\trunk_no_commit\include\viewsE:\Skia\code\google\trunk_no_commit\include\views\animatedE:\Skia\code\google\trunk_no_commit\include\xmlE:\Skia\code\google\trunk_no_commit\tools

(3)、C/C++ -> Preprocessor: 

  Debug:

WIN32_DEBUG_CONSOLESK_INTERNALSK_GAMMA_SRGBSK_GAMMA_APPLY_TO_A8SK_SCALAR_TO_FLOAT_EXCLUDEDSK_ALLOW_STATIC_GLOBAL_INITIALIZERS=1SK_SUPPORT_GPU=1SK_SUPPORT_OPENCL=0SK_FORCE_DISTANCEFIELD_FONTS=0SK_SCALAR_IS_FLOATSK_CAN_USE_FLOATSK_BUILD_FOR_WIN32_CRT_SECURE_NO_WARNINGSGR_GL_FUNCTION_TYPE=__stdcallSK_BUILD_JSON_WRITERSK_SUPPORT_PDFSK_DEBUGSK_DEVELOPER=1

Release:

WIN32NDEBUG_CONSOLESK_INTERNALSK_GAMMA_SRGBSK_GAMMA_APPLY_TO_A8SK_SCALAR_TO_FLOAT_EXCLUDEDSK_ALLOW_STATIC_GLOBAL_INITIALIZERS=1SK_SUPPORT_GPU=1SK_SUPPORT_OPENCL=0SK_FORCE_DISTANCEFIELD_FONTS=0SK_SCALAR_IS_FLOATSK_CAN_USE_FLOATSK_BUILD_FOR_WIN32_CRT_SECURE_NO_WARNINGSGR_GL_FUNCTION_TYPE=__stdcallSK_BUILD_JSON_WRITERSK_SUPPORT_PDFSK_RELEASE

(4)、Linker -> Input -> Additional Dependencies:

OpenGL32.libusp10.libDelayImp.libwindowscodecs.lib

(5)、将trunk_no_commit->tools->flags中的SkCommandLineFlags两个文件拷贝到此测试工程下并加入到工程中;

 (6)、stdafx.h:

#pragma once#include "targetver.h"#include <stdio.h>#include "SkCanvas.h"#include "SkData.h"#include "SkDocument.h"#include "SkForceLinking.h"#include "SkGraphics.h"#include "SkSurface.h"#include "SkImage.h"#include "SkStream.h"#include "SkString.h"#include "SkCommandLineFlags.h"

(7)、stdafx.cpp:

#include "stdafx.h"// TODO: reference any additional headers you need in STDAFX.H// and not in this file#ifdef _DEBUG #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/lib/experimental.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/lib/libetc1.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/lib/libjpeg.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/lib/libwebp_dec.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/lib/libwebp_dsp.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/lib/libwebp_enc.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/lib/libwebp_utils.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/lib/lua.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/lib/skflate.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/lib/views_animated.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/skia_animator.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/skia_core.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/skia_effects.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/skia_images.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/skia_opts.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/skia_opts_ssse3.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/skia_pdf.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/skia_ports.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/skia_sfnt.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/skia_skgpu.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/skia_skgputest.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/skia_utils.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/skia_views.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Debug/skia_xml.lib")#else #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/lib/experimental.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/lib/libetc1.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/lib/libjpeg.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/lib/libwebp_dec.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/lib/libwebp_dsp.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/lib/libwebp_enc.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/lib/libwebp_utils.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/lib/lua.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/lib/skflate.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/lib/views_animated.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/skia_animator.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/skia_core.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/skia_effects.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/skia_images.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/skia_opts.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/skia_opts_ssse3.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/skia_pdf.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/skia_ports.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/skia_sfnt.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/skia_skgpu.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/skia_skgputest.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/skia_utils.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/skia_views.lib") #pragma comment(lib, "E:/Skia/code/google/trunk_no_commit/out/Release/skia_xml.lib")#endif

(8)、TestSkia.cpp:

#include "stdafx.h"#include <iostream>using namespace std;__SK_FORCE_IMAGE_DECODER_LINKING;DEFINE_string2(outFile, o, "skhello", "The filename to write the image.");DEFINE_string2(text, t, "Hello", "The string to write.");static void doDraw(SkCanvas* canvas, const SkPaint& paint, const char text[]) { SkRect bounds; canvas->getClipBounds(&bounds); canvas->drawColor(SK_ColorWHITE); canvas->drawText(text, strlen(text), bounds.centerX(), bounds.centerY(), paint);}static bool do_surface(int w, int h, const char path[], const char text[], const SkPaint& paint) { SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(w, h)); doDraw(surface->getCanvas(), paint, text); SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); SkAutoDataUnref data(image->encode())if (NULL == data.get()) {  return false; } SkFILEWStream stream(path)return stream.write(data->data(), data->size());}static bool do_document(int w, int h, const char path[], const char text[], const SkPaint& paint) { SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(path)); if (doc.get()) {  SkScalar width = SkIntToScalar(w);  SkScalar height = SkIntToScalar(h);  doDraw(doc->beginPage(width, height, NULL), paint, text);  return true; } return false;}int tool_main(int argc, char** argv) { SkCommandLineFlags::SetUsage(""); SkCommandLineFlags::Parse(argc, argv); SkAutoGraphics ag; SkString path("skhello")SkString text("Hello")if (!FLAGS_outFile.isEmpty()) {  path.set(FLAGS_outFile[0]); } if (!FLAGS_text.isEmpty()) {  text.set(FLAGS_text[0]); } SkPaint paint; paint.setAntiAlias(true); paint.setTextSize(SkIntToScalar(30)); paint.setTextAlign(SkPaint::kCenter_Align); SkScalar width = paint.measureText(text.c_str(), text.size()); SkScalar spacing = paint.getFontSpacing(); int w = SkScalarRoundToInt(width) + 30int h = SkScalarRoundToInt(spacing) + 30static const struct {  bool (*fProc)(int w, int h, const char path[], const char text[],   const SkPaint&);  const char* fSuffix; } gRec[] = {  { do_surface, ".png" },  { do_document, ".pdf" }, }; for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {  SkString file;  file.printf("%s%s", path.c_str(), gRec[i].fSuffix);  if (!gRec[i].fProc(w, h, file.c_str(), text.c_str(), paint)) {   return -1;  } } return 0;}int main(int argc, char* argv[])int flag = tool_main(argc, (char**) argv); if (flag == 0)  cout<<"ok"<<endlelse   cout<<"error"<<endlreturn 0;}

运行此工程,会在此工程目录下生成 skhello.pdf 和 skhello.png 两个文件,文件内容为Hello

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

Published by

风君子

独自遨游何稽首 揭天掀地慰生平