Capture MSKPs from RE more reliably

Bug: 187078781
Test: capture an MSKP

The old method relied on checking the file size of the created file and
assuming it was finished when it stopped growing for a certain amount of
time. This lead to pulling the file early, and possibly deleting it
while the device is still attempting to write to it.

Instead, add an extra debugging system property that represents the name
of the latest MSKP captured. Set it to "" when beginning recording. When
it is no longer "", this means that the file has been written. adb pull
that file.

Also print the name of the file in logcat.

Defensively lock mMutex during setupMultiFrameCapture(), which modifies
variables that may be modified by another thread.

In record.sh rootandsetup, set the backend to "skiaglthreaded". This is
the version we're planning to ship (and is already the default), and
"skiagl" doesn't seem to boot currently.

Remove the timeout waiting for the file to be written. Writing the file
can take a very long time, and it's more useful to get a file than to
prevent a long hang.

If the phone doesn't have the right permissions to write files to disk,
write a helpful error message and exit.

Change-Id: I39c08bee5e7f47fc0fbe1475c872fd755935c90b
diff --git a/libs/renderengine/skia/debug/SkiaCapture.cpp b/libs/renderengine/skia/debug/SkiaCapture.cpp
index 40f5cf2..856fff4 100644
--- a/libs/renderengine/skia/debug/SkiaCapture.cpp
+++ b/libs/renderengine/skia/debug/SkiaCapture.cpp
@@ -34,7 +34,7 @@
 namespace skia {
 
 // The root of the filename to write a recorded SKP to. In order for this file to
-// be written to /data/user/, user must run 'adb shell setenforce 0' in the device.
+// be written to /data/user/, user must run 'adb shell setenforce 0' on the device.
 static const std::string CAPTURED_FILENAME_BASE = "/data/user/re_skiacapture";
 
 SkiaCapture::~SkiaCapture() {
@@ -152,11 +152,12 @@
     // a smart pointer makes the lambda non-copyable. The lambda is only called
     // once, so this is safe.
     SkFILEWStream* stream = mOpenMultiPicStream.release();
-    CommonPool::post([doc = std::move(mMultiPic), stream] {
+    CommonPool::post([doc = std::move(mMultiPic), stream, name = std::move(mCaptureFile)] {
         ALOGD("Finalizing multi frame SKP");
         doc->close();
         delete stream;
-        ALOGD("Multi frame SKP complete.");
+        ALOGD("Multi frame SKP saved to %s.", name.c_str());
+        base::SetProperty(PROPERTY_DEBUG_RENDERENGINE_CAPTURE_FILENAME, name);
     });
     mCaptureRunning = false;
 }
@@ -164,12 +165,14 @@
 bool SkiaCapture::setupMultiFrameCapture() {
     ATRACE_CALL();
     ALOGD("Set up multi-frame capture, ms = %llu", mTimerInterval.count());
+    base::SetProperty(PROPERTY_DEBUG_RENDERENGINE_CAPTURE_FILENAME, "");
+    const std::scoped_lock lock(mMutex);
 
-    std::string captureFile;
     // Attach a timestamp to the file.
-    base::StringAppendF(&captureFile, "%s_%lld.mskp", CAPTURED_FILENAME_BASE.c_str(),
+    mCaptureFile.clear();
+    base::StringAppendF(&mCaptureFile, "%s_%lld.mskp", CAPTURED_FILENAME_BASE.c_str(),
                         std::chrono::steady_clock::now().time_since_epoch().count());
-    auto stream = std::make_unique<SkFILEWStream>(captureFile.c_str());
+    auto stream = std::make_unique<SkFILEWStream>(mCaptureFile.c_str());
     // We own this stream and need to hold it until close() finishes.
     if (stream->isValid()) {
         mOpenMultiPicStream = std::move(stream);
@@ -194,7 +197,7 @@
         mCaptureRunning = true;
         return true;
     } else {
-        ALOGE("Could not open \"%s\" for writing.", captureFile.c_str());
+        ALOGE("Could not open \"%s\" for writing.", mCaptureFile.c_str());
         return false;
     }
 }