Merge "Add GPU completion to FrameMetrics (1/3)" into sc-dev

This commit is contained in:
Jorim Jaggi
2021-02-17 17:38:20 +00:00
committed by Android (Google) Code Review
18 changed files with 280 additions and 50 deletions

View File

@@ -46439,8 +46439,10 @@ package android.view {
method public long getMetric(int); method public long getMetric(int);
field public static final int ANIMATION_DURATION = 2; // 0x2 field public static final int ANIMATION_DURATION = 2; // 0x2
field public static final int COMMAND_ISSUE_DURATION = 6; // 0x6 field public static final int COMMAND_ISSUE_DURATION = 6; // 0x6
field public static final int DEADLINE = 13; // 0xd
field public static final int DRAW_DURATION = 4; // 0x4 field public static final int DRAW_DURATION = 4; // 0x4
field public static final int FIRST_DRAW_FRAME = 9; // 0x9 field public static final int FIRST_DRAW_FRAME = 9; // 0x9
field public static final int GPU_DURATION = 12; // 0xc
field public static final int INPUT_HANDLING_DURATION = 1; // 0x1 field public static final int INPUT_HANDLING_DURATION = 1; // 0x1
field public static final int INTENDED_VSYNC_TIMESTAMP = 10; // 0xa field public static final int INTENDED_VSYNC_TIMESTAMP = 10; // 0xa
field public static final int LAYOUT_MEASURE_DURATION = 3; // 0x3 field public static final int LAYOUT_MEASURE_DURATION = 3; // 0x3

View File

@@ -154,6 +154,24 @@ public final class FrameMetrics {
*/ */
public static final int VSYNC_TIMESTAMP = 11; public static final int VSYNC_TIMESTAMP = 11;
/**
* Metric identifier for GPU duration.
* <p>
* Represents the total time in nanoseconds this frame took to complete on the GPU.
* </p>
**/
public static final int GPU_DURATION = 12;
/**
* Metric identifier for the total duration that was available to the app to produce a frame.
* <p>
* Represents the total time in nanoseconds the system allocated for the app to produce its
* frame. If FrameMetrics.TOTAL_DURATION < FrameMetrics.DEADLINE, the app hit its intended
* deadline and there was no jank visible to the user.
* </p>
**/
public static final int DEADLINE = 13;
private static final int FRAME_INFO_FLAG_FIRST_DRAW = 1 << 0; private static final int FRAME_INFO_FLAG_FIRST_DRAW = 1 << 0;
/** /**
@@ -175,6 +193,8 @@ public final class FrameMetrics {
FIRST_DRAW_FRAME, FIRST_DRAW_FRAME,
INTENDED_VSYNC_TIMESTAMP, INTENDED_VSYNC_TIMESTAMP,
VSYNC_TIMESTAMP, VSYNC_TIMESTAMP,
GPU_DURATION,
DEADLINE,
}) })
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
public @interface Metric {} public @interface Metric {}
@@ -205,6 +225,8 @@ public final class FrameMetrics {
Index.ISSUE_DRAW_COMMANDS_START, Index.ISSUE_DRAW_COMMANDS_START,
Index.SWAP_BUFFERS, Index.SWAP_BUFFERS,
Index.FRAME_COMPLETED, Index.FRAME_COMPLETED,
Index.GPU_COMPLETED,
Index.SWAP_BUFFERS_COMPLETED
}) })
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
public @interface Index { public @interface Index {
@@ -224,8 +246,10 @@ public final class FrameMetrics {
int ISSUE_DRAW_COMMANDS_START = 13; int ISSUE_DRAW_COMMANDS_START = 13;
int SWAP_BUFFERS = 14; int SWAP_BUFFERS = 14;
int FRAME_COMPLETED = 15; int FRAME_COMPLETED = 15;
int GPU_COMPLETED = 18;
int SWAP_BUFFERS_COMPLETED = 19;
int FRAME_STATS_COUNT = 19; // must always be last and in sync with int FRAME_STATS_COUNT = 20; // must always be last and in sync with
// FrameInfoIndex::NumIndexes in libs/hwui/FrameInfo.h // FrameInfoIndex::NumIndexes in libs/hwui/FrameInfo.h
} }
@@ -251,9 +275,19 @@ public final class FrameMetrics {
// COMMAND_ISSUE // COMMAND_ISSUE
Index.ISSUE_DRAW_COMMANDS_START, Index.SWAP_BUFFERS, Index.ISSUE_DRAW_COMMANDS_START, Index.SWAP_BUFFERS,
// SWAP_BUFFERS // SWAP_BUFFERS
Index.SWAP_BUFFERS, Index.FRAME_COMPLETED, Index.SWAP_BUFFERS, Index.SWAP_BUFFERS_COMPLETED,
// TOTAL_DURATION // TOTAL_DURATION
Index.INTENDED_VSYNC, Index.FRAME_COMPLETED, Index.INTENDED_VSYNC, Index.FRAME_COMPLETED,
// RESERVED for FIRST_DRAW_FRAME
0, 0,
// RESERVED forINTENDED_VSYNC_TIMESTAMP
0, 0,
// RESERVED VSYNC_TIMESTAMP
0, 0,
// GPU_DURATION
Index.SWAP_BUFFERS, Index.GPU_COMPLETED,
// DEADLINE
Index.INTENDED_VSYNC, Index.FRAME_DEADLINE,
}; };
/** /**
@@ -294,7 +328,7 @@ public final class FrameMetrics {
* @return the value of the metric or -1 if it is not available. * @return the value of the metric or -1 if it is not available.
*/ */
public long getMetric(@Metric int id) { public long getMetric(@Metric int id) {
if (id < UNKNOWN_DELAY_DURATION || id > VSYNC_TIMESTAMP) { if (id < UNKNOWN_DELAY_DURATION || id > DEADLINE) {
return -1; return -1;
} }

View File

@@ -481,6 +481,8 @@ cc_defaults {
target: { target: {
android: { android: {
header_libs: ["libandroid_headers_private" ],
srcs: [ srcs: [
"hwui/AnimatedImageThread.cpp", "hwui/AnimatedImageThread.cpp",
"pipeline/skia/ATraceMemoryDump.cpp", "pipeline/skia/ATraceMemoryDump.cpp",
@@ -568,6 +570,7 @@ cc_defaults {
name: "hwui_test_defaults", name: "hwui_test_defaults",
defaults: ["hwui_defaults"], defaults: ["hwui_defaults"],
test_suites: ["device-tests"], test_suites: ["device-tests"],
header_libs: ["libandroid_headers_private"],
target: { target: {
android: { android: {
shared_libs: [ shared_libs: [
@@ -605,7 +608,6 @@ cc_test {
shared_libs: [ shared_libs: [
"libmemunreachable", "libmemunreachable",
], ],
srcs: [ srcs: [
"tests/unit/main.cpp", "tests/unit/main.cpp",
"tests/unit/ABitmapTests.cpp", "tests/unit/ABitmapTests.cpp",

View File

@@ -42,7 +42,7 @@ const std::array<std::string, static_cast<int>(FrameInfoIndex::NumIndexes)> Fram
"GpuCompleted", "GpuCompleted",
}; };
static_assert(static_cast<int>(FrameInfoIndex::NumIndexes) == 19, static_assert(static_cast<int>(FrameInfoIndex::NumIndexes) == 20,
"Must update value in FrameMetrics.java#FRAME_STATS_COUNT (and here)"); "Must update value in FrameMetrics.java#FRAME_STATS_COUNT (and here)");
void FrameInfo::importUiThreadInfo(int64_t* info) { void FrameInfo::importUiThreadInfo(int64_t* info) {

View File

@@ -55,6 +55,7 @@ enum class FrameInfoIndex {
QueueBufferDuration, QueueBufferDuration,
GpuCompleted, GpuCompleted,
SwapBuffersCompleted,
// Must be the last value! // Must be the last value!
// Also must be kept in sync with FrameMetrics.java#FRAME_STATS_COUNT // Also must be kept in sync with FrameMetrics.java#FRAME_STATS_COUNT
@@ -120,6 +121,10 @@ public:
void markSwapBuffers() { set(FrameInfoIndex::SwapBuffers) = systemTime(SYSTEM_TIME_MONOTONIC); } void markSwapBuffers() { set(FrameInfoIndex::SwapBuffers) = systemTime(SYSTEM_TIME_MONOTONIC); }
void markSwapBuffersCompleted() {
set(FrameInfoIndex::SwapBuffersCompleted) = systemTime(SYSTEM_TIME_MONOTONIC);
}
void markFrameCompleted() { set(FrameInfoIndex::FrameCompleted) = systemTime(SYSTEM_TIME_MONOTONIC); } void markFrameCompleted() { set(FrameInfoIndex::FrameCompleted) = systemTime(SYSTEM_TIME_MONOTONIC); }
void addFlag(int frameInfoFlag) { void addFlag(int frameInfoFlag) {

View File

@@ -16,14 +16,16 @@
#pragma once #pragma once
#include <utils/Mutex.h>
#include <utils/Log.h> #include <utils/Log.h>
#include <utils/RefBase.h> #include <utils/RefBase.h>
#include <ui/FatVector.h>
#include "FrameInfo.h" #include "FrameInfo.h"
#include "FrameMetricsObserver.h" #include "FrameMetricsObserver.h"
#include <string.h> #include <string.h>
#include <vector>
namespace android { namespace android {
namespace uirenderer { namespace uirenderer {
@@ -32,9 +34,13 @@ class FrameMetricsReporter {
public: public:
FrameMetricsReporter() {} FrameMetricsReporter() {}
void addObserver(FrameMetricsObserver* observer) { mObservers.push_back(observer); } void addObserver(FrameMetricsObserver* observer) {
std::lock_guard lock(mObserversLock);
mObservers.push_back(observer);
}
bool removeObserver(FrameMetricsObserver* observer) { bool removeObserver(FrameMetricsObserver* observer) {
std::lock_guard lock(mObserversLock);
for (size_t i = 0; i < mObservers.size(); i++) { for (size_t i = 0; i < mObservers.size(); i++) {
if (mObservers[i].get() == observer) { if (mObservers[i].get() == observer) {
mObservers.erase(mObservers.begin() + i); mObservers.erase(mObservers.begin() + i);
@@ -44,16 +50,28 @@ public:
return false; return false;
} }
bool hasObservers() { return mObservers.size() > 0; } bool hasObservers() {
std::lock_guard lock(mObserversLock);
return mObservers.size() > 0;
}
void reportFrameMetrics(const int64_t* stats) { void reportFrameMetrics(const int64_t* stats) {
for (size_t i = 0; i < mObservers.size(); i++) { FatVector<sp<FrameMetricsObserver>, 10> copy;
mObservers[i]->notify(stats); {
std::lock_guard lock(mObserversLock);
copy.reserve(mObservers.size());
for (size_t i = 0; i < mObservers.size(); i++) {
copy.push_back(mObservers[i]);
}
}
for (size_t i = 0; i < copy.size(); i++) {
copy[i]->notify(stats);
} }
} }
private: private:
std::vector<sp<FrameMetricsObserver> > mObservers; FatVector<sp<FrameMetricsObserver>, 10> mObservers GUARDED_BY(mObserversLock);
std::mutex mObserversLock;
}; };
} // namespace uirenderer } // namespace uirenderer

View File

@@ -79,7 +79,9 @@ static const int64_t EXEMPT_FRAMES_FLAGS = FrameInfoFlags::SurfaceCanvas;
// and filter it out of the frame profile data // and filter it out of the frame profile data
static FrameInfoIndex sFrameStart = FrameInfoIndex::IntendedVsync; static FrameInfoIndex sFrameStart = FrameInfoIndex::IntendedVsync;
JankTracker::JankTracker(ProfileDataContainer* globalData) { JankTracker::JankTracker(ProfileDataContainer* globalData)
: mData(globalData->getDataMutex())
, mDataMutex(globalData->getDataMutex()) {
mGlobalData = globalData; mGlobalData = globalData;
nsecs_t frameIntervalNanos = DeviceInfo::getVsyncPeriod(); nsecs_t frameIntervalNanos = DeviceInfo::getVsyncPeriod();
nsecs_t sfOffset = DeviceInfo::getCompositorOffset(); nsecs_t sfOffset = DeviceInfo::getCompositorOffset();
@@ -107,6 +109,8 @@ void JankTracker::setFrameInterval(nsecs_t frameInterval) {
} }
void JankTracker::finishFrame(const FrameInfo& frame) { void JankTracker::finishFrame(const FrameInfo& frame) {
std::lock_guard lock(mDataMutex);
// Fast-path for jank-free frames // Fast-path for jank-free frames
int64_t totalDuration = frame.duration(sFrameStart, FrameInfoIndex::FrameCompleted); int64_t totalDuration = frame.duration(sFrameStart, FrameInfoIndex::FrameCompleted);
if (mDequeueTimeForgiveness && frame[FrameInfoIndex::DequeueBufferDuration] > 500_us) { if (mDequeueTimeForgiveness && frame[FrameInfoIndex::DequeueBufferDuration] > 500_us) {
@@ -125,7 +129,11 @@ void JankTracker::finishFrame(const FrameInfo& frame) {
} }
} }
LOG_ALWAYS_FATAL_IF(totalDuration <= 0, "Impossible totalDuration %" PRId64, totalDuration); LOG_ALWAYS_FATAL_IF(totalDuration <= 0, "Impossible totalDuration %" PRId64 " start=%" PRIi64
" gpuComplete=%" PRIi64, totalDuration,
frame[FrameInfoIndex::IntendedVsync],
frame[FrameInfoIndex::GpuCompleted]);
mData->reportFrame(totalDuration); mData->reportFrame(totalDuration);
(*mGlobalData)->reportFrame(totalDuration); (*mGlobalData)->reportFrame(totalDuration);
@@ -188,6 +196,7 @@ void JankTracker::finishFrame(const FrameInfo& frame) {
void JankTracker::dumpData(int fd, const ProfileDataDescription* description, void JankTracker::dumpData(int fd, const ProfileDataDescription* description,
const ProfileData* data) { const ProfileData* data) {
if (description) { if (description) {
switch (description->type) { switch (description->type) {
case JankTrackerType::Generic: case JankTrackerType::Generic:
@@ -227,6 +236,7 @@ void JankTracker::dumpFrames(int fd) {
} }
void JankTracker::reset() { void JankTracker::reset() {
std::lock_guard lock(mDataMutex);
mFrames.clear(); mFrames.clear();
mData->reset(); mData->reset();
(*mGlobalData)->reset(); (*mGlobalData)->reset();
@@ -235,6 +245,7 @@ void JankTracker::reset() {
} }
void JankTracker::finishGpuDraw(const FrameInfo& frame) { void JankTracker::finishGpuDraw(const FrameInfo& frame) {
std::lock_guard lock(mDataMutex);
int64_t totalGPUDrawTime = frame.gpuDrawTime(); int64_t totalGPUDrawTime = frame.gpuDrawTime();
if (totalGPUDrawTime >= 0) { if (totalGPUDrawTime >= 0) {
mData->reportGPUFrame(totalGPUDrawTime); mData->reportGPUFrame(totalGPUDrawTime);

View File

@@ -84,12 +84,15 @@ private:
// This is only used if we are in pipelined mode and are using HWC2, // This is only used if we are in pipelined mode and are using HWC2,
// otherwise it's 0. // otherwise it's 0.
nsecs_t mDequeueTimeForgiveness = 0; nsecs_t mDequeueTimeForgiveness = 0;
ProfileDataContainer mData; ProfileDataContainer mData GUARDED_BY(mDataMutex);
ProfileDataContainer* mGlobalData; ProfileDataContainer* mGlobalData GUARDED_BY(mDataMutex);
ProfileDataDescription mDescription; ProfileDataDescription mDescription;
// Ring buffer large enough for 2 seconds worth of frames // Ring buffer large enough for 2 seconds worth of frames
RingBuffer<FrameInfo, 120> mFrames; RingBuffer<FrameInfo, 120> mFrames;
// Mutex to protect acccess to mData and mGlobalData obtained from mGlobalData->getDataMutex
std::mutex& mDataMutex;
}; };
} /* namespace uirenderer */ } /* namespace uirenderer */

View File

@@ -38,6 +38,8 @@ void ProfileDataContainer::freeData() {
} }
void ProfileDataContainer::rotateStorage() { void ProfileDataContainer::rotateStorage() {
std::lock_guard lock(mJankDataMutex);
// If we are mapped we want to stop using the ashmem backend and switch to malloc // If we are mapped we want to stop using the ashmem backend and switch to malloc
// We are expecting a switchStorageToAshmem call to follow this, but it's not guaranteed // We are expecting a switchStorageToAshmem call to follow this, but it's not guaranteed
// If we aren't sitting on top of ashmem then just do a reset() as it's functionally // If we aren't sitting on top of ashmem then just do a reset() as it's functionally
@@ -50,6 +52,7 @@ void ProfileDataContainer::rotateStorage() {
} }
void ProfileDataContainer::switchStorageToAshmem(int ashmemfd) { void ProfileDataContainer::switchStorageToAshmem(int ashmemfd) {
std::lock_guard lock(mJankDataMutex);
int regionSize = ashmem_get_size_region(ashmemfd); int regionSize = ashmem_get_size_region(ashmemfd);
if (regionSize < 0) { if (regionSize < 0) {
int err = errno; int err = errno;
@@ -70,7 +73,9 @@ void ProfileDataContainer::switchStorageToAshmem(int ashmemfd) {
return; return;
} }
newData->mergeWith(*mData); if (mData != nullptr) {
newData->mergeWith(*mData);
}
freeData(); freeData();
mData = newData; mData = newData;
mIsMapped = true; mIsMapped = true;

View File

@@ -19,6 +19,9 @@
#include "ProfileData.h" #include "ProfileData.h"
#include "utils/Macros.h" #include "utils/Macros.h"
#include <mutex>
#include <utils/Mutex.h>
namespace android { namespace android {
namespace uirenderer { namespace uirenderer {
@@ -26,7 +29,8 @@ class ProfileDataContainer {
PREVENT_COPY_AND_ASSIGN(ProfileDataContainer); PREVENT_COPY_AND_ASSIGN(ProfileDataContainer);
public: public:
explicit ProfileDataContainer() {} explicit ProfileDataContainer(std::mutex& jankDataMutex)
: mData(new ProfileData()), mJankDataMutex(jankDataMutex) {}
~ProfileDataContainer() { freeData(); } ~ProfileDataContainer() { freeData(); }
@@ -36,13 +40,16 @@ public:
ProfileData* get() { return mData; } ProfileData* get() { return mData; }
ProfileData* operator->() { return mData; } ProfileData* operator->() { return mData; }
std::mutex& getDataMutex() { return mJankDataMutex; }
private: private:
void freeData(); void freeData();
// By default this will use malloc memory. It may be moved later to ashmem // By default this will use malloc memory. It may be moved later to ashmem
// if there is shared space for it and a request comes in to do that. // if there is shared space for it and a request comes in to do that.
ProfileData* mData = new ProfileData; ProfileData* mData GUARDED_BY(mJankDataMutex);
bool mIsMapped = false; bool mIsMapped = false;
std::mutex& mJankDataMutex;
}; };
} /* namespace uirenderer */ } /* namespace uirenderer */

View File

@@ -178,12 +178,16 @@ void CanvasContext::setSurfaceControl(ASurfaceControl* surfaceControl) {
if (surfaceControl == mSurfaceControl) return; if (surfaceControl == mSurfaceControl) return;
auto funcs = mRenderThread.getASurfaceControlFunctions(); auto funcs = mRenderThread.getASurfaceControlFunctions();
if (mSurfaceControl != nullptr) { if (mSurfaceControl != nullptr) {
funcs.unregisterListenerFunc(this, &onSurfaceStatsAvailable);
funcs.releaseFunc(mSurfaceControl); funcs.releaseFunc(mSurfaceControl);
} }
mSurfaceControl = surfaceControl; mSurfaceControl = surfaceControl;
mExpectSurfaceStats = surfaceControl != nullptr;
if (mSurfaceControl != nullptr) { if (mSurfaceControl != nullptr) {
funcs.acquireFunc(mSurfaceControl); funcs.acquireFunc(mSurfaceControl);
funcs.registerListenerFunc(surfaceControl, this, &onSurfaceStatsAvailable);
} }
} }
@@ -332,8 +336,8 @@ void CanvasContext::prepareTree(TreeInfo& info, int64_t* uiFrameInfo, int64_t sy
// just keep using the previous frame's structure instead // just keep using the previous frame's structure instead
if (!wasSkipped(mCurrentFrameInfo)) { if (!wasSkipped(mCurrentFrameInfo)) {
mCurrentFrameInfo = mJankTracker.startFrame(); mCurrentFrameInfo = mJankTracker.startFrame();
mLast4FrameInfos.next().first = mCurrentFrameInfo;
} }
mCurrentFrameInfo->importUiThreadInfo(uiFrameInfo); mCurrentFrameInfo->importUiThreadInfo(uiFrameInfo);
mCurrentFrameInfo->set(FrameInfoIndex::SyncQueued) = syncQueued; mCurrentFrameInfo->set(FrameInfoIndex::SyncQueued) = syncQueued;
mCurrentFrameInfo->markSyncStart(); mCurrentFrameInfo->markSyncStart();
@@ -538,17 +542,14 @@ void CanvasContext::draw() {
} }
mCurrentFrameInfo->set(FrameInfoIndex::DequeueBufferDuration) = swap.dequeueDuration; mCurrentFrameInfo->set(FrameInfoIndex::DequeueBufferDuration) = swap.dequeueDuration;
mCurrentFrameInfo->set(FrameInfoIndex::QueueBufferDuration) = swap.queueDuration; mCurrentFrameInfo->set(FrameInfoIndex::QueueBufferDuration) = swap.queueDuration;
mLast4FrameInfos[-1].second = frameCompleteNr;
mHaveNewSurface = false; mHaveNewSurface = false;
mFrameNumber = -1; mFrameNumber = -1;
} else { } else {
mCurrentFrameInfo->set(FrameInfoIndex::DequeueBufferDuration) = 0; mCurrentFrameInfo->set(FrameInfoIndex::DequeueBufferDuration) = 0;
mCurrentFrameInfo->set(FrameInfoIndex::QueueBufferDuration) = 0; mCurrentFrameInfo->set(FrameInfoIndex::QueueBufferDuration) = 0;
mLast4FrameInfos[-1].second = -1;
} }
// TODO: Use a fence for real completion? mCurrentFrameInfo->markSwapBuffersCompleted();
mCurrentFrameInfo->markFrameCompleted();
#if LOG_FRAMETIME_MMA #if LOG_FRAMETIME_MMA
float thisFrame = mCurrentFrameInfo->duration(FrameInfoIndex::IssueDrawCommandsStart, float thisFrame = mCurrentFrameInfo->duration(FrameInfoIndex::IssueDrawCommandsStart,
@@ -572,30 +573,73 @@ void CanvasContext::draw() {
mFrameCompleteCallbacks.clear(); mFrameCompleteCallbacks.clear();
} }
mJankTracker.finishFrame(*mCurrentFrameInfo); if (requireSwap) {
if (CC_UNLIKELY(mFrameMetricsReporter.get() != nullptr)) { if (mExpectSurfaceStats) {
mFrameMetricsReporter->reportFrameMetrics(mCurrentFrameInfo->data()); std::lock_guard lock(mLast4FrameInfosMutex);
} std::pair<FrameInfo*, int64_t>& next = mLast4FrameInfos.next();
next.first = mCurrentFrameInfo;
if (mLast4FrameInfos.size() == mLast4FrameInfos.capacity()) { next.second = frameCompleteNr;
// By looking 4 frames back, we guarantee all SF stats are available. There are at } else {
// most 3 buffers in BufferQueue. Surface object keeps stats for the last 8 frames. mCurrentFrameInfo->markFrameCompleted();
FrameInfo* forthBehind = mLast4FrameInfos.front().first; mCurrentFrameInfo->set(FrameInfoIndex::GpuCompleted)
int64_t composedFrameId = mLast4FrameInfos.front().second; = mCurrentFrameInfo->get(FrameInfoIndex::FrameCompleted);
nsecs_t acquireTime = -1; finishFrame(mCurrentFrameInfo);
if (mNativeSurface) {
native_window_get_frame_timestamps(mNativeSurface->getNativeWindow(), composedFrameId,
nullptr, &acquireTime, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
} }
// Ignore default -1, NATIVE_WINDOW_TIMESTAMP_INVALID and NATIVE_WINDOW_TIMESTAMP_PENDING
forthBehind->set(FrameInfoIndex::GpuCompleted) = acquireTime > 0 ? acquireTime : -1;
mJankTracker.finishGpuDraw(*forthBehind);
} }
mRenderThread.cacheManager().onFrameCompleted(); mRenderThread.cacheManager().onFrameCompleted();
} }
void CanvasContext::finishFrame(FrameInfo* frameInfo) {
// TODO (b/169858044): Consolidate this into a single call.
mJankTracker.finishFrame(*frameInfo);
mJankTracker.finishGpuDraw(*frameInfo);
// TODO (b/169858044): Move this into JankTracker to adjust deadline when queue is
// double-stuffed.
if (CC_UNLIKELY(mFrameMetricsReporter.get() != nullptr)) {
mFrameMetricsReporter->reportFrameMetrics(frameInfo->data());
}
}
void CanvasContext::onSurfaceStatsAvailable(void* context, ASurfaceControl* control,
ASurfaceControlStats* stats) {
CanvasContext* instance = static_cast<CanvasContext*>(context);
const ASurfaceControlFunctions& functions =
instance->mRenderThread.getASurfaceControlFunctions();
nsecs_t gpuCompleteTime = functions.getAcquireTimeFunc(stats);
uint64_t frameNumber = functions.getFrameNumberFunc(stats);
FrameInfo* frameInfo = nullptr;
{
std::lock_guard(instance->mLast4FrameInfosMutex);
for (size_t i = 0; i < instance->mLast4FrameInfos.size(); i++) {
if (instance->mLast4FrameInfos[i].second == frameNumber) {
frameInfo = instance->mLast4FrameInfos[i].first;
break;
}
}
}
if (frameInfo != nullptr) {
if (gpuCompleteTime == -1) {
gpuCompleteTime = frameInfo->get(FrameInfoIndex::SwapBuffersCompleted);
}
if (gpuCompleteTime < frameInfo->get(FrameInfoIndex::SwapBuffers)) {
// TODO (b/180488606): Investigate why this can happen for first frames.
ALOGW("Impossible GPU complete time swapBuffers=%" PRIi64 " gpuComplete=%" PRIi64,
frameInfo->get(FrameInfoIndex::SwapBuffers), gpuCompleteTime);
gpuCompleteTime = frameInfo->get(FrameInfoIndex::SwapBuffersCompleted);
}
frameInfo->set(FrameInfoIndex::FrameCompleted) = gpuCompleteTime;
frameInfo->set(FrameInfoIndex::GpuCompleted) = gpuCompleteTime;
instance->finishFrame(frameInfo);
}
}
// Called by choreographer to do an RT-driven animation // Called by choreographer to do an RT-driven animation
void CanvasContext::doFrame() { void CanvasContext::doFrame() {
if (!mRenderPipeline->isSurfaceReady()) return; if (!mRenderPipeline->isSurfaceReady()) return;

View File

@@ -37,6 +37,7 @@
#include <SkSize.h> #include <SkSize.h>
#include <cutils/compiler.h> #include <cutils/compiler.h>
#include <utils/Functor.h> #include <utils/Functor.h>
#include <utils/Mutex.h>
#include <functional> #include <functional>
#include <future> #include <future>
@@ -196,6 +197,10 @@ public:
SkISize getNextFrameSize() const; SkISize getNextFrameSize() const;
// Called when SurfaceStats are available.
static void onSurfaceStatsAvailable(void* context, ASurfaceControl* control,
ASurfaceControlStats* stats);
private: private:
CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode, CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode,
IContextFactory* contextFactory, std::unique_ptr<IRenderPipeline> renderPipeline); IContextFactory* contextFactory, std::unique_ptr<IRenderPipeline> renderPipeline);
@@ -212,6 +217,7 @@ private:
void setupPipelineSurface(); void setupPipelineSurface();
SkRect computeDirtyRect(const Frame& frame, SkRect* dirty); SkRect computeDirtyRect(const Frame& frame, SkRect* dirty);
void finishFrame(FrameInfo* frameInfo);
// The same type as Frame.mWidth and Frame.mHeight // The same type as Frame.mWidth and Frame.mHeight
int32_t mLastFrameWidth = 0; int32_t mLastFrameWidth = 0;
@@ -261,7 +267,12 @@ private:
std::vector<sp<RenderNode>> mRenderNodes; std::vector<sp<RenderNode>> mRenderNodes;
FrameInfo* mCurrentFrameInfo = nullptr; FrameInfo* mCurrentFrameInfo = nullptr;
RingBuffer<std::pair<FrameInfo*, int64_t>, 4> mLast4FrameInfos;
// List of frames that are awaiting GPU completion reporting
RingBuffer<std::pair<FrameInfo*, int64_t>, 4> mLast4FrameInfos
GUARDED_BY(mLast4FrameInfosMutex);
std::mutex mLast4FrameInfosMutex;
std::string mName; std::string mName;
JankTracker mJankTracker; JankTracker mJankTracker;
FrameInfoVisualizer mProfiler; FrameInfoVisualizer mProfiler;
@@ -276,6 +287,9 @@ private:
std::unique_ptr<IRenderPipeline> mRenderPipeline; std::unique_ptr<IRenderPipeline> mRenderPipeline;
std::vector<std::function<void(int64_t)>> mFrameCompleteCallbacks; std::vector<std::function<void(int64_t)>> mFrameCompleteCallbacks;
// If set to true, we expect that callbacks into onSurfaceStatsAvailable
bool mExpectSurfaceStats = false;
}; };
} /* namespace renderthread */ } /* namespace renderthread */

View File

@@ -215,6 +215,7 @@ void RenderProxy::notifyFramePending() {
void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) { void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) {
mRenderThread.queue().runSync([&]() { mRenderThread.queue().runSync([&]() {
std::lock_guard lock(mRenderThread.getJankDataMutex());
mContext->profiler().dumpData(fd); mContext->profiler().dumpData(fd);
if (dumpFlags & DumpFlags::FrameStats) { if (dumpFlags & DumpFlags::FrameStats) {
mContext->dumpFrames(fd); mContext->dumpFrames(fd);
@@ -234,6 +235,7 @@ void RenderProxy::resetProfileInfo() {
uint32_t RenderProxy::frameTimePercentile(int percentile) { uint32_t RenderProxy::frameTimePercentile(int percentile) {
return mRenderThread.queue().runSync([&]() -> auto { return mRenderThread.queue().runSync([&]() -> auto {
std::lock_guard lock(mRenderThread.globalProfileData().getDataMutex());
return mRenderThread.globalProfileData()->findPercentile(percentile); return mRenderThread.globalProfileData()->findPercentile(percentile);
}); });
} }

View File

@@ -59,6 +59,26 @@ ASurfaceControlFunctions::ASurfaceControlFunctions() {
releaseFunc = (ASC_release) dlsym(handle_, "ASurfaceControl_release"); releaseFunc = (ASC_release) dlsym(handle_, "ASurfaceControl_release");
LOG_ALWAYS_FATAL_IF(releaseFunc == nullptr, LOG_ALWAYS_FATAL_IF(releaseFunc == nullptr,
"Failed to find required symbol ASurfaceControl_release!"); "Failed to find required symbol ASurfaceControl_release!");
registerListenerFunc = (ASC_registerSurfaceStatsListener) dlsym(handle_,
"ASurfaceControl_registerSurfaceStatsListener");
LOG_ALWAYS_FATAL_IF(registerListenerFunc == nullptr,
"Failed to find required symbol ASurfaceControl_registerSurfaceStatsListener!");
unregisterListenerFunc = (ASC_unregisterSurfaceStatsListener) dlsym(handle_,
"ASurfaceControl_unregisterSurfaceStatsListener");
LOG_ALWAYS_FATAL_IF(unregisterListenerFunc == nullptr,
"Failed to find required symbol ASurfaceControl_unregisterSurfaceStatsListener!");
getAcquireTimeFunc = (ASCStats_getAcquireTime) dlsym(handle_,
"ASurfaceControlStats_getAcquireTime");
LOG_ALWAYS_FATAL_IF(getAcquireTimeFunc == nullptr,
"Failed to find required symbol ASurfaceControlStats_getAcquireTime!");
getFrameNumberFunc = (ASCStats_getFrameNumber) dlsym(handle_,
"ASurfaceControlStats_getFrameNumber");
LOG_ALWAYS_FATAL_IF(getFrameNumberFunc == nullptr,
"Failed to find required symbol ASurfaceControlStats_getFrameNumber!");
} }
void RenderThread::frameCallback(int64_t frameTimeNanos, void* data) { void RenderThread::frameCallback(int64_t frameTimeNanos, void* data) {
@@ -146,7 +166,8 @@ RenderThread::RenderThread()
, mFrameCallbackTaskPending(false) , mFrameCallbackTaskPending(false)
, mRenderState(nullptr) , mRenderState(nullptr)
, mEglManager(nullptr) , mEglManager(nullptr)
, mFunctorManager(WebViewFunctorManager::instance()) { , mFunctorManager(WebViewFunctorManager::instance())
, mGlobalProfileData(mJankDataMutex) {
Properties::load(); Properties::load();
start("RenderThread"); start("RenderThread");
} }

View File

@@ -17,6 +17,7 @@
#ifndef RENDERTHREAD_H_ #ifndef RENDERTHREAD_H_
#define RENDERTHREAD_H_ #define RENDERTHREAD_H_
#include <surface_control_private.h>
#include <GrDirectContext.h> #include <GrDirectContext.h>
#include <SkBitmap.h> #include <SkBitmap.h>
#include <cutils/compiler.h> #include <cutils/compiler.h>
@@ -81,11 +82,22 @@ struct VsyncSource {
typedef void (*ASC_acquire)(ASurfaceControl* control); typedef void (*ASC_acquire)(ASurfaceControl* control);
typedef void (*ASC_release)(ASurfaceControl* control); typedef void (*ASC_release)(ASurfaceControl* control);
typedef void (*ASC_registerSurfaceStatsListener)(ASurfaceControl* control, void* context,
ASurfaceControl_SurfaceStatsListener func);
typedef void (*ASC_unregisterSurfaceStatsListener)(void* context,
ASurfaceControl_SurfaceStatsListener func);
typedef int64_t (*ASCStats_getAcquireTime)(ASurfaceControlStats* stats);
typedef uint64_t (*ASCStats_getFrameNumber)(ASurfaceControlStats* stats);
struct ASurfaceControlFunctions { struct ASurfaceControlFunctions {
ASurfaceControlFunctions(); ASurfaceControlFunctions();
ASC_acquire acquireFunc; ASC_acquire acquireFunc;
ASC_release releaseFunc; ASC_release releaseFunc;
ASC_registerSurfaceStatsListener registerListenerFunc;
ASC_unregisterSurfaceStatsListener unregisterListenerFunc;
ASCStats_getAcquireTime getAcquireTimeFunc;
ASCStats_getFrameNumber getFrameNumberFunc;
}; };
class ChoreographerSource; class ChoreographerSource;
@@ -114,6 +126,7 @@ public:
RenderState& renderState() const { return *mRenderState; } RenderState& renderState() const { return *mRenderState; }
EglManager& eglManager() const { return *mEglManager; } EglManager& eglManager() const { return *mEglManager; }
ProfileDataContainer& globalProfileData() { return mGlobalProfileData; } ProfileDataContainer& globalProfileData() { return mGlobalProfileData; }
std::mutex& getJankDataMutex() { return mJankDataMutex; }
Readback& readback(); Readback& readback();
GrDirectContext* getGrContext() const { return mGrContext.get(); } GrDirectContext* getGrContext() const { return mGrContext.get(); }
@@ -205,6 +218,7 @@ private:
sp<VulkanManager> mVkManager; sp<VulkanManager> mVkManager;
ASurfaceControlFunctions mASurfaceControlFunctions; ASurfaceControlFunctions mASurfaceControlFunctions;
std::mutex mJankDataMutex;
}; };
} /* namespace renderthread */ } /* namespace renderthread */

View File

@@ -88,7 +88,7 @@ cc_library_shared {
"libarect", "libarect",
], ],
header_libs: [ "libhwui_internal_headers",], header_libs: [ "libhwui_internal_headers", "libandroid_headers_private"],
whole_static_libs: ["libnativewindow"], whole_static_libs: ["libnativewindow"],

View File

@@ -300,3 +300,13 @@ LIBANDROID {
local: local:
*; *;
}; };
LIBANDROID_PLATFORM {
global:
extern "C++" {
ASurfaceControl_registerSurfaceStatsListener*;
ASurfaceControl_unregisterSurfaceStatsListener*;
ASurfaceControlStats_getAcquireTime*;
ASurfaceControlStats_getFrameNumber*;
};
} LIBANDROID;

View File

@@ -17,6 +17,7 @@
#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h> #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
#include <android/native_window.h> #include <android/native_window.h>
#include <android/surface_control.h> #include <android/surface_control.h>
#include <surface_control_private.h>
#include <configstore/Utils.h> #include <configstore/Utils.h>
@@ -197,6 +198,48 @@ void ASurfaceControl_release(ASurfaceControl* aSurfaceControl) {
SurfaceControl_release(surfaceControl); SurfaceControl_release(surfaceControl);
} }
struct ASurfaceControlStats {
int64_t acquireTime;
sp<Fence> previousReleaseFence;
uint64_t frameNumber;
};
void ASurfaceControl_registerSurfaceStatsListener(ASurfaceControl* control, void* context,
ASurfaceControl_SurfaceStatsListener func) {
SurfaceStatsCallback callback = [func](void* callback_context,
nsecs_t,
const sp<Fence>&,
const SurfaceStats& surfaceStats) {
ASurfaceControlStats aSurfaceControlStats;
ASurfaceControl* aSurfaceControl =
reinterpret_cast<ASurfaceControl*>(surfaceStats.surfaceControl.get());
aSurfaceControlStats.acquireTime = surfaceStats.acquireTime;
aSurfaceControlStats.previousReleaseFence = surfaceStats.previousReleaseFence;
aSurfaceControlStats.frameNumber = surfaceStats.eventStats.frameNumber;
(*func)(callback_context, aSurfaceControl, &aSurfaceControlStats);
};
TransactionCompletedListener::getInstance()->addSurfaceStatsListener(context,
reinterpret_cast<void*>(func), ASurfaceControl_to_SurfaceControl(control), callback);
}
void ASurfaceControl_unregisterSurfaceStatsListener(void* context,
ASurfaceControl_SurfaceStatsListener func) {
TransactionCompletedListener::getInstance()->removeSurfaceStatsListener(context,
reinterpret_cast<void*>(func));
}
int64_t ASurfaceControlStats_getAcquireTime(ASurfaceControlStats* stats) {
return stats->acquireTime;
}
uint64_t ASurfaceControlStats_getFrameNumber(ASurfaceControlStats* stats) {
return stats->frameNumber;
}
ASurfaceTransaction* ASurfaceTransaction_create() { ASurfaceTransaction* ASurfaceTransaction_create() {
Transaction* transaction = new Transaction; Transaction* transaction = new Transaction;
return reinterpret_cast<ASurfaceTransaction*>(transaction); return reinterpret_cast<ASurfaceTransaction*>(transaction);
@@ -215,11 +258,6 @@ void ASurfaceTransaction_apply(ASurfaceTransaction* aSurfaceTransaction) {
transaction->apply(); transaction->apply();
} }
typedef struct ASurfaceControlStats {
int64_t acquireTime;
sp<Fence> previousReleaseFence;
} ASurfaceControlStats;
struct ASurfaceTransactionStats { struct ASurfaceTransactionStats {
std::unordered_map<ASurfaceControl*, ASurfaceControlStats> aSurfaceControlStats; std::unordered_map<ASurfaceControl*, ASurfaceControlStats> aSurfaceControlStats;
int64_t latchTime; int64_t latchTime;