1. 程式人生 > >android非硬件加速繪制簡單流程

android非硬件加速繪制簡單流程

left surface fff asa face cpp erp erer enter

這裏的硬件加速是指openGL + GPU

如果不適用硬件加速:

1 ViewRootImpl.java draw:
if (!dirty.isEmpty() || mIsAnimating || accessibilityFocusDirty) {
if (mAttachInfo.mHardwareRenderer != null && mAttachInfo.mHardwareRenderer.isEnabled()) {
...
mAttachInfo.mHardwareRenderer.draw(mView, mAttachInfo, this); //使用硬件繪制
...
} else {
//使用軟件繪制
if (!drawSoftware(surface, mAttachInfo, xOffset, yOffset, scalingRequired, dirty)) {
return;
}
}

2. drawSoftware:
...
canvas = mSurface.lockCanvas(dirty); //dequeueBuffer, 向sf申請buffer 構造一個canvas
...
mView.draw(canvas); //調用view類中的draw,使用canvas畫圖
...
surface.unlockCanvasAndPost(canvas); // queueBuffer 提交到sf
...

3. canvas 畫圖的實現如 Canvas.java 中drawArc:
=》 native_drawArc

4. android_graphics_canvas.cpp:
static JNINativeMethod gMethods[] = {
...
{"native_drawArc","(JFFFFFFZJ)V", (void*) CanvasJNI::drawArc},
...
}
=> static void drawArc(JNIEnv* env, jobject, jlong canvasHandle, jfloat left, jfloat top,
jfloat right, jfloat bottom, jfloat startAngle, jfloat sweepAngle,
jboolean useCenter, jlong paintHandle) {
const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
get_canvas(canvasHandle)->drawArc(left, top, right, bottom, startAngle, sweepAngle,
useCenter, *paint);
}
// canvasHandle從何而來?
static jlong initRaster(JNIEnv* env, jobject, jobject jbitmap) {
SkBitmap bitmap;
if (jbitmap != NULL) {
GraphicsJNI::getSkBitmap(env, jbitmap, &bitmap);
}
return reinterpret_cast<jlong>(Canvas::create_canvas(bitmap));
}

5. SkiaCanvas.cpp:
Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
return new SkiaCanvas(bitmap);
}
=>
SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) : mHighContrastText(false) {
mCanvas.reset(new SkCanvas(bitmap));
}

//最終調到了SkCanvas ? 它是google的2d圖像庫,這裏用bitmap構造,在bitmap上畫圖
源碼位於android/external/skia/

所以封裝層次是 Canvas.java (Java) -》 android_graphics_canvas.cpp(JNI) -》SkiaCanvas.cpp(native) -》 SkCanvas (so lib)

android非硬件加速繪制簡單流程