blob: d65a579916f8fbd27f5a2b90e7305b8b7b19714c [file] [log] [blame]
Ana Krulec70d15b1b2020-12-01 10:05:15 -08001/*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <SkDocument.h>
20#include <SkNWayCanvas.h>
Derek Sollenberger76664d62021-02-04 11:13:09 -050021#include <SkPictureRecorder.h>
Kevin Lubick8e0ef682022-03-04 10:34:35 -050022#include <SkRefCnt.h>
23#include <SkStream.h>
Ana Krulec70d15b1b2020-12-01 10:05:15 -080024#include <SkSurface.h>
Kevin Lubick8e0ef682022-03-04 10:34:35 -050025#include "tools/SkSharingProc.h"
Derek Sollenberger76664d62021-02-04 11:13:09 -050026
Ana Krulec70d15b1b2020-12-01 10:05:15 -080027#include <chrono>
Derek Sollenberger76664d62021-02-04 11:13:09 -050028#include <mutex>
29
Ana Krulec70d15b1b2020-12-01 10:05:15 -080030#include "CaptureTimer.h"
Ana Krulec70d15b1b2020-12-01 10:05:15 -080031
32namespace android {
33namespace renderengine {
34namespace skia {
35
36using namespace std::chrono_literals;
37
38/**
39 * Class that captures frames that are sent to Skia in Render Engine. It sets up
40 * a multi frame capture and writes it into a file on the device. The capture is
41 * done based on a timer.
42 */
43class SkiaCapture {
44 using Interval = std::chrono::milliseconds;
45
46public:
47 SkiaCapture() {}
48 virtual ~SkiaCapture();
49 // Called every frame. Normally returns early with screen canvas.
50 // But when capture is enabled, returns an nwaycanvas where commands are also recorded.
51 SkCanvas* tryCapture(SkSurface* surface);
52 // Called at the end of every frame.
53 void endCapture();
Ana Krulec6eab17a2020-12-09 15:52:36 -080054 // Returns whether the capture is running.
55 bool isCaptureRunning() { return mCaptureRunning; }
Ana Krulec70d15b1b2020-12-01 10:05:15 -080056
Derek Sollenberger76664d62021-02-04 11:13:09 -050057 // Offscreen state member variables are private to SkiaCapture, but the allocation
58 // and lifetime is managed by the caller. This enables nested offscreen
59 // captures to occur.
60 struct OffscreenState {
61 std::unique_ptr<SkPictureRecorder> offscreenRecorder;
62 std::unique_ptr<SkNWayCanvas> offscreenCanvas;
63 };
64 SkCanvas* tryOffscreenCapture(SkSurface* surface, OffscreenState* state);
65 uint64_t endOffscreenCapture(OffscreenState* state);
66
Ana Krulec70d15b1b2020-12-01 10:05:15 -080067private:
68 // Performs the first-frame work of a multi frame SKP capture. Returns true if successful.
69 bool setupMultiFrameCapture();
70
71 // Closes the recording and serializes sequence to a file.
72 void writeToFile();
73
74 // Multi frame serialization stream and writer used when serializing more than one frame.
75 std::unique_ptr<SkFILEWStream> mOpenMultiPicStream;
76 sk_sp<SkDocument> mMultiPic;
77 std::unique_ptr<SkSharingSerialContext> mSerialContext;
78 std::unique_ptr<SkNWayCanvas> mNwayCanvas;
79
Greg Kaisere5f84262021-02-16 06:57:36 -080080 SkCanvas* mCurrentPageCanvas = nullptr;
Derek Sollenberger76664d62021-02-04 11:13:09 -050081
Ana Krulec70d15b1b2020-12-01 10:05:15 -080082 // Capturing and interval control.
83 bool mCaptureRunning = false;
84 CaptureTimer mTimer;
85 Interval mTimerInterval = 0ms;
Derek Sollenberger76664d62021-02-04 11:13:09 -050086
87 // Mutex to ensure that a frame in progress when the timer fires is allowed to run to
88 // completion before we write the file to disk.
89 std::mutex mMutex;
Leon Scroggins III2eca03f2021-05-25 15:30:43 -040090
91 std::string mCaptureFile;
Ana Krulec70d15b1b2020-12-01 10:05:15 -080092};
93
94} // namespace skia
95} // namespace renderengine
96} // namespace android