blob: 29b4dd7f32e7b22c9ab07587d8c9b2cc232015f9 [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
Hal Canary10219fb2016-11-23 20:41:22 -050019#include <SkImageEncoder.h>
Peiyong Lin3bff1352018-12-11 07:56:07 -080020#include <SkImageInfo.h>
Leon Scroggins IIIee708fa2016-12-12 15:31:39 -050021#include <SkImagePriv.h>
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040022#include <SkMultiPictureDocument.h>
Matt Sarettf58cc922016-11-14 18:33:38 -050023#include <SkOverdrawCanvas.h>
24#include <SkOverdrawColorFilter.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040025#include <SkPicture.h>
26#include <SkPictureRecorder.h>
Nathaniel Nifong429fff42020-01-30 14:38:21 -050027#include <SkTypeface.h>
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040028#include <SkSerialProcs.h>
Fedor Kudasov90df0562019-06-19 11:41:34 +010029#include "LightingInfo.h"
Stan Iliev23c38a92017-03-23 00:12:50 -040030#include "VectorDrawable.h"
John Reck322b8ab2019-03-14 13:15:28 -070031#include "thread/CommonPool.h"
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040032#include "tools/SkSharingProc.h"
John Reck1bcacfd2017-11-03 10:12:19 -070033#include "utils/TraceUtils.h"
Nathaniel Nifong2945bff2019-11-25 09:34:21 -050034#include "utils/String8.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040035
Ben Wagner3aeda5c2017-03-17 17:22:01 -040036#include <unistd.h>
37
Jerome Gaillarda02a12d2019-05-28 18:07:56 +010038#include <android-base/properties.h>
39
Stan Iliev500a0c32016-10-26 10:30:09 -040040using namespace android::uirenderer::renderthread;
41
42namespace android {
43namespace uirenderer {
44namespace skiapipeline {
45
John Reck1bcacfd2017-11-03 10:12:19 -070046SkiaPipeline::SkiaPipeline(RenderThread& thread) : mRenderThread(thread) {
Derek Sollenberger1863d942020-02-05 15:41:51 -050047 setSurfaceColorProperties(mColorMode);
Stan Iliev23c38a92017-03-23 00:12:50 -040048}
Stan Iliev500a0c32016-10-26 10:30:09 -040049
Stan Iliev232f3622017-08-23 17:15:09 -040050SkiaPipeline::~SkiaPipeline() {
51 unpinImages();
52}
53
Stan Iliev500a0c32016-10-26 10:30:09 -040054void SkiaPipeline::onDestroyHardwareResources() {
Derek Sollenberger92a9eb92018-04-12 13:42:19 -040055 unpinImages();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040056 mRenderThread.cacheManager().trimStaleResources();
Stan Iliev500a0c32016-10-26 10:30:09 -040057}
58
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -040059bool SkiaPipeline::pinImages(std::vector<SkImage*>& mutableImages) {
60 for (SkImage* image : mutableImages) {
Derek Sollenberger189e8742016-11-16 16:00:17 -050061 if (SkImage_pinAsTexture(image, mRenderThread.getGrContext())) {
62 mPinnedImages.emplace_back(sk_ref_sp(image));
63 } else {
64 return false;
65 }
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -040066 }
67 return true;
68}
69
70void SkiaPipeline::unpinImages() {
71 for (auto& image : mPinnedImages) {
72 SkImage_unpinAsTexture(image.get(), mRenderThread.getGrContext());
73 }
74 mPinnedImages.clear();
75}
76
John Reckd9d7f122018-05-03 14:40:56 -070077void SkiaPipeline::renderLayers(const LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070078 LayerUpdateQueue* layerUpdateQueue, bool opaque,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070079 const LightInfo& lightInfo) {
Fedor Kudasov90df0562019-06-19 11:41:34 +010080 LightingInfo::updateLighting(lightGeometry, lightInfo);
Stan Iliev500a0c32016-10-26 10:30:09 -040081 ATRACE_NAME("draw layers");
Peiyong Lin1f6aa122018-09-10 16:28:08 -070082 renderLayersImpl(*layerUpdateQueue, opaque);
Stan Iliev500a0c32016-10-26 10:30:09 -040083 layerUpdateQueue->clear();
84}
85
Peiyong Lin1f6aa122018-09-10 16:28:08 -070086void SkiaPipeline::renderLayersImpl(const LayerUpdateQueue& layers, bool opaque) {
Derek Sollenbergerf7df1842017-09-05 11:15:58 -040087 sk_sp<GrContext> cachedContext;
88
Stan Iliev500a0c32016-10-26 10:30:09 -040089 // Render all layers that need to be updated, in order.
90 for (size_t i = 0; i < layers.entries().size(); i++) {
John Reckfc29f7acd2017-03-02 13:23:16 -080091 RenderNode* layerNode = layers.entries()[i].renderNode.get();
Stan Iliev500a0c32016-10-26 10:30:09 -040092 // only schedule repaint if node still on layer - possible it may have been
93 // removed during a dropped frame, but layers may still remain scheduled so
94 // as not to lose info on what portion is damaged
Nathaniel Nifong2945bff2019-11-25 09:34:21 -050095 if (CC_UNLIKELY(layerNode->getLayerSurface() == nullptr)) {
96 continue;
97 }
98 SkASSERT(layerNode->getLayerSurface());
99 SkiaDisplayList* displayList = (SkiaDisplayList*)layerNode->getDisplayList();
100 if (!displayList || displayList->isEmpty()) {
101 ALOGE("%p drawLayers(%s) : missing drawable", layerNode, layerNode->getName());
102 return;
103 }
104
105 const Rect& layerDamage = layers.entries()[i].damage;
106
107 SkCanvas* layerCanvas = layerNode->getLayerSurface()->getCanvas();
108
109 int saveCount = layerCanvas->save();
110 SkASSERT(saveCount == 1);
111
112 layerCanvas->androidFramework_setDeviceClipRestriction(layerDamage.toSkIRect());
113
114 // TODO: put localized light center calculation and storage to a drawable related code.
115 // It does not seem right to store something localized in a global state
116 // fix here and in recordLayers
117 const Vector3 savedLightCenter(LightingInfo::getLightCenterRaw());
118 Vector3 transformedLightCenter(savedLightCenter);
119 // map current light center into RenderNode's coordinate space
120 layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(transformedLightCenter);
121 LightingInfo::setLightCenterRaw(transformedLightCenter);
122
123 const RenderProperties& properties = layerNode->properties();
124 const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
125 if (properties.getClipToBounds() && layerCanvas->quickReject(bounds)) {
126 return;
127 }
128
129 ATRACE_FORMAT("drawLayer [%s] %.1f x %.1f", layerNode->getName(), bounds.width(),
130 bounds.height());
131
132 layerNode->getSkiaLayer()->hasRenderedSinceRepaint = false;
133 layerCanvas->clear(SK_ColorTRANSPARENT);
134
135 RenderNodeDrawable root(layerNode, layerCanvas, false);
136 root.forceDraw(layerCanvas);
137 layerCanvas->restoreToCount(saveCount);
138
139 LightingInfo::setLightCenterRaw(savedLightCenter);
140
141 // cache the current context so that we can defer flushing it until
142 // either all the layers have been rendered or the context changes
143 GrContext* currentContext = layerNode->getLayerSurface()->getCanvas()->getGrContext();
144 if (cachedContext.get() != currentContext) {
145 if (cachedContext.get()) {
146 ATRACE_NAME("flush layers (context changed)");
147 cachedContext->flush();
Stan Iliev500a0c32016-10-26 10:30:09 -0400148 }
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500149 cachedContext.reset(SkSafeRef(currentContext));
Stan Iliev500a0c32016-10-26 10:30:09 -0400150 }
151 }
Derek Sollenbergerf7df1842017-09-05 11:15:58 -0400152
153 if (cachedContext.get()) {
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -0400154 ATRACE_NAME("flush layers");
Derek Sollenbergerf7df1842017-09-05 11:15:58 -0400155 cachedContext->flush();
156 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400157}
158
John Reck1bcacfd2017-11-03 10:12:19 -0700159bool SkiaPipeline::createOrUpdateLayer(RenderNode* node, const DamageAccumulator& damageAccumulator,
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700160 ErrorHandler* errorHandler) {
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500161 // compute the size of the surface (i.e. texture) to be allocated for this layer
162 const int surfaceWidth = ceilf(node->getWidth() / float(LAYER_SIZE)) * LAYER_SIZE;
163 const int surfaceHeight = ceilf(node->getHeight() / float(LAYER_SIZE)) * LAYER_SIZE;
164
Stan Iliev500a0c32016-10-26 10:30:09 -0400165 SkSurface* layer = node->getLayerSurface();
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500166 if (!layer || layer->width() != surfaceWidth || layer->height() != surfaceHeight) {
Stan Iliev08fc19a2017-07-24 10:20:33 -0400167 SkImageInfo info;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700168 info = SkImageInfo::Make(surfaceWidth, surfaceHeight, getSurfaceColorType(),
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400169 kPremul_SkAlphaType, getSurfaceColorSpace());
Stan Iliev500a0c32016-10-26 10:30:09 -0400170 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
171 SkASSERT(mRenderThread.getGrContext() != nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -0700172 node->setLayerSurface(SkSurface::MakeRenderTarget(mRenderThread.getGrContext(),
Derek Sollenberger25833d22019-01-14 13:55:55 -0500173 SkBudgeted::kYes, info, 0,
174 this->getSurfaceOrigin(), &props));
Stan Iliev500a0c32016-10-26 10:30:09 -0400175 if (node->getLayerSurface()) {
176 // update the transform in window of the layer to reset its origin wrt light source
177 // position
178 Matrix4 windowTransform;
179 damageAccumulator.computeCurrentTransform(&windowTransform);
Stan Ilievda7c19c2019-05-22 14:43:44 -0400180 node->getSkiaLayer()->inverseTransformInWindow.loadInverse(windowTransform);
Stan Iliev216b1572018-03-26 14:29:50 -0400181 } else {
182 String8 cachesOutput;
183 mRenderThread.cacheManager().dumpMemoryUsage(cachesOutput,
John Reck283bb462018-12-13 16:40:14 -0800184 &mRenderThread.renderState());
Stan Iliev216b1572018-03-26 14:29:50 -0400185 ALOGE("%s", cachesOutput.string());
186 if (errorHandler) {
187 std::ostringstream err;
188 err << "Unable to create layer for " << node->getName();
189 const int maxTextureSize = DeviceInfo::get()->maxTextureSize();
190 err << ", size " << info.width() << "x" << info.height() << " max size "
John Reck283bb462018-12-13 16:40:14 -0800191 << maxTextureSize << " color type " << (int)info.colorType() << " has context "
192 << (int)(mRenderThread.getGrContext() != nullptr);
Stan Iliev216b1572018-03-26 14:29:50 -0400193 errorHandler->onError(err.str());
194 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400195 }
196 return true;
197 }
198 return false;
199}
200
Stan Iliev500a0c32016-10-26 10:30:09 -0400201void SkiaPipeline::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) {
202 GrContext* context = thread.getGrContext();
203 if (context) {
204 ATRACE_FORMAT("Bitmap#prepareToDraw %dx%d", bitmap->width(), bitmap->height());
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400205 auto image = bitmap->makeImage();
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400206 if (image.get() && !bitmap->isHardware()) {
207 SkImage_pinAsTexture(image.get(), context);
208 SkImage_unpinAsTexture(image.get(), context);
209 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400210 }
211}
212
John Reck322b8ab2019-03-14 13:15:28 -0700213static void savePictureAsync(const sk_sp<SkData>& data, const std::string& filename) {
214 CommonPool::post([data, filename] {
215 if (0 == access(filename.c_str(), F_OK)) {
Stan Ilieve9d00122017-09-19 12:07:10 -0400216 return;
217 }
218
John Reck322b8ab2019-03-14 13:15:28 -0700219 SkFILEWStream stream(filename.c_str());
Stan Ilieve9d00122017-09-19 12:07:10 -0400220 if (stream.isValid()) {
John Reck322b8ab2019-03-14 13:15:28 -0700221 stream.write(data->data(), data->size());
Stan Ilieve9d00122017-09-19 12:07:10 -0400222 stream.flush();
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400223 ALOGD("SKP Captured Drawing Output (%zu bytes) for frame. %s", stream.bytesWritten(),
John Reck322b8ab2019-03-14 13:15:28 -0700224 filename.c_str());
Stan Ilieve9d00122017-09-19 12:07:10 -0400225 }
John Reck322b8ab2019-03-14 13:15:28 -0700226 });
227}
Stan Ilieve9d00122017-09-19 12:07:10 -0400228
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400229// Note multiple SkiaPipeline instances may be loaded if more than one app is visible.
230// Each instance may observe the filename changing and try to record to a file of the same name.
231// Only the first one will succeed. There is no scope available here where we could coordinate
232// to cause this function to return true for only one of the instances.
233bool SkiaPipeline::shouldStartNewFileCapture() {
234 // Don't start a new file based capture if one is currently ongoing.
235 if (mCaptureMode != CaptureMode::None) { return false; }
236
237 // A new capture is started when the filename property changes.
238 // Read the filename property.
239 std::string prop = base::GetProperty(PROPERTY_CAPTURE_SKP_FILENAME, "0");
240 // if the filename property changed to a valid value
241 if (prop[0] != '0' && mCapturedFile != prop) {
242 // remember this new filename
243 mCapturedFile = prop;
244 // and get a property indicating how many frames to capture.
245 mCaptureSequence = base::GetIntProperty(PROPERTY_CAPTURE_SKP_FRAMES, 1);
John Reck5cca8f22018-12-10 17:06:22 -0800246 if (mCaptureSequence <= 0) {
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400247 return false;
248 } else if (mCaptureSequence == 1) {
249 mCaptureMode = CaptureMode::SingleFrameSKP;
250 } else {
251 mCaptureMode = CaptureMode::MultiFrameSKP;
Stan Ilieve9d00122017-09-19 12:07:10 -0400252 }
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400253 return true;
254 }
255 return false;
256}
257
258// performs the first-frame work of a multi frame SKP capture. Returns true if successful.
259bool SkiaPipeline::setupMultiFrameCapture() {
260 ALOGD("Set up multi-frame capture, frames = %d", mCaptureSequence);
261 // We own this stream and need to hold it until close() finishes.
262 auto stream = std::make_unique<SkFILEWStream>(mCapturedFile.c_str());
263 if (stream->isValid()) {
264 mOpenMultiPicStream = std::move(stream);
265 mSerialContext.reset(new SkSharingSerialContext());
266 SkSerialProcs procs;
267 procs.fImageProc = SkSharingSerialContext::serializeImage;
268 procs.fImageCtx = mSerialContext.get();
Nathaniel Nifong429fff42020-01-30 14:38:21 -0500269 procs.fTypefaceProc = [](SkTypeface* tf, void* ctx){
270 return tf->serialize(SkTypeface::SerializeBehavior::kDoIncludeData);
271 };
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400272 // SkDocuments don't take owership of the streams they write.
273 // we need to keep it until after mMultiPic.close()
274 // procs is passed as a pointer, but just as a method of having an optional default.
275 // procs doesn't need to outlive this Make call.
276 mMultiPic = SkMakeMultiPictureDocument(mOpenMultiPicStream.get(), &procs);
277 return true;
278 } else {
279 ALOGE("Could not open \"%s\" for writing.", mCapturedFile.c_str());
280 mCaptureSequence = 0;
281 mCaptureMode = CaptureMode::None;
282 return false;
283 }
284}
285
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500286// recurse through the rendernode's children, add any nodes which are layers to the queue.
287static void collectLayers(RenderNode* node, LayerUpdateQueue* layers) {
288 SkiaDisplayList* dl = (SkiaDisplayList*)node->getDisplayList();
289 if (dl) {
290 const auto& prop = node->properties();
291 if (node->hasLayer()) {
292 layers->enqueueLayerWithDamage(node, Rect(prop.getWidth(), prop.getHeight()));
293 }
294 // The way to recurse through rendernodes is to call this with a lambda.
295 dl->updateChildren([&](RenderNode* child) { collectLayers(child, layers); });
296 }
297}
298
299// record the provided layers to the provided canvas as self-contained skpictures.
300static void recordLayers(const LayerUpdateQueue& layers,
301 SkCanvas* mskpCanvas) {
302 const Vector3 savedLightCenter(LightingInfo::getLightCenterRaw());
303 // Record the commands to re-draw each dirty layer into an SkPicture
304 for (size_t i = 0; i < layers.entries().size(); i++) {
305 RenderNode* layerNode = layers.entries()[i].renderNode.get();
306 const Rect& layerDamage = layers.entries()[i].damage;
307 const RenderProperties& properties = layerNode->properties();
308
309 // Temporarily map current light center into RenderNode's coordinate space
310 Vector3 transformedLightCenter(savedLightCenter);
311 layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(transformedLightCenter);
312 LightingInfo::setLightCenterRaw(transformedLightCenter);
313
314 SkPictureRecorder layerRec;
315 auto* recCanvas = layerRec.beginRecording(properties.getWidth(),
316 properties.getHeight());
317 // This is not recorded but still causes clipping.
318 recCanvas->androidFramework_setDeviceClipRestriction(layerDamage.toSkIRect());
319 RenderNodeDrawable root(layerNode, recCanvas, false);
320 root.forceDraw(recCanvas);
321 // Now write this picture into the SKP canvas with an annotation indicating what it is
322 mskpCanvas->drawAnnotation(layerDamage.toSkRect(), String8::format(
323 "OffscreenLayerDraw|%" PRId64, layerNode->uniqueId()).c_str(), nullptr);
324 mskpCanvas->drawPicture(layerRec.finishRecordingAsPicture());
325 }
326 LightingInfo::setLightCenterRaw(savedLightCenter);
327}
328
329SkCanvas* SkiaPipeline::tryCapture(SkSurface* surface, RenderNode* root,
330 const LayerUpdateQueue& dirtyLayers) {
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400331 if (CC_LIKELY(!Properties::skpCaptureEnabled)) {
332 return surface->getCanvas(); // Bail out early when capture is not turned on.
333 }
334 // Note that shouldStartNewFileCapture tells us if this is the *first* frame of a capture.
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500335 bool firstFrameOfAnim = false;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400336 if (shouldStartNewFileCapture() && mCaptureMode == CaptureMode::MultiFrameSKP) {
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500337 // set a reminder to record every layer near the end of this method, after we have set up
338 // the nway canvas.
339 firstFrameOfAnim = true;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400340 if (!setupMultiFrameCapture()) {
341 return surface->getCanvas();
Stan Ilieve9d00122017-09-19 12:07:10 -0400342 }
343 }
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400344
345 // Create a canvas pointer, fill it depending on what kind of capture is requested (if any)
346 SkCanvas* pictureCanvas = nullptr;
347 switch (mCaptureMode) {
348 case CaptureMode::CallbackAPI:
349 case CaptureMode::SingleFrameSKP:
350 mRecorder.reset(new SkPictureRecorder());
Nathaniel Nifong31fee3a2019-07-11 16:27:14 -0400351 pictureCanvas = mRecorder->beginRecording(surface->width(), surface->height());
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400352 break;
353 case CaptureMode::MultiFrameSKP:
354 // If a multi frame recording is active, initialize recording for a single frame of a
355 // multi frame file.
356 pictureCanvas = mMultiPic->beginPage(surface->width(), surface->height());
357 break;
358 case CaptureMode::None:
359 // Returning here in the non-capture case means we can count on pictureCanvas being
360 // non-null below.
361 return surface->getCanvas();
362 }
363
364 // Setting up an nway canvas is common to any kind of capture.
365 mNwayCanvas = std::make_unique<SkNWayCanvas>(surface->width(), surface->height());
366 mNwayCanvas->addCanvas(surface->getCanvas());
367 mNwayCanvas->addCanvas(pictureCanvas);
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500368
369 if (firstFrameOfAnim) {
370 // On the first frame of any mskp capture we want to record any layers that are needed in
371 // frame but may have been rendered offscreen before recording began.
372 // We do not maintain a list of all layers, since it isn't needed outside this rare,
373 // recording use case. Traverse the tree to find them and put them in this LayerUpdateQueue.
374 LayerUpdateQueue luq;
375 collectLayers(root, &luq);
376 recordLayers(luq, mNwayCanvas.get());
377 } else {
378 // on non-first frames, we record any normal layer draws (dirty regions)
379 recordLayers(dirtyLayers, mNwayCanvas.get());
380 }
381
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400382 return mNwayCanvas.get();
Stan Ilieve9d00122017-09-19 12:07:10 -0400383}
384
385void SkiaPipeline::endCapture(SkSurface* surface) {
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400386 if (CC_LIKELY(mCaptureMode == CaptureMode::None)) { return; }
John Reck5cca8f22018-12-10 17:06:22 -0800387 mNwayCanvas.reset();
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400388 ATRACE_CALL();
389 if (mCaptureSequence > 0 && mCaptureMode == CaptureMode::MultiFrameSKP) {
390 mMultiPic->endPage();
391 mCaptureSequence--;
392 if (mCaptureSequence == 0) {
393 mCaptureMode = CaptureMode::None;
394 // Pass mMultiPic and mOpenMultiPicStream to a background thread, which will handle
395 // the heavyweight serialization work and destroy them. mOpenMultiPicStream is released
396 // to a bare pointer because keeping it in a smart pointer makes the lambda
397 // non-copyable. The lambda is only called once, so this is safe.
398 SkFILEWStream* stream = mOpenMultiPicStream.release();
399 CommonPool::post([doc = std::move(mMultiPic), stream]{
400 ALOGD("Finalizing multi frame SKP");
401 doc->close();
402 delete stream;
403 ALOGD("Multi frame SKP complete.");
404 });
405 }
406 } else {
Stan Ilieve9d00122017-09-19 12:07:10 -0400407 sk_sp<SkPicture> picture = mRecorder->finishRecordingAsPicture();
Stan Ilieve9d00122017-09-19 12:07:10 -0400408 if (picture->approximateOpCount() > 0) {
John Reck5cca8f22018-12-10 17:06:22 -0800409 if (mPictureCapturedCallback) {
410 std::invoke(mPictureCapturedCallback, std::move(picture));
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400411 } else {
412 // single frame skp to file
Nathaniel Nifong429fff42020-01-30 14:38:21 -0500413 SkSerialProcs procs;
414 procs.fTypefaceProc = [](SkTypeface* tf, void* ctx){
415 return tf->serialize(SkTypeface::SerializeBehavior::kDoIncludeData);
416 };
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400417 auto data = picture->serialize();
418 savePictureAsync(data, mCapturedFile);
419 mCaptureSequence = 0;
Stan Ilieve9d00122017-09-19 12:07:10 -0400420 }
Stan Ilieve9d00122017-09-19 12:07:10 -0400421 }
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400422 mCaptureMode = CaptureMode::None;
Stan Ilieve9d00122017-09-19 12:07:10 -0400423 mRecorder.reset();
424 }
425}
426
Stan Iliev500a0c32016-10-26 10:30:09 -0400427void SkiaPipeline::renderFrame(const LayerUpdateQueue& layers, const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700428 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Greg Danielc4076782019-01-08 16:01:18 -0500429 const Rect& contentDrawBounds, sk_sp<SkSurface> surface,
430 const SkMatrix& preTransform) {
John Reck5cca8f22018-12-10 17:06:22 -0800431 bool previousSkpEnabled = Properties::skpCaptureEnabled;
432 if (mPictureCapturedCallback) {
433 Properties::skpCaptureEnabled = true;
434 }
435
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500436 // Initialize the canvas for the current frame, that might be a recording canvas if SKP
437 // capture is enabled.
438 SkCanvas* canvas = tryCapture(surface.get(), nodes[0].get(), layers);
439
Stan Iliev500a0c32016-10-26 10:30:09 -0400440 // draw all layers up front
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700441 renderLayersImpl(layers, opaque);
Stan Iliev500a0c32016-10-26 10:30:09 -0400442
Nathaniel Nifongdc19a652019-11-11 11:47:50 -0500443 renderFrameImpl(clip, nodes, opaque, contentDrawBounds, canvas, preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -0500444
Stan Ilieve9d00122017-09-19 12:07:10 -0400445 endCapture(surface.get());
Matt Sarettf58cc922016-11-14 18:33:38 -0500446
447 if (CC_UNLIKELY(Properties::debugOverdraw)) {
Nathaniel Nifongdc19a652019-11-11 11:47:50 -0500448 renderOverdraw(clip, nodes, contentDrawBounds, surface, preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -0500449 }
450
451 ATRACE_NAME("flush commands");
Stan Ilieve9d00122017-09-19 12:07:10 -0400452 surface->getCanvas()->flush();
John Reck5cca8f22018-12-10 17:06:22 -0800453
454 Properties::skpCaptureEnabled = previousSkpEnabled;
Matt Sarettf58cc922016-11-14 18:33:38 -0500455}
456
Stan Iliev52771272016-11-17 09:54:38 -0500457namespace {
458static Rect nodeBounds(RenderNode& node) {
459 auto& props = node.properties();
John Reck1bcacfd2017-11-03 10:12:19 -0700460 return Rect(props.getLeft(), props.getTop(), props.getRight(), props.getBottom());
Stan Iliev52771272016-11-17 09:54:38 -0500461}
John Reck0fa0cbc2019-04-05 16:57:46 -0700462} // namespace
Stan Iliev52771272016-11-17 09:54:38 -0500463
Nathaniel Nifongdc19a652019-11-11 11:47:50 -0500464void SkiaPipeline::renderFrameImpl(const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700465 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Greg Danielc4076782019-01-08 16:01:18 -0500466 const Rect& contentDrawBounds, SkCanvas* canvas,
467 const SkMatrix& preTransform) {
Stan Ilievb66b8bb2016-12-15 18:17:42 -0500468 SkAutoCanvasRestore saver(canvas, true);
Nathaniel Nifongcff969f2020-01-09 14:03:49 -0500469 auto clipRestriction = preTransform.mapRect(clip).roundOut();
Nathaniel Nifong89a5dc52020-01-17 15:32:17 -0500470 if (CC_UNLIKELY(mCaptureMode == CaptureMode::SingleFrameSKP
471 || mCaptureMode == CaptureMode::MultiFrameSKP)) {
472 canvas->drawAnnotation(SkRect::Make(clipRestriction), "AndroidDeviceClipRestriction",
473 nullptr);
474 } else {
475 // clip drawing to dirty region only when not recording SKP files (which should contain all
476 // draw ops on every frame)
477 canvas->androidFramework_setDeviceClipRestriction(clipRestriction);
478 }
Greg Danielc4076782019-01-08 16:01:18 -0500479 canvas->concat(preTransform);
Stan Iliev500a0c32016-10-26 10:30:09 -0400480
Stan Ilievaadc0322018-03-23 10:50:11 -0400481 // STOPSHIP: Revert, temporary workaround to clear always F16 frame buffer for b/74976293
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700482 if (!opaque || getSurfaceColorType() == kRGBA_F16_SkColorType) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400483 canvas->clear(SK_ColorTRANSPARENT);
484 }
485
Stan Iliev52771272016-11-17 09:54:38 -0500486 if (1 == nodes.size()) {
487 if (!nodes[0]->nothingToDraw()) {
Stan Iliev52771272016-11-17 09:54:38 -0500488 RenderNodeDrawable root(nodes[0].get(), canvas);
489 root.draw(canvas);
490 }
491 } else if (0 == nodes.size()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700492 // nothing to draw
Stan Iliev52771272016-11-17 09:54:38 -0500493 } else {
494 // It there are multiple render nodes, they are laid out as follows:
495 // #0 - backdrop (content + caption)
496 // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop)
497 // #2 - additional overlay nodes
John Reck1bcacfd2017-11-03 10:12:19 -0700498 // Usually the backdrop cannot be seen since it will be entirely covered by the content.
499 // While
500 // resizing however it might become partially visible. The following render loop will crop
501 // the
502 // backdrop against the content and draw the remaining part of it. It will then draw the
503 // content
Stan Iliev52771272016-11-17 09:54:38 -0500504 // cropped to the backdrop (since that indicates a shrinking of the window).
505 //
506 // Additional nodes will be drawn on top with no particular clipping semantics.
Stan Iliev500a0c32016-10-26 10:30:09 -0400507
Stan Iliev52771272016-11-17 09:54:38 -0500508 // Usually the contents bounds should be mContentDrawBounds - however - we will
509 // move it towards the fixed edge to give it a more stable appearance (for the moment).
510 // If there is no content bounds we ignore the layering as stated above and start with 2.
Stan Iliev500a0c32016-10-26 10:30:09 -0400511
Stan Iliev52771272016-11-17 09:54:38 -0500512 // Backdrop bounds in render target space
513 const Rect backdrop = nodeBounds(*nodes[0]);
Stan Iliev500a0c32016-10-26 10:30:09 -0400514
John Reck1bcacfd2017-11-03 10:12:19 -0700515 // Bounds that content will fill in render target space (note content node bounds may be
516 // bigger)
Stan Iliev52771272016-11-17 09:54:38 -0500517 Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight());
518 content.translate(backdrop.left, backdrop.top);
519 if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) {
520 // Content doesn't entirely overlap backdrop, so fill around content (right/bottom)
Stan Iliev500a0c32016-10-26 10:30:09 -0400521
Stan Iliev52771272016-11-17 09:54:38 -0500522 // 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 -0700523 // also fill left/top. Currently, both 2up and freeform position content at the top/left
524 // of
Stan Iliev52771272016-11-17 09:54:38 -0500525 // the backdrop, so this isn't necessary.
526 RenderNodeDrawable backdropNode(nodes[0].get(), canvas);
527 if (content.right < backdrop.right) {
528 // draw backdrop to right side of content
529 SkAutoCanvasRestore acr(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700530 canvas->clipRect(SkRect::MakeLTRB(content.right, backdrop.top, backdrop.right,
531 backdrop.bottom));
Stan Iliev52771272016-11-17 09:54:38 -0500532 backdropNode.draw(canvas);
533 }
534 if (content.bottom < backdrop.bottom) {
535 // draw backdrop to bottom of content
536 // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill
537 SkAutoCanvasRestore acr(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700538 canvas->clipRect(SkRect::MakeLTRB(content.left, content.bottom, content.right,
539 backdrop.bottom));
Stan Iliev52771272016-11-17 09:54:38 -0500540 backdropNode.draw(canvas);
541 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400542 }
543
Stan Iliev52771272016-11-17 09:54:38 -0500544 RenderNodeDrawable contentNode(nodes[1].get(), canvas);
545 if (!backdrop.isEmpty()) {
546 // content node translation to catch up with backdrop
547 float dx = backdrop.left - contentDrawBounds.left;
548 float dy = backdrop.top - contentDrawBounds.top;
549
550 SkAutoCanvasRestore acr(canvas, true);
551 canvas->translate(dx, dy);
John Reck1bcacfd2017-11-03 10:12:19 -0700552 const SkRect contentLocalClip =
553 SkRect::MakeXYWH(contentDrawBounds.left, contentDrawBounds.top,
554 backdrop.getWidth(), backdrop.getHeight());
Stan Iliev52771272016-11-17 09:54:38 -0500555 canvas->clipRect(contentLocalClip);
556 contentNode.draw(canvas);
557 } else {
558 SkAutoCanvasRestore acr(canvas, true);
559 contentNode.draw(canvas);
560 }
561
562 // remaining overlay nodes, simply defer
563 for (size_t index = 2; index < nodes.size(); index++) {
564 if (!nodes[index]->nothingToDraw()) {
565 SkAutoCanvasRestore acr(canvas, true);
566 RenderNodeDrawable overlayNode(nodes[index].get(), canvas);
567 overlayNode.draw(canvas);
568 }
569 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400570 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400571}
572
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500573void SkiaPipeline::dumpResourceCacheUsage() const {
Robert Phillips57bb0bf2019-09-06 13:18:17 -0400574 int resources;
575 size_t bytes;
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500576 mRenderThread.getGrContext()->getResourceCacheUsage(&resources, &bytes);
Robert Phillips57bb0bf2019-09-06 13:18:17 -0400577 size_t maxBytes = mRenderThread.getGrContext()->getResourceCacheLimit();
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500578
579 SkString log("Resource Cache Usage:\n");
Robert Phillips57bb0bf2019-09-06 13:18:17 -0400580 log.appendf("%8d items\n", resources);
John Reck1bcacfd2017-11-03 10:12:19 -0700581 log.appendf("%8zu bytes (%.2f MB) out of %.2f MB maximum\n", bytes,
582 bytes * (1.0f / (1024.0f * 1024.0f)), maxBytes * (1.0f / (1024.0f * 1024.0f)));
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500583
584 ALOGD("%s", log.c_str());
585}
586
Peiyong Lin3bff1352018-12-11 07:56:07 -0800587void SkiaPipeline::setSurfaceColorProperties(ColorMode colorMode) {
Derek Sollenberger1863d942020-02-05 15:41:51 -0500588 mColorMode = colorMode;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800589 if (colorMode == ColorMode::SRGB) {
590 mSurfaceColorType = SkColorType::kN32_SkColorType;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800591 mSurfaceColorSpace = SkColorSpace::MakeSRGB();
592 } else if (colorMode == ColorMode::WideColorGamut) {
593 mSurfaceColorType = DeviceInfo::get()->getWideColorType();
Peiyong Lin3bff1352018-12-11 07:56:07 -0800594 mSurfaceColorSpace = DeviceInfo::get()->getWideColorSpace();
595 } else {
596 LOG_ALWAYS_FATAL("Unreachable: unsupported color mode.");
597 }
598}
599
Matt Sarettf58cc922016-11-14 18:33:38 -0500600// Overdraw debugging
601
602// These colors should be kept in sync with Caches::getOverdrawColor() with a few differences.
603// This implementation:
604// (1) Requires transparent entries for "no overdraw" and "single draws".
605// (2) Requires premul colors (instead of unpremul).
606// (3) Requires RGBA colors (instead of BGRA).
607static const uint32_t kOverdrawColors[2][6] = {
John Reck1bcacfd2017-11-03 10:12:19 -0700608 {
John Reck0fa0cbc2019-04-05 16:57:46 -0700609 0x00000000,
610 0x00000000,
611 0x2f2f0000,
612 0x2f002f00,
613 0x3f00003f,
614 0x7f00007f,
John Reck1bcacfd2017-11-03 10:12:19 -0700615 },
616 {
John Reck0fa0cbc2019-04-05 16:57:46 -0700617 0x00000000,
618 0x00000000,
619 0x2f2f0000,
620 0x4f004f4f,
621 0x5f50335f,
622 0x7f00007f,
John Reck1bcacfd2017-11-03 10:12:19 -0700623 },
Matt Sarettf58cc922016-11-14 18:33:38 -0500624};
625
Nathaniel Nifongdc19a652019-11-11 11:47:50 -0500626void SkiaPipeline::renderOverdraw(const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700627 const std::vector<sp<RenderNode>>& nodes,
Greg Danielc4076782019-01-08 16:01:18 -0500628 const Rect& contentDrawBounds, sk_sp<SkSurface> surface,
629 const SkMatrix& preTransform) {
Matt Sarettf58cc922016-11-14 18:33:38 -0500630 // Set up the overdraw canvas.
631 SkImageInfo offscreenInfo = SkImageInfo::MakeA8(surface->width(), surface->height());
632 sk_sp<SkSurface> offscreen = surface->makeSurface(offscreenInfo);
Nathaniel Nifongfe05b7c2019-09-17 12:52:52 -0400633 LOG_ALWAYS_FATAL_IF(!offscreen, "Failed to create offscreen SkSurface for overdraw viz.");
Matt Sarettf58cc922016-11-14 18:33:38 -0500634 SkOverdrawCanvas overdrawCanvas(offscreen->getCanvas());
635
636 // Fake a redraw to replay the draw commands. This will increment the alpha channel
637 // each time a pixel would have been drawn.
638 // Pass true for opaque so we skip the clear - the overdrawCanvas is already zero
639 // initialized.
Nathaniel Nifongdc19a652019-11-11 11:47:50 -0500640 renderFrameImpl(clip, nodes, true, contentDrawBounds, &overdrawCanvas, preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -0500641 sk_sp<SkImage> counts = offscreen->makeImageSnapshot();
642
643 // Draw overdraw colors to the canvas. The color filter will convert counts to colors.
644 SkPaint paint;
645 const SkPMColor* colors = kOverdrawColors[static_cast<int>(Properties::overdrawColorSet)];
646 paint.setColorFilter(SkOverdrawColorFilter::Make(colors));
647 surface->getCanvas()->drawImage(counts.get(), 0.0f, 0.0f, &paint);
648}
649
Stan Iliev500a0c32016-10-26 10:30:09 -0400650} /* namespace skiapipeline */
651} /* namespace uirenderer */
652} /* namespace android */