• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "skia_recording.h"
17 
18 #include <iostream>
19 
20 #include "parameters.h"
21 
22 namespace OHOS::Rosen {
GetCaptureEnabled() const23 bool SkiaRecording::GetCaptureEnabled() const
24 {
25     return captureEnabled_;
26 }
27 
InitConfigsFromParam()28 void SkiaRecording::InitConfigsFromParam()
29 {
30     captureEnabled_ = std::stoi(system::GetParameter("skpcapture.enabled", "0"));
31     if (captureEnabled_) {
32         captureFrameNum_ = std::stoi(system::GetParameter("skpcapture.frameNum", "0"));
33         if (captureFrameNum_ < 1) {
34             captureMode_ = SkiaCaptureMode::NONE;
35         } else if (captureFrameNum_ == 1) {
36             captureMode_ = SkiaCaptureMode::SINGLE_FRAME;
37         } else {
38             captureMode_ = SkiaCaptureMode::MULTI_FRAME;
39         }
40         captureFileName_ = system::GetParameter("skpcapture.path", "");
41         std::string fileExtension = ".skp";
42         if (captureFileName_.rfind(fileExtension) == (captureFileName_.size() - fileExtension.size())) {
43             captureMode_ = SkiaCaptureMode::SINGLE_FRAME;
44         }
45     }
46 }
47 
SetupMultiFrame()48 bool SkiaRecording::SetupMultiFrame()
49 {
50     std::cout<< "Set up multi-frame capture, the frame number is " << captureFrameNum_ << std::endl;
51     auto stream = std::make_unique<SkFILEWStream>(captureFileName_.c_str());
52     if (!stream->isValid()) {
53         std::cout<< "Could not open " << captureFileName_ << " for writing." << std::endl;
54         captureFrameNum_ = 0;
55         captureMode_ = SkiaCaptureMode::NONE;
56         return false;
57     }
58     openMultiPicStream_ = std::move(stream);
59     serialContext_ = std::make_unique<SkSharingSerialContext>();
60     SkSerialProcs procs;
61     procs.fImageProc = SkSharingSerialContext::serializeImage;
62     procs.fImageCtx = serialContext_.get();
63     procs.fTypefaceProc = [](SkTypeface* tf, void* ctx) {
64         return tf->serialize(SkTypeface::SerializeBehavior::kDoIncludeData);
65     };
66     // SkDocuments don't take owership of the streams they write.
67     // we need to keep it until after multiPic_.close()
68     // procs is passed as a pointer, but just as a method of having an optional default.
69     // procs doesn't need to outlive this Make call.
70     multiPic_ = SkMakeMultiPictureDocument(openMultiPicStream_.get(), &procs,
71         [sharingCtx = serialContext_.get()](const SkPicture* pic) {
72         SkSharingSerialContext::collectNonTextureImagesFromPicture(pic, sharingCtx);
73     });
74     return true;
75 }
76 
BeginCapture(SkCanvas * canvas,int width,int height)77 SkCanvas* SkiaRecording::BeginCapture(SkCanvas* canvas, int width, int height)
78 {
79     if (canvas == nullptr) {
80         return nullptr;
81     }
82     static bool isFirstFrame = true;
83     if (isFirstFrame && captureMode_ == SkiaCaptureMode::MULTI_FRAME) {
84         isFirstFrame = false;
85         if (!SetupMultiFrame()) {
86             return nullptr;
87         }
88     }
89     // Create a canvas pointer, fill it depending on what kind of capture is requested (if any)
90     switch (captureMode_) {
91         case SkiaCaptureMode::SINGLE_FRAME:
92             recorder_ = std::make_unique<SkPictureRecorder>();
93             pictureCanvas_ = recorder_->beginRecording(width, height);
94             break;
95         case SkiaCaptureMode::MULTI_FRAME:
96             // If a multi frame recording is active, initialize recording for a single frame of a
97             // multi frame file.
98             pictureCanvas_ = multiPic_->beginPage(width, height);
99             break;
100         case SkiaCaptureMode::NONE:
101             // Returning here in the non-capture case means we can count on pictureCanvas being
102             // non-null below.
103             return nullptr;
104     }
105 
106     // Setting up an nway canvas is common to any kind of capture.
107     if (nwayCanvas_ == nullptr) {
108         nwayCanvas_ = std::make_unique<SkNWayCanvas>(width, height);
109     }
110     nwayCanvas_->addCanvas(canvas);
111     nwayCanvas_->addCanvas(pictureCanvas_);
112 
113     return nwayCanvas_.get();
114 }
115 
EndCapture()116 void SkiaRecording::EndCapture()
117 {
118     if (captureMode_ == SkiaCaptureMode::NONE) {
119         return;
120     }
121     nwayCanvas_->removeAll();
122 
123     if (captureFrameNum_ > 0 && captureMode_ == SkiaCaptureMode::MULTI_FRAME) {
124         if (!multiPic_) {
125             std::cout << "multiPic_ is nullptr" << std::endl;
126             return;
127         }
128         multiPic_->endPage();
129         captureFrameNum_--;
130         if (captureFrameNum_ == 0) {
131             captureEnabled_ = false;
132             captureMode_ = SkiaCaptureMode::NONE;
133             // Pass multiPic_ and openMultiPicStream_ to a background thread, which will handle
134             // the heavyweight serialization work and destroy them. openMultiPicStream_ is released
135             // to a bare pointer because keeping it in a smart pointer makes the lambda
136             // non-copyable. The lambda is only called once, so this is safe.
137             SkFILEWStream* stream = openMultiPicStream_.release();
138             multiPic_->close();
139             delete stream;
140             std::cout << "Multi frame SKP complete." << std::endl;
141         }
142     } else if (!recorder_) {
143         std::cout << "recorder_ is nullptr" << std::endl;
144     } else {
145         sk_sp<SkPicture> picture = recorder_->finishRecordingAsPicture();
146         if (picture->approximateOpCount() > 0) {
147             // single frame skp to file
148             SkSerialProcs procs;
149             procs.fTypefaceProc = [](SkTypeface* tf, void* ctx) {
150                 return tf->serialize(SkTypeface::SerializeBehavior::kDoIncludeData);
151             };
152             auto data = picture->serialize(&procs);
153             static int tmpId = 0;
154             std::string fileName = captureFileName_;
155             std::string fileExtension = ".skp";
156             if (captureFileName_.rfind(fileExtension) == (captureFileName_.size() - fileExtension.size())) {
157                 fileName.insert(fileName.size() - fileExtension.size(), "_" + std::to_string(tmpId++));
158             } else {
159                 fileName = fileName + "_" + std::to_string(tmpId++) + fileExtension;
160             }
161             SavePicture(data, fileName);
162             if (--captureFrameNum_ == 0) {
163                 captureMode_ = SkiaCaptureMode::NONE;
164             }
165         }
166         recorder_.reset();
167     }
168 }
169 
SavePicture(const sk_sp<SkData> & data,const std::string & filename)170 void SkiaRecording::SavePicture(const sk_sp<SkData>& data, const std::string& filename)
171 {
172     SkFILEWStream stream(filename.c_str());
173     if (stream.isValid()) {
174         stream.write(data->data(), data->size());
175         stream.flush();
176         std::cout << "SKP Captured Drawing Output (" << stream.bytesWritten() << " bytes) for frame. " <<
177             filename << std::endl;
178     } else {
179         std::cout << "Save picture failed. " << filename << " is not valid for frame." << filename << std::endl;
180     }
181 }
182 }