首页 > 代码库 > android非硬件加速绘制简单流程

android非硬件加速绘制简单流程

这里的硬件加速是指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非硬件加速绘制简单流程