原文出处:http://blog.linux.org.tw/~jserv/archives/002095.html
2008 年九月,Google 宣佈以改良過的 WebKit 為核心的網路瀏覽器 Chrome,揭露了眾多新特徵,比方說嶄新的 [ V8] JavaScript (ECMAscript) 執行引擎,或許因為太亮眼,掩蔽了所使用另一個開放原始碼專案 [ skia],後者是個 2D 向量圖形處理函式庫,包含字型、座標轉換,以及點陣圖都有高效能且簡潔的表現。不僅用於 Google Chrome 瀏覽器,新興的 Android 開放手機平台也採用 skia 作為繪圖處理,搭配 OpenGL/ES 與特定的硬體特徵,強化顯示的效果,本文簡介 Google Skia 的歷史背景、應用層面,並探討其程式設計模型。
# svn co http://skia.googlecode.com/svn/trunk skia-trunk乍看這「清爽」的目錄架構,很難想像過去這是商業軟體,或許 Google 有些「不能說的秘密」,除了 samplecode/ 目錄若干的程式碼之外,就幾乎沒有充分的文檔了。用 svn log 可瀏覽 Skia 開發的紀錄,"[email protected]" 就是 Mike Reed 本人,至今仍相當活躍地改良 Skia 的實做。編譯方式很單純,先看看說明:(本文對應於 svn r130)
# cd skia-trunk # make help可得到以下說明:
Targets: : out/libskia.a bench: out/bench/bench tests: out/tests/tests clean: removes entire out/ directory help: this text Options: (after make, or in bash shell) SKIA_DEBUG=true for debug build SKIA_SCALAR=fixed for fixed-point build SKIA_BUILD_FOR=mac for mac build (e.g. CG for image decoding)期望的編譯輸出就是靜態函式庫 out/libskia.a,而 Skia 的內部運算可選擇浮點數與定點 (fixed-point),不過筆者發現,目前尙未能透地選擇,但這不影響我們理解 Skia 的使用與體驗其威力。以筆者使用的 GNU/Linux 來說,可下達以下指令要求編譯:
# make SKIA_BUILD_FOR=linux沒意外的話,系統就會乖乖的編譯:
compiling out/src/core/Sk64.o compiling out/src/core/SkAlphaRuns.o compiling out/src/core/SkBitmap.o ...至於編譯 benchmark 程式,則可透過以下指令:
# make SKIA_BUILD_FOR=linux benchbenchmark 程式算是除了 Chromium 之外,最佳的「文件」了,不過 SKia API 本來就簡潔強大,這也不妨礙。執行 benchmark 程式:
./out/bench/bench -o `pwd`陸續會有類似以下的輸出:
running bench polygon running bench lines running bench points running bench rrects3 running bench rrects1 running bench ovals3 running bench ovals1 running bench rects3 running bench rects1 running bench bitmap_index8 running bench bitmap_4444 running bench bitmap_565 running bench bitmap_8888可大概窺知 Skia 涵蓋的範疇,接著筆者就寫個小程式,使用 Skia C++ API: [ test-skia.c]
/* Simple vector graphics demo utilizing Skia toolkit. * Authored by Jim Huang編譯方式:*/ #include "SkBitmap.h" #include "SkDevice.h" #include "SkPaint.h" #include "SkRect.h" #include "SkImageEncoder.h" int main() { // Declare a raster bitmap, which has an integer width and height, // and a format (config), and a pointer to the actual pixels. // Bitmaps can be drawn into a SkCanvas, but they are also used to // specify the target of a SkCanvas' drawing operations. SkBitmap bitmap; bitmap.setConfig(SkBitmap::kARGB_8888_Config, 200, 200); bitmap.allocPixels(); // A Canvas encapsulates all of the state about drawing into a // device (bitmap). This includes a reference to the device itself, // and a stack of matrix/clip values. For any given draw call (e.g. // drawRect), the geometry of the object being drawn is transformed // by the concatenation of all the matrices in the stack. The // transformed geometry is clipped by the intersection of all of the // clips in the stack. SkCanvas canvas(new SkDevice(bitmap)); // SkPaint class holds the style and color information about how to // draw geometries, text and bitmaps. SkPaint paint; // SkIRect holds four 32 bit integer coordinates for a rectangle. SkRect r; paint.setARGB(255, 255, 0, 0); r.set(25, 25, 145, 145); canvas.drawRect(r, paint); /** Draw the specified rectangle using the specified paint. The rectangle will be filled or stroked based on the Style in the paint. */ paint.setARGB(255, 0, 255, 0); r.offset(20, 20); canvas.drawRect(r, paint); paint.setARGB(255, 0, 0, 255); r.offset(20, 20); canvas.drawRect(r, paint); // SkImageEncoder is the base class for encoding compressed images // from a specific SkBitmap. SkImageEncoder::EncodeFile("snapshot.png", bitmap, SkImageEncoder::kPNG_Type, /* Quality ranges from 0..100 */ 100); return 0; }
g++ / -I./include / -I./include/core / -I./include/images / -Wall -o test-skia test-skia.c / out/src/images/SkImageDecoder_libpng.o out/libskia.a / -lpng -lpthread -g筆者做了簡要的註解,大概可知曉 Sk 開頭的這些 API 的功用,而上述的範例程式一開始就要求 Skia 配置畫布 (SkCanvas),接著透過一份 SkRect 物件 r,給定 ARGB 的描述,使其有著不同的顏色,再來就是調整向量物件的位移並繪製。正如前文提及,Skia 僅是繪圖引擎,並未如 Cairo 一般廣泛對應到 PDF, X11, GDI 等等底層繪圖裝置,所以為了方便觀察繪圖結果,我們透過 Skia 內建的 image codec 來輸出 PNG 圖檔,所以執行前述編譯後的執行檔 "test-skia",應該會得到以下圖檔:(本無外框與底色,但為了清楚於文章呈現,額外用繪圖軟體追加)
paint.setARGB(255, 0, 255, 0); r.offset(20, 20); canvas.drawRect(r, paint);由於 Skia 與 Cairo 的同質性相當高,也可參照 [ Cairo :: documentation] 建立所需的背景知識。