在上一篇关于帧绘制的原理中,做好了EGLSuface切换,同步好了UI的更新,为需要进行GPU绘制的RenderNode创好了SKSurface,最后通过ANativeWindow为下一帧调用了dequeueBuffer。所有的资源和数据都准备好了,从而可以进行绘制,这个任务将由RenderPipeline来完成。我们先不考虑Fence的逻辑,直接接着上一篇文章,从CanvasContext.draw方法出发。
1 CanvasContext.draw
frameworks/base/libs/hwui/renderthread/CanvasContext.cpp
nsecs_t CanvasContext::draw() {
...
Frame frame = mRenderPipeline->getFrame();
SkRect windowDirty = computeDirtyRect(frame, &dirty);
bool drew = mRenderPipeline->draw(frame, windowDirty, dirty, mLightGeometry, &mLayerUpdateQueue,
mContentDrawBounds, mOpaque, mLightInfo, mRenderNodes,
&(profiler()));
...
bool didSwap = mRenderPipeline->swapBuffers(frame, drew, windowDirty, mCurrentFrameInfo, &requireSwap);
...
for (auto& func : mFrameCompleteCallbacks) {
std::invoke(func, frameCompleteNr);
}
mFrameCompleteCallbacks.clear();
}
...
cleanupResources();
mRenderThread.cacheManager().onFrameCompleted();
return mCurrentFrameInfo->get(FrameInfoIndex::DequeueBufferDuration);
}
这个draw方法比较复杂,这里摘取主要逻辑来分析一下。可以看到具体的绘制是继续委托到mRenderPipeline去完成的,仅仅分析一下使用OpenGL绘制的情况,因此对应的SkiaOpenGLPipeline。这里主要由这个几个步骤
- 创建Frame对象
- 调用mRenderPipeline->draw进行绘制
- mRenderPipeline->swapBuffers 切换GragphicBuffer
- 回调FrameCompleteCalback。
下面就按这个流程来分析
2 mRenderPipeline->getFrame
Frame是一个帧的模型
class Frame {
public:
Frame(int32_t width, int32_t height, int32_t bufferAge)
: mWidth(width), mHeight(height), mBufferAge(bufferAge) {}
int32_t width() const { return mWidth; }
int32_t height() const { return mHeight; }
int32_t bufferAge() const { return mBufferAge; }
private:
Frame() {}
friend class EglManager;
int32_t mWidth;
int32_t mHeight;
int32_t mBufferAge;
EGLSurface mSurface;
// Maps from 0,0 in top-left to 0,0 in bottom-left
// If out is not an int32_t[4] you're going to have a bad time
void map(const SkRect& in, int32_t* out) const;
};
它封装的是一个EGLSurface, 前面分析过,EGLSurface关联着一个ANativeWindow, 也就是一个Surface对象,所以Frame可以代表一个Surface对象。
frameworks/base/libs/hwui/pipeline/skia/SkiaOpenGLPipeline.cpp
Frame SkiaOpenGLPipeline::getFrame() {
LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
"drawRenderNode called on a context with no surface!");
return mEglManager.beginFrame(mEglSurface);
}
进入到mEglManager的beginFrame方法
Frame EglManager::beginFrame(EGLSurface surface) {
LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, "Tried to beginFrame on EGL_NO_SURFACE!");
makeCurrent(surface);
Frame frame;
frame.mSurface = surface;
eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
frame.mBufferAge = queryBufferAge(surface);
eglBeginFrame(mEglDisplay, surface);
return frame;
}
将这个surface切换到当前后,新创建一个Frame对象,并将surface赋给这个frame对象,后设在对象的长宽属性等,然后返回这个新的Frame对象。
3 mRenderPipeline->draw
使用OpenGL来绘制的时候,mRenderPipeline是一个SkiaOpenGLPipeline对象,它是SkiaPipeline的子类,我们来分析一下它的draw方法
frameworks/base/libs/hwui/pipeline/skia/SkiaOpenGLPipeline.cpp
bool SkiaOpenGLPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
const LightGeometry& lightGeometry,
LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds,
bool opaque, const LightInfo& lightInfo,
const std::vector<sp<RenderNode>>& renderNodes,
FrameInfoVisualizer* profiler) {
...
GrGLFramebufferInfo fboInfo;
fboInfo.fFBOID = 0;
....
GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE, fboInfo);
...
SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(
mRenderThread.getGrContext(), backendRT, this->getSurfaceOrigin(), colorType,
mSurfaceColorSpace, &props));
...
renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
SkMatrix::I());
...
{
ATRACE_NAME("flush commands");
surface->flushAndSubmit();
}
layerUpdateQueue->clear();
return true;
}
这里首先生成了一个GrGLFramebufferInfo和GrBackendRenderTarget,它是属于skia库里的api,但是只包含一些j简单的属性信息。
external/skia/include/gpu/gl/GrGLTypes.h
struct GrGLFramebufferInfo {
GrGLuint fFBOID;
GrGLenum fFormat = 0;
bool operator==(const GrGLFramebufferInfo& that) const {
return fFBOID == that.fFBOID && fFormat == that.fFormat;
}
};
external/skia/src/gpu/GrBackendSurface.cpp
GrBackendRenderTarget::GrBackendRenderTarget(int width,
int height,
int sampleCnt,
int stencilBits,
const GrGLFramebufferInfo& glInfo)
: fWidth(width)
, fHeight(height)
, fSampleCnt(std::max(1, sampleCnt))
, fStencilBits(stencilBits)
, fBackend(GrBackendApi::kOpenGL)
, fGLInfo(glInfo) {
fIsValid = SkToBool(glInfo.fFormat); // the glInfo must have a valid format
}
随后调用SkSurface::MakeFromBackendRenderTarget生成一个SkSurface,这是Skia在GPU上申请用于绘制的surface。我们来看看这个sksurface 的生成流程。
sk_sp<SkSurface> SkSurface::MakeFromBackendRenderTarget(GrRecordingContext* context,
const GrBackendRenderTarget& rt,
GrSurfaceOrigin origin,
SkColorType colorType,
sk_sp<SkColorSpace> colorSpace,
const SkSurfaceProps* props,
SkSurface::RenderTargetReleaseProc relProc,
SkSurface::ReleaseContext releaseContext) {
...
auto sdc = GrSurfaceDrawContext::MakeFromBackendRenderTarget(context,
grColorType,
std::move(colorSpace),
rt,
origin,
SkSurfacePropsCopyOrDefault(props),
std::move(releaseHelper));
if (!sdc) {
return nullptr;
}
auto device = SkGpuDevice::Make(std::move(sdc), SkGpuDevice::kUninit_InitContents);
if (!device) {
return nullptr;
}
return sk_make_sp<SkSurface_Gpu>(std::move(device));
}
首先生成一个GrSurfaceDrawContext的对象sdc,由它来持有上面生成的GrBackendRenderTarget和GrRecordingContext,
sk_sp<SkGpuDevice> SkGpuDevice::Make(std::unique_ptr<GrSurfaceDrawContext> surfaceDrawContext,
InitContents init) {
if (!surfaceDrawContext) {
return nullptr;
}
GrRecordingContext* rContext = surfaceDrawContext->recordingContext();
if (rContext->abandoned()) {
return nullptr;
}
SkColorType ct = GrColorTypeToSkColorType(surfaceDrawContext->colorInfo().colorType());
unsigned flags;
if (!rContext->colorTypeSupportedAsSurface(ct) ||
!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
return nullptr;
}
return sk_sp<SkGpuDevice>(new SkGpuDevice(std::move(surfaceDrawContext), flags));
}
SkGpuDevice::SkGpuDevice(std::unique_ptr<GrSurfaceDrawContext> surfaceDrawContext, unsigned flags)
: INHERITED(make_info(surfaceDrawContext.get(), SkToBool(flags & kIsOpaque_Flag)),
surfaceDrawContext->surfaceProps())
, fContext(sk_ref_sp(surfaceDrawContext->recordingContext()))
, fSurfaceDrawContext(std::move(surfaceDrawContext))
#if !defined(SK_DISABLE_NEW_GR_CLIP_STACK)
, fClip(SkIRect::MakeSize(fSurfaceDrawContext->dimensions()),
&this->asMatrixProvider(),
force_aa_clip(fSurfaceDrawContext.get())) {
#else
, fClip(fSurfaceDrawContext->dimensions(), &this->cs(), &this->asMatrixProvider()) {
#endif
if (flags & kNeedClear_Flag) {
this->clearAll();
}
}
这里创将了一个SkGpuDevice对象,它的成员变量 fContext 是通过外部传入的 surfaceDrawContext 调用 recordingContext 方法的得来的,而这个surfaceDrawContext就是上面的 sdc 局部变量,它的 recordingContext 实质上来自 mRenderThread.getGrContext() 方法。因此 SkGpuDevice的fContext指向的是mRenderThread.getGrContext()返回的对象 。
之后此构建了一个SkSurface_Gpu对象,它是SkSurface的子类,因为传入的是SkGpuDevice,所以绘制命令将通过它提交到GPU进行像素渲染。准备好了SkSurface之后,调用renderFrame在该SkSurface上绘制。最后调用 surface->flushAndSubmit();提交到GPU。这里的内容比较多,在后面的文章中再展开。
4 总结
本文分析了帧绘制的流程,这个抽象成了一个RenderPipeline,根据使用不同渲染引擎,提供了SkiaOpenGLPipeline和SkiaVulkanPipeline两个实现,本文仅仅分析SkiaOpenGLPipeline,它的绘制总共被分成了3个步骤:
- 创建SkSurface
- renderFrame 将记录的描述数据记录的SkSurface
- flushAndSubmit 提交绘制命令到GPU进行像素渲染
在更大的视角上看,绘制的步骤包含:
- 绘制前准备Frame模型
- SkiaOpenGLPipeline 调用GPU绘制
- mRenderPipeline->swapBuffers 通知HWComposer进行屏幕合成
- 回调FrameCompleteCalback结尾
标签:surfaceDrawContext,return,frame,RenderPipeline,surface,const,Android,Frame,View From: https://blog.51cto.com/u_16175630/7047159