前言
RenderEngine是一個靜態(tài)庫磨德,主要作用于SurfaceFlinger進行client layer合成汁雷。
簡單理解就是將client buffer layer合成到FramebufferSurface
frameworks\native\libs\renderengine\
一跳座、RenderEngineType
RenderEngineType是指RenderEngine的合成方式實現(xiàn)類型杯巨,目前有四種類型實現(xiàn)如下。
enum class RenderEngineType {
GLES = 1,//opengl
THREADED = 2,//opengl異步線程
SKIA_GL = 3,//skia
SKIA_GL_THREADED = 4,//skia異步線程
};
同步 | 異步 | |
---|---|---|
skia | SKIA_GL | SKIA_GL_THREADED |
opengl | GLES | THREADED |
異步的意思會另起一個名叫RenderEngine的線程中處理合成任務练般。
同步的意思是會在SurfaceFlinger的主線程中處理合成任務
如圖:異步處理合成任務的Trace。
二锈候、RenderEngine初始化流程
基于最新的代碼安卓代碼
1.1 SurfaceFlinger.cpp
SurfaceFlinger init的時候會構建RenderEngine薄料,并且確認RenderEngineType
frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
void SurfaceFlinger::init() {
...
// Get a RenderEngine for the given display / config (can't fail)
// TODO(b/77156734): We need to stop casting and use HAL types when possible.
// Sending maxFrameBufferAcquiredBuffers as the cache size is tightly tuned to single-display.
mCompositionEngine->setRenderEngine(renderengine::RenderEngine::create(
renderengine::RenderEngineCreationArgs::Builder()
.setPixelFormat(static_cast<int32_t>(defaultCompositionPixelFormat))
.setImageCacheSize(maxFrameBufferAcquiredBuffers)
.setUseColorManagerment(useColorManagement)
.setEnableProtectedContext(enable_protected_contents(false))
.setPrecacheToneMapperShaderOnly(false)
.setSupportsBackgroundBlur(mSupportsBlur)
.setContextPriority(
useContextPriority
? renderengine::RenderEngine::ContextPriority::REALTIME
: renderengine::RenderEngine::ContextPriority::MEDIUM)
.build()));//跳轉到1.2
mMaxRenderTargetSize =
std::min(getRenderEngine().getMaxTextureSize(), getRenderEngine().getMaxViewportDims());
....
}
1.2 RenderEngine
提供了setRenderEngineType的接口,默認缺省值是SKIA_GL_THREADED
frameworks/native/libs/renderengine/include/renderengine/RenderEngine.h
struct RenderEngineCreationArgs::Builder {
...
Builder& setRenderEngineType(RenderEngine::RenderEngineType renderEngineType) {
this->renderEngineType = renderEngineType;
return *this;
}
...
private:
// 1 means RGBA_8888
int pixelFormat = 1;
uint32_t imageCacheSize = 0;
bool useColorManagement = true;
bool enableProtectedContext = false;
bool precacheToneMapperShaderOnly = false;
bool supportsBackgroundBlur = false;
RenderEngine::ContextPriority contextPriority = RenderEngine::ContextPriority::MEDIUM;
RenderEngine::RenderEngineType renderEngineType =
RenderEngine::RenderEngineType::SKIA_GL_THREADED;//缺省值
};
} // namespace renderengine
可以通過設置"debug.renderengine.backend"泵琳,改變RenderEngineType摄职。
配置項:gles,threaded, skiagl,skiaglthread 分別對應四種類型
frameworks/native/libs/renderengine/RenderEngine.cpp
#define PROPERTY_DEBUG_RENDERENGINE_BACKEND "debug.renderengine.backend"
std::unique_ptr<RenderEngine> RenderEngine::create(RenderEngineCreationArgs args) {
// Keep the ability to override by PROPERTIES:
char prop[PROPERTY_VALUE_MAX];
property_get(PROPERTY_DEBUG_RENDERENGINE_BACKEND, prop, "");
if (strcmp(prop, "gles") == 0) {
args.renderEngineType = RenderEngineType::GLES;
}
if (strcmp(prop, "threaded") == 0) {
args.renderEngineType = RenderEngineType::THREADED;
}
if (strcmp(prop, "skiagl") == 0) {
args.renderEngineType = RenderEngineType::SKIA_GL;
}
if (strcmp(prop, "skiaglthreaded") == 0) {
args.renderEngineType = RenderEngineType::SKIA_GL_THREADED;
}
switch (args.renderEngineType) {
case RenderEngineType::THREADED:
ALOGD("Threaded RenderEngine with GLES Backend");
return renderengine::threaded::RenderEngineThreaded::create(
[args]() { return android::renderengine::gl::GLESRenderEngine::create(args); },
args.renderEngineType);
case RenderEngineType::SKIA_GL:
ALOGD("RenderEngine with SkiaGL Backend");
return renderengine::skia::SkiaGLRenderEngine::create(args);
case RenderEngineType::SKIA_GL_THREADED: {
ALOGD("Threaded RenderEngine with SkiaGL Backend");
return renderengine::threaded::RenderEngineThreaded::create(
[args]() {
return android::renderengine::skia::SkiaGLRenderEngine::create(args);
},
args.renderEngineType);
}
case RenderEngineType::GLES:
default:
ALOGD("RenderEngine with GLES Backend");
return renderengine::gl::GLESRenderEngine::create(args);
}
}
三誊役、小結
android S以后原生配置的是SKIA_GL_THREADED,目前窗口的blurs效果谷市,只在skia實現(xiàn)上
opengl模式不支持blurs蛔垢。
Some new features (e.g. cross window blurs) are only implemented in Skia
RenderEngine. Turn it on for all Pixel devices. Other devices can still
turn it off with PROPERTY_DEBUG_RENDERENGINE_BACKEND.
細心你可能會發(fā)現(xiàn)前面SKIA_GL_THREADED的drawlayers中并沒有發(fā)揮異步的作用。本質上還是會Block SF的主線程迫悠。
目前我只發(fā)現(xiàn)REThreaded::unmapExternalTextureBuffer這個方法是異步處理了鹏漆,相比SKIA_GL,有一定程度上的性能提升创泄。
但是據(jù)說可能一些極限情況的下并發(fā)讀寫的問題艺玲,我相信后續(xù)android會不斷的完善SKIA_GL_THREADED,后續(xù)可能就漸漸放棄GLES了鞠抑。