Ana Krulec | 70d15b1b | 2020-12-01 10:05:15 -0800 | [diff] [blame] | 1 | /* |
| 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 Sollenberger | 76664d6 | 2021-02-04 11:13:09 -0500 | [diff] [blame] | 21 | #include <SkPictureRecorder.h> |
Kevin Lubick | 8e0ef68 | 2022-03-04 10:34:35 -0500 | [diff] [blame^] | 22 | #include <SkRefCnt.h> |
| 23 | #include <SkStream.h> |
Ana Krulec | 70d15b1b | 2020-12-01 10:05:15 -0800 | [diff] [blame] | 24 | #include <SkSurface.h> |
Kevin Lubick | 8e0ef68 | 2022-03-04 10:34:35 -0500 | [diff] [blame^] | 25 | #include "tools/SkSharingProc.h" |
Derek Sollenberger | 76664d6 | 2021-02-04 11:13:09 -0500 | [diff] [blame] | 26 | |
Ana Krulec | 70d15b1b | 2020-12-01 10:05:15 -0800 | [diff] [blame] | 27 | #include <chrono> |
Derek Sollenberger | 76664d6 | 2021-02-04 11:13:09 -0500 | [diff] [blame] | 28 | #include <mutex> |
| 29 | |
Ana Krulec | 70d15b1b | 2020-12-01 10:05:15 -0800 | [diff] [blame] | 30 | #include "CaptureTimer.h" |
Ana Krulec | 70d15b1b | 2020-12-01 10:05:15 -0800 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | namespace renderengine { |
| 34 | namespace skia { |
| 35 | |
| 36 | using 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 | */ |
| 43 | class SkiaCapture { |
| 44 | using Interval = std::chrono::milliseconds; |
| 45 | |
| 46 | public: |
| 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 Krulec | 6eab17a | 2020-12-09 15:52:36 -0800 | [diff] [blame] | 54 | // Returns whether the capture is running. |
| 55 | bool isCaptureRunning() { return mCaptureRunning; } |
Ana Krulec | 70d15b1b | 2020-12-01 10:05:15 -0800 | [diff] [blame] | 56 | |
Derek Sollenberger | 76664d6 | 2021-02-04 11:13:09 -0500 | [diff] [blame] | 57 | // 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 Krulec | 70d15b1b | 2020-12-01 10:05:15 -0800 | [diff] [blame] | 67 | private: |
| 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 Kaiser | e5f8426 | 2021-02-16 06:57:36 -0800 | [diff] [blame] | 80 | SkCanvas* mCurrentPageCanvas = nullptr; |
Derek Sollenberger | 76664d6 | 2021-02-04 11:13:09 -0500 | [diff] [blame] | 81 | |
Ana Krulec | 70d15b1b | 2020-12-01 10:05:15 -0800 | [diff] [blame] | 82 | // Capturing and interval control. |
| 83 | bool mCaptureRunning = false; |
| 84 | CaptureTimer mTimer; |
| 85 | Interval mTimerInterval = 0ms; |
Derek Sollenberger | 76664d6 | 2021-02-04 11:13:09 -0500 | [diff] [blame] | 86 | |
| 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 III | 2eca03f | 2021-05-25 15:30:43 -0400 | [diff] [blame] | 90 | |
| 91 | std::string mCaptureFile; |
Ana Krulec | 70d15b1b | 2020-12-01 10:05:15 -0800 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | } // namespace skia |
| 95 | } // namespace renderengine |
| 96 | } // namespace android |