blob: dc669a5eca73ba3b3345c035924134418079db19 [file] [log] [blame]
Stan Iliev500a0c32016-10-26 10:30:09 -04001/*
2 * Copyright (C) 2016 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
Jerome Gaillardbea67ce2024-03-28 14:21:16 +000017#include "pipeline/skia/SkiaPipeline.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040018
Kevin Lubick1175dc02022-02-28 12:41:27 -050019#include <SkCanvas.h>
20#include <SkColor.h>
21#include <SkColorSpace.h>
22#include <SkData.h>
23#include <SkImage.h>
Kevin Lubick93671082023-03-13 18:30:55 +000024#include <SkImageAndroid.h>
Peiyong Lin3bff1352018-12-11 07:56:07 -080025#include <SkImageInfo.h>
Kevin Lubick1175dc02022-02-28 12:41:27 -050026#include <SkMatrix.h>
Matt Sarettf58cc922016-11-14 18:33:38 -050027#include <SkOverdrawCanvas.h>
28#include <SkOverdrawColorFilter.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040029#include <SkPicture.h>
30#include <SkPictureRecorder.h>
Kevin Lubick1175dc02022-02-28 12:41:27 -050031#include <SkRect.h>
32#include <SkRefCnt.h>
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040033#include <SkSerialProcs.h>
Kevin Lubick1175dc02022-02-28 12:41:27 -050034#include <SkStream.h>
35#include <SkString.h>
Alec Mouri43fe6fc2019-12-23 07:46:19 -080036#include <SkTypeface.h>
37#include <android-base/properties.h>
John Reck29b1ee02023-04-04 17:44:23 -040038#include <gui/TraceUtils.h>
Jerome Gaillardbea67ce2024-03-28 14:21:16 +000039#include <include/android/SkSurfaceAndroid.h>
Kevin Lubicka63d8092024-04-12 17:43:17 +000040#include <include/docs/SkMultiPictureDocument.h>
Jerome Gaillardbea67ce2024-03-28 14:21:16 +000041#include <include/encode/SkPngEncoder.h>
42#include <include/gpu/ganesh/SkSurfaceGanesh.h>
Alec Mouri43fe6fc2019-12-23 07:46:19 -080043#include <unistd.h>
44
45#include <sstream>
46
Fedor Kudasov90df0562019-06-19 11:41:34 +010047#include "LightingInfo.h"
Stan Iliev23c38a92017-03-23 00:12:50 -040048#include "VectorDrawable.h"
John Reck29b1ee02023-04-04 17:44:23 -040049#include "include/gpu/GpuTypes.h" // from Skia
John Reck322b8ab2019-03-14 13:15:28 -070050#include "thread/CommonPool.h"
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040051#include "tools/SkSharingProc.h"
John Reckb36bfdd2020-07-23 13:47:49 -070052#include "utils/Color.h"
Nathaniel Nifong2945bff2019-11-25 09:34:21 -050053#include "utils/String8.h"
Jerome Gaillarda02a12d2019-05-28 18:07:56 +010054
Stan Iliev500a0c32016-10-26 10:30:09 -040055using namespace android::uirenderer::renderthread;
56
57namespace android {
58namespace uirenderer {
59namespace skiapipeline {
60
John Reck1bcacfd2017-11-03 10:12:19 -070061SkiaPipeline::SkiaPipeline(RenderThread& thread) : mRenderThread(thread) {
Derek Sollenberger1863d942020-02-05 15:41:51 -050062 setSurfaceColorProperties(mColorMode);
Stan Iliev23c38a92017-03-23 00:12:50 -040063}
Stan Iliev500a0c32016-10-26 10:30:09 -040064
Jerome Gaillardbea67ce2024-03-28 14:21:16 +000065SkiaPipeline::~SkiaPipeline() {}
Stan Iliev232f3622017-08-23 17:15:09 -040066
Stan Iliev500a0c32016-10-26 10:30:09 -040067void SkiaPipeline::onDestroyHardwareResources() {
Derek Sollenberger92a9eb92018-04-12 13:42:19 -040068 unpinImages();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040069 mRenderThread.cacheManager().trimStaleResources();
Stan Iliev500a0c32016-10-26 10:30:09 -040070}
71
John Reckd9d7f122018-05-03 14:40:56 -070072void SkiaPipeline::renderLayers(const LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070073 LayerUpdateQueue* layerUpdateQueue, bool opaque,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070074 const LightInfo& lightInfo) {
Fedor Kudasov90df0562019-06-19 11:41:34 +010075 LightingInfo::updateLighting(lightGeometry, lightInfo);
Stan Iliev500a0c32016-10-26 10:30:09 -040076 ATRACE_NAME("draw layers");
Peiyong Lin1f6aa122018-09-10 16:28:08 -070077 renderLayersImpl(*layerUpdateQueue, opaque);
Stan Iliev500a0c32016-10-26 10:30:09 -040078 layerUpdateQueue->clear();
79}
80
Jerome Gaillardbea67ce2024-03-28 14:21:16 +000081bool SkiaPipeline::renderLayerImpl(RenderNode* layerNode, const Rect& layerDamage) {
82 SkASSERT(layerNode->getLayerSurface());
83 SkiaDisplayList* displayList = layerNode->getDisplayList().asSkiaDl();
84 if (!displayList || displayList->isEmpty()) {
85 ALOGE("%p drawLayers(%s) : missing drawable", layerNode, layerNode->getName());
86 return false;
Stan Iliev500a0c32016-10-26 10:30:09 -040087 }
Derek Sollenbergerf7df1842017-09-05 11:15:58 -040088
Jerome Gaillardbea67ce2024-03-28 14:21:16 +000089 SkCanvas* layerCanvas = layerNode->getLayerSurface()->getCanvas();
Jerome Gaillard09a38e42024-03-14 15:20:56 +000090
Jerome Gaillardbea67ce2024-03-28 14:21:16 +000091 int saveCount = layerCanvas->save();
92 SkASSERT(saveCount == 1);
Jerome Gaillard09a38e42024-03-14 15:20:56 +000093
Jerome Gaillardbea67ce2024-03-28 14:21:16 +000094 layerCanvas->androidFramework_setDeviceClipRestriction(layerDamage.toSkIRect());
Jerome Gaillard09a38e42024-03-14 15:20:56 +000095
Jerome Gaillardbea67ce2024-03-28 14:21:16 +000096 // TODO: put localized light center calculation and storage to a drawable related code.
97 // It does not seem right to store something localized in a global state
98 // fix here and in recordLayers
99 const Vector3 savedLightCenter(LightingInfo::getLightCenterRaw());
100 Vector3 transformedLightCenter(savedLightCenter);
101 // map current light center into RenderNode's coordinate space
102 layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(transformedLightCenter);
103 LightingInfo::setLightCenterRaw(transformedLightCenter);
104
105 const RenderProperties& properties = layerNode->properties();
106 const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
107 if (properties.getClipToBounds() && layerCanvas->quickReject(bounds)) {
108 return false;
John Reckce966302024-03-25 19:28:29 +0000109 }
Jerome Gaillardbea67ce2024-03-28 14:21:16 +0000110
111 ATRACE_FORMAT("drawLayer [%s] %.1f x %.1f", layerNode->getName(), bounds.width(),
112 bounds.height());
113
114 layerNode->getSkiaLayer()->hasRenderedSinceRepaint = false;
115 layerCanvas->clear(SK_ColorTRANSPARENT);
116
117 RenderNodeDrawable root(layerNode, layerCanvas, false);
118 root.forceDraw(layerCanvas);
119 layerCanvas->restoreToCount(saveCount);
120
121 LightingInfo::setLightCenterRaw(savedLightCenter);
122 return true;
Stan Iliev500a0c32016-10-26 10:30:09 -0400123}
124
John Reck322b8ab2019-03-14 13:15:28 -0700125static void savePictureAsync(const sk_sp<SkData>& data, const std::string& filename) {
126 CommonPool::post([data, filename] {
127 if (0 == access(filename.c_str(), F_OK)) {
Stan Ilieve9d00122017-09-19 12:07:10 -0400128 return;
129 }
130
John Reck322b8ab2019-03-14 13:15:28 -0700131 SkFILEWStream stream(filename.c_str());
Stan Ilieve9d00122017-09-19 12:07:10 -0400132 if (stream.isValid()) {
John Reck322b8ab2019-03-14 13:15:28 -0700133 stream.write(data->data(), data->size());
Stan Ilieve9d00122017-09-19 12:07:10 -0400134 stream.flush();
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400135 ALOGD("SKP Captured Drawing Output (%zu bytes) for frame. %s", stream.bytesWritten(),
John Reck322b8ab2019-03-14 13:15:28 -0700136 filename.c_str());
Stan Ilieve9d00122017-09-19 12:07:10 -0400137 }
John Reck322b8ab2019-03-14 13:15:28 -0700138 });
139}
Stan Ilieve9d00122017-09-19 12:07:10 -0400140
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400141// Note multiple SkiaPipeline instances may be loaded if more than one app is visible.
142// Each instance may observe the filename changing and try to record to a file of the same name.
143// Only the first one will succeed. There is no scope available here where we could coordinate
144// to cause this function to return true for only one of the instances.
145bool SkiaPipeline::shouldStartNewFileCapture() {
146 // Don't start a new file based capture if one is currently ongoing.
147 if (mCaptureMode != CaptureMode::None) { return false; }
148
149 // A new capture is started when the filename property changes.
150 // Read the filename property.
151 std::string prop = base::GetProperty(PROPERTY_CAPTURE_SKP_FILENAME, "0");
152 // if the filename property changed to a valid value
153 if (prop[0] != '0' && mCapturedFile != prop) {
154 // remember this new filename
155 mCapturedFile = prop;
156 // and get a property indicating how many frames to capture.
157 mCaptureSequence = base::GetIntProperty(PROPERTY_CAPTURE_SKP_FRAMES, 1);
John Reck5cca8f22018-12-10 17:06:22 -0800158 if (mCaptureSequence <= 0) {
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400159 return false;
160 } else if (mCaptureSequence == 1) {
161 mCaptureMode = CaptureMode::SingleFrameSKP;
162 } else {
163 mCaptureMode = CaptureMode::MultiFrameSKP;
Stan Ilieve9d00122017-09-19 12:07:10 -0400164 }
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400165 return true;
166 }
167 return false;
168}
169
170// performs the first-frame work of a multi frame SKP capture. Returns true if successful.
171bool SkiaPipeline::setupMultiFrameCapture() {
172 ALOGD("Set up multi-frame capture, frames = %d", mCaptureSequence);
173 // We own this stream and need to hold it until close() finishes.
174 auto stream = std::make_unique<SkFILEWStream>(mCapturedFile.c_str());
175 if (stream->isValid()) {
176 mOpenMultiPicStream = std::move(stream);
177 mSerialContext.reset(new SkSharingSerialContext());
178 SkSerialProcs procs;
179 procs.fImageProc = SkSharingSerialContext::serializeImage;
180 procs.fImageCtx = mSerialContext.get();
Nathaniel Nifong429fff42020-01-30 14:38:21 -0500181 procs.fTypefaceProc = [](SkTypeface* tf, void* ctx){
182 return tf->serialize(SkTypeface::SerializeBehavior::kDoIncludeData);
183 };
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400184 // SkDocuments don't take owership of the streams they write.
185 // we need to keep it until after mMultiPic.close()
186 // procs is passed as a pointer, but just as a method of having an optional default.
187 // procs doesn't need to outlive this Make call.
Kevin Lubicka63d8092024-04-12 17:43:17 +0000188 mMultiPic = SkMultiPictureDocument::Make(mOpenMultiPicStream.get(), &procs,
Nathaniel Nifong7c216772021-01-22 13:23:25 -0500189 [sharingCtx = mSerialContext.get()](const SkPicture* pic) {
190 SkSharingSerialContext::collectNonTextureImagesFromPicture(pic, sharingCtx);
191 });
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400192 return true;
193 } else {
194 ALOGE("Could not open \"%s\" for writing.", mCapturedFile.c_str());
195 mCaptureSequence = 0;
196 mCaptureMode = CaptureMode::None;
197 return false;
198 }
199}
200
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500201// recurse through the rendernode's children, add any nodes which are layers to the queue.
202static void collectLayers(RenderNode* node, LayerUpdateQueue* layers) {
John Reckbe671952021-01-13 22:39:32 -0500203 SkiaDisplayList* dl = node->getDisplayList().asSkiaDl();
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500204 if (dl) {
205 const auto& prop = node->properties();
206 if (node->hasLayer()) {
207 layers->enqueueLayerWithDamage(node, Rect(prop.getWidth(), prop.getHeight()));
208 }
209 // The way to recurse through rendernodes is to call this with a lambda.
210 dl->updateChildren([&](RenderNode* child) { collectLayers(child, layers); });
211 }
212}
213
214// record the provided layers to the provided canvas as self-contained skpictures.
215static void recordLayers(const LayerUpdateQueue& layers,
216 SkCanvas* mskpCanvas) {
217 const Vector3 savedLightCenter(LightingInfo::getLightCenterRaw());
218 // Record the commands to re-draw each dirty layer into an SkPicture
219 for (size_t i = 0; i < layers.entries().size(); i++) {
220 RenderNode* layerNode = layers.entries()[i].renderNode.get();
221 const Rect& layerDamage = layers.entries()[i].damage;
222 const RenderProperties& properties = layerNode->properties();
223
224 // Temporarily map current light center into RenderNode's coordinate space
225 Vector3 transformedLightCenter(savedLightCenter);
226 layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(transformedLightCenter);
227 LightingInfo::setLightCenterRaw(transformedLightCenter);
228
229 SkPictureRecorder layerRec;
230 auto* recCanvas = layerRec.beginRecording(properties.getWidth(),
231 properties.getHeight());
232 // This is not recorded but still causes clipping.
233 recCanvas->androidFramework_setDeviceClipRestriction(layerDamage.toSkIRect());
234 RenderNodeDrawable root(layerNode, recCanvas, false);
235 root.forceDraw(recCanvas);
236 // Now write this picture into the SKP canvas with an annotation indicating what it is
237 mskpCanvas->drawAnnotation(layerDamage.toSkRect(), String8::format(
238 "OffscreenLayerDraw|%" PRId64, layerNode->uniqueId()).c_str(), nullptr);
239 mskpCanvas->drawPicture(layerRec.finishRecordingAsPicture());
240 }
241 LightingInfo::setLightCenterRaw(savedLightCenter);
242}
243
244SkCanvas* SkiaPipeline::tryCapture(SkSurface* surface, RenderNode* root,
245 const LayerUpdateQueue& dirtyLayers) {
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400246 if (CC_LIKELY(!Properties::skpCaptureEnabled)) {
247 return surface->getCanvas(); // Bail out early when capture is not turned on.
248 }
249 // Note that shouldStartNewFileCapture tells us if this is the *first* frame of a capture.
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500250 bool firstFrameOfAnim = false;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400251 if (shouldStartNewFileCapture() && mCaptureMode == CaptureMode::MultiFrameSKP) {
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500252 // set a reminder to record every layer near the end of this method, after we have set up
253 // the nway canvas.
254 firstFrameOfAnim = true;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400255 if (!setupMultiFrameCapture()) {
256 return surface->getCanvas();
Stan Ilieve9d00122017-09-19 12:07:10 -0400257 }
258 }
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400259
260 // Create a canvas pointer, fill it depending on what kind of capture is requested (if any)
261 SkCanvas* pictureCanvas = nullptr;
262 switch (mCaptureMode) {
263 case CaptureMode::CallbackAPI:
264 case CaptureMode::SingleFrameSKP:
265 mRecorder.reset(new SkPictureRecorder());
Nathaniel Nifong31fee3a2019-07-11 16:27:14 -0400266 pictureCanvas = mRecorder->beginRecording(surface->width(), surface->height());
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400267 break;
268 case CaptureMode::MultiFrameSKP:
269 // If a multi frame recording is active, initialize recording for a single frame of a
270 // multi frame file.
271 pictureCanvas = mMultiPic->beginPage(surface->width(), surface->height());
272 break;
273 case CaptureMode::None:
274 // Returning here in the non-capture case means we can count on pictureCanvas being
275 // non-null below.
276 return surface->getCanvas();
277 }
278
279 // Setting up an nway canvas is common to any kind of capture.
280 mNwayCanvas = std::make_unique<SkNWayCanvas>(surface->width(), surface->height());
281 mNwayCanvas->addCanvas(surface->getCanvas());
282 mNwayCanvas->addCanvas(pictureCanvas);
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500283
284 if (firstFrameOfAnim) {
285 // On the first frame of any mskp capture we want to record any layers that are needed in
286 // frame but may have been rendered offscreen before recording began.
287 // We do not maintain a list of all layers, since it isn't needed outside this rare,
288 // recording use case. Traverse the tree to find them and put them in this LayerUpdateQueue.
289 LayerUpdateQueue luq;
290 collectLayers(root, &luq);
291 recordLayers(luq, mNwayCanvas.get());
292 } else {
293 // on non-first frames, we record any normal layer draws (dirty regions)
294 recordLayers(dirtyLayers, mNwayCanvas.get());
295 }
296
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400297 return mNwayCanvas.get();
Stan Ilieve9d00122017-09-19 12:07:10 -0400298}
299
300void SkiaPipeline::endCapture(SkSurface* surface) {
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400301 if (CC_LIKELY(mCaptureMode == CaptureMode::None)) { return; }
John Reck5cca8f22018-12-10 17:06:22 -0800302 mNwayCanvas.reset();
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400303 ATRACE_CALL();
304 if (mCaptureSequence > 0 && mCaptureMode == CaptureMode::MultiFrameSKP) {
305 mMultiPic->endPage();
306 mCaptureSequence--;
307 if (mCaptureSequence == 0) {
308 mCaptureMode = CaptureMode::None;
309 // Pass mMultiPic and mOpenMultiPicStream to a background thread, which will handle
310 // the heavyweight serialization work and destroy them. mOpenMultiPicStream is released
311 // to a bare pointer because keeping it in a smart pointer makes the lambda
312 // non-copyable. The lambda is only called once, so this is safe.
313 SkFILEWStream* stream = mOpenMultiPicStream.release();
314 CommonPool::post([doc = std::move(mMultiPic), stream]{
315 ALOGD("Finalizing multi frame SKP");
316 doc->close();
317 delete stream;
318 ALOGD("Multi frame SKP complete.");
319 });
320 }
321 } else {
Stan Ilieve9d00122017-09-19 12:07:10 -0400322 sk_sp<SkPicture> picture = mRecorder->finishRecordingAsPicture();
Stan Ilieve9d00122017-09-19 12:07:10 -0400323 if (picture->approximateOpCount() > 0) {
John Reck5cca8f22018-12-10 17:06:22 -0800324 if (mPictureCapturedCallback) {
325 std::invoke(mPictureCapturedCallback, std::move(picture));
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400326 } else {
327 // single frame skp to file
Nathaniel Nifong429fff42020-01-30 14:38:21 -0500328 SkSerialProcs procs;
329 procs.fTypefaceProc = [](SkTypeface* tf, void* ctx){
330 return tf->serialize(SkTypeface::SerializeBehavior::kDoIncludeData);
331 };
Kevin Lubick14d969d2023-09-07 14:28:59 +0000332 procs.fImageProc = [](SkImage* img, void* ctx) -> sk_sp<SkData> {
333 GrDirectContext* dCtx = static_cast<GrDirectContext*>(ctx);
334 return SkPngEncoder::Encode(dCtx,
335 img,
336 SkPngEncoder::Options{});
337 };
338 procs.fImageCtx = mRenderThread.getGrContext();
John Reck76005182021-06-09 22:43:05 -0400339 auto data = picture->serialize(&procs);
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400340 savePictureAsync(data, mCapturedFile);
341 mCaptureSequence = 0;
Derek Sollenberger5f9753d2020-04-01 15:59:02 -0400342 mCaptureMode = CaptureMode::None;
Stan Ilieve9d00122017-09-19 12:07:10 -0400343 }
Stan Ilieve9d00122017-09-19 12:07:10 -0400344 }
345 mRecorder.reset();
346 }
347}
348
Stan Iliev500a0c32016-10-26 10:30:09 -0400349void SkiaPipeline::renderFrame(const LayerUpdateQueue& layers, const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700350 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Greg Danielc4076782019-01-08 16:01:18 -0500351 const Rect& contentDrawBounds, sk_sp<SkSurface> surface,
352 const SkMatrix& preTransform) {
John Reck5cca8f22018-12-10 17:06:22 -0800353 bool previousSkpEnabled = Properties::skpCaptureEnabled;
354 if (mPictureCapturedCallback) {
355 Properties::skpCaptureEnabled = true;
356 }
357
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500358 // Initialize the canvas for the current frame, that might be a recording canvas if SKP
359 // capture is enabled.
360 SkCanvas* canvas = tryCapture(surface.get(), nodes[0].get(), layers);
361
Stan Iliev500a0c32016-10-26 10:30:09 -0400362 // draw all layers up front
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700363 renderLayersImpl(layers, opaque);
Stan Iliev500a0c32016-10-26 10:30:09 -0400364
Nathaniel Nifongdc19a652019-11-11 11:47:50 -0500365 renderFrameImpl(clip, nodes, opaque, contentDrawBounds, canvas, preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -0500366
Stan Ilieve9d00122017-09-19 12:07:10 -0400367 endCapture(surface.get());
Matt Sarettf58cc922016-11-14 18:33:38 -0500368
369 if (CC_UNLIKELY(Properties::debugOverdraw)) {
Nathaniel Nifongdc19a652019-11-11 11:47:50 -0500370 renderOverdraw(clip, nodes, contentDrawBounds, surface, preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -0500371 }
372
John Reck5cca8f22018-12-10 17:06:22 -0800373 Properties::skpCaptureEnabled = previousSkpEnabled;
Matt Sarettf58cc922016-11-14 18:33:38 -0500374}
375
Stan Iliev52771272016-11-17 09:54:38 -0500376namespace {
377static Rect nodeBounds(RenderNode& node) {
378 auto& props = node.properties();
John Reck1bcacfd2017-11-03 10:12:19 -0700379 return Rect(props.getLeft(), props.getTop(), props.getRight(), props.getBottom());
Stan Iliev52771272016-11-17 09:54:38 -0500380}
John Reck0fa0cbc2019-04-05 16:57:46 -0700381} // namespace
Stan Iliev52771272016-11-17 09:54:38 -0500382
Nathaniel Nifongdc19a652019-11-11 11:47:50 -0500383void SkiaPipeline::renderFrameImpl(const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700384 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Greg Danielc4076782019-01-08 16:01:18 -0500385 const Rect& contentDrawBounds, SkCanvas* canvas,
386 const SkMatrix& preTransform) {
Stan Ilievb66b8bb2016-12-15 18:17:42 -0500387 SkAutoCanvasRestore saver(canvas, true);
Nathaniel Nifongcff969f2020-01-09 14:03:49 -0500388 auto clipRestriction = preTransform.mapRect(clip).roundOut();
John Reck76005182021-06-09 22:43:05 -0400389 if (CC_UNLIKELY(isCapturingSkp())) {
Nathaniel Nifong89a5dc52020-01-17 15:32:17 -0500390 canvas->drawAnnotation(SkRect::Make(clipRestriction), "AndroidDeviceClipRestriction",
391 nullptr);
392 } else {
393 // clip drawing to dirty region only when not recording SKP files (which should contain all
394 // draw ops on every frame)
395 canvas->androidFramework_setDeviceClipRestriction(clipRestriction);
396 }
Greg Danielc4076782019-01-08 16:01:18 -0500397 canvas->concat(preTransform);
Stan Iliev500a0c32016-10-26 10:30:09 -0400398
Nader Jawadf5ce4522023-03-27 13:02:41 -0700399 if (!opaque) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400400 canvas->clear(SK_ColorTRANSPARENT);
401 }
402
Stan Iliev52771272016-11-17 09:54:38 -0500403 if (1 == nodes.size()) {
404 if (!nodes[0]->nothingToDraw()) {
Stan Iliev52771272016-11-17 09:54:38 -0500405 RenderNodeDrawable root(nodes[0].get(), canvas);
406 root.draw(canvas);
407 }
408 } else if (0 == nodes.size()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700409 // nothing to draw
Stan Iliev52771272016-11-17 09:54:38 -0500410 } else {
411 // It there are multiple render nodes, they are laid out as follows:
412 // #0 - backdrop (content + caption)
413 // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop)
414 // #2 - additional overlay nodes
John Reck1bcacfd2017-11-03 10:12:19 -0700415 // Usually the backdrop cannot be seen since it will be entirely covered by the content.
416 // While
417 // resizing however it might become partially visible. The following render loop will crop
418 // the
419 // backdrop against the content and draw the remaining part of it. It will then draw the
420 // content
Stan Iliev52771272016-11-17 09:54:38 -0500421 // cropped to the backdrop (since that indicates a shrinking of the window).
422 //
423 // Additional nodes will be drawn on top with no particular clipping semantics.
Stan Iliev500a0c32016-10-26 10:30:09 -0400424
Stan Iliev52771272016-11-17 09:54:38 -0500425 // Usually the contents bounds should be mContentDrawBounds - however - we will
426 // move it towards the fixed edge to give it a more stable appearance (for the moment).
427 // If there is no content bounds we ignore the layering as stated above and start with 2.
Stan Iliev500a0c32016-10-26 10:30:09 -0400428
Stan Iliev52771272016-11-17 09:54:38 -0500429 // Backdrop bounds in render target space
430 const Rect backdrop = nodeBounds(*nodes[0]);
Stan Iliev500a0c32016-10-26 10:30:09 -0400431
John Reck1bcacfd2017-11-03 10:12:19 -0700432 // Bounds that content will fill in render target space (note content node bounds may be
433 // bigger)
Stan Iliev52771272016-11-17 09:54:38 -0500434 Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight());
435 content.translate(backdrop.left, backdrop.top);
436 if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) {
437 // Content doesn't entirely overlap backdrop, so fill around content (right/bottom)
Stan Iliev500a0c32016-10-26 10:30:09 -0400438
Stan Iliev52771272016-11-17 09:54:38 -0500439 // Note: in the future, if content doesn't snap to backdrop's left/top, this may need to
John Reck1bcacfd2017-11-03 10:12:19 -0700440 // also fill left/top. Currently, both 2up and freeform position content at the top/left
441 // of
Stan Iliev52771272016-11-17 09:54:38 -0500442 // the backdrop, so this isn't necessary.
443 RenderNodeDrawable backdropNode(nodes[0].get(), canvas);
444 if (content.right < backdrop.right) {
445 // draw backdrop to right side of content
446 SkAutoCanvasRestore acr(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700447 canvas->clipRect(SkRect::MakeLTRB(content.right, backdrop.top, backdrop.right,
448 backdrop.bottom));
Stan Iliev52771272016-11-17 09:54:38 -0500449 backdropNode.draw(canvas);
450 }
451 if (content.bottom < backdrop.bottom) {
452 // draw backdrop to bottom of content
453 // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill
454 SkAutoCanvasRestore acr(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700455 canvas->clipRect(SkRect::MakeLTRB(content.left, content.bottom, content.right,
456 backdrop.bottom));
Stan Iliev52771272016-11-17 09:54:38 -0500457 backdropNode.draw(canvas);
458 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400459 }
460
Stan Iliev52771272016-11-17 09:54:38 -0500461 RenderNodeDrawable contentNode(nodes[1].get(), canvas);
462 if (!backdrop.isEmpty()) {
463 // content node translation to catch up with backdrop
464 float dx = backdrop.left - contentDrawBounds.left;
465 float dy = backdrop.top - contentDrawBounds.top;
466
467 SkAutoCanvasRestore acr(canvas, true);
468 canvas->translate(dx, dy);
John Reck1bcacfd2017-11-03 10:12:19 -0700469 const SkRect contentLocalClip =
470 SkRect::MakeXYWH(contentDrawBounds.left, contentDrawBounds.top,
471 backdrop.getWidth(), backdrop.getHeight());
Stan Iliev52771272016-11-17 09:54:38 -0500472 canvas->clipRect(contentLocalClip);
473 contentNode.draw(canvas);
474 } else {
475 SkAutoCanvasRestore acr(canvas, true);
476 contentNode.draw(canvas);
477 }
478
479 // remaining overlay nodes, simply defer
480 for (size_t index = 2; index < nodes.size(); index++) {
481 if (!nodes[index]->nothingToDraw()) {
482 SkAutoCanvasRestore acr(canvas, true);
483 RenderNodeDrawable overlayNode(nodes[index].get(), canvas);
484 overlayNode.draw(canvas);
485 }
486 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400487 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400488}
489
Peiyong Lin3bff1352018-12-11 07:56:07 -0800490void SkiaPipeline::setSurfaceColorProperties(ColorMode colorMode) {
Derek Sollenberger1863d942020-02-05 15:41:51 -0500491 mColorMode = colorMode;
John Reckb36bfdd2020-07-23 13:47:49 -0700492 switch (colorMode) {
493 case ColorMode::Default:
494 mSurfaceColorType = SkColorType::kN32_SkColorType;
495 mSurfaceColorSpace = SkColorSpace::MakeSRGB();
496 break;
497 case ColorMode::WideColorGamut:
498 mSurfaceColorType = DeviceInfo::get()->getWideColorType();
499 mSurfaceColorSpace = DeviceInfo::get()->getWideColorSpace();
500 break;
501 case ColorMode::Hdr:
Alec Mouri4a3035e2024-03-04 23:12:42 +0000502 if (DeviceInfo::get()->isSupportRgba10101010ForHdr()) {
503 mSurfaceColorType = SkColorType::kRGBA_10x6_SkColorType;
504 mSurfaceColorSpace = SkColorSpace::MakeRGB(
505 GetExtendedTransferFunction(mTargetSdrHdrRatio), SkNamedGamut::kDisplayP3);
506 } else if (DeviceInfo::get()->isSupportFp16ForHdr()) {
Alec Mouri22ab7f32023-09-06 02:11:56 +0000507 mSurfaceColorType = SkColorType::kRGBA_F16_SkColorType;
508 mSurfaceColorSpace = SkColorSpace::MakeSRGB();
509 } else {
510 mSurfaceColorType = SkColorType::kN32_SkColorType;
511 mSurfaceColorSpace = SkColorSpace::MakeRGB(
512 GetExtendedTransferFunction(mTargetSdrHdrRatio), SkNamedGamut::kDisplayP3);
513 }
John Reckb36bfdd2020-07-23 13:47:49 -0700514 break;
John Reck0b3f3312023-01-31 16:21:28 -0500515 case ColorMode::Hdr10:
516 mSurfaceColorType = SkColorType::kRGBA_1010102_SkColorType;
517 mSurfaceColorSpace = SkColorSpace::MakeRGB(
518 GetExtendedTransferFunction(mTargetSdrHdrRatio), SkNamedGamut::kDisplayP3);
519 break;
Leon Scroggins IIIcbdbb662021-11-30 13:59:00 -0500520 case ColorMode::A8:
521 mSurfaceColorType = SkColorType::kAlpha_8_SkColorType;
522 mSurfaceColorSpace = nullptr;
523 break;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800524 }
525}
526
John Reck55887762023-01-25 16:51:18 -0500527void SkiaPipeline::setTargetSdrHdrRatio(float ratio) {
John Reck0b3f3312023-01-31 16:21:28 -0500528 if (mColorMode == ColorMode::Hdr || mColorMode == ColorMode::Hdr10) {
John Reck55887762023-01-25 16:51:18 -0500529 mTargetSdrHdrRatio = ratio;
Alec Mouri22ab7f32023-09-06 02:11:56 +0000530
Alec Mouri4a3035e2024-03-04 23:12:42 +0000531 if (mColorMode == ColorMode::Hdr && DeviceInfo::get()->isSupportFp16ForHdr() &&
532 !DeviceInfo::get()->isSupportRgba10101010ForHdr()) {
Alec Mouri22ab7f32023-09-06 02:11:56 +0000533 mSurfaceColorSpace = SkColorSpace::MakeSRGB();
534 } else {
535 mSurfaceColorSpace = SkColorSpace::MakeRGB(
536 GetExtendedTransferFunction(mTargetSdrHdrRatio), SkNamedGamut::kDisplayP3);
537 }
John Reck55887762023-01-25 16:51:18 -0500538 } else {
539 mTargetSdrHdrRatio = 1.f;
540 }
541}
542
Matt Sarettf58cc922016-11-14 18:33:38 -0500543// Overdraw debugging
544
545// These colors should be kept in sync with Caches::getOverdrawColor() with a few differences.
Mike Reed331c4e12020-02-13 10:21:54 -0500546// This implementation requires transparent entries for "no overdraw" and "single draws".
547static const SkColor kOverdrawColors[2][6] = {
548 {
549 0x00000000,
550 0x00000000,
551 0x2f0000ff,
552 0x2f00ff00,
553 0x3fff0000,
554 0x7fff0000,
555 },
556 {
557 0x00000000,
558 0x00000000,
559 0x2f0000ff,
560 0x4fffff00,
561 0x5fff89d7,
562 0x7fff0000,
563 },
Matt Sarettf58cc922016-11-14 18:33:38 -0500564};
565
Nathaniel Nifongdc19a652019-11-11 11:47:50 -0500566void SkiaPipeline::renderOverdraw(const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700567 const std::vector<sp<RenderNode>>& nodes,
Greg Danielc4076782019-01-08 16:01:18 -0500568 const Rect& contentDrawBounds, sk_sp<SkSurface> surface,
569 const SkMatrix& preTransform) {
Matt Sarettf58cc922016-11-14 18:33:38 -0500570 // Set up the overdraw canvas.
571 SkImageInfo offscreenInfo = SkImageInfo::MakeA8(surface->width(), surface->height());
572 sk_sp<SkSurface> offscreen = surface->makeSurface(offscreenInfo);
Nathaniel Nifongfe05b7c2019-09-17 12:52:52 -0400573 LOG_ALWAYS_FATAL_IF(!offscreen, "Failed to create offscreen SkSurface for overdraw viz.");
Matt Sarettf58cc922016-11-14 18:33:38 -0500574 SkOverdrawCanvas overdrawCanvas(offscreen->getCanvas());
575
576 // Fake a redraw to replay the draw commands. This will increment the alpha channel
577 // each time a pixel would have been drawn.
578 // Pass true for opaque so we skip the clear - the overdrawCanvas is already zero
579 // initialized.
Nathaniel Nifongdc19a652019-11-11 11:47:50 -0500580 renderFrameImpl(clip, nodes, true, contentDrawBounds, &overdrawCanvas, preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -0500581 sk_sp<SkImage> counts = offscreen->makeImageSnapshot();
582
583 // Draw overdraw colors to the canvas. The color filter will convert counts to colors.
584 SkPaint paint;
Mike Reed331c4e12020-02-13 10:21:54 -0500585 const SkColor* colors = kOverdrawColors[static_cast<int>(Properties::overdrawColorSet)];
586 paint.setColorFilter(SkOverdrawColorFilter::MakeWithSkColors(colors));
Mike Reed7994a312021-01-28 18:06:26 -0500587 surface->getCanvas()->drawImage(counts.get(), 0.0f, 0.0f, SkSamplingOptions(), &paint);
Matt Sarettf58cc922016-11-14 18:33:38 -0500588}
589
Stan Iliev500a0c32016-10-26 10:30:09 -0400590} /* namespace skiapipeline */
591} /* namespace uirenderer */
592} /* namespace android */