blob: 1f929685b62c46ef18fab8e0c14fdcda8d192b44 [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
17#include "SkiaPipeline.h"
18
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>
Hal Canary10219fb2016-11-23 20:41:22 -050024#include <SkImageEncoder.h>
Peiyong Lin3bff1352018-12-11 07:56:07 -080025#include <SkImageInfo.h>
Leon Scroggins IIIee708fa2016-12-12 15:31:39 -050026#include <SkImagePriv.h>
Kevin Lubick1175dc02022-02-28 12:41:27 -050027#include <SkMatrix.h>
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040028#include <SkMultiPictureDocument.h>
Matt Sarettf58cc922016-11-14 18:33:38 -050029#include <SkOverdrawCanvas.h>
30#include <SkOverdrawColorFilter.h>
John Reck29b1ee02023-04-04 17:44:23 -040031#include <SkPaintFilterCanvas.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040032#include <SkPicture.h>
33#include <SkPictureRecorder.h>
Kevin Lubick1175dc02022-02-28 12:41:27 -050034#include <SkRect.h>
35#include <SkRefCnt.h>
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040036#include <SkSerialProcs.h>
Kevin Lubick1175dc02022-02-28 12:41:27 -050037#include <SkStream.h>
38#include <SkString.h>
Alec Mouri43fe6fc2019-12-23 07:46:19 -080039#include <SkTypeface.h>
40#include <android-base/properties.h>
John Reck29b1ee02023-04-04 17:44:23 -040041#include <gui/TraceUtils.h>
Alec Mouri43fe6fc2019-12-23 07:46:19 -080042#include <unistd.h>
43
44#include <sstream>
45
Fedor Kudasov90df0562019-06-19 11:41:34 +010046#include "LightingInfo.h"
Stan Iliev23c38a92017-03-23 00:12:50 -040047#include "VectorDrawable.h"
John Reck29b1ee02023-04-04 17:44:23 -040048#include "include/gpu/GpuTypes.h" // from Skia
John Reck322b8ab2019-03-14 13:15:28 -070049#include "thread/CommonPool.h"
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040050#include "tools/SkSharingProc.h"
John Reckb36bfdd2020-07-23 13:47:49 -070051#include "utils/Color.h"
Nathaniel Nifong2945bff2019-11-25 09:34:21 -050052#include "utils/String8.h"
Jerome Gaillarda02a12d2019-05-28 18:07:56 +010053
Stan Iliev500a0c32016-10-26 10:30:09 -040054using namespace android::uirenderer::renderthread;
55
56namespace android {
57namespace uirenderer {
58namespace skiapipeline {
59
John Reck1bcacfd2017-11-03 10:12:19 -070060SkiaPipeline::SkiaPipeline(RenderThread& thread) : mRenderThread(thread) {
Derek Sollenberger1863d942020-02-05 15:41:51 -050061 setSurfaceColorProperties(mColorMode);
Stan Iliev23c38a92017-03-23 00:12:50 -040062}
Stan Iliev500a0c32016-10-26 10:30:09 -040063
Stan Iliev232f3622017-08-23 17:15:09 -040064SkiaPipeline::~SkiaPipeline() {
65 unpinImages();
66}
67
Stan Iliev500a0c32016-10-26 10:30:09 -040068void SkiaPipeline::onDestroyHardwareResources() {
Derek Sollenberger92a9eb92018-04-12 13:42:19 -040069 unpinImages();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040070 mRenderThread.cacheManager().trimStaleResources();
Stan Iliev500a0c32016-10-26 10:30:09 -040071}
72
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -040073bool SkiaPipeline::pinImages(std::vector<SkImage*>& mutableImages) {
Greg Danielf8cb5252021-06-30 10:40:34 -040074 if (!mRenderThread.getGrContext()) {
75 ALOGD("Trying to pin an image with an invalid GrContext");
76 return false;
77 }
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -040078 for (SkImage* image : mutableImages) {
Derek Sollenberger189e8742016-11-16 16:00:17 -050079 if (SkImage_pinAsTexture(image, mRenderThread.getGrContext())) {
80 mPinnedImages.emplace_back(sk_ref_sp(image));
81 } else {
82 return false;
83 }
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -040084 }
85 return true;
86}
87
88void SkiaPipeline::unpinImages() {
89 for (auto& image : mPinnedImages) {
90 SkImage_unpinAsTexture(image.get(), mRenderThread.getGrContext());
91 }
92 mPinnedImages.clear();
93}
94
John Reckd9d7f122018-05-03 14:40:56 -070095void SkiaPipeline::renderLayers(const LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070096 LayerUpdateQueue* layerUpdateQueue, bool opaque,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070097 const LightInfo& lightInfo) {
Fedor Kudasov90df0562019-06-19 11:41:34 +010098 LightingInfo::updateLighting(lightGeometry, lightInfo);
Stan Iliev500a0c32016-10-26 10:30:09 -040099 ATRACE_NAME("draw layers");
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700100 renderLayersImpl(*layerUpdateQueue, opaque);
Stan Iliev500a0c32016-10-26 10:30:09 -0400101 layerUpdateQueue->clear();
102}
103
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700104void SkiaPipeline::renderLayersImpl(const LayerUpdateQueue& layers, bool opaque) {
Adlai Hollerab762152020-09-16 10:37:19 -0400105 sk_sp<GrDirectContext> cachedContext;
Derek Sollenbergerf7df1842017-09-05 11:15:58 -0400106
Stan Iliev500a0c32016-10-26 10:30:09 -0400107 // Render all layers that need to be updated, in order.
108 for (size_t i = 0; i < layers.entries().size(); i++) {
John Reckfc29f7acd2017-03-02 13:23:16 -0800109 RenderNode* layerNode = layers.entries()[i].renderNode.get();
Stan Iliev500a0c32016-10-26 10:30:09 -0400110 // only schedule repaint if node still on layer - possible it may have been
111 // removed during a dropped frame, but layers may still remain scheduled so
112 // as not to lose info on what portion is damaged
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500113 if (CC_UNLIKELY(layerNode->getLayerSurface() == nullptr)) {
114 continue;
115 }
116 SkASSERT(layerNode->getLayerSurface());
John Reckbe671952021-01-13 22:39:32 -0500117 SkiaDisplayList* displayList = layerNode->getDisplayList().asSkiaDl();
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500118 if (!displayList || displayList->isEmpty()) {
119 ALOGE("%p drawLayers(%s) : missing drawable", layerNode, layerNode->getName());
120 return;
121 }
122
123 const Rect& layerDamage = layers.entries()[i].damage;
124
125 SkCanvas* layerCanvas = layerNode->getLayerSurface()->getCanvas();
126
127 int saveCount = layerCanvas->save();
128 SkASSERT(saveCount == 1);
129
130 layerCanvas->androidFramework_setDeviceClipRestriction(layerDamage.toSkIRect());
131
132 // TODO: put localized light center calculation and storage to a drawable related code.
133 // It does not seem right to store something localized in a global state
134 // fix here and in recordLayers
135 const Vector3 savedLightCenter(LightingInfo::getLightCenterRaw());
136 Vector3 transformedLightCenter(savedLightCenter);
137 // map current light center into RenderNode's coordinate space
138 layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(transformedLightCenter);
139 LightingInfo::setLightCenterRaw(transformedLightCenter);
140
141 const RenderProperties& properties = layerNode->properties();
142 const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
143 if (properties.getClipToBounds() && layerCanvas->quickReject(bounds)) {
144 return;
145 }
146
147 ATRACE_FORMAT("drawLayer [%s] %.1f x %.1f", layerNode->getName(), bounds.width(),
148 bounds.height());
149
150 layerNode->getSkiaLayer()->hasRenderedSinceRepaint = false;
151 layerCanvas->clear(SK_ColorTRANSPARENT);
152
153 RenderNodeDrawable root(layerNode, layerCanvas, false);
154 root.forceDraw(layerCanvas);
155 layerCanvas->restoreToCount(saveCount);
156
157 LightingInfo::setLightCenterRaw(savedLightCenter);
158
159 // cache the current context so that we can defer flushing it until
160 // either all the layers have been rendered or the context changes
Adlai Hollerab762152020-09-16 10:37:19 -0400161 GrDirectContext* currentContext =
162 GrAsDirectContext(layerNode->getLayerSurface()->getCanvas()->recordingContext());
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500163 if (cachedContext.get() != currentContext) {
164 if (cachedContext.get()) {
165 ATRACE_NAME("flush layers (context changed)");
Greg Danielc7ad4082020-05-14 15:38:26 -0400166 cachedContext->flushAndSubmit();
Stan Iliev500a0c32016-10-26 10:30:09 -0400167 }
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500168 cachedContext.reset(SkSafeRef(currentContext));
Stan Iliev500a0c32016-10-26 10:30:09 -0400169 }
170 }
Derek Sollenbergerf7df1842017-09-05 11:15:58 -0400171
172 if (cachedContext.get()) {
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -0400173 ATRACE_NAME("flush layers");
Greg Danielc7ad4082020-05-14 15:38:26 -0400174 cachedContext->flushAndSubmit();
Derek Sollenbergerf7df1842017-09-05 11:15:58 -0400175 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400176}
177
John Reck1bcacfd2017-11-03 10:12:19 -0700178bool SkiaPipeline::createOrUpdateLayer(RenderNode* node, const DamageAccumulator& damageAccumulator,
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700179 ErrorHandler* errorHandler) {
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500180 // compute the size of the surface (i.e. texture) to be allocated for this layer
181 const int surfaceWidth = ceilf(node->getWidth() / float(LAYER_SIZE)) * LAYER_SIZE;
182 const int surfaceHeight = ceilf(node->getHeight() / float(LAYER_SIZE)) * LAYER_SIZE;
183
Stan Iliev500a0c32016-10-26 10:30:09 -0400184 SkSurface* layer = node->getLayerSurface();
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500185 if (!layer || layer->width() != surfaceWidth || layer->height() != surfaceHeight) {
Stan Iliev08fc19a2017-07-24 10:20:33 -0400186 SkImageInfo info;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700187 info = SkImageInfo::Make(surfaceWidth, surfaceHeight, getSurfaceColorType(),
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400188 kPremul_SkAlphaType, getSurfaceColorSpace());
Stan Iliev500a0c32016-10-26 10:30:09 -0400189 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
190 SkASSERT(mRenderThread.getGrContext() != nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -0700191 node->setLayerSurface(SkSurface::MakeRenderTarget(mRenderThread.getGrContext(),
Kevin Lubick8099b672023-01-09 14:48:45 +0000192 skgpu::Budgeted::kYes, info, 0,
Derek Sollenberger25833d22019-01-14 13:55:55 -0500193 this->getSurfaceOrigin(), &props));
Stan Iliev500a0c32016-10-26 10:30:09 -0400194 if (node->getLayerSurface()) {
195 // update the transform in window of the layer to reset its origin wrt light source
196 // position
197 Matrix4 windowTransform;
198 damageAccumulator.computeCurrentTransform(&windowTransform);
Stan Ilievda7c19c2019-05-22 14:43:44 -0400199 node->getSkiaLayer()->inverseTransformInWindow.loadInverse(windowTransform);
Stan Iliev216b1572018-03-26 14:29:50 -0400200 } else {
201 String8 cachesOutput;
202 mRenderThread.cacheManager().dumpMemoryUsage(cachesOutput,
John Reck283bb462018-12-13 16:40:14 -0800203 &mRenderThread.renderState());
Stan Iliev216b1572018-03-26 14:29:50 -0400204 ALOGE("%s", cachesOutput.string());
205 if (errorHandler) {
206 std::ostringstream err;
207 err << "Unable to create layer for " << node->getName();
208 const int maxTextureSize = DeviceInfo::get()->maxTextureSize();
209 err << ", size " << info.width() << "x" << info.height() << " max size "
John Reck283bb462018-12-13 16:40:14 -0800210 << maxTextureSize << " color type " << (int)info.colorType() << " has context "
211 << (int)(mRenderThread.getGrContext() != nullptr);
Stan Iliev216b1572018-03-26 14:29:50 -0400212 errorHandler->onError(err.str());
213 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400214 }
215 return true;
216 }
217 return false;
218}
219
Stan Iliev500a0c32016-10-26 10:30:09 -0400220void SkiaPipeline::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) {
Adlai Hollerab762152020-09-16 10:37:19 -0400221 GrDirectContext* context = thread.getGrContext();
John Reckcf1170f2021-07-14 15:52:19 -0400222 if (context && !bitmap->isHardware()) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400223 ATRACE_FORMAT("Bitmap#prepareToDraw %dx%d", bitmap->width(), bitmap->height());
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400224 auto image = bitmap->makeImage();
John Reckcf1170f2021-07-14 15:52:19 -0400225 if (image.get()) {
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400226 SkImage_pinAsTexture(image.get(), context);
227 SkImage_unpinAsTexture(image.get(), context);
John Reckcf1170f2021-07-14 15:52:19 -0400228 // A submit is necessary as there may not be a frame coming soon, so without a call
229 // to submit these texture uploads can just sit in the queue building up until
230 // we run out of RAM
231 context->flushAndSubmit();
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400232 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400233 }
234}
235
John Reck322b8ab2019-03-14 13:15:28 -0700236static void savePictureAsync(const sk_sp<SkData>& data, const std::string& filename) {
237 CommonPool::post([data, filename] {
238 if (0 == access(filename.c_str(), F_OK)) {
Stan Ilieve9d00122017-09-19 12:07:10 -0400239 return;
240 }
241
John Reck322b8ab2019-03-14 13:15:28 -0700242 SkFILEWStream stream(filename.c_str());
Stan Ilieve9d00122017-09-19 12:07:10 -0400243 if (stream.isValid()) {
John Reck322b8ab2019-03-14 13:15:28 -0700244 stream.write(data->data(), data->size());
Stan Ilieve9d00122017-09-19 12:07:10 -0400245 stream.flush();
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400246 ALOGD("SKP Captured Drawing Output (%zu bytes) for frame. %s", stream.bytesWritten(),
John Reck322b8ab2019-03-14 13:15:28 -0700247 filename.c_str());
Stan Ilieve9d00122017-09-19 12:07:10 -0400248 }
John Reck322b8ab2019-03-14 13:15:28 -0700249 });
250}
Stan Ilieve9d00122017-09-19 12:07:10 -0400251
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400252// Note multiple SkiaPipeline instances may be loaded if more than one app is visible.
253// Each instance may observe the filename changing and try to record to a file of the same name.
254// Only the first one will succeed. There is no scope available here where we could coordinate
255// to cause this function to return true for only one of the instances.
256bool SkiaPipeline::shouldStartNewFileCapture() {
257 // Don't start a new file based capture if one is currently ongoing.
258 if (mCaptureMode != CaptureMode::None) { return false; }
259
260 // A new capture is started when the filename property changes.
261 // Read the filename property.
262 std::string prop = base::GetProperty(PROPERTY_CAPTURE_SKP_FILENAME, "0");
263 // if the filename property changed to a valid value
264 if (prop[0] != '0' && mCapturedFile != prop) {
265 // remember this new filename
266 mCapturedFile = prop;
267 // and get a property indicating how many frames to capture.
268 mCaptureSequence = base::GetIntProperty(PROPERTY_CAPTURE_SKP_FRAMES, 1);
John Reck5cca8f22018-12-10 17:06:22 -0800269 if (mCaptureSequence <= 0) {
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400270 return false;
271 } else if (mCaptureSequence == 1) {
272 mCaptureMode = CaptureMode::SingleFrameSKP;
273 } else {
274 mCaptureMode = CaptureMode::MultiFrameSKP;
Stan Ilieve9d00122017-09-19 12:07:10 -0400275 }
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400276 return true;
277 }
278 return false;
279}
280
281// performs the first-frame work of a multi frame SKP capture. Returns true if successful.
282bool SkiaPipeline::setupMultiFrameCapture() {
283 ALOGD("Set up multi-frame capture, frames = %d", mCaptureSequence);
284 // We own this stream and need to hold it until close() finishes.
285 auto stream = std::make_unique<SkFILEWStream>(mCapturedFile.c_str());
286 if (stream->isValid()) {
287 mOpenMultiPicStream = std::move(stream);
288 mSerialContext.reset(new SkSharingSerialContext());
289 SkSerialProcs procs;
290 procs.fImageProc = SkSharingSerialContext::serializeImage;
291 procs.fImageCtx = mSerialContext.get();
Nathaniel Nifong429fff42020-01-30 14:38:21 -0500292 procs.fTypefaceProc = [](SkTypeface* tf, void* ctx){
293 return tf->serialize(SkTypeface::SerializeBehavior::kDoIncludeData);
294 };
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400295 // SkDocuments don't take owership of the streams they write.
296 // we need to keep it until after mMultiPic.close()
297 // procs is passed as a pointer, but just as a method of having an optional default.
298 // procs doesn't need to outlive this Make call.
Nathaniel Nifong7c216772021-01-22 13:23:25 -0500299 mMultiPic = SkMakeMultiPictureDocument(mOpenMultiPicStream.get(), &procs,
300 [sharingCtx = mSerialContext.get()](const SkPicture* pic) {
301 SkSharingSerialContext::collectNonTextureImagesFromPicture(pic, sharingCtx);
302 });
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400303 return true;
304 } else {
305 ALOGE("Could not open \"%s\" for writing.", mCapturedFile.c_str());
306 mCaptureSequence = 0;
307 mCaptureMode = CaptureMode::None;
308 return false;
309 }
310}
311
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500312// recurse through the rendernode's children, add any nodes which are layers to the queue.
313static void collectLayers(RenderNode* node, LayerUpdateQueue* layers) {
John Reckbe671952021-01-13 22:39:32 -0500314 SkiaDisplayList* dl = node->getDisplayList().asSkiaDl();
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500315 if (dl) {
316 const auto& prop = node->properties();
317 if (node->hasLayer()) {
318 layers->enqueueLayerWithDamage(node, Rect(prop.getWidth(), prop.getHeight()));
319 }
320 // The way to recurse through rendernodes is to call this with a lambda.
321 dl->updateChildren([&](RenderNode* child) { collectLayers(child, layers); });
322 }
323}
324
325// record the provided layers to the provided canvas as self-contained skpictures.
326static void recordLayers(const LayerUpdateQueue& layers,
327 SkCanvas* mskpCanvas) {
328 const Vector3 savedLightCenter(LightingInfo::getLightCenterRaw());
329 // Record the commands to re-draw each dirty layer into an SkPicture
330 for (size_t i = 0; i < layers.entries().size(); i++) {
331 RenderNode* layerNode = layers.entries()[i].renderNode.get();
332 const Rect& layerDamage = layers.entries()[i].damage;
333 const RenderProperties& properties = layerNode->properties();
334
335 // Temporarily map current light center into RenderNode's coordinate space
336 Vector3 transformedLightCenter(savedLightCenter);
337 layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(transformedLightCenter);
338 LightingInfo::setLightCenterRaw(transformedLightCenter);
339
340 SkPictureRecorder layerRec;
341 auto* recCanvas = layerRec.beginRecording(properties.getWidth(),
342 properties.getHeight());
343 // This is not recorded but still causes clipping.
344 recCanvas->androidFramework_setDeviceClipRestriction(layerDamage.toSkIRect());
345 RenderNodeDrawable root(layerNode, recCanvas, false);
346 root.forceDraw(recCanvas);
347 // Now write this picture into the SKP canvas with an annotation indicating what it is
348 mskpCanvas->drawAnnotation(layerDamage.toSkRect(), String8::format(
349 "OffscreenLayerDraw|%" PRId64, layerNode->uniqueId()).c_str(), nullptr);
350 mskpCanvas->drawPicture(layerRec.finishRecordingAsPicture());
351 }
352 LightingInfo::setLightCenterRaw(savedLightCenter);
353}
354
355SkCanvas* SkiaPipeline::tryCapture(SkSurface* surface, RenderNode* root,
356 const LayerUpdateQueue& dirtyLayers) {
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400357 if (CC_LIKELY(!Properties::skpCaptureEnabled)) {
358 return surface->getCanvas(); // Bail out early when capture is not turned on.
359 }
360 // Note that shouldStartNewFileCapture tells us if this is the *first* frame of a capture.
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500361 bool firstFrameOfAnim = false;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400362 if (shouldStartNewFileCapture() && mCaptureMode == CaptureMode::MultiFrameSKP) {
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500363 // set a reminder to record every layer near the end of this method, after we have set up
364 // the nway canvas.
365 firstFrameOfAnim = true;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400366 if (!setupMultiFrameCapture()) {
367 return surface->getCanvas();
Stan Ilieve9d00122017-09-19 12:07:10 -0400368 }
369 }
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400370
371 // Create a canvas pointer, fill it depending on what kind of capture is requested (if any)
372 SkCanvas* pictureCanvas = nullptr;
373 switch (mCaptureMode) {
374 case CaptureMode::CallbackAPI:
375 case CaptureMode::SingleFrameSKP:
376 mRecorder.reset(new SkPictureRecorder());
Nathaniel Nifong31fee3a2019-07-11 16:27:14 -0400377 pictureCanvas = mRecorder->beginRecording(surface->width(), surface->height());
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400378 break;
379 case CaptureMode::MultiFrameSKP:
380 // If a multi frame recording is active, initialize recording for a single frame of a
381 // multi frame file.
382 pictureCanvas = mMultiPic->beginPage(surface->width(), surface->height());
383 break;
384 case CaptureMode::None:
385 // Returning here in the non-capture case means we can count on pictureCanvas being
386 // non-null below.
387 return surface->getCanvas();
388 }
389
390 // Setting up an nway canvas is common to any kind of capture.
391 mNwayCanvas = std::make_unique<SkNWayCanvas>(surface->width(), surface->height());
392 mNwayCanvas->addCanvas(surface->getCanvas());
393 mNwayCanvas->addCanvas(pictureCanvas);
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500394
395 if (firstFrameOfAnim) {
396 // On the first frame of any mskp capture we want to record any layers that are needed in
397 // frame but may have been rendered offscreen before recording began.
398 // We do not maintain a list of all layers, since it isn't needed outside this rare,
399 // recording use case. Traverse the tree to find them and put them in this LayerUpdateQueue.
400 LayerUpdateQueue luq;
401 collectLayers(root, &luq);
402 recordLayers(luq, mNwayCanvas.get());
403 } else {
404 // on non-first frames, we record any normal layer draws (dirty regions)
405 recordLayers(dirtyLayers, mNwayCanvas.get());
406 }
407
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400408 return mNwayCanvas.get();
Stan Ilieve9d00122017-09-19 12:07:10 -0400409}
410
411void SkiaPipeline::endCapture(SkSurface* surface) {
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400412 if (CC_LIKELY(mCaptureMode == CaptureMode::None)) { return; }
John Reck5cca8f22018-12-10 17:06:22 -0800413 mNwayCanvas.reset();
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400414 ATRACE_CALL();
415 if (mCaptureSequence > 0 && mCaptureMode == CaptureMode::MultiFrameSKP) {
416 mMultiPic->endPage();
417 mCaptureSequence--;
418 if (mCaptureSequence == 0) {
419 mCaptureMode = CaptureMode::None;
420 // Pass mMultiPic and mOpenMultiPicStream to a background thread, which will handle
421 // the heavyweight serialization work and destroy them. mOpenMultiPicStream is released
422 // to a bare pointer because keeping it in a smart pointer makes the lambda
423 // non-copyable. The lambda is only called once, so this is safe.
424 SkFILEWStream* stream = mOpenMultiPicStream.release();
425 CommonPool::post([doc = std::move(mMultiPic), stream]{
426 ALOGD("Finalizing multi frame SKP");
427 doc->close();
428 delete stream;
429 ALOGD("Multi frame SKP complete.");
430 });
431 }
432 } else {
Stan Ilieve9d00122017-09-19 12:07:10 -0400433 sk_sp<SkPicture> picture = mRecorder->finishRecordingAsPicture();
Stan Ilieve9d00122017-09-19 12:07:10 -0400434 if (picture->approximateOpCount() > 0) {
John Reck5cca8f22018-12-10 17:06:22 -0800435 if (mPictureCapturedCallback) {
436 std::invoke(mPictureCapturedCallback, std::move(picture));
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400437 } else {
438 // single frame skp to file
Nathaniel Nifong429fff42020-01-30 14:38:21 -0500439 SkSerialProcs procs;
440 procs.fTypefaceProc = [](SkTypeface* tf, void* ctx){
441 return tf->serialize(SkTypeface::SerializeBehavior::kDoIncludeData);
442 };
John Reck76005182021-06-09 22:43:05 -0400443 auto data = picture->serialize(&procs);
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400444 savePictureAsync(data, mCapturedFile);
445 mCaptureSequence = 0;
Derek Sollenberger5f9753d2020-04-01 15:59:02 -0400446 mCaptureMode = CaptureMode::None;
Stan Ilieve9d00122017-09-19 12:07:10 -0400447 }
Stan Ilieve9d00122017-09-19 12:07:10 -0400448 }
449 mRecorder.reset();
450 }
451}
452
John Reck29b1ee02023-04-04 17:44:23 -0400453class ForceDitherCanvas : public SkPaintFilterCanvas {
454public:
455 ForceDitherCanvas(SkCanvas* canvas) : SkPaintFilterCanvas(canvas) {}
456
457protected:
458 bool onFilter(SkPaint& paint) const override {
459 paint.setDither(true);
460 return true;
461 }
462
463 void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override {
464 // We unroll the drawable using "this" canvas, so that draw calls contained inside will
465 // get dithering applied
466 drawable->draw(this, matrix);
467 }
468};
469
Stan Iliev500a0c32016-10-26 10:30:09 -0400470void SkiaPipeline::renderFrame(const LayerUpdateQueue& layers, const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700471 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Greg Danielc4076782019-01-08 16:01:18 -0500472 const Rect& contentDrawBounds, sk_sp<SkSurface> surface,
473 const SkMatrix& preTransform) {
John Reck5cca8f22018-12-10 17:06:22 -0800474 bool previousSkpEnabled = Properties::skpCaptureEnabled;
475 if (mPictureCapturedCallback) {
476 Properties::skpCaptureEnabled = true;
477 }
478
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500479 // Initialize the canvas for the current frame, that might be a recording canvas if SKP
480 // capture is enabled.
481 SkCanvas* canvas = tryCapture(surface.get(), nodes[0].get(), layers);
482
Stan Iliev500a0c32016-10-26 10:30:09 -0400483 // draw all layers up front
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700484 renderLayersImpl(layers, opaque);
Stan Iliev500a0c32016-10-26 10:30:09 -0400485
Nathaniel Nifongdc19a652019-11-11 11:47:50 -0500486 renderFrameImpl(clip, nodes, opaque, contentDrawBounds, canvas, preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -0500487
Stan Ilieve9d00122017-09-19 12:07:10 -0400488 endCapture(surface.get());
Matt Sarettf58cc922016-11-14 18:33:38 -0500489
490 if (CC_UNLIKELY(Properties::debugOverdraw)) {
Nathaniel Nifongdc19a652019-11-11 11:47:50 -0500491 renderOverdraw(clip, nodes, contentDrawBounds, surface, preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -0500492 }
493
John Reck5cca8f22018-12-10 17:06:22 -0800494 Properties::skpCaptureEnabled = previousSkpEnabled;
Matt Sarettf58cc922016-11-14 18:33:38 -0500495}
496
Stan Iliev52771272016-11-17 09:54:38 -0500497namespace {
498static Rect nodeBounds(RenderNode& node) {
499 auto& props = node.properties();
John Reck1bcacfd2017-11-03 10:12:19 -0700500 return Rect(props.getLeft(), props.getTop(), props.getRight(), props.getBottom());
Stan Iliev52771272016-11-17 09:54:38 -0500501}
John Reck0fa0cbc2019-04-05 16:57:46 -0700502} // namespace
Stan Iliev52771272016-11-17 09:54:38 -0500503
Nathaniel Nifongdc19a652019-11-11 11:47:50 -0500504void SkiaPipeline::renderFrameImpl(const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700505 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Greg Danielc4076782019-01-08 16:01:18 -0500506 const Rect& contentDrawBounds, SkCanvas* canvas,
507 const SkMatrix& preTransform) {
Stan Ilievb66b8bb2016-12-15 18:17:42 -0500508 SkAutoCanvasRestore saver(canvas, true);
Nathaniel Nifongcff969f2020-01-09 14:03:49 -0500509 auto clipRestriction = preTransform.mapRect(clip).roundOut();
John Reck76005182021-06-09 22:43:05 -0400510 if (CC_UNLIKELY(isCapturingSkp())) {
Nathaniel Nifong89a5dc52020-01-17 15:32:17 -0500511 canvas->drawAnnotation(SkRect::Make(clipRestriction), "AndroidDeviceClipRestriction",
512 nullptr);
513 } else {
514 // clip drawing to dirty region only when not recording SKP files (which should contain all
515 // draw ops on every frame)
516 canvas->androidFramework_setDeviceClipRestriction(clipRestriction);
517 }
Greg Danielc4076782019-01-08 16:01:18 -0500518 canvas->concat(preTransform);
Stan Iliev500a0c32016-10-26 10:30:09 -0400519
Nader Jawadf5ce4522023-03-27 13:02:41 -0700520 if (!opaque) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400521 canvas->clear(SK_ColorTRANSPARENT);
522 }
523
John Reck29b1ee02023-04-04 17:44:23 -0400524 std::optional<ForceDitherCanvas> forceDitherCanvas;
525 if (shouldForceDither()) {
526 forceDitherCanvas.emplace(canvas);
527 canvas = &forceDitherCanvas.value();
528 }
529
Stan Iliev52771272016-11-17 09:54:38 -0500530 if (1 == nodes.size()) {
531 if (!nodes[0]->nothingToDraw()) {
Stan Iliev52771272016-11-17 09:54:38 -0500532 RenderNodeDrawable root(nodes[0].get(), canvas);
533 root.draw(canvas);
534 }
535 } else if (0 == nodes.size()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700536 // nothing to draw
Stan Iliev52771272016-11-17 09:54:38 -0500537 } else {
538 // It there are multiple render nodes, they are laid out as follows:
539 // #0 - backdrop (content + caption)
540 // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop)
541 // #2 - additional overlay nodes
John Reck1bcacfd2017-11-03 10:12:19 -0700542 // Usually the backdrop cannot be seen since it will be entirely covered by the content.
543 // While
544 // resizing however it might become partially visible. The following render loop will crop
545 // the
546 // backdrop against the content and draw the remaining part of it. It will then draw the
547 // content
Stan Iliev52771272016-11-17 09:54:38 -0500548 // cropped to the backdrop (since that indicates a shrinking of the window).
549 //
550 // Additional nodes will be drawn on top with no particular clipping semantics.
Stan Iliev500a0c32016-10-26 10:30:09 -0400551
Stan Iliev52771272016-11-17 09:54:38 -0500552 // Usually the contents bounds should be mContentDrawBounds - however - we will
553 // move it towards the fixed edge to give it a more stable appearance (for the moment).
554 // If there is no content bounds we ignore the layering as stated above and start with 2.
Stan Iliev500a0c32016-10-26 10:30:09 -0400555
Stan Iliev52771272016-11-17 09:54:38 -0500556 // Backdrop bounds in render target space
557 const Rect backdrop = nodeBounds(*nodes[0]);
Stan Iliev500a0c32016-10-26 10:30:09 -0400558
John Reck1bcacfd2017-11-03 10:12:19 -0700559 // Bounds that content will fill in render target space (note content node bounds may be
560 // bigger)
Stan Iliev52771272016-11-17 09:54:38 -0500561 Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight());
562 content.translate(backdrop.left, backdrop.top);
563 if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) {
564 // Content doesn't entirely overlap backdrop, so fill around content (right/bottom)
Stan Iliev500a0c32016-10-26 10:30:09 -0400565
Stan Iliev52771272016-11-17 09:54:38 -0500566 // 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 -0700567 // also fill left/top. Currently, both 2up and freeform position content at the top/left
568 // of
Stan Iliev52771272016-11-17 09:54:38 -0500569 // the backdrop, so this isn't necessary.
570 RenderNodeDrawable backdropNode(nodes[0].get(), canvas);
571 if (content.right < backdrop.right) {
572 // draw backdrop to right side of content
573 SkAutoCanvasRestore acr(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700574 canvas->clipRect(SkRect::MakeLTRB(content.right, backdrop.top, backdrop.right,
575 backdrop.bottom));
Stan Iliev52771272016-11-17 09:54:38 -0500576 backdropNode.draw(canvas);
577 }
578 if (content.bottom < backdrop.bottom) {
579 // draw backdrop to bottom of content
580 // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill
581 SkAutoCanvasRestore acr(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700582 canvas->clipRect(SkRect::MakeLTRB(content.left, content.bottom, content.right,
583 backdrop.bottom));
Stan Iliev52771272016-11-17 09:54:38 -0500584 backdropNode.draw(canvas);
585 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400586 }
587
Stan Iliev52771272016-11-17 09:54:38 -0500588 RenderNodeDrawable contentNode(nodes[1].get(), canvas);
589 if (!backdrop.isEmpty()) {
590 // content node translation to catch up with backdrop
591 float dx = backdrop.left - contentDrawBounds.left;
592 float dy = backdrop.top - contentDrawBounds.top;
593
594 SkAutoCanvasRestore acr(canvas, true);
595 canvas->translate(dx, dy);
John Reck1bcacfd2017-11-03 10:12:19 -0700596 const SkRect contentLocalClip =
597 SkRect::MakeXYWH(contentDrawBounds.left, contentDrawBounds.top,
598 backdrop.getWidth(), backdrop.getHeight());
Stan Iliev52771272016-11-17 09:54:38 -0500599 canvas->clipRect(contentLocalClip);
600 contentNode.draw(canvas);
601 } else {
602 SkAutoCanvasRestore acr(canvas, true);
603 contentNode.draw(canvas);
604 }
605
606 // remaining overlay nodes, simply defer
607 for (size_t index = 2; index < nodes.size(); index++) {
608 if (!nodes[index]->nothingToDraw()) {
609 SkAutoCanvasRestore acr(canvas, true);
610 RenderNodeDrawable overlayNode(nodes[index].get(), canvas);
611 overlayNode.draw(canvas);
612 }
613 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400614 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400615}
616
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500617void SkiaPipeline::dumpResourceCacheUsage() const {
Robert Phillips57bb0bf2019-09-06 13:18:17 -0400618 int resources;
619 size_t bytes;
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500620 mRenderThread.getGrContext()->getResourceCacheUsage(&resources, &bytes);
Robert Phillips57bb0bf2019-09-06 13:18:17 -0400621 size_t maxBytes = mRenderThread.getGrContext()->getResourceCacheLimit();
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500622
623 SkString log("Resource Cache Usage:\n");
Robert Phillips57bb0bf2019-09-06 13:18:17 -0400624 log.appendf("%8d items\n", resources);
John Reck1bcacfd2017-11-03 10:12:19 -0700625 log.appendf("%8zu bytes (%.2f MB) out of %.2f MB maximum\n", bytes,
626 bytes * (1.0f / (1024.0f * 1024.0f)), maxBytes * (1.0f / (1024.0f * 1024.0f)));
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500627
628 ALOGD("%s", log.c_str());
629}
630
Nader Jawada3521852023-01-30 20:23:46 -0800631void SkiaPipeline::setHardwareBuffer(AHardwareBuffer* buffer) {
632 if (mHardwareBuffer) {
633 AHardwareBuffer_release(mHardwareBuffer);
634 mHardwareBuffer = nullptr;
635 }
636
637 if (buffer) {
638 AHardwareBuffer_acquire(buffer);
639 mHardwareBuffer = buffer;
640 }
641}
642
643sk_sp<SkSurface> SkiaPipeline::getBufferSkSurface(
644 const renderthread::HardwareBufferRenderParams& bufferParams) {
645 auto bufferColorSpace = bufferParams.getColorSpace();
646 if (mBufferSurface == nullptr || mBufferColorSpace == nullptr ||
647 !SkColorSpace::Equals(mBufferColorSpace.get(), bufferColorSpace.get())) {
648 mBufferSurface = SkSurface::MakeFromAHardwareBuffer(
649 mRenderThread.getGrContext(), mHardwareBuffer, kTopLeft_GrSurfaceOrigin,
650 bufferColorSpace, nullptr, true);
651 mBufferColorSpace = bufferColorSpace;
652 }
653 return mBufferSurface;
654}
655
Peiyong Lin3bff1352018-12-11 07:56:07 -0800656void SkiaPipeline::setSurfaceColorProperties(ColorMode colorMode) {
Derek Sollenberger1863d942020-02-05 15:41:51 -0500657 mColorMode = colorMode;
John Reckb36bfdd2020-07-23 13:47:49 -0700658 switch (colorMode) {
659 case ColorMode::Default:
660 mSurfaceColorType = SkColorType::kN32_SkColorType;
661 mSurfaceColorSpace = SkColorSpace::MakeSRGB();
662 break;
663 case ColorMode::WideColorGamut:
664 mSurfaceColorType = DeviceInfo::get()->getWideColorType();
665 mSurfaceColorSpace = DeviceInfo::get()->getWideColorSpace();
666 break;
667 case ColorMode::Hdr:
John Reck55887762023-01-25 16:51:18 -0500668 mSurfaceColorType = SkColorType::kN32_SkColorType;
669 mSurfaceColorSpace = SkColorSpace::MakeRGB(
670 GetExtendedTransferFunction(mTargetSdrHdrRatio), SkNamedGamut::kDisplayP3);
John Reckb36bfdd2020-07-23 13:47:49 -0700671 break;
John Reck0b3f3312023-01-31 16:21:28 -0500672 case ColorMode::Hdr10:
673 mSurfaceColorType = SkColorType::kRGBA_1010102_SkColorType;
674 mSurfaceColorSpace = SkColorSpace::MakeRGB(
675 GetExtendedTransferFunction(mTargetSdrHdrRatio), SkNamedGamut::kDisplayP3);
676 break;
Leon Scroggins IIIcbdbb662021-11-30 13:59:00 -0500677 case ColorMode::A8:
678 mSurfaceColorType = SkColorType::kAlpha_8_SkColorType;
679 mSurfaceColorSpace = nullptr;
680 break;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800681 }
682}
683
John Reck55887762023-01-25 16:51:18 -0500684void SkiaPipeline::setTargetSdrHdrRatio(float ratio) {
John Reck0b3f3312023-01-31 16:21:28 -0500685 if (mColorMode == ColorMode::Hdr || mColorMode == ColorMode::Hdr10) {
John Reck55887762023-01-25 16:51:18 -0500686 mTargetSdrHdrRatio = ratio;
687 mSurfaceColorSpace = SkColorSpace::MakeRGB(GetExtendedTransferFunction(mTargetSdrHdrRatio),
688 SkNamedGamut::kDisplayP3);
689 } else {
690 mTargetSdrHdrRatio = 1.f;
691 }
692}
693
Matt Sarettf58cc922016-11-14 18:33:38 -0500694// Overdraw debugging
695
696// These colors should be kept in sync with Caches::getOverdrawColor() with a few differences.
Mike Reed331c4e12020-02-13 10:21:54 -0500697// This implementation requires transparent entries for "no overdraw" and "single draws".
698static const SkColor kOverdrawColors[2][6] = {
699 {
700 0x00000000,
701 0x00000000,
702 0x2f0000ff,
703 0x2f00ff00,
704 0x3fff0000,
705 0x7fff0000,
706 },
707 {
708 0x00000000,
709 0x00000000,
710 0x2f0000ff,
711 0x4fffff00,
712 0x5fff89d7,
713 0x7fff0000,
714 },
Matt Sarettf58cc922016-11-14 18:33:38 -0500715};
716
Nathaniel Nifongdc19a652019-11-11 11:47:50 -0500717void SkiaPipeline::renderOverdraw(const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700718 const std::vector<sp<RenderNode>>& nodes,
Greg Danielc4076782019-01-08 16:01:18 -0500719 const Rect& contentDrawBounds, sk_sp<SkSurface> surface,
720 const SkMatrix& preTransform) {
Matt Sarettf58cc922016-11-14 18:33:38 -0500721 // Set up the overdraw canvas.
722 SkImageInfo offscreenInfo = SkImageInfo::MakeA8(surface->width(), surface->height());
723 sk_sp<SkSurface> offscreen = surface->makeSurface(offscreenInfo);
Nathaniel Nifongfe05b7c2019-09-17 12:52:52 -0400724 LOG_ALWAYS_FATAL_IF(!offscreen, "Failed to create offscreen SkSurface for overdraw viz.");
Matt Sarettf58cc922016-11-14 18:33:38 -0500725 SkOverdrawCanvas overdrawCanvas(offscreen->getCanvas());
726
727 // Fake a redraw to replay the draw commands. This will increment the alpha channel
728 // each time a pixel would have been drawn.
729 // Pass true for opaque so we skip the clear - the overdrawCanvas is already zero
730 // initialized.
Nathaniel Nifongdc19a652019-11-11 11:47:50 -0500731 renderFrameImpl(clip, nodes, true, contentDrawBounds, &overdrawCanvas, preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -0500732 sk_sp<SkImage> counts = offscreen->makeImageSnapshot();
733
734 // Draw overdraw colors to the canvas. The color filter will convert counts to colors.
735 SkPaint paint;
Mike Reed331c4e12020-02-13 10:21:54 -0500736 const SkColor* colors = kOverdrawColors[static_cast<int>(Properties::overdrawColorSet)];
737 paint.setColorFilter(SkOverdrawColorFilter::MakeWithSkColors(colors));
Mike Reed7994a312021-01-28 18:06:26 -0500738 surface->getCanvas()->drawImage(counts.get(), 0.0f, 0.0f, SkSamplingOptions(), &paint);
Matt Sarettf58cc922016-11-14 18:33:38 -0500739}
740
Stan Iliev500a0c32016-10-26 10:30:09 -0400741} /* namespace skiapipeline */
742} /* namespace uirenderer */
743} /* namespace android */