DisplayEventReceiver
的创建SurfaceFlinger
注册回调接口SurfaceFlinger
的传递SurfaceFlinger
分发事件到应用DecorView
重绘ThreadedRenderer.updateRootDisplayList()
DecorView
子对象重绘ColorDrawable
重绘子View
背景Canvas
提交绘制请求
ThreadedRenderer.syncAndDrawFrame()
执行绘制操作SkiaPipeline::renderFrame()
对界面执行渲染SkSurface::flushAndSubmit()
提交绘制请求swapBuffer()
交换窗口至SurfaceFlinger
SurfaceFlinger
合成窗口Composer
DisplayEventReceiver
的创建应用启动时通过ActivityThread.H
接收Framewroks的消息, 其handleMessage()
中:
// frameworks/base/core/java/android/app/ActivityThread.java
public final class ActivityThread extends ClientTransactionHandler
implements ActivityThreadInternal {
final H mH = new H();
... ...
class H extends Handler {
public static final int BIND_APPLICATION = 110;
public void handleMessage(Message msg) {
if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
switch (msg.what) {
... ...
case EXECUTE_TRANSACTION:
final ClientTransaction transaction = (ClientTransaction) msg.obj;
mTransactionExecutor.execute(transaction);
... ...
break;
mTransactionExecutor
的类型是TransactionExecutor
, 因此:
// frameworks/base/core/java/android/app/servertransaction/TransactionExecutor.java
public class TransactionExecutor {
... ...
public void execute(ClientTransaction transaction) {
if (DEBUG_RESOLVER) Slog.d(TAG, tId(transaction) + "Start resolving transaction");
... ...
executeLifecycleState(transaction);
mPendingActions.clear();
if (DEBUG_RESOLVER) Slog.d(TAG, tId(transaction) + "End resolving transaction");
}
... ...
private void executeLifecycleState(ClientTransaction transaction) {
... ...
lifecycleItem.execute(mTransactionHandler, token, mPendingActions);
lifecycleItem.postExecute(mTransactionHandler, token, mPendingActions);
}
生命周期对象lifecycleItem
的类型是ActivityTransactionItem
, 因此:
// frameworks/base/core/java/android/app/servertransaction/ActivityTransactionItem.java
public abstract class ActivityTransactionItem extends ClientTransactionItem {
@Override
public final void execute(ClientTransactionHandler client, IBinder token,
PendingTransactionActions pendingActions) {
final ActivityClientRecord r = getActivityClientRecord(client, token);
execute(client, r, pendingActions);
}
而此处的ActivityTransactionItem
其实有子类的实现ResumeActivityItem
, 因此:
// frameworks/base/core/java/android/app/servertransaction/ResumeActivityItem.java
public class ResumeActivityItem extends ActivityLifecycleItem {
... ...
@Override
public void execute(ClientTransactionHandler client, ActivityClientRecord r,
PendingTransactionActions pendingActions) {
client.handleResumeActivity(r, true /* finalStateRequest */, mIsForward,
"RESUME_ACTIVITY");
}
此处的client
类型为ClientTransactionHandler
, 其实现又是上文的ActivityThread
:
// frameworks/base/core/java/android/app/ActivityThread.java
@Override
public void handleResumeActivity(ActivityClientRecord r, boolean finalStateRequest,
boolean isForward, String reason) {
if (r.window == null && !a.mFinished && willBeVisible) {
if (a.mVisibleFromClient) {
if (!a.mWindowAdded) {
a.mWindowAdded = true;
wm.addView(decor, l);
... ...
wm
的类型是WindowManagerImpl
, decor
的类型是DecorView
, 因此:
// frameworks/base/core/java/android/view/WindowManagerImpl.java
public final class WindowManagerImpl implements WindowManager {
@Override
public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
applyTokens(params);
mGlobal.addView(view, params, mContext.getDisplayNoVerify(), mParentWindow,
mContext.getUserId());
}
mGlobal
的类型是WindowManagerGlobal
, 是单例的, 因此:
// frameworks/base/core/java/android/view/WindowManagerGlobal.java
public void addView(View view, ViewGroup.LayoutParams params,
Display display, Window parentWindow, int userId) {
... ...
synchronized (mLock) {
... ...
root = new ViewRootImpl(view.getContext(), display);
view.setLayoutParams(wparams);
mViews.add(view);
mRoots.add(root);
mParams.add(wparams);
try {
root.setView(view, wparams, panelParentView, userId);
}
... ...
可以看出ViewRootImpl
被创建, DecoView
通过ViewRootImpl.setView()
方法设置给ViewRootImpl
, 对于ViewRootImpl
, 其构造方法:
// frameworks/base/core/java/android/view/ViewRootImpl.java
public ViewRootImpl(Context context, Display display) {
this(context, display, WindowManagerGlobal.getWindowSession(),
false /* useSfChoreographer */);
}
... ...
public ViewRootImpl(@UiContext Context context, Display display, IWindowSession session,
boolean useSfChoreographer) {
... ...
mChoreographer = useSfChoreographer
? Choreographer.getSfInstance() : Choreographer.getInstance();
ViewRootImpl
在初始化mChoreographer
成员时, 第一次调用Choreographer.getSfInstance()
对Choreographer
类进行例化:
// frameworks/base/core/java/android/view/Choreographer.java
public static Choreographer getInstance() {
return sThreadInstance.get();
}
sThreadInstance
的类型是ThreadLocal
其实现:
// libcore/ojluni/src/main/java/java/lang/ThreadLocal.java
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
return setInitialValue();
}
private T setInitialValue() {
T value = initialValue();
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
return value;
}
显然ThreadLocal.getMap(t)
返回空, 因为还没有Choreographer
, 因此调用了setInitialValue()
进而调用回Choreographer
中的:
// frameworks/base/core/java/android/view/Choreographer.java
private static final ThreadLocal sThreadInstance =
new ThreadLocal() {
@Override
protected Choreographer initialValue() {
Looper looper = Looper.myLooper();
... ...
Choreographer choreographer = new Choreographer(looper, VSYNC_SOURCE_APP);
... ...
return choreographer;
}
};
private Choreographer(Looper looper, int vsyncSource) {
mLooper = looper;
mHandler = new FrameHandler(looper);
mDisplayEventReceiver = USE_VSYNC
? new FrameDisplayEventReceiver(looper, vsyncSource)
: null;
... ...
}
... ...
private final class FrameDisplayEventReceiver extends DisplayEventReceiver
implements Runnable {
private boolean mHavePendingVsync;
private long mTimestampNanos;
private int mFrame;
private VsyncEventData mLastVsyncEventData = new VsyncEventData();
public FrameDisplayEventReceiver(Looper looper, int vsyncSource) {
super(looper, vsyncSource, 0);
}
此刻Choreographer
类开始构造, 初始化成员mDisplayEventReceiver
, 其类型FrameDisplayEventReceiver
, 构造FrameDisplayEventReceiver
时构造其父类:
// frameworks/base/core/java/android/view/DisplayEventReceiver.java
private static native long nativeInit(WeakReference receiver,
MessageQueue messageQueue, int vsyncSource, int eventRegistration);
public DisplayEventReceiver(Looper looper, int vsyncSource, int eventRegistration) {
if (looper == null) {
throw new IllegalArgumentException("looper must not be null");
}
mMessageQueue = looper.getQueue();
mReceiverPtr = nativeInit(new WeakReference(this), mMessageQueue,
vsyncSource, eventRegistration);
}
nativeInit()
是Native方法, 其代码:
// frameworks/base/core/jni/android_view_DisplayEventReceiver.cpp
static const JNINativeMethod gMethods[] = {
/* name, signature, funcPtr */
{ "nativeInit",
"(Ljava/lang/ref/WeakReference;Landroid/os/MessageQueue;II)J",
(void*)nativeInit },
... ...
};
static jlong nativeInit(JNIEnv* env, jclass clazz, jobject receiverWeak, jobject messageQueueObj,
jint vsyncSource, jint eventRegistration) {
sp messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
... ...
sp receiver =
new NativeDisplayEventReceiver(env, receiverWeak, messageQueue, vsyncSource,
eventRegistration);
status_t status = receiver->initialize();
... ...
return reinterpret_cast(receiver.get());
}
NativeDisplayEventReceiver::NativeDisplayEventReceiver(JNIEnv* env, jobject receiverWeak,
const sp& messageQueue,
jint vsyncSource, jint eventRegistration)
: DisplayEventDispatcher(messageQueue->getLooper(),
static_cast(vsyncSource),
static_cast(eventRegistration)),
mReceiverWeakGlobal(env->NewGlobalRef(receiverWeak)),
mMessageQueue(messageQueue) {
ALOGV("receiver %p ~ Initializing display event receiver.", this);
}
分两步, 一来是NativeDisplayEventReceiver
的构造, 二来是NativeDisplayEventReceiver
父类的DisplayEventDispatcher::initialize()
.
SurfaceFlinger
注册回调接口上文构造NativeDisplayEventReceiver
的同时构造了DisplayEventDispatcher
:
// frameworks/native/libs/gui/DisplayEventReceiver.cpp
DisplayEventReceiver::DisplayEventReceiver(
ISurfaceComposer::VsyncSource vsyncSource,
ISurfaceComposer::EventRegistrationFlags eventRegistration) {
sp sf(ComposerService::getComposerService());
if (sf != nullptr) {
mEventConnection = sf->createDisplayEventConnection(vsyncSource, eventRegistration);
if (mEventConnection != nullptr) {
mDataChannel = std::make_unique();
mEventConnection->stealReceiveChannel(mDataChannel.get());
}
}
}
通过Binder调用到SurfaceFlinger::createDisplayEventConnection()
:
// frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
sp SurfaceFlinger::createDisplayEventConnection(
ISurfaceComposer::VsyncSource vsyncSource,
ISurfaceComposer::EventRegistrationFlags eventRegistration) {
... ...
return mScheduler->createDisplayEventConnection(handle, eventRegistration);
}
显然mScheduler
是Scheduler
, 进一步调用``
// frameworks/native/services/surfaceflinger/Scheduler/Scheduler.cpp
sp Scheduler::createDisplayEventConnection(
ConnectionHandle handle, ISurfaceComposer::EventRegistrationFlags eventRegistration) {
... ...
return createConnectionInternal(mConnections[handle].thread.get(), eventRegistration);
}
sp Scheduler::createConnectionInternal(
EventThread* eventThread, ISurfaceComposer::EventRegistrationFlags eventRegistration) {
return eventThread->createEventConnection([&] { resync(); }, eventRegistration);
}
显然, 继续调用EventThread::createEventConnection()
:
// frameworks/native/services/surfaceflinger/Scheduler/EventThread.cpp
sp EventThread::createEventConnection(
ResyncCallback resyncCallback,
ISurfaceComposer::EventRegistrationFlags eventRegistration) const {
return new EventThreadConnection(const_cast(this),
IPCThreadState::self()->getCallingUid(),
std::move(resyncCallback), eventRegistration);
}
SurfaceFlinger
创建EventThreadConnection
并作为IDisplayEventConnection
返回给应用, 然后回到函数DisplayEventReceiver::DisplayEventReceiver()
:
// frameworks/native/libs/gui/DisplayEventReceiver.cpp
DisplayEventReceiver::DisplayEventReceiver(
ISurfaceComposer::VsyncSource vsyncSource,
ISurfaceComposer::EventRegistrationFlags eventRegistration) {
sp sf(ComposerService::getComposerService());
if (sf != nullptr) {
mEventConnection = sf->createDisplayEventConnection(vsyncSource, eventRegistration);
if (mEventConnection != nullptr) {
mDataChannel = std::make_unique();
mEventConnection->stealReceiveChannel(mDataChannel.get());
}
}
}
应用拿到IDisplayEventConnection
并再次通过Binder调用到SurfaceFlinger
中的EventThreadConnection::stealReceiveChannel()
:
// frameworks/native/services/surfaceflinger/Scheduler/EventThread.cpp
status_t EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) {
outChannel->setReceiveFd(mChannel.moveReceiveFd());
outChannel->setSendFd(base::unique_fd(dup(mChannel.getSendFd())));
return NO_ERROR;
}
至此, 应用和SurfaceFlinger
之间已经通过BitTube
建立了连接, 也就是说后续SurfaceFlinger
有事件, 应用都会通过DisplayEventReceiver
来进行接收. 但是此时应用的BitTube
中的mFD
还未被应用监听, 回到上文的DisplayEventDispatcher::initialize()
方法:
// frameworks/native/libs/gui/DisplayEventDispatcher.cpp
status_t DisplayEventDispatcher::initialize() {
status_t result = mReceiver.initCheck();
... ...
if (mLooper != nullptr) {
int rc = mLooper->addFd(mReceiver.getFd(), 0, Looper::EVENT_INPUT, this, NULL);
... ...
自此应用都会通过DisplayEventReceiver
对DisplayEventReceiver::Event
时间进行接收.
SurfaceFlinger
的传递ComposerCallbackBridge
由SurfaceFlinger
创建并持有, 该类继承IComposerCallback
接口, 先看一下该类的注册:
// frameworks/native/services/surfaceflinger/DisplayHardware/HWComposer.cpp
void HWComposer::setCallback(HWC2::ComposerCallback* callback) {
... ...
mComposer->registerCallback(
sp::make(callback, mComposer->isVsyncPeriodSwitchSupported()));
}
mComposer
的实现是Composer
,所以:
// frameworks/native/services/surfaceflinger/DisplayHardware/ComposerHal.cpp
void Composer::registerCallback(const sp& callback)
{
android::hardware::setMinSchedulerPolicy(callback, SCHED_FIFO, 2);
auto ret = [&]() {
if (mClient_2_4) {
return mClient_2_4->registerCallback_2_4(callback);
}
return mClient->registerCallback(callback);
}();
... ...
}
那最后就是IComposerClient::registerCallback_2_4()
了. 所以Composer负责通过IComposerCallback
接口调用ComposerCallbackBridge
通知SurfaceFlinger
由VSYNC事件. 对应的ComposerCallbackBridge::onVsync()
被调用:
// frameworks/native/services/surfaceflinger/DisplayHardware/HWComposer.cpp
Return onVsync_2_4(hal::HWDisplayId display, int64_t timestamp,
hal::VsyncPeriodNanos vsyncPeriodNanos) override {
if (mVsyncSwitchingSupported) {
mCallback->onComposerHalVsync(display, timestamp, vsyncPeriodNanos);
} else {
ALOGW("Unexpected onVsync_2_4 callback on composer <= 2.3, ignoring.");
}
return Void();
}
这里的mCallback
类型为HW2::ComposerCallback
, 实际上是SurfaceFlinger
, 因为SurfaceFlinger
的定义:
// frameworks/native/services/surfaceflinger/SurfaceFlinger.h
class SurfaceFlinger : public BnSurfaceComposer,
public PriorityDumper,
private IBinder::DeathRecipient,
private HWC2::ComposerCallback,
private ISchedulerCallback {
public:
...
所以调用到SurfaceFlinger::onComposerHalVsync()
:
// frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
void SurfaceFlinger::onComposerHalVsync(hal::HWDisplayId hwcDisplayId, int64_t timestamp,
std::optional vsyncPeriod) {
... ...
mScheduler->addResyncSample(timestamp, vsyncPeriod, &periodFlushed);
if (periodFlushed) {
modulateVsync(&VsyncModulator::onRefreshRateChangeCompleted);
}
}
mScheduler
的类型是Scheduler
, 所以调用Scheduler::addResyncSample()
:
// frameworks/native/services/surfaceflinger/Scheduler/Scheduler.cpp
void Scheduler::addResyncSample(nsecs_t timestamp, std::optional hwcVsyncPeriod,
bool* periodFlushed) {
bool needsHwVsync = false;
*periodFlushed = false;
{ // Scope for the lock
std::lock_guard lock(mHWVsyncLock);
if (mPrimaryHWVsyncEnabled) {
needsHwVsync = mVsyncSchedule.controller->addHwVsyncTimestamp(timestamp, hwcVsyncPeriod,
periodFlushed);
}
}
... ...
}
Scheduer
会通过TimerKeeper
进一步调用到VSyncDispatch::timerCallback()
, VSyncDispatch
的实现是VSyncDispatchTimerQueue
, 因此最终调用到VSyncDispatchTimerQueue::timerCallback()
:
// frameworks/native/services/surfaceflinger/Scheduler/VSyncDispatchTimerQueue.cpp
void VSyncDispatchTimerQueue::timerCallback() {
... ...
for (auto const& invocation : invocations) {
invocation.callback->callback(invocation.vsyncTimestamp, invocation.wakeupTimestamp,
invocation.deadlineTimestamp);
}
}
invocation.callback
其实还是从mCallbacks
中统计过来的, mCallbacks
的类型是CallbackRepeater
, 因此调用到CallbackRepeater::callback
:
// frameworks/native/services/surfaceflinger/Scheduler/DispSyncSource.cpp
void callback(nsecs_t vsyncTime, nsecs_t wakeupTime, nsecs_t readyTime) {
... ...
mCallback(vsyncTime, wakeupTime, readyTime);
... ..
}
mCallback
为创建DispSyncSource
时通过std::bind()
设置的std::function
, 其绑定的方法为DispSyncSource::onVsyncCallback()
:
// frameworks/native/services/surfaceflinger/Scheduler/DispSyncSource.cpp
void DispSyncSource::onVsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime,
nsecs_t readyTime) {
... ...
if (callback != nullptr) {
callback->onVSyncEvent(targetWakeupTime, vsyncTime, readyTime);
}
}
callback
就是mCallback
类型为VSyncSource::Callback
, 其实现是EventThread
, 因此回调到EventThread::onVsyncEvent()
:
// frameworks/native/services/surfaceflinger/Scheduler/EventThread.cpp
mCondition.notify_all();
}
void EventThread::onVSyncEvent(nsecs_t timestamp, nsecs_t expectedVSyncTimestamp,
nsecs_t deadlineTimestamp) {
... ...
mPendingEvents.push_back(makeVSync(mVSyncState->displayId, timestamp, ++mVSyncState->count,
expectedVSyncTimestamp, deadlineTimestamp, vsyncId));
mCondition.notify_all();
}
mPendingEvents
记录了通过makeVSync
生成的Event
事件, 该事件在通过std::condition_variable
通知到EventThread::threadMain()
:
// frameworks/native/services/surfaceflinger/Scheduler/EventThread.cpp
void EventThread::threadMain(std::unique_lock& lock) {
... ...
... ...
if (!consumers.empty()) {
dispatchEvent(*event, consumers);
consumers.clear();
}
... ...
... ...
}
void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event,
const DisplayEventConsumers& consumers) {
for (const auto& consumer : consumers) {
... ...
switch (consumer->postEvent(copy)) {
... ...
}
}
}
consumers
实际上是从mDisplayEventConnections
成员中统计的事件接收方, 调用到EventThreadConnection::postEvent()
:
// frameworks/native/services/surfaceflinger/Scheduler/EventThread.cpp
status_t EventThreadConnection::postEvent(const DisplayEventReceiver::Event& event) {
... ...
auto size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1);
return toStatus(size);
}
// frameworks/native/libs/gui/DisplayEventReceiver.cpp
ssize_t DisplayEventReceiver::sendEvents(gui::BitTube* dataChannel,
Event const* events, size_t count)
{
return gui::BitTube::sendObjects(dataChannel, events, count);
}
上文我们说过, 应用会通过BitTube
接受来自SurfaceFlinger
的通知, 揪下来再看应用一侧的处理.
SurfaceFlinger
分发事件到应用应用对事件的监听是从Looper
开始的:
// /system/core/libutils/Looper.cpp
int Looper::pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outData) {
int result = 0;
for (;;) {
... ...
result = pollInner(timeoutMillis);
}
}
int Looper::pollInner(int timeoutMillis) {
... ...
// Invoke all response callbacks.
for (size_t i = 0; i < mResponses.size(); i++) {
Response& response = mResponses.editItemAt(i);
if (response.request.ident == POLL_CALLBACK) {
int fd = response.request.fd;
... ...
int callbackResult = response.request.callback->handleEvent(fd, events, data);
... ...
}
... ...
}
... ...
}
此处的callback
是DisplayEventDispatcher
, 所以此时调用的是DisplayEventDispatcher::handleEvent()
:
// frameworks/native/libs/gui/DisplayEventDispatcher.cpp
int DisplayEventDispatcher::handleEvent(int, int events, void*) {
if (processPendingEvents(&vsyncTimestamp, &vsyncDisplayId, &vsyncCount, &vsyncEventData)) {
... ...
dispatchVsync(vsyncTimestamp, vsyncDisplayId, vsyncCount, vsyncEventData);
}
return 1; // keep the callback
}
此时DisplayEventDispatcher
其实是NativeDisplayEventReceiver
的子类, 因此直接看NativeDisplayEventReceiver
:
// frameworks/base/core/jni/android_view_DisplayEventReceiver.cpp
void NativeDisplayEventReceiver::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId,
uint32_t count, VsyncEventData vsyncEventData) {
JNIEnv* env = AndroidRuntime::getJNIEnv();
ScopedLocalRef receiverObj(env, jniGetReferent(env, mReceiverWeakGlobal));
if (receiverObj.get()) {
ALOGV("receiver %p ~ Invoking vsync handler.", this);
env->CallVoidMethod(receiverObj.get(), gDisplayEventReceiverClassInfo.dispatchVsync,
timestamp, displayId.value, count, vsyncEventData.id,
vsyncEventData.deadlineTimestamp, vsyncEventData.frameInterval);
ALOGV("receiver %p ~ Returned from vsync handler.", this);
}
mMessageQueue->raiseAndClearException(env, "dispatchVsync");
}
CallVoidMethod()
通过JNI调用的是DisplayEventReceiver.dispatchVsync()
:
// frameworks/base/core/java/android/view/DisplayEventReceiver.java
// Called from native code.
@SuppressWarnings("unused")
private void dispatchVsync(long timestampNanos, long physicalDisplayId, int frame,
long frameTimelineVsyncId, long frameDeadline, long frameInterval) {
onVsync(timestampNanos, physicalDisplayId, frame,
new VsyncEventData(frameTimelineVsyncId, frameDeadline, frameInterval));
}
此时的DisplayEventReceiver
其实是FrameDisplayEventReceiver
, 所以调用的其实是FrameDisplayEventReceiver.onVsync()
:
// frameworks/base/core/java/android/view/Choreographer.java
private final class FrameDisplayEventReceiver extends DisplayEventReceiver implements Runnable {
@Override
public void onVsync(long timestampNanos, long physicalDisplayId, int frame,
VsyncEventData vsyncEventData) {
try {
... ...
Message msg = Message.obtain(mHandler, this);
msg.setAsynchronous(true);
mHandler.sendMessageAtTime(msg, timestampNanos / TimeUtils.NANOS_PER_MS);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_VIEW);
}
}
... ...
... ...
此处的mHandler
类型是FrameHandler
, 特别的Message
的what
未设置, 其值为MSG_DO_FRAME
, 这是需要特别注意的, 因此其FrameHandler.handleMessage()
会被调用:
// frameworks/base/core/java/android/view/Choreographer.java
private final class FrameHandler extends Handler {
public FrameHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_DO_FRAME:
doFrame(System.nanoTime(), 0, new DisplayEventReceiver.VsyncEventData());
break;
// 此场景下该位置的消息类型不是 MSG_DO_SCHEDULE_VSYNC
case MSG_DO_SCHEDULE_VSYNC:
... ...
... ...
}
}
}
void doFrame(long frameTimeNanos, int frame,
DisplayEventReceiver.VsyncEventData vsyncEventData) {
try {
synchronized (mLock) {
... ...
doCallbacks(Choreographer.CALLBACK_TRAVERSAL, frameTimeNanos, frameIntervalNanos);
... ...
}
... ...
}
... ...
}
void doCallbacks(int callbackType, long frameTimeNanos, long frameIntervalNanos) {
CallbackRecord callbacks;
synchronized (mLock) {
callbacks = mCallbackQueues[callbackType].extractDueCallbacksLocked(
now / TimeUtils.NANOS_PER_MS);
... ...
}
try {
Trace.traceBegin(Trace.TRACE_TAG_VIEW, CALLBACK_TRACE_TITLES[callbackType]);
for (CallbackRecord c = callbacks; c != null; c = c.next) {
... ...
c.run(frameTimeNanos);
}
} ... ...
}
上文mCallbackQueues
中存储的是应用中所有等待更新的层的回调, 类型是TraversalRunnable
, 该类是在ViewRootImpl.scheduleTraversals()
时通过Choreographer.postCallback()
注册过来的, 其实现是:
// frameworks/base/core/java/android/view/ViewRootImpl.java
public final class ViewRootImpl implements ViewParent,
View.AttachInfo.Callbacks, ThreadedRenderer.DrawCallbacks,
AttachedSurfaceControl {
... ...
final class TraversalRunnable implements Runnable {
@Override
public void run() {
doTraversal();
}
}
final TraversalRunnable mTraversalRunnable = new TraversalRunnable();
void doTraversal() {
if (mTraversalScheduled) {
... ...
performTraversals();
... ...
}
}
private void performTraversals() {
... ...
if (!cancelDraw) {
... ...
performDraw();
}
... ...
}
private void performDraw() {
... ...
try {
boolean canUseAsync = draw(fullRedrawNeeded);
... ...
}
... ...
}
private boolean draw(boolean fullRedrawNeeded) {
Surface surface = mSurface;
if (!dirty.isEmpty() || mIsAnimating || accessibilityFocusDirty || mNextDrawUseBlastSync) {
if (isHardwareEnabled()) {
mAttachInfo.mThreadedRenderer.draw(mView, mAttachInfo, this);
}
... ...
}
... ...
}
... ...
}
mAttachInfo.mThreadRender
的类型显然是ThreadedRenderer
, 此时应用开始渲染界面.
DecorView
重绘// frameworks/base/core/java/android/view/ThreadedRenderer.java
public final class ThreadedRenderer extends HardwareRenderer {
void draw(View view, AttachInfo attachInfo, DrawCallbacks callbacks) {
attachInfo.mViewRootImpl.mViewFrameInfo.markDrawStart();
updateRootDisplayList(view, callbacks)
... ...
int syncResult = syncAndDrawFrame(frameInfo);
... ...
}
ThreadedRenderer.updateRootDisplayList()
对于updateRootDisplayList()
方法:
// frameworks/base/core/java/android/view/ThreadedRenderer.java
private void updateRootDisplayList(View view, DrawCallbacks callbacks) {
Trace.traceBegin(Trace.TRACE_TAG_VIEW, "Record View#draw()");
updateViewTreeDisplayList(view);
... ...
}
private void updateViewTreeDisplayList(View view) {
view.mPrivateFlags |= View.PFLAG_DRAWN;
view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED)
== View.PFLAG_INVALIDATED;
view.mPrivateFlags &= ~View.PFLAG_INVALIDATED;
view.updateDisplayListIfDirty();
view.mRecreateDisplayList = false;
}
对于view
, 在窗口中, 其实现是:DecorView
, 但此处调用的还是View.updateDisplayListIfDirty
:
// frameworks/base/core/java/android/view/View.java
public RenderNode updateDisplayListIfDirty() {
final RenderNode renderNode = mRenderNode;
... ...
if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0
|| !renderNode.hasDisplayList()
|| (mRecreateDisplayList)) {
... ...
try {
if (layerType == LAYER_TYPE_SOFTWARE) {
... ...
} else {
... ...
if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
... ...
} else {
draw(canvas);
}
}
}
此时调用父类的方法, 即DecorView.draw()
:
// frameworks/base/core/java/com/android/internal/policy/DecorView.java
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
if (mMenuBackground != null) {
mMenuBackground.draw(canvas);
}
}
又调回父类的View.draw()
方法, 对DecorView
进行绘制:
// frameworks/base/core/java/android/view/View.java
@CallSuper
public void draw(Canvas canvas) {
... ...
if (!verticalEdges && !horizontalEdges) {
// Step 3, draw the content
onDraw(canvas);
// Step 4, draw the children
dispatchDraw(canvas);
... ...
DecorView
子对象重绘可以看到绘制DecorView
时, 会绘制其子控, 这里先说下继承关系:View
-> ViewGroup
-> FrameLayout
-> DecorView
, 那么此处调用的其实是ViewGroup.dispatchDraw()
``件:
// frameworks/base/core/java/android/view/ViewGroup.java
@Override
protected void dispatchDraw(Canvas canvas) {
final int childrenCount = mChildrenCount;
final View[] children = mChildren;
int flags = mGroupFlags;
... ...
for (int i = 0; i < childrenCount; i++) {
... ...
if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
more |= drawChild(canvas, child, drawingTime);
}
}
while (transientIndex >= 0) {
... ...
if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE ||
transientChild.getAnimation() != null) {
more |= drawChild(canvas, transientChild, drawingTime);
}
... ...
}
if (preorderedList != null) preorderedList.clear();
// Draw any disappearing views that have animations
if (mDisappearingChildren != null) {
... ...
for (int i = disappearingCount; i >= 0; i--) {
final View child = disappearingChildren.get(i);
more |= drawChild(canvas, child, drawingTime);
}
}
... ...
}
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
return child.draw(canvas, this, drawingTime);
}
此处ViewGroup.drawChild()
将调用第一个子View的: View.draw()
-> View.drawBackground()
方法绘制背景:
// frameworks/base/core/java/android/view/View.java
@CallSuper
public void draw(Canvas canvas) {
final int privateFlags = mPrivateFlags;
mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
int saveCount;
drawBackground(canvas);
... ...
}
@UnsupportedAppUsage
private void drawBackground(Canvas canvas) {
... ...
if (canvas.isHardwareAccelerated() && mAttachInfo != null
&& mAttachInfo.mThreadedRenderer != null) {
mBackgroundRenderNode = getDrawableRenderNode(background, mBackgroundRenderNode);
... ...
}
... ...
}
注意, 这里View.draw()
中的drawBackground()
方法, 应用是不能自己调用的, 继续查看getDrawableRenderNode()
方法:
// frameworks/base/core/java/android/view/View.java
private RenderNode getDrawableRenderNode(Drawable drawable, RenderNode renderNode) {
... ...
try {
drawable.draw(canvas);
} finally {
renderNode.endRecording();
}
... ...
}
ColorDrawable
重绘子View
背景此时renderNode
的类型为:ColorDrawable
, 因此调用ColorDrawable.draw()
:
// frameworks/base/graphics/java/android/graphics/drawable/ColorDrawable.java
@Override
public void draw(Canvas canvas) {
final ColorFilter colorFilter = mPaint.getColorFilter();
if ((mColorState.mUseColor >>> 24) != 0 || colorFilter != null
|| mBlendModeColorFilter != null) {
if (colorFilter == null) {
mPaint.setColorFilter(mBlendModeColorFilter);
}
mPaint.setColor(mColorState.mUseColor);
canvas.drawRect(getBounds(), mPaint);
// Restore original color filter.
mPaint.setColorFilter(colorFilter);
}
}
此时的canvas
为类型Canvas
因此查看Canvas.drawRect()
的实现:
// frameworks/base/graphics/java/android/graphics/Canvas.java
public class Canvas extends BaseCanvas {
... ...
public void drawRect(float left, float top, float right, float bottom, @NonNull Paint paint) {
super.drawRect(left, top, right, bottom, paint);
}
到父类:
// frameworks/base/graphics/java/android/graphics/BaseCanvas.java
public abstract class BaseCanvas {
... ...
public void drawRect(float left, float top, float right, float bottom, @NonNull Paint paint) {
throwIfHasHwBitmapInSwMode(paint);
nDrawRect(mNativeCanvasWrapper, left, top, right, bottom, paint.getNativeInstance());
}
Canvas
提交绘制请求nDrawRect
在Native层的实现 是CanvasJNI::drawRect
:
// frameworks/base/libs/hwui/jni/android_graphics_Canvas.cpp
static void drawRect(JNIEnv* env, jobject, jlong canvasHandle, jfloat left, jfloat top,
jfloat right, jfloat bottom, jlong paintHandle) {
const Paint* paint = reinterpret_cast(paintHandle);
get_canvas(canvasHandle)->drawRect(left, top, right, bottom, *paint);
}
get_canvas()
返回的其实是SkiaCanvas
类型, 因此继续调用SkiaCanvas::drawRect()
:
// frameworks/base/libs/hwui/SkiaCanvas.cpp
void SkiaCanvas::drawRect(float left, float top, float right, float bottom, const Paint& paint) {
if (CC_UNLIKELY(paint.nothingToDraw())) return;
applyLooper(&paint, [&](const SkPaint& p) {
mCanvas->drawRect({left, top, right, bottom}, p);
});
}
// frameworks/base/libs/hwui/SkiaCanvas.h
template
void applyLooper(const Paint* paint, Proc proc, void (*preFilter)(SkPaint&) = nullptr) {
... ...
this->onFilterPaint(skp);
if (looper) {
... ...
} else {
proc(skp);
}
}
直接调用mCanvas->drawRect()
, 注意, 此处虽然mCanvas
的类型是SkCanvas
, 但其还有子类RecordingCanvas
因此此处调用的是RecordingCanvas::drawRect()
:
// frameworks/base/libs/hwui/RecordingCanvas.cpp
void RecordingCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
fDL->drawRect(rect, paint);
}
fDL
的类型是DisplayListData
, 因此此处调用:DisplayListData::drawRect()
:
// frameworks/base/libs/hwui/RecordingCanvas.cpp
void DisplayListData::drawRect(const SkRect& rect, const SkPaint& paint) {
this->push(0, rect, paint);
}
template
void* DisplayListData::push(size_t pod, Args&&... args) {
size_t skip = SkAlignPtr(sizeof(T) + pod);
SkASSERT(skip < (1 << 24));
if (fUsed + skip > fReserved) {
static_assert(SkIsPow2(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);
}
SkASSERT(fUsed + skip <= fReserved);
auto op = (T*)(fBytes.get() + fUsed);
fUsed += skip;
new (op) T{std::forward(args)...};
op->type = (uint32_t)T::kType;
op->skip = skip;
return op + 1;
}
此处的this->push
直接插入了一条回调到fBytes
中, 这些记录将在DisplayListData::map()
执行时被调用.
ThreadedRenderer.syncAndDrawFrame()
执行绘制操作继续调用基类HardwareRenderer
的syncAndDrawFrame
方法:
// frameworks/base/graphics/java/android/graphics/HardwareRenderer.java
public class HardwareRenderer {
@SyncAndDrawResult
public int syncAndDrawFrame(@NonNull FrameInfo frameInfo) {
return nSyncAndDrawFrame(mNativeProxy, frameInfo.frameInfo, frameInfo.frameInfo.length);
}
... ...
}
nSyncAndDrawFrame()
是个native方法:
// frameworks/base/libs/hwui/jni/android_graphics_HardwareRenderer.cpp
static int android_view_ThreadedRenderer_syncAndDrawFrame(JNIEnv* env, jobject clazz,
jlong proxyPtr, jlongArray frameInfo, jint frameInfoSize) {
... ...
RenderProxy* proxy = reinterpret_cast(proxyPtr);
env->GetLongArrayRegion(frameInfo, 0, frameInfoSize, proxy->frameInfo());
return proxy->syncAndDrawFrame();
}
// frameworks/base/libs/hwui/renderthread/RenderProxy.cpp
int RenderProxy::syncAndDrawFrame() {
return mDrawFrameTask.drawFrame();
}
// frameworks/base/libs/hwui/renderthread/DrawFrameTask.cpp
int DrawFrameTask::drawFrame() {
... ...
postAndWait();
return mSyncResult;
}
void DrawFrameTask::postAndWait() {
AutoMutex _lock(mLock);
mRenderThread->queue().post([this]() { run(); });
mSignal.wait(mLock);
}
DrawFrameTask::postAndWait()
负责提交绘图请求, 并触发RenderThread
执行渲染, 因此DrawFrameTask::run()
会调度到:
// frameworks/base/libs/hwui/renderthread/DrawFrameTask.cpp
void DrawFrameTask::run() {
... ...
if (CC_LIKELY(canDrawThisFrame)) {
dequeueBufferDuration = context->draw();
}
... ...
}
context
的类型是CanvasContext
, 因此调用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);
... ...
}
mRenderPipeline
的类型是SkiaOpenGLPipeline
, 注意它的基类是SkiaPipeline
, 先看SkiaOpenGLPipeline::draw()
;
在SkiaOpenGLPipeline::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>& renderNodes,
FrameInfoVisualizer* profiler) {
... ...
renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
SkMatrix::I());
... ...
{
ATRACE_NAME("flush commands");
surface->flushAndSubmit();
}
... ...
}
SkiaPipeline::renderFrame()
对界面执行渲染在SkiaPipeline::renderFrame()
中:
这里的renderFrame()
, 其属于父类, 因此::
// frameworks/base/libs/hwui/pipeline/skia/SkiaPipeline.cpp
void SkiaPipeline::renderFrame(const LayerUpdateQueue& layers, const SkRect& clip,
const std::vector>& nodes, bool opaque,
const Rect& contentDrawBounds, sk_sp surface,
const SkMatrix& preTransform) {
... ...
renderFrameImpl(clip, nodes, opaque, contentDrawBounds, canvas, preTransform);
void SkiaPipeline::renderFrameImpl(const SkRect& clip,
const std::vector>& nodes, bool opaque,
const Rect& contentDrawBounds, SkCanvas* canvas,
const SkMatrix& preTransform) {
... ...
if (1 == nodes.size()) {
if (!nodes[0]->nothingToDraw()) {
RenderNodeDrawable root(nodes[0].get(), canvas);
root.draw(canvas);
}
} else if (0 == nodes.size()) {
// nothing to draw
} else {
... ...
RenderNodeDrawable contentNode(nodes[1].get(), canvas);
if (!backdrop.isEmpty()) {
... ...
contentNode.draw(canvas);
} else {
SkAutoCanvasRestore acr(canvas, true);
contentNode.draw(canvas);
}
... ...
这里分两种情况, 就是nodes.size()
为1
或者为其它
(0
不做任何操作), 我们先关注contentNode.draw(canvas)
, 其中 contentNode
是RenderNodeDrawable
, 其draw()
方法是父类的, 调用是会调回RenderNodeDrawable::onDraw()
:
// frameworks/base/libs/hwui/pipeline/skia/RenderNodeDrawable.cpp
void RenderNodeDrawable::onDraw(SkCanvas* canvas) {
... ...
if ((!mInReorderingSection) || MathUtils::isZero(mRenderNode->properties().getZ())) {
this->forceDraw(canvas);
}
}
void RenderNodeDrawable::forceDraw(SkCanvas* canvas) const {
RenderNode* renderNode = mRenderNode.get();
... ...
if (!properties.getProjectBackwards()) {
drawContent(canvas);
... ...
void RenderNodeDrawable::drawContent(SkCanvas* canvas) const {
RenderNode* renderNode = mRenderNode.get();
... ...
if (!quickRejected) {
SkiaDisplayList* displayList = renderNode->getDisplayList().asSkiaDl();
const LayerProperties& layerProperties = properties.layerProperties();
// composing a hardware layer
if (renderNode->getLayerSurface() && mComposeLayer) {
...
} else {
if (alphaMultiplier < 1.0f) {
// Non-layer draw for a view with getHasOverlappingRendering=false, will apply
// the alpha to the paint of each nested draw.
AlphaFilterCanvas alphaCanvas(canvas, alphaMultiplier);
displayList->draw(&alphaCanvas);
} else {
displayList->draw(canvas);
}
}
}
关注displayList->draw(canvas)
的调用, 对应的是SkiaDisplayList::draw()
:
// frameworks/base/libs/hwui/pipeline/skia/SkiaDisplayList.h
class SkiaDisplayList {
public:
void draw(SkCanvas* canvas) { mDisplayList.draw(canvas); }
mDisplayList
的类型为DisplayListData
, 因此调用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());
}
template
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;
}
}
查看draw_fns
的定义:
// 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
中:
X(Flush)
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(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)
回顾上文的DisplayListData::drawRect()
:
// frameworks/base/libs/hwui/RecordingCanvas.cpp
void DisplayListData::drawRect(const SkRect& rect, const SkPaint& paint) {
this->push(0, rect, paint);
}
可以看到, 这里采用的是X(DrawRect)
, 因此, 调用的方法为:DrawRect::draw()
:
// frameworks/base/libs/hwui/RecordingCanvas.cpp
struct DrawRect final : Op {
... ...
void draw(SkCanvas* c, const SkMatrix&) const { c->drawRect(rect, paint); }
};
调用到上文的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);
}
void SkCanvas::onDrawRect(const SkRect& r, const SkPaint& paint) {
... ...
this->topDevice()->drawRect(r, layer.paint());
}
this->topDevice()
得到的是SkGpuDevice
, 因此此时调用的是SkGpuDevice::drawRect()
:
// external/skia/src/gpu/SkGpuDevice.cpp
void SkGpuDevice::drawRect(const SkRect& rect, const SkPaint& paint) {
... ...
fSurfaceDrawContext->drawRect(this->clip(), std::move(grPaint),
fSurfaceDrawContext->chooseAA(paint), this->localToDevice(), rect,
&style);
}
fSurfaceDrawContext
的类型是GrSurfaceDrawContext
, 故GrSurfaceDrawContext::drawRect()
void GrSurfaceDrawContext::drawRect(const GrClip* clip,
GrPaint&& paint,
GrAA aa,
const SkMatrix& viewMatrix,
const SkRect& rect,
const GrStyle* style) {
... ...
const SkStrokeRec& stroke = style->strokeRec();
if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
... ...
} else if ((stroke.getStyle() == SkStrokeRec::kStroke_Style ||
stroke.getStyle() == SkStrokeRec::kHairline_Style) &&
(rect.width() && rect.height())) {
... ...
GrOp::Owner op = GrStrokeRectOp::Make(
fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
if (op) {
this->addDrawOp(clip, std::move(op));
return;
}
}
... ...
}
void GrSurfaceDrawContext::addDrawOp(const GrClip* clip,
GrOp::Owner op,
const std::function& willAddFn) {
... ...
auto opsTask = this->getOpsTask();
opsTask->addDrawOp(this->drawingManager(), std::move(op), fixedFunctionFlags, analysis,
std::move(appliedClip), dstProxyView,
GrTextureResolveManager(this->drawingManager()), *this->caps());
}
this->getOpsTask()
得到的就是GrOpsTask
, 因此:
// external/skia/src/gpu/GrOpsTask.cpp
void GrOpsTask::addDrawOp(GrDrawingManager* drawingMgr, GrOp::Owner op,
GrDrawOp::FixedFunctionFlags fixedFunctionFlags,
const GrProcessorSet::Analysis& processorAnalysis, GrAppliedClip&& clip,
const DstProxyView& dstProxyView,
GrTextureResolveManager textureResolveManager, const GrCaps& caps) {
... ...
this->recordOp(std::move(op), processorAnalysis, clip.doesClip() ? &clip : nullptr,
&dstProxyView, caps);
}
void GrOpsTask::recordOp(
GrOp::Owner op, GrProcessorSet::Analysis processorAnalysis, GrAppliedClip* clip,
const DstProxyView* dstProxyView, const GrCaps& caps) {
... ...
fOpChains.emplace_back(std::move(op), processorAnalysis, clip, dstProxyView);
}
fOpChains
记录了所有应该绘制的操作, 这些操作会在GrOpsTask::execute()
时执行, 相应的操作流程将在本文后续内容介绍.
SkSurface::flushAndSubmit()
提交绘制请求在函数末尾的surface->flushAndSubmit()
:
surface
的类型时SkSurface
, 故此处调用SkSurface::flushAndSubmit()
:
// external/skia/src/image/SkSurface.cpp
void SkSurface::flushAndSubmit(bool syncCpu) {
this->flush(BackendSurfaceAccess::kNoAccess, GrFlushInfo());
}
GrSemaphoresSubmitted SkSurface::flush(BackendSurfaceAccess access, const GrFlushInfo& flushInfo) {
return asSB(this)->onFlush(access, flushInfo, nullptr);
}
其实此处SkSurface
由SkSurface_Gpu
继承, 因此调用SkSurface_Gpu::onDraw()
:
// external/skqp/src/image/SkSurface_Gpu.cpp
GrSemaphoresSubmitted SkSurface_Gpu::onFlush(BackendSurfaceAccess access, const GrFlushInfo& info,
const GrBackendSurfaceMutableState* newState) {
auto dContext = fDevice->recordingContext()->asDirectContext();
... ...
GrSurfaceDrawContext* sdc = fDevice->surfaceDrawContext();
return dContext->priv().flushSurface(sdc->asSurfaceProxy(), access, info, newState);
}
dContext
的类型时GrDirectContext
, dContext->priv()
返回的是GrDirectContextPriv
, 因此:
// external/skia/src/gpu/GrDirectContextPriv.h
class GrDirectContextPriv {
GrSemaphoresSubmitted flushSurface(
GrSurfaceProxy* proxy,
SkSurface::BackendSurfaceAccess access = SkSurface::BackendSurfaceAccess::kNoAccess,
const GrFlushInfo& info = {},
const GrBackendSurfaceMutableState* newState = nullptr) {
size_t size = proxy ? 1 : 0;
return this->flushSurfaces({&proxy, size}, access, info, newState);
}
实现在:
// external/skia/src/gpu/GrDirectContextPriv.cpp
GrSemaphoresSubmitted GrDirectContextPriv::flushSurfaces(
SkSpan proxies,
SkSurface::BackendSurfaceAccess access,
const GrFlushInfo& info,
const GrBackendSurfaceMutableState* newState) {
... ...
return fContext->drawingManager()->flushSurfaces(proxies, access, info, newState);
}
fContext->drawingManager()
返回GrDrawingManager
, 所以调用:
// external/skia/src/gpu/GrDrawingManager.cpp
GrSemaphoresSubmitted GrDrawingManager::flushSurfaces(
SkSpan proxies,
SkSurface::BackendSurfaceAccess access,
const GrFlushInfo& info,
const GrBackendSurfaceMutableState* newState) {
... ...
bool didFlush = this->flush(proxies, access, info, newState);
... ...
}
bool GrDrawingManager::flush(
SkSpan proxies,
SkSurface::BackendSurfaceAccess access,
const GrFlushInfo& info,
const GrBackendSurfaceMutableState* newState) {
... ...
bool flushed = !resourceAllocator.failedInstantiation() &&
this->executeRenderTasks(&flushState);
this->removeRenderTasks();
... ...
}
bool GrDrawingManager::executeRenderTasks(GrOpFlushState* flushState) {
// Execute the normal op lists.
for (const auto& renderTask : fDAG) {
... ...
if (renderTask->execute(flushState)) {
anyRenderTasksExecuted = true;
}
if (++numRenderTasksExecuted >= kMaxRenderTasksBeforeFlush) {
flushState->gpu()->submitToGpu(false);
numRenderTasksExecuted = 0;
}
}
renderTask
的类型是GrRenderTask
, 其实现是GrOpsTask
, 因此:
// external/skia/src/gpu/GrRenderTask.h
class GrRenderTask : public SkRefCnt {
public:
bool execute(GrOpFlushState* flushState) { return this->onExecute(flushState); }
// external/skia/src/gpu/GrOpsTask.cpp
bool GrOpsTask::onExecute(GrOpFlushState* flushState) {
... ...
// Draw all the generated geometry.
for (const auto& chain : fOpChains) {
if (!chain.shouldExecute()) {
continue;
}
GrOpFlushState::OpArgs opArgs(chain.head(),
dstView,
fUsesMSAASurface,
chain.appliedClip(),
chain.dstProxyView(),
fRenderPassXferBarriers,
fColorLoadOp);
flushState->setOpArgs(&opArgs);
chain.head()->execute(flushState, chain.bounds());
flushState->setOpArgs(nullptr);
}
此处开始依次执行每个OpChain::head()
的execute()
方法. 本文中此处的GrOp
为GrStrokeRectOp::Make()
所创建的NonAAStrokeRectOp
, 其继承关系:GrOp
-> GrDrawOp
-> GrMeshDrawOp
-> NonAAStrokeRectOp
, 那么此时:
// external/skia/src/gpu/ops/GrOp.h
class GrOp : private SkNoncopyable {
public:
... ...
/** Issues the op's commands to GrGpu. */
void execute(GrOpFlushState* state, const SkRect& chainBounds) {
TRACE_EVENT0("skia.gpu", name());
this->onExecute(state, chainBounds);
}
// external/skia/src/gpu/ops/GrStrokeRectOp.cpp
class NonAAStrokeRectOp final : public GrMeshDrawOp {
... ...
void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
... ...
flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
flushState->drawMesh(*fMesh);
}
显然这时调用了GrOpFlushState
的各种方法完成绘图.
swapBuffer()
交换窗口至SurfaceFlinger
在SkiaOpenGLPipeline::draw()
完成工作后, CanvasContext::draw()
将调用SkiaOpenGLPipeline::swapBuffers()
, 查看其实现:
// frameworks/base/libs/hwui/pipeline/skia/SkiaOpenGLPipeline.cpp
bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
FrameInfo* currentFrameInfo, bool* requireSwap) {
... ...
if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
return false;
}
... ...
}
mEglManager
的类型是EglManager
, 故:
// frameworks/base/libs/hwui/renderthread/EglManager.cpp
bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
... ...
eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, screenDirty.isEmpty() ? 0 : 1);
... ...
}
eglSwapBuffersWithDamageKHR()
属于libEGL.so
, 没有源码, 该方法执行完成后, libEGL.so
将回调ANativeWindow::queueBuffer
进一步回调到Surface::hook_queueBuffer()
, 代码:
// frameworks/native/libs/gui/Surface.cpp
int Surface::hook_queueBuffer(ANativeWindow* window,
ANativeWindowBuffer* buffer, int fenceFd) {
Surface* c = getSelf(window);
{
std::shared_lock lock(c->mInterceptorMutex);
if (c->mQueueInterceptor != nullptr) {
auto interceptor = c->mQueueInterceptor;
auto data = c->mQueueInterceptorData;
return interceptor(window, Surface::queueBufferInternal, data, buffer, fenceFd);
}
}
return c->queueBuffer(buffer, fenceFd);
}
int Surface::queueBufferInternal(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
Surface* c = getSelf(window);
return c->queueBuffer(buffer, fenceFd);
}
int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
... ...
status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
... ...
}
Android S以后BufferQueue改到应用侧, 因此此处mGraphicBufferProducer
为本地得BufferQueueProducer
, 而不再通过Binder传递GraphicBuffer
到SurfaceFlinger
, 因此:
// frameworks/native/libs/gui/BufferQueueProducer.cpp
status_t BufferQueueProducer::queueBuffer(int slot,
const QueueBufferInput &input, QueueBufferOutput *output) {
... ...
{ // scope for the lock
... ...
if (frameAvailableListener != nullptr) {
frameAvailableListener->onFrameAvailable(item);
} else if (frameReplacedListener != nullptr) {
frameReplacedListener->onFrameReplaced(item);
}
此处得frameAvailableListener
是BLASTBufferQueue
, 因此BLASTBufferQueue::onFrameAvailable()
被调用:
// frameworks/native/libs/gui/BLASTBufferQueue.cpp
return item.mCrop;
}
void BLASTBufferQueue::onFrameAvailable(const BufferItem& item) {
... ...
processNextBufferLocked(nextTransactionSet /* useNextTransaction */);
}
void BLASTBufferQueue::processNextBufferLocked(bool useNextTransaction) {
... ...
SurfaceComposerClient::Transaction* t = &localTransaction;
... ...
if (applyTransaction) {
t->setApplyToken(mApplyToken).apply();
}
显然调用到SurfaceComposerClient::Transaction::apply()
:
// frameworks/native/libs/gui/SurfaceComposerClient.cpp
status_t SurfaceComposerClient::Transaction::apply(bool synchronous) {
... ...
sf->setTransactionState(mFrameTimelineInfo, composerStates, displayStates, flags, applyToken,
mInputWindowCommands, mDesiredPresentTime, mIsAutoTimestamp,
{} /*uncacheBuffer - only set in doUncacheBufferTransaction*/,
hasListenerCallbacks, listenerCallbacks, mId);
... ...
}
此时通过Binder, SurfaceFlinger::setTransactionState()
被调用:
// frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
status_t SurfaceFlinger::setTransactionState(
const FrameTimelineInfo& frameTimelineInfo, const Vector& states,
const Vector& displays, uint32_t flags, const sp& applyToken,
const InputWindowCommands& inputWindowCommands, int64_t desiredPresentTime,
bool isAutoTimestamp, const client_cache_t& uncacheBuffer, bool hasListenerCallbacks,
const std::vector& listenerCallbacks, uint64_t transactionId) {
... ...
queueTransaction(state);
... ...
void SurfaceFlinger::queueTransaction(TransactionState& state) {
... ...
setTransactionFlags(eTransactionFlushNeeded, schedule, state.applyToken);
}
uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
return setTransactionFlags(flags, TransactionSchedule::Late);
}
uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags, TransactionSchedule schedule,
const sp& token) {
... ...
if ((old & flags) == 0) signalTransaction();
return old;
}
void SurfaceFlinger::signalTransaction() {
mScheduler->resetIdleTimer();
mPowerAdvisor.notifyDisplayUpdateImminent();
mEventQueue->invalidate();
}
此处比较关键的操作是:mEventQueue->invalidate()
, 对应MessageQueue::invalidate()
:
// frameworks/native/services/surfaceflinger/Scheduler/MessageQueue.cpp
mLooper->sendMessage(handler, Message());
}
void MessageQueue::invalidate() {
... ...
mVsync.expectedWakeupTime =
mVsync.registration->schedule({.workDuration = mVsync.workDuration.get().count(),
.readyDuration = 0,
.earliestVsync = mVsync.lastCallbackTime.count()});
}
尤其注意此处的mVsync.registration->schedule()
, 该方法调度了一个VSyncCallbackRegistration
在Scheduler
, 而此处的VSyncCallbackRegistration
在如下位置被注册:
// frameworks/native/services/surfaceflinger/Scheduler/MessageQueue.cpp
void MessageQueue::initVsync(scheduler::VSyncDispatch& dispatch,
frametimeline::TokenManager& tokenManager,
std::chrono::nanoseconds workDuration) {
setDuration(workDuration);
mVsync.tokenManager = &tokenManager;
mVsync.registration = std::make_unique<
scheduler::VSyncCallbackRegistration>(dispatch,
std::bind(&MessageQueue::vsyncCallback, this,
std::placeholders::_1,
std::placeholders::_2,
std::placeholders::_3),
"sf");
}
回到VSyncDispatchTimerQueue::timerCallback()
方法中, 其通过invocation.callback->callback()
通知App完成绘制时, 也继续调用了MessageQueue::vsyncCallback()
方法, 因此:
// frameworks/native/services/surfaceflinger/Scheduler/MessageQueue.cpp
void MessageQueue::vsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime) {
... ...
mHandler->dispatchInvalidate(mVsync.tokenManager->generateTokenForPredictions(
{targetWakeupTime, readyTime, vsyncTime}),
... ...
}
void MessageQueue::Handler::dispatchInvalidate(int64_t vsyncId, nsecs_t expectedVSyncTimestamp) {
if ((mEventMask.fetch_or(eventMaskInvalidate) & eventMaskInvalidate) == 0) {
... ....
mQueue.mLooper->sendMessage(this, Message(MessageQueue::INVALIDATE));
}
}
这里的MessageQueue::INVALIDATE
消息会被MessageQueue::Handler::handleMessage
响应, 因此:
void MessageQueue::Handler::handleMessage(const Message& message) {
switch (message.what) {
case INVALIDATE:
mEventMask.fetch_and(~eventMaskInvalidate);
mQueue.mFlinger->onMessageReceived(message.what, mVsyncId, mExpectedVSyncTime);
break;
case REFRESH:
... ...
break;
}
}
SurfaceFlinger
合成窗口显然SurfaceFlinger::onMessageReceived()
被调用:
// frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
void SurfaceFlinger::onMessageReceived(int32_t what, int64_t vsyncId, nsecs_t expectedVSyncTime) {
switch (what) {
case MessageQueue::INVALIDATE: {
onMessageInvalidate(vsyncId, expectedVSyncTime);
break;
}
... ...
}
}
void SurfaceFlinger::onMessageInvalidate(int64_t vsyncId, nsecs_t expectedVSyncTime) {
... ...
mCompositionEngine->present(refreshArgs);
... ...
SurfaceFlinger
调用了CompositionEngine::present()
对App发送过来的Layer
进行合成:
// frameworks/native/services/surfaceflinger/CompositionEngine/src/CompositionEngine.cpp
void CompositionEngine::present(CompositionRefreshArgs& args) {
preComposition(args);
{
... ...
LayerFESet latchedLayers;
for (const auto& output : args.outputs) {
output->prepare(args, latchedLayers);
}
}
updateLayerStateFromFE(args);
for (const auto& output : args.outputs) {
output->present(args);
}
}
显然有:
void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
ATRACE_CALL();
ALOGV(__FUNCTION__);
updateColorProfile(refreshArgs);
updateCompositionState(refreshArgs);
planComposition();
writeCompositionState(refreshArgs);
setColorTransform(refreshArgs);
beginFrame();
prepareFrame();
devOptRepaintFlash(refreshArgs);
finishFrame(refreshArgs);
postFramebuffer();
renderCachedSets(refreshArgs);
}
在Output::finishFrame()
时有:
void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
... ...
// swap buffers (presentation)
mRenderSurface->queueBuffer(std::move(*optReadyFence));
}
Composer
mRenderSurface->queueBuffer()
完成将合成的屏幕内容送显.
在Output::postFramebuffer()
时有:
void Output::postFramebuffer() {
... ...
mRenderSurface->flip();
auto frame = presentAndGetFrameFences();
... ...
}
compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
compositionengine::Output::FrameFences result;
if (getState().usesClientComposition) {
result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
}
return result;
}
mRenderSurface->flip()
完成交换动作, presentAndGetFrameFences()
用于等待mRenderSurface
释放交换回来的无用缓冲区.