安卓中Canvas是用来绘制图像的常用api。Canvas在java,hwui和skia中都有定义。
java
BaseCanvas
frameworks/base/graphics/java/android/graphics/BaseCanvas.java
/**
* This class is a base class for Canvas's drawing operations. Any modifications here
* should be accompanied by a similar modification to {@link BaseRecordingCanvas}.
*
* The purpose of this class is to minimize the cost of deciding between regular JNI
* and @FastNative JNI to just the virtual call that Canvas already has.
*
* @hide
*/
public abstract class BaseCanvas {
/**
* Should only be assigned in constructors (or setBitmap if software canvas),
* freed by NativeAllocation.
* @hide
*/
@UnsupportedAppUsage
protected long mNativeCanvasWrapper;
...
// ---------------------------------------------------------------------------
// Drawing methods
// These are also implemented in RecordingCanvas so that we can
// selectively apply on them
// Everything below here is copy/pasted from Canvas.java
// The JNI registration is handled by android_graphics_Canvas.cpp
// ---------------------------------------------------------------------------
public void drawArc(float left, float top, float right, float bottom, float startAngle,
float sweepAngle, boolean useCenter, @NonNull Paint paint) {
throwIfHasHwFeaturesInSwMode(paint);
nDrawArc(mNativeCanvasWrapper, left, top, right, bottom, startAngle, sweepAngle,
useCenter, paint.getNativeInstance());
}
...
}
BaseCanvas主要有一个C++对象mNativeCanvasWrapper,其draw方法都直接调用了C++对象的对应draw方法。
Canvas
frameworks/base/graphics/java/android/graphics/Canvas.java
/**
* The Canvas class holds the "draw" calls. To draw something, you need
* 4 basic components: A Bitmap to hold the pixels, a Canvas to host
* the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect,
* Path, text, Bitmap), and a paint (to describe the colors and styles for the
* drawing).
*
* <div class="special reference">
* <h3>Developer Guides</h3>
* <p>For more information about how to use Canvas, read the
* <a href="{@docRoot}guide/topics/graphics/2d-graphics.html">
* Canvas and Drawables</a> developer guide.</p></div>
*/
public class Canvas extends BaseCanvas {
...
// may be null
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 117521088)
private Bitmap mBitmap;
...
/**
* Construct a canvas with the specified bitmap to draw into. The bitmap
* must be mutable.
*
* <p>The initial target density of the canvas is the same as the given
* bitmap's density.
*
* @param bitmap Specifies a mutable bitmap for the canvas to draw into.
*/
public Canvas(@NonNull Bitmap bitmap) {
if (!bitmap.isMutable()) {
throw new IllegalStateException("Immutable bitmap passed to Canvas constructor");
}
throwIfCannotDraw(bitmap);
bitmap.setGainmap(null);
mNativeCanvasWrapper = nInitRaster(bitmap.getNativeInstance());
mFinalizer = NoImagePreloadHolder.sRegistry.registerNativeAllocation(
this, mNativeCanvasWrapper);
mBitmap = bitmap;
mDensity = bitmap.mDensity;
}
...
// ---------------- Draw Methods -------------------
/**
* <p>
* Draw the specified arc, which will be scaled to fit inside the specified oval.
* </p>
* <p>
* If the start angle is negative or >= 360, the start angle is treated as start angle modulo
* 360.
* </p>
* <p>
* If the sweep angle is >= 360, then the oval is drawn completely. Note that this differs
* slightly from SkPath::arcTo, which treats the sweep angle modulo 360. If the sweep angle is
* negative, the sweep angle is treated as sweep angle modulo 360
* </p>
* <p>
* The arc is drawn clockwise. An angle of 0 degrees correspond to the geometric angle of 0
* degrees (3 o'clock on a watch.)
* </p>
*
* @param oval The bounds of oval used to define the shape and size of the arc
* @param startAngle Starting angle (in degrees) where the arc begins
* @param sweepAngle Sweep angle (in degrees) measured clockwise
* @param useCenter If true, include the center of the oval in the arc, and close it if it is
* being stroked. This will draw a wedge
* @param paint The paint used to draw the arc
*/
public void drawArc(@NonNull RectF oval, float startAngle, float sweepAngle, boolean useCenter,
@NonNull Paint paint) {
super.drawArc(oval, startAngle, sweepAngle, useCenter, paint);
}
Canvas继承了BaseCanvas,Canvas还有一个bitmap,Canvas主要是用来在bitmap上进行绘制,其draw方法直接调用了super的draw方法。
BaseRecordingCanvas
frameworks/base/graphics/java/android/graphics/BaseRecordingCanvas.java
/**
* This class is a base class for canvases that defer drawing operations, so all
* the draw operations can be marked @FastNative. It contains a re-implementation of
* all the methods in {@link BaseCanvas}.
*
* @hide
*/
public class BaseRecordingCanvas extends Canvas {
public BaseRecordingCanvas(long nativeCanvas) {
super(nativeCanvas);
}
@Override
public final void drawArc(float left, float top, float right, float bottom, float startAngle,
float sweepAngle, boolean useCenter, @NonNull Paint paint) {
nDrawArc(mNativeCanvasWrapper, left, top, right, bottom, startAngle, sweepAngle,
useCenter, paint.getNativeInstance());
}
...
}
BaseRecordingCanvas继承了Canvas,所有的draw方法直接调用了C++对象的draw方法。
RecordingCanvas
frameworks/base/graphics/java/android/graphics/RecordingCanvas.java
/**
* A Canvas implementation that records view system drawing operations for deferred rendering.
* This is used in combination with RenderNode. This class keeps a list of all the Paint and
* Bitmap objects that it draws, preventing the backing memory of Bitmaps from being released while
* the RecordingCanvas is still holding a native reference to the memory.
*
* This is obtained by calling {@link RenderNode#beginRecording()} and is valid until the matching
* {@link RenderNode#endRecording()} is called. It must not be retained beyond that as it is
* internally reused.
*/
public final class RecordingCanvas extends BaseRecordingCanvas {
...
/**
* TODO: Temporarily exposed for RenderNodeAnimator(Set)
* @hide */
public RenderNode mNode;
private int mWidth;
private int mHeight;
/*package*/
static RecordingCanvas obtain(@NonNull RenderNode node, int width, int height) {
if (node == null) throw new IllegalArgumentException("node cannot be null");
RecordingCanvas canvas = sPool.acquire();
if (canvas == null) {
canvas = new RecordingCanvas(node, width, height);
} else {
nResetDisplayListCanvas(canvas.mNativeCanvasWrapper, node.mNativeRenderNode,
width, height);
}
canvas.mNode = node;
canvas.mWidth = width;
canvas.mHeight = height;
return canvas;
}
/*package*/
void recycle() {
mNode = null;
sPool.release(this);
}
/*package*/
void finishRecording(RenderNode node) {
nFinishRecording(mNativeCanvasWrapper, node.mNativeRenderNode);
}
///
// Constructors
///
private RecordingCanvas(@NonNull RenderNode node, int width, int height) {
super(nCreateDisplayListCanvas(node.mNativeRenderNode, width, height));
mDensity = 0; // disable bitmap density scaling
}
...
///
// Drawing
///
/**
* Draws a circle
*
* @param cx
* @param cy
* @param radius
* @param paint
*
* @hide
*/
public void drawCircle(CanvasProperty<Float> cx, CanvasProperty<Float> cy,
CanvasProperty<Float> radius, CanvasProperty<Paint> paint) {
nDrawCircle(mNativeCanvasWrapper, cx.getNativeContainer(), cy.getNativeContainer(),
radius.getNativeContainer(), paint.getNativeContainer());
}
...
}
RecordingCanvas继承BaseRecordingCanvas,主要是用来给RenderNode录制绘制操作用的。
在View的绘制流程中,会调用每个View的updateDisplayListIfDirty方法:(frameworks/base/core/java/android/view/View.java)
/**
* Gets the RenderNode for the view, and updates its DisplayList (if needed and supported)
* @hide
*/
@NonNull
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
public RenderNode updateDisplayListIfDirty() {
final RenderNode renderNode = mRenderNode;
...
if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0
|| !renderNode.hasDisplayList()
|| (mRecreateDisplayList)) {
...
final RecordingCanvas canvas = renderNode.beginRecording(width, height);
try {
if (layerType == LAYER_TYPE_SOFTWARE) {
...
} else {
...
// Fast path for layouts with no backgrounds
if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
dispatchDraw(canvas);
...
} else {
draw(canvas);
}
}
} finally {
renderNode.endRecording();
setDisplayListProperties(renderNode);
}
} else {
...
}
return renderNode;
}
首先通过renderNode.beginRecording方法获取一个RecordingCanvas,然后调用View.draw方法使用这个canvas录制绘制命令,最后调用renderNode.endRecording完成绘制命令录制。
hwui
Canvas
frameworks/base/libs/hwui/hwui/Canvas.h
class ANDROID_API Canvas {
public:
...
virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
uirenderer::CanvasPropertyPrimitive* top,
uirenderer::CanvasPropertyPrimitive* right,
uirenderer::CanvasPropertyPrimitive* bottom,
uirenderer::CanvasPropertyPrimitive* rx,
uirenderer::CanvasPropertyPrimitive* ry,
uirenderer::CanvasPropertyPaint* paint) = 0;
...
}
Canvas是一个抽象类,定义了许多draw方法,供子类去实现。
SkiaCanvas
frameworks/base/libs/hwui/SkiaCanvas.h
// Holds an SkCanvas reference plus additional native data.
class SkiaCanvas : public Canvas {
public:
...
virtual void drawColor(int color, SkBlendMode mode) override;
...
private:
...
std::unique_ptr<SkCanvas> mCanvasOwned; // Might own a canvas we allocated.
SkCanvas* mCanvas; // We do NOT own this canvas, it must survive us
// unless it is the same as mCanvasOwned.get().
...
};
SkiaCanvas继承了Canvas,SkiaCanvas包装了一个SkCanvas,所有的draw方法都直接调用了这个SkCanvas的draw方法,例如drawColor的实现:(frameworks/base/libs/hwui/SkiaCanvas.cpp)
void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
mCanvas->drawColor(color, mode);
}
在java的Canvas构造器中使用nInitRaster方法创建C++的Canvas对象:(frameworks/base/libs/hwui/jni/android_graphics_Canvas.cpp)
// Native wrapper constructor used by Canvas(Bitmap)
static jlong initRaster(JNIEnv* env, jobject, jlong bitmapHandle) {
SkBitmap bitmap;
if (bitmapHandle != 0) {
bitmap::toBitmap(bitmapHandle).getSkBitmap(&bitmap);
}
return reinterpret_cast<jlong>(Canvas::create_canvas(bitmap));
}
这里调用了Canvas的create_canvas方法:(frameworks/base/libs/hwui/SkiaCanvas.cpp)
Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
return new SkiaCanvas(bitmap);
}
实际上就是创建了一个C++的SkiaCanvas对象。
SkiaRecordingCanvas
frameworks/base/libs/hwui/pipeline/skia/SkiaRecordingCanvas.h
/**
* A SkiaCanvas implementation that records drawing operations for deferred rendering backed by a
* SkLiteRecorder and a SkiaDisplayList.
*/
class SkiaRecordingCanvas : public SkiaCanvas {
public:
explicit SkiaRecordingCanvas(uirenderer::RenderNode* renderNode, int width, int height) {
initDisplayList(renderNode, width, height);
}
...
virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
uirenderer::CanvasPropertyPrimitive* top,
uirenderer::CanvasPropertyPrimitive* right,
uirenderer::CanvasPropertyPrimitive* bottom,
uirenderer::CanvasPropertyPrimitive* rx,
uirenderer::CanvasPropertyPrimitive* ry,
uirenderer::CanvasPropertyPaint* paint) override;
...
private:
RecordingCanvas mRecorder;
std::unique_ptr<SkiaDisplayList> mDisplayList;
...
/**
* A new SkiaDisplayList is created or recycled if available.
*
* @param renderNode is optional and used to recycle an old display list.
* @param width used to calculate recording bounds.
* @param height used to calculate recording bounds.
*/
void initDisplayList(uirenderer::RenderNode* renderNode, int width, int height);
...
};
SkiaRecordingCanvas继承SkiaCanvas,存有RecordingCanvas和SkiaDisplayList。
SkiaRecordingCanvas主要是用过initDisplayList方法初始化:(frameworks/base/libs/hwui/pipeline/skia/SkiaRecordingCanvas.cpp)
void SkiaRecordingCanvas::initDisplayList(uirenderer::RenderNode* renderNode, int width,
int height) {
mCurrentBarrier = nullptr;
LOG_FATAL_IF(mDisplayList.get() != nullptr);
if (renderNode) {
mDisplayList = renderNode->detachAvailableList();
}
if (!mDisplayList) {
mDisplayList.reset(new SkiaDisplayList());
}
mDisplayList->attachRecorder(&mRecorder, SkIRect::MakeWH(width, height));
SkiaCanvas::reset(&mRecorder);
mDisplayList->setHasHolePunches(false);
}
这里调用了SkiaCanvas的reset方法:(frameworks/base/libs/hwui/SkiaCanvas.cpp)
void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
if (mCanvas != skiaCanvas) {
mCanvas = skiaCanvas;
mCanvasOwned.reset();
}
mSaveStack.reset(nullptr);
}
将RecordingCanvas设置到了内部包装的SkCanvas上面。
SkiaRecordingCanvas还实现了一些draw方法,例如SkiaRecordingCanvas的drawCircle方法:(frameworks/base/libs/hwui/pipeline/skia/SkiaRecordingCanvas.cpp)
void SkiaRecordingCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
uirenderer::CanvasPropertyPrimitive* y,
uirenderer::CanvasPropertyPrimitive* radius,
uirenderer::CanvasPropertyPaint* paint) {
drawDrawable(mDisplayList->allocateDrawable<AnimatedCircle>(x, y, radius, paint));
}
调用了SkiaCanvas的drawDrawable方法:(frameworks/base/libs/hwui/SkiaCanvas.h)
// Holds an SkCanvas reference plus additional native data.
class SkiaCanvas : public Canvas {
...
void drawDrawable(SkDrawable* drawable) { mCanvas->drawDrawable(drawable); }
...
}
最终调用了内部包装的SkCanvas的drawDrawable方法。
在java的RecordingCanvas的构造器中使用nCreateDisplayListCanvas方法创建C++的Canvas对象:(frameworks/base/libs/hwui/jni/android_graphics_DisplayListCanvas.cpp)
static jlong android_view_DisplayListCanvas_createDisplayListCanvas(CRITICAL_JNI_PARAMS_COMMA jlong renderNodePtr,
jint width, jint height) {
RenderNode* renderNode = reinterpret_cast<RenderNode*>(renderNodePtr);
return reinterpret_cast<jlong>(Canvas::create_recording_canvas(width, height, renderNode));
}
调用了Canvas的reate_recording_canvas方法:(frameworks/base/libs/hwui/hwui/Canvas.cpp)
Canvas* Canvas::create_recording_canvas(int width, int height, uirenderer::RenderNode* renderNode) {
return new uirenderer::skiapipeline::SkiaRecordingCanvas(renderNode, width, height);
}
实际上就是创建了一个SkiaRecordingCanvas对象。
RecordingCanvas
frameworks/base/libs/hwui/RecordingCanvas.h
class RecordingCanvas final : public SkCanvasVirtualEnforcer<SkNoDrawCanvas> {
public:
RecordingCanvas();
void reset(DisplayListData*, const SkIRect& bounds);
...
void onDrawRect(const SkRect&, const SkPaint&) override;
...
private:
...
DisplayListData* fDL;
...
};
RecordingCanvas里面主要是有一个DisplayListData,这个DisplayListData在reset方法里面赋值:(frameworks/base/libs/hwui/RecordingCanvas.cpp)
void RecordingCanvas::reset(DisplayListData* dl, const SkIRect& bounds) {
this->resetCanvas(bounds.right(), bounds.bottom());
fDL = dl;
mClipMayBeComplex = false;
mSaveCount = mComplexSaveCount = 0;
}
调用位置是在SkiaRecordingCanvas::initDisplayList中调用了SkiaDisplayList的attachRecorder方法:(frameworks/base/libs/hwui/RecordingCanvas.cpp)
class SkiaDisplayList {
...
void attachRecorder(RecordingCanvas* recorder, const SkIRect& bounds) {
recorder->reset(&mDisplayList, bounds);
}
...
}
所以SkiaRecordingCanvas的RecordingCanvas的DisplayListData被设置成了指向SkiaRecordingCanvas的SkiaDisplayList的DisplayListData。也就是:
SkiaRecordingCanvas.mRecorder.fDL = &SkiaRecordingCanvas.mDisplayList.mDisplayList
SkiaRecordingCanvas.mCanvas = &SkiaRecordingCanvas.mRecorder
RecordingCanvas还重写了onDraw方法,例如RecordingCanvas的onDrawRect方法:(frameworks/base/libs/hwui/RecordingCanvas.cpp)
void RecordingCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
fDL->drawRect(rect, paint);
}
直接调用了其DisplayListData的drawRect方法去录制。
DisplayListData
frameworks/base/libs/hwui/RecordingCanvas.h
class DisplayListData final {
public:
...
void draw(SkCanvas* canvas) const;
...
private:
...
void drawRect(const SkRect&, const SkPaint&);
...
template <typename T, typename... Args>
void* push(size_t, Args&&...);
template <typename Fn, typename... Args>
void map(const Fn[], Args...) const;
AutoTMalloc<uint8_t> fBytes;
size_t fUsed = 0;
size_t fReserved = 0;
bool mHasText : 1;
bool mHasFill : 1;
};
DisplayListData的录制操作例如drawRect方法:(frameworks/base/libs/hwui/RecordingCanvas.cpp)
void DisplayListData::drawRect(const SkRect& rect, const SkPaint& paint) {
this->push<DrawRect>(0, rect, paint);
}
调用了DisplayListData的push方法:(frameworks/base/libs/hwui/RecordingCanvas.cpp)
template <typename T, typename... Args>
void* DisplayListData::push(size_t pod, Args&&... args) {
size_t skip = SkAlignPtr(sizeof(T) + pod);
LOG_FATAL_IF(skip >= (1 << 24));
if (fUsed + skip > fReserved) {
static_assert(is_power_of_two(SKLITEDL_PAGE),
"This math needs updating for non-pow2.");
// Next greater multiple of SKLITEDL_PAGE.
fReserved = (fUsed + skip + SKLITEDL_PAGE) & ~(SKLITEDL_PAGE - 1);
fBytes.realloc(fReserved);
LOG_ALWAYS_FATAL_IF(fBytes.get() == nullptr, "realloc(%zd) failed", fReserved);
}
LOG_FATAL_IF((fUsed + skip) > fReserved);
auto op = (T*)(fBytes.get() + fUsed);
fUsed += skip;
new (op) T{std::forward<Args>(args)...};
op->type = (uint32_t)T::kType;
op->skip = skip;
// check if this is a fill op or not, in case we need to avoid messing with it with force invert
if constexpr (!std::is_same_v<T, DrawTextBlob>) {
if (hasPaintWithFill(args...)) {
mHasFill = true;
}
}
return op + 1;
}
这里使用了C++的placement new方式,在fBytes的最后构建并添加了类型为T的结构,此时T是DrawRect:(frameworks/base/libs/hwui/RecordingCanvas.cpp)
struct Op {
uint32_t type : 8;
uint32_t skip : 24;
};
struct DrawRect final : Op {
static const auto kType = Type::DrawRect;
DrawRect(const SkRect& rect, const SkPaint& paint) : rect(rect), paint(paint) {}
SkRect rect;
SkPaint paint;
void draw(SkCanvas* c, const SkMatrix&) const { c->drawRect(rect, paint); }
};
DrawRect继承Op,并保存了这次操作所需的数据SkRect和SkPaint。这样就实现了绘制操作保存到了DisplayListData的fBytes中。
最后实际绘制之前录制的操作的时候,会调用DisplayListData的draw方法:(frameworks/base/libs/hwui/RecordingCanvas.cpp)
void DisplayListData::draw(SkCanvas* canvas) const {
SkAutoCanvasRestore acr(canvas, false);
this->map(draw_fns, canvas, canvas->getTotalMatrix());
}
调用了DisplayListData的map方法:(frameworks/base/libs/hwui/RecordingCanvas.cpp)
template <typename Fn, typename... Args>
inline void DisplayListData::map(const Fn fns[], Args... args) const {
auto end = fBytes.get() + fUsed;
for (const uint8_t* ptr = fBytes.get(); ptr < end;) {
auto op = (const Op*)ptr;
auto type = op->type;
auto skip = op->skip;
if (auto fn = fns[type]) { // We replace no-op functions with nullptrs
fn(op, args...); // to avoid the overhead of a pointless call.
}
ptr += skip;
}
}
这里遍历fBytes,对其中的每个op调用fn函数,fn函数是作为参数传进来的draw_fns的下标为type的元素。draw_fns定义:(frameworks/base/libs/hwui/RecordingCanvas.cpp)
// All ops implement draw().
#define X(T) \
[](const void* op, SkCanvas* c, const SkMatrix& original) { \
((const T*)op)->draw(c, original); \
},
static const draw_fn draw_fns[] = {
#include "DisplayListOps.in"
};
#undef X
其中DisplayListOps.in的内容:(frameworks/base/libs/hwui/DisplayListOps.in)
X(Save)
X(Restore)
X(SaveLayer)
X(SaveBehind)
X(Concat)
X(SetMatrix)
X(Scale)
X(Translate)
X(ClipPath)
X(ClipRect)
X(ClipRRect)
X(ClipRegion)
X(ClipShader)
X(ResetClip)
X(DrawPaint)
X(DrawBehind)
X(DrawPath)
X(DrawRect)
X(DrawRegion)
X(DrawOval)
X(DrawArc)
X(DrawRRect)
X(DrawDRRect)
X(DrawAnnotation)
X(DrawDrawable)
X(DrawPicture)
X(DrawImage)
X(DrawImageRect)
X(DrawImageLattice)
X(DrawTextBlob)
X(DrawPatch)
X(DrawPoints)
X(DrawVertices)
X(DrawAtlas)
X(DrawShadowRec)
X(DrawVectorDrawable)
X(DrawRippleDrawable)
X(DrawWebView)
X(DrawSkMesh)
X(DrawMesh)
这个X是写在draw_fns之前的宏定义,实际上就是一个lambda函数,函数里面调用了每种Op的draw方法。就这样实现了将录制好了的操作绘制到传进来的SkCanvas里面。
skia
SkCanvas
external/skia/include/core/SkCanvas.h
/** \class SkCanvas
SkCanvas provides an interface for drawing, and how the drawing is clipped and transformed.
SkCanvas contains a stack of SkMatrix and clip values.
SkCanvas and SkPaint together provide the state to draw into SkSurface or SkDevice.
Each SkCanvas draw call transforms the geometry of the object by the concatenation of all
SkMatrix values in the stack. The transformed geometry is clipped by the intersection
of all of clip values in the stack. The SkCanvas draw calls use SkPaint to supply drawing
state such as color, SkTypeface, text size, stroke width, SkShader and so on.
To draw to a pixel-based destination, create raster surface or GPU surface.
Request SkCanvas from SkSurface to obtain the interface to draw.
SkCanvas generated by raster surface draws to memory visible to the CPU.
SkCanvas generated by GPU surface uses Vulkan or OpenGL to draw to the GPU.
To draw to a document, obtain SkCanvas from SVG canvas, document PDF, or SkPictureRecorder.
SkDocument based SkCanvas and other SkCanvas subclasses reference SkDevice describing the
destination.
SkCanvas can be constructed to draw to SkBitmap without first creating raster surface.
This approach may be deprecated in the future.
*/
class SK_API SkCanvas {
public:
...
/** Draws SkRect rect using clip, SkMatrix, and SkPaint paint.
In paint: SkPaint::Style determines if rectangle is stroked or filled;
if stroked, SkPaint stroke width describes the line thickness, and
SkPaint::Join draws the corners rounded or square.
@param rect rectangle to draw
@param paint stroke or fill, blend, color, and so on, used to draw
example: https://fiddle.skia.org/c/@Canvas_drawRect
*/
void drawRect(const SkRect& rect, const SkPaint& paint);
...
protected:
...
// NOTE: If you are adding a new onDraw virtual to SkCanvas, PLEASE add an override to
// SkCanvasVirtualEnforcer (in SkCanvasVirtualEnforcer.h). This ensures that subclasses using
// that mechanism will be required to implement the new function.
virtual void onDrawRect(const SkRect& rect, const SkPaint& paint);
...
};
SkCanvas有很多draw方法,例如SkCanvas的drawRect方法:(external/skia/src/core/SkCanvas.cpp)
void SkCanvas::drawRect(const SkRect& r, const SkPaint& paint) {
TRACE_EVENT0("skia", TRACE_FUNC);
// To avoid redundant logic in our culling code and various backends, we always sort rects
// before passing them along.
this->onDrawRect(r.makeSorted(), paint);
}
直接调用了SkCanvas的onDrawRect方法:(external/skia/src/core/SkCanvas.cpp)
void SkCanvas::onDrawRect(const SkRect& r, const SkPaint& paint) {
SkASSERT(r.isSorted());
if (this->internalQuickReject(r, paint)) {
return;
}
auto layer = this->aboutToDraw(paint, &r, PredrawFlags::kCheckForOverwrite);
if (layer) {
this->topDevice()->drawRect(r, layer->paint());
}
}
调用了SkDevice的drawRect方法,drawRect方法的内容取决于继承SkDevice的类的具体实现,将在其它文章中分析。
SkCanvasVirtualEnforcer
external/skia/include/core/SkCanvasVirtualEnforcer.h
// If you would ordinarily want to inherit from Base (eg SkCanvas, SkNWayCanvas), instead
// inherit from SkCanvasVirtualEnforcer<Base>, which will make the build fail if you forget
// to override one of SkCanvas' key virtual hooks.
template <typename Base>
class SkCanvasVirtualEnforcer : public Base {
public:
using Base::Base;
protected:
void onDrawPaint(const SkPaint& paint) override = 0;
void onDrawBehind(const SkPaint&) override {} // make zero after android updates
void onDrawRect(const SkRect& rect, const SkPaint& paint) override = 0;
void onDrawRRect(const SkRRect& rrect, const SkPaint& paint) override = 0;
void onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
const SkPaint& paint) override = 0;
void onDrawOval(const SkRect& rect, const SkPaint& paint) override = 0;
void onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle, bool useCenter,
const SkPaint& paint) override = 0;
void onDrawPath(const SkPath& path, const SkPaint& paint) override = 0;
void onDrawRegion(const SkRegion& region, const SkPaint& paint) override = 0;
void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
const SkPaint& paint) override = 0;
void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
const SkPoint texCoords[4], SkBlendMode mode,
const SkPaint& paint) override = 0;
void onDrawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint pts[],
const SkPaint& paint) override = 0;
#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
// This is under active development for Chrome and not used in Android. Hold off on adding
// implementations in Android's SkCanvas subclasses until this stabilizes.
void onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
SkCanvas::QuadAAFlags aaFlags, const SkColor4f& color, SkBlendMode mode) override {}
#else
void onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
SkCanvas::QuadAAFlags aaFlags, const SkColor4f& color, SkBlendMode mode) override = 0;
#endif
void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) override = 0;
void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override = 0;
void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override = 0;
void onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
const SkPaint* paint) override = 0;
};
SkCanvasVirtualEnforcer是一个抽象基类,将所有关键的onDraw方法都重写成抽象的了,作用是如果继承类少实现了某个方法就会导致编译不通过,从而确保继承类不会漏实现某个onDraw方法。
SkNoDrawCanvas
external/skia/include/utils/SkNoDrawCanvas.h
// SkNoDrawCanvas is a helper for SkCanvas subclasses which do not need to
// actually rasterize (e.g., analysis of the draw calls).
//
// It provides the following simplifications:
//
// * not backed by any device/pixels
// * conservative clipping (clipping calls only use rectangles)
//
class SK_API SkNoDrawCanvas : public SkCanvasVirtualEnforcer<SkCanvas> {
public:
SkNoDrawCanvas(int width, int height);
SkNoDrawCanvas(const SkIRect&);
// Optimization to reset state to be the same as after construction.
void resetCanvas(int w, int h) { this->resetForNextPicture(SkIRect::MakeWH(w, h)); }
void resetCanvas(const SkIRect& rect) { this->resetForNextPicture(rect); }
protected:
SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec& rec) override;
bool onDoSaveBehind(const SkRect*) override;
// No-op overrides for aborting rasterization earlier than SkNullBlitter.
void onDrawAnnotation(const SkRect&, const char[], SkData*) override {}
void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override {}
void onDrawDrawable(SkDrawable*, const SkMatrix*) override {}
void onDrawTextBlob(const SkTextBlob*, SkScalar, SkScalar, const SkPaint&) override {}
void onDrawPatch(const SkPoint[12], const SkColor[4], const SkPoint[4], SkBlendMode,
const SkPaint&) override {}
void onDrawPaint(const SkPaint&) override {}
void onDrawBehind(const SkPaint&) override {}
void onDrawPoints(PointMode, size_t, const SkPoint[], const SkPaint&) override {}
void onDrawRect(const SkRect&, const SkPaint&) override {}
void onDrawRegion(const SkRegion&, const SkPaint&) override {}
void onDrawOval(const SkRect&, const SkPaint&) override {}
void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override {}
void onDrawRRect(const SkRRect&, const SkPaint&) override {}
void onDrawPath(const SkPath&, const SkPaint&) override {}
void onDrawImage2(const SkImage*, SkScalar, SkScalar, const SkSamplingOptions&,
const SkPaint*) override {}
void onDrawImageRect2(const SkImage*, const SkRect&, const SkRect&, const SkSamplingOptions&,
const SkPaint*, SrcRectConstraint) override {}
void onDrawImageLattice2(const SkImage*, const Lattice&, const SkRect&, SkFilterMode,
const SkPaint*) override {}
void onDrawAtlas2(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int,
SkBlendMode, const SkSamplingOptions&, const SkRect*, const SkPaint*) override {}
void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override {}
void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override {}
void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override {}
void onDrawEdgeAAQuad(const SkRect&, const SkPoint[4], QuadAAFlags, const SkColor4f&,
SkBlendMode) override {}
void onDrawEdgeAAImageSet2(const ImageSetEntry[], int, const SkPoint[], const SkMatrix[],
const SkSamplingOptions&, const SkPaint*,
SrcRectConstraint) override {}
private:
using INHERITED = SkCanvasVirtualEnforcer<SkCanvas>;
};
SkNoDrawCanvas继承了SkCanvasVirtualEnforcer<SkCanvas>,重写了所有onDraw方法为空方法,也就是不执行任何具体的绘制代码。
RecordingCanvas就是继承了SkCanvasVirtualEnforcer<SkNoDrawCanvas>。