| 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> | 
 | 21 | #include <SkSurface.h> | 
 | 22 | #include <chrono> | 
 | 23 | #include "CaptureTimer.h" | 
 | 24 | #include "tools/SkSharingProc.h" | 
 | 25 |  | 
 | 26 | namespace android { | 
 | 27 | namespace renderengine { | 
 | 28 | namespace skia { | 
 | 29 |  | 
 | 30 | using namespace std::chrono_literals; | 
 | 31 |  | 
 | 32 | /** | 
 | 33 |  * Class that captures frames that are sent to Skia in Render Engine. It sets up | 
 | 34 |  * a multi frame capture and writes it into a file on the device. The capture is | 
 | 35 |  * done based on a timer. | 
 | 36 |  */ | 
 | 37 | class SkiaCapture { | 
 | 38 |     using Interval = std::chrono::milliseconds; | 
 | 39 |  | 
 | 40 | public: | 
 | 41 |     SkiaCapture() {} | 
 | 42 |     virtual ~SkiaCapture(); | 
 | 43 |     // Called every frame. Normally returns early with screen canvas. | 
 | 44 |     // But when capture is enabled, returns an nwaycanvas where commands are also recorded. | 
 | 45 |     SkCanvas* tryCapture(SkSurface* surface); | 
 | 46 |     // Called at the end of every frame. | 
 | 47 |     void endCapture(); | 
 | 48 |  | 
 | 49 | private: | 
 | 50 |     // Performs the first-frame work of a multi frame SKP capture. Returns true if successful. | 
 | 51 |     bool setupMultiFrameCapture(); | 
 | 52 |  | 
 | 53 |     // Closes the recording and serializes sequence to a file. | 
 | 54 |     void writeToFile(); | 
 | 55 |  | 
 | 56 |     // Multi frame serialization stream and writer used when serializing more than one frame. | 
 | 57 |     std::unique_ptr<SkFILEWStream> mOpenMultiPicStream; | 
 | 58 |     sk_sp<SkDocument> mMultiPic; | 
 | 59 |     std::unique_ptr<SkSharingSerialContext> mSerialContext; | 
 | 60 |     std::unique_ptr<SkNWayCanvas> mNwayCanvas; | 
 | 61 |  | 
 | 62 |     // Capturing and interval control. | 
 | 63 |     bool mCaptureRunning = false; | 
 | 64 |     CaptureTimer mTimer; | 
 | 65 |     Interval mTimerInterval = 0ms; | 
 | 66 | }; | 
 | 67 |  | 
 | 68 | } // namespace skia | 
 | 69 | } // namespace renderengine | 
 | 70 | } // namespace android |