blob: 0cd1c151629d7cffed5a3bfb6c5dd0035b32e7ed [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>
Leon Scroggins IIIee708fa2016-12-12 15:31:39 -050020#include <SkImagePriv.h>
Matt Sarettf58cc922016-11-14 18:33:38 -050021#include <SkOverdrawCanvas.h>
22#include <SkOverdrawColorFilter.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040023#include <SkPicture.h>
24#include <SkPictureRecorder.h>
Stan Iliev23c38a92017-03-23 00:12:50 -040025#include "VectorDrawable.h"
John Reck1bcacfd2017-11-03 10:12:19 -070026#include "utils/TraceUtils.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040027
Ben Wagner3aeda5c2017-03-17 17:22:01 -040028#include <unistd.h>
29
Stan Iliev500a0c32016-10-26 10:30:09 -040030using namespace android::uirenderer::renderthread;
31
32namespace android {
33namespace uirenderer {
34namespace skiapipeline {
35
John Reck1bcacfd2017-11-03 10:12:19 -070036float SkiaPipeline::mLightRadius = 0;
Stan Iliev500a0c32016-10-26 10:30:09 -040037uint8_t SkiaPipeline::mAmbientShadowAlpha = 0;
38uint8_t SkiaPipeline::mSpotShadowAlpha = 0;
39
40Vector3 SkiaPipeline::mLightCenter = {FLT_MIN, FLT_MIN, FLT_MIN};
41
John Reck1bcacfd2017-11-03 10:12:19 -070042SkiaPipeline::SkiaPipeline(RenderThread& thread) : mRenderThread(thread) {
Stan Iliev23c38a92017-03-23 00:12:50 -040043 mVectorDrawables.reserve(30);
44}
Stan Iliev500a0c32016-10-26 10:30:09 -040045
Stan Iliev232f3622017-08-23 17:15:09 -040046SkiaPipeline::~SkiaPipeline() {
47 unpinImages();
48}
49
Stan Iliev500a0c32016-10-26 10:30:09 -040050TaskManager* SkiaPipeline::getTaskManager() {
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040051 return mRenderThread.cacheManager().getTaskManager();
Stan Iliev500a0c32016-10-26 10:30:09 -040052}
53
54void SkiaPipeline::onDestroyHardwareResources() {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040055 mRenderThread.cacheManager().trimStaleResources();
Stan Iliev500a0c32016-10-26 10:30:09 -040056}
57
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -040058bool SkiaPipeline::pinImages(std::vector<SkImage*>& mutableImages) {
59 for (SkImage* image : mutableImages) {
Derek Sollenberger189e8742016-11-16 16:00:17 -050060 if (SkImage_pinAsTexture(image, mRenderThread.getGrContext())) {
61 mPinnedImages.emplace_back(sk_ref_sp(image));
62 } else {
63 return false;
64 }
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -040065 }
66 return true;
67}
68
69void SkiaPipeline::unpinImages() {
70 for (auto& image : mPinnedImages) {
71 SkImage_unpinAsTexture(image.get(), mRenderThread.getGrContext());
72 }
73 mPinnedImages.clear();
74}
75
Stan Iliev47fed6ba2017-10-18 17:56:43 -040076void SkiaPipeline::onPrepareTree() {
77 // The only time mVectorDrawables is not empty is if prepare tree was called 2 times without
78 // a renderFrame in the middle.
79 mVectorDrawables.clear();
80}
81
Stan Iliev500a0c32016-10-26 10:30:09 -040082void SkiaPipeline::renderLayers(const FrameBuilder::LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070083 LayerUpdateQueue* layerUpdateQueue, bool opaque,
84 bool wideColorGamut, const BakedOpRenderer::LightInfo& lightInfo) {
Stan Iliev500a0c32016-10-26 10:30:09 -040085 updateLighting(lightGeometry, lightInfo);
86 ATRACE_NAME("draw layers");
Stan Iliev23c38a92017-03-23 00:12:50 -040087 renderVectorDrawableCache();
Romain Guy07ae5052017-06-13 18:25:32 -070088 renderLayersImpl(*layerUpdateQueue, opaque, wideColorGamut);
Stan Iliev500a0c32016-10-26 10:30:09 -040089 layerUpdateQueue->clear();
90}
91
John Reck1bcacfd2017-11-03 10:12:19 -070092void SkiaPipeline::renderLayersImpl(const LayerUpdateQueue& layers, bool opaque,
93 bool wideColorGamut) {
Derek Sollenbergerf7df1842017-09-05 11:15:58 -040094 sk_sp<GrContext> cachedContext;
95
Stan Iliev500a0c32016-10-26 10:30:09 -040096 // Render all layers that need to be updated, in order.
97 for (size_t i = 0; i < layers.entries().size(); i++) {
John Reckfc29f7acd2017-03-02 13:23:16 -080098 RenderNode* layerNode = layers.entries()[i].renderNode.get();
Stan Iliev500a0c32016-10-26 10:30:09 -040099 // only schedule repaint if node still on layer - possible it may have been
100 // removed during a dropped frame, but layers may still remain scheduled so
101 // as not to lose info on what portion is damaged
102 if (CC_LIKELY(layerNode->getLayerSurface() != nullptr)) {
103 SkASSERT(layerNode->getLayerSurface());
104 SkASSERT(layerNode->getDisplayList()->isSkiaDL());
105 SkiaDisplayList* displayList = (SkiaDisplayList*)layerNode->getDisplayList();
106 if (!displayList || displayList->isEmpty()) {
Stan Iliev18b388d2017-07-18 16:41:48 -0400107 SkDEBUGF(("%p drawLayers(%s) : missing drawable", layerNode, layerNode->getName()));
Stan Iliev500a0c32016-10-26 10:30:09 -0400108 return;
109 }
110
111 const Rect& layerDamage = layers.entries()[i].damage;
112
Stan Ilieve9d00122017-09-19 12:07:10 -0400113 SkCanvas* layerCanvas = tryCapture(layerNode->getLayerSurface());
Stan Iliev500a0c32016-10-26 10:30:09 -0400114
115 int saveCount = layerCanvas->save();
116 SkASSERT(saveCount == 1);
117
Stan Ilievb66b8bb2016-12-15 18:17:42 -0500118 layerCanvas->androidFramework_setDeviceClipRestriction(layerDamage.toSkIRect());
Stan Iliev500a0c32016-10-26 10:30:09 -0400119
120 auto savedLightCenter = mLightCenter;
121 // map current light center into RenderNode's coordinate space
122 layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(mLightCenter);
123
124 const RenderProperties& properties = layerNode->properties();
125 const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
126 if (properties.getClipToBounds() && layerCanvas->quickReject(bounds)) {
127 return;
128 }
129
John Reck1bcacfd2017-11-03 10:12:19 -0700130 ATRACE_FORMAT("drawLayer [%s] %.1f x %.1f", layerNode->getName(), bounds.width(),
131 bounds.height());
Derek Sollenberger9552c2c2017-08-31 15:37:52 -0400132
Matt Sarett79756be2016-11-09 16:13:54 -0500133 layerNode->getSkiaLayer()->hasRenderedSinceRepaint = false;
Stan Iliev500a0c32016-10-26 10:30:09 -0400134 layerCanvas->clear(SK_ColorTRANSPARENT);
135
136 RenderNodeDrawable root(layerNode, layerCanvas, false);
137 root.forceDraw(layerCanvas);
138 layerCanvas->restoreToCount(saveCount);
Stan Iliev500a0c32016-10-26 10:30:09 -0400139 mLightCenter = savedLightCenter;
Derek Sollenbergerf7df1842017-09-05 11:15:58 -0400140
Stan Ilieve9d00122017-09-19 12:07:10 -0400141 endCapture(layerNode->getLayerSurface());
142
Derek Sollenbergerf7df1842017-09-05 11:15:58 -0400143 // cache the current context so that we can defer flushing it until
144 // either all the layers have been rendered or the context changes
Stan Ilieve9d00122017-09-19 12:07:10 -0400145 GrContext* currentContext = layerNode->getLayerSurface()->getCanvas()->getGrContext();
Derek Sollenbergerf7df1842017-09-05 11:15:58 -0400146 if (cachedContext.get() != currentContext) {
147 if (cachedContext.get()) {
148 cachedContext->flush();
149 }
150 cachedContext.reset(SkSafeRef(currentContext));
151 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400152 }
153 }
Derek Sollenbergerf7df1842017-09-05 11:15:58 -0400154
155 if (cachedContext.get()) {
156 cachedContext->flush();
157 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400158}
159
John Reck1bcacfd2017-11-03 10:12:19 -0700160bool SkiaPipeline::createOrUpdateLayer(RenderNode* node, const DamageAccumulator& damageAccumulator,
161 bool wideColorGamut) {
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500162 // compute the size of the surface (i.e. texture) to be allocated for this layer
163 const int surfaceWidth = ceilf(node->getWidth() / float(LAYER_SIZE)) * LAYER_SIZE;
164 const int surfaceHeight = ceilf(node->getHeight() / float(LAYER_SIZE)) * LAYER_SIZE;
165
Stan Iliev500a0c32016-10-26 10:30:09 -0400166 SkSurface* layer = node->getLayerSurface();
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500167 if (!layer || layer->width() != surfaceWidth || layer->height() != surfaceHeight) {
Stan Iliev08fc19a2017-07-24 10:20:33 -0400168 SkImageInfo info;
169 if (wideColorGamut) {
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500170 info = SkImageInfo::Make(surfaceWidth, surfaceHeight, kRGBA_F16_SkColorType,
John Reck1bcacfd2017-11-03 10:12:19 -0700171 kPremul_SkAlphaType);
Stan Iliev08fc19a2017-07-24 10:20:33 -0400172 } else {
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500173 info = SkImageInfo::MakeN32Premul(surfaceWidth, surfaceHeight);
Stan Iliev08fc19a2017-07-24 10:20:33 -0400174 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400175 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
176 SkASSERT(mRenderThread.getGrContext() != nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -0700177 node->setLayerSurface(SkSurface::MakeRenderTarget(mRenderThread.getGrContext(),
178 SkBudgeted::kYes, info, 0, &props));
Stan Iliev500a0c32016-10-26 10:30:09 -0400179 if (node->getLayerSurface()) {
180 // update the transform in window of the layer to reset its origin wrt light source
181 // position
182 Matrix4 windowTransform;
183 damageAccumulator.computeCurrentTransform(&windowTransform);
184 node->getSkiaLayer()->inverseTransformInWindow = windowTransform;
185 }
186 return true;
187 }
188 return false;
189}
190
191void SkiaPipeline::destroyLayer(RenderNode* node) {
192 node->setLayerSurface(nullptr);
193}
194
195void SkiaPipeline::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) {
196 GrContext* context = thread.getGrContext();
197 if (context) {
198 ATRACE_FORMAT("Bitmap#prepareToDraw %dx%d", bitmap->width(), bitmap->height());
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400199 sk_sp<SkColorFilter> colorFilter;
200 auto image = bitmap->makeImage(&colorFilter);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400201 if (image.get() && !bitmap->isHardware()) {
202 SkImage_pinAsTexture(image.get(), context);
203 SkImage_unpinAsTexture(image.get(), context);
204 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400205 }
206}
207
Stan Iliev23c38a92017-03-23 00:12:50 -0400208void SkiaPipeline::renderVectorDrawableCache() {
Stan Iliev3310fb12017-03-23 16:56:51 -0400209 if (!mVectorDrawables.empty()) {
210 sp<VectorDrawableAtlas> atlas = mRenderThread.cacheManager().acquireVectorDrawableAtlas();
211 auto grContext = mRenderThread.getGrContext();
212 atlas->prepareForDraw(grContext);
Derek Sollenberger9552c2c2017-08-31 15:37:52 -0400213 ATRACE_NAME("Update VectorDrawables");
Stan Iliev3310fb12017-03-23 16:56:51 -0400214 for (auto vd : mVectorDrawables) {
215 vd->updateCache(atlas, grContext);
Stan Iliev23c38a92017-03-23 00:12:50 -0400216 }
Stan Iliev3310fb12017-03-23 16:56:51 -0400217 mVectorDrawables.clear();
Stan Iliev23c38a92017-03-23 00:12:50 -0400218 }
Stan Iliev23c38a92017-03-23 00:12:50 -0400219}
220
Stan Ilieve9d00122017-09-19 12:07:10 -0400221class SkiaPipeline::SavePictureProcessor : public TaskProcessor<bool> {
222public:
223 explicit SavePictureProcessor(TaskManager* taskManager) : TaskProcessor<bool>(taskManager) {}
224
225 struct SavePictureTask : public Task<bool> {
226 sk_sp<SkData> data;
227 std::string filename;
228 };
229
230 void savePicture(const sk_sp<SkData>& data, const std::string& filename) {
231 sp<SavePictureTask> task(new SavePictureTask());
232 task->data = data;
233 task->filename = filename;
234 TaskProcessor<bool>::add(task);
235 }
236
John Reck1bcacfd2017-11-03 10:12:19 -0700237 virtual void onProcess(const sp<Task<bool>>& task) override {
Stan Ilieve9d00122017-09-19 12:07:10 -0400238 SavePictureTask* t = static_cast<SavePictureTask*>(task.get());
239
240 if (0 == access(t->filename.c_str(), F_OK)) {
241 task->setResult(false);
242 return;
243 }
244
245 SkFILEWStream stream(t->filename.c_str());
246 if (stream.isValid()) {
247 stream.write(t->data->data(), t->data->size());
248 stream.flush();
John Reck1bcacfd2017-11-03 10:12:19 -0700249 SkDebugf("SKP Captured Drawing Output (%d bytes) for frame. %s", stream.bytesWritten(),
250 t->filename.c_str());
Stan Ilieve9d00122017-09-19 12:07:10 -0400251 }
252
253 task->setResult(true);
254 }
255};
256
257SkCanvas* SkiaPipeline::tryCapture(SkSurface* surface) {
258 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
259 bool recordingPicture = mCaptureSequence > 0;
260 char prop[PROPERTY_VALUE_MAX] = {'\0'};
261 if (!recordingPicture) {
262 property_get(PROPERTY_CAPTURE_SKP_FILENAME, prop, "0");
John Reck1bcacfd2017-11-03 10:12:19 -0700263 recordingPicture = prop[0] != '0' &&
264 mCapturedFile != prop; // ensure we capture only once per filename
Stan Ilieve9d00122017-09-19 12:07:10 -0400265 if (recordingPicture) {
266 mCapturedFile = prop;
267 mCaptureSequence = property_get_int32(PROPERTY_CAPTURE_SKP_FRAMES, 1);
268 }
269 }
270 if (recordingPicture) {
271 mRecorder.reset(new SkPictureRecorder());
272 return mRecorder->beginRecording(surface->width(), surface->height(), nullptr,
John Reck1bcacfd2017-11-03 10:12:19 -0700273 SkPictureRecorder::kPlaybackDrawPicture_RecordFlag);
Stan Ilieve9d00122017-09-19 12:07:10 -0400274 }
275 }
276 return surface->getCanvas();
277}
278
279void SkiaPipeline::endCapture(SkSurface* surface) {
280 if (CC_UNLIKELY(mRecorder.get())) {
281 sk_sp<SkPicture> picture = mRecorder->finishRecordingAsPicture();
282 surface->getCanvas()->drawPicture(picture);
283 if (picture->approximateOpCount() > 0) {
Mike Reedebf96fb2017-12-13 13:43:16 -0500284 auto data = picture->serialize();
Stan Ilieve9d00122017-09-19 12:07:10 -0400285
John Reck1bcacfd2017-11-03 10:12:19 -0700286 // offload saving to file in a different thread
Stan Ilieve9d00122017-09-19 12:07:10 -0400287 if (!mSavePictureProcessor.get()) {
288 TaskManager* taskManager = getTaskManager();
289 mSavePictureProcessor = new SavePictureProcessor(
290 taskManager->canRunTasks() ? taskManager : nullptr);
291 }
292 if (1 == mCaptureSequence) {
Mike Reedebf96fb2017-12-13 13:43:16 -0500293 mSavePictureProcessor->savePicture(data, mCapturedFile);
Stan Ilieve9d00122017-09-19 12:07:10 -0400294 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700295 mSavePictureProcessor->savePicture(
Mike Reedebf96fb2017-12-13 13:43:16 -0500296 data,
John Reck1bcacfd2017-11-03 10:12:19 -0700297 mCapturedFile + "_" + std::to_string(mCaptureSequence));
Stan Ilieve9d00122017-09-19 12:07:10 -0400298 }
299 mCaptureSequence--;
300 }
301 mRecorder.reset();
302 }
303}
304
Stan Iliev500a0c32016-10-26 10:30:09 -0400305void SkiaPipeline::renderFrame(const LayerUpdateQueue& layers, const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700306 const std::vector<sp<RenderNode>>& nodes, bool opaque,
307 bool wideColorGamut, const Rect& contentDrawBounds,
308 sk_sp<SkSurface> surface) {
Stan Iliev23c38a92017-03-23 00:12:50 -0400309 renderVectorDrawableCache();
310
Stan Iliev500a0c32016-10-26 10:30:09 -0400311 // draw all layers up front
Romain Guy07ae5052017-06-13 18:25:32 -0700312 renderLayersImpl(layers, opaque, wideColorGamut);
Stan Iliev500a0c32016-10-26 10:30:09 -0400313
Stan Ilieve9d00122017-09-19 12:07:10 -0400314 // initialize the canvas for the current frame, that might be a recording canvas if SKP
315 // capture is enabled.
Stan Iliev500a0c32016-10-26 10:30:09 -0400316 std::unique_ptr<SkPictureRecorder> recorder;
Stan Ilieve9d00122017-09-19 12:07:10 -0400317 SkCanvas* canvas = tryCapture(surface.get());
Stan Iliev500a0c32016-10-26 10:30:09 -0400318
Romain Guy07ae5052017-06-13 18:25:32 -0700319 renderFrameImpl(layers, clip, nodes, opaque, wideColorGamut, contentDrawBounds, canvas);
Matt Sarettf58cc922016-11-14 18:33:38 -0500320
Stan Ilieve9d00122017-09-19 12:07:10 -0400321 endCapture(surface.get());
Matt Sarettf58cc922016-11-14 18:33:38 -0500322
323 if (CC_UNLIKELY(Properties::debugOverdraw)) {
324 renderOverdraw(layers, clip, nodes, contentDrawBounds, surface);
325 }
326
327 ATRACE_NAME("flush commands");
Stan Ilieve9d00122017-09-19 12:07:10 -0400328 surface->getCanvas()->flush();
Matt Sarettf58cc922016-11-14 18:33:38 -0500329}
330
Stan Iliev52771272016-11-17 09:54:38 -0500331namespace {
332static Rect nodeBounds(RenderNode& node) {
333 auto& props = node.properties();
John Reck1bcacfd2017-11-03 10:12:19 -0700334 return Rect(props.getLeft(), props.getTop(), props.getRight(), props.getBottom());
Stan Iliev52771272016-11-17 09:54:38 -0500335}
336}
337
Matt Sarettf58cc922016-11-14 18:33:38 -0500338void SkiaPipeline::renderFrameImpl(const LayerUpdateQueue& layers, const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700339 const std::vector<sp<RenderNode>>& nodes, bool opaque,
340 bool wideColorGamut, const Rect& contentDrawBounds,
341 SkCanvas* canvas) {
Stan Ilievb66b8bb2016-12-15 18:17:42 -0500342 SkAutoCanvasRestore saver(canvas, true);
343 canvas->androidFramework_setDeviceClipRestriction(clip.roundOut());
Stan Iliev500a0c32016-10-26 10:30:09 -0400344
Stan Ilievaadc0322018-03-23 10:50:11 -0400345 // STOPSHIP: Revert, temporary workaround to clear always F16 frame buffer for b/74976293
346 if (!opaque || wideColorGamut) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400347 canvas->clear(SK_ColorTRANSPARENT);
348 }
349
Stan Iliev52771272016-11-17 09:54:38 -0500350 if (1 == nodes.size()) {
351 if (!nodes[0]->nothingToDraw()) {
Stan Iliev52771272016-11-17 09:54:38 -0500352 RenderNodeDrawable root(nodes[0].get(), canvas);
353 root.draw(canvas);
354 }
355 } else if (0 == nodes.size()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700356 // nothing to draw
Stan Iliev52771272016-11-17 09:54:38 -0500357 } else {
358 // It there are multiple render nodes, they are laid out as follows:
359 // #0 - backdrop (content + caption)
360 // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop)
361 // #2 - additional overlay nodes
John Reck1bcacfd2017-11-03 10:12:19 -0700362 // Usually the backdrop cannot be seen since it will be entirely covered by the content.
363 // While
364 // resizing however it might become partially visible. The following render loop will crop
365 // the
366 // backdrop against the content and draw the remaining part of it. It will then draw the
367 // content
Stan Iliev52771272016-11-17 09:54:38 -0500368 // cropped to the backdrop (since that indicates a shrinking of the window).
369 //
370 // Additional nodes will be drawn on top with no particular clipping semantics.
Stan Iliev500a0c32016-10-26 10:30:09 -0400371
Stan Iliev52771272016-11-17 09:54:38 -0500372 // Usually the contents bounds should be mContentDrawBounds - however - we will
373 // move it towards the fixed edge to give it a more stable appearance (for the moment).
374 // If there is no content bounds we ignore the layering as stated above and start with 2.
Stan Iliev500a0c32016-10-26 10:30:09 -0400375
Stan Iliev52771272016-11-17 09:54:38 -0500376 // Backdrop bounds in render target space
377 const Rect backdrop = nodeBounds(*nodes[0]);
Stan Iliev500a0c32016-10-26 10:30:09 -0400378
John Reck1bcacfd2017-11-03 10:12:19 -0700379 // Bounds that content will fill in render target space (note content node bounds may be
380 // bigger)
Stan Iliev52771272016-11-17 09:54:38 -0500381 Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight());
382 content.translate(backdrop.left, backdrop.top);
383 if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) {
384 // Content doesn't entirely overlap backdrop, so fill around content (right/bottom)
Stan Iliev500a0c32016-10-26 10:30:09 -0400385
Stan Iliev52771272016-11-17 09:54:38 -0500386 // 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 -0700387 // also fill left/top. Currently, both 2up and freeform position content at the top/left
388 // of
Stan Iliev52771272016-11-17 09:54:38 -0500389 // the backdrop, so this isn't necessary.
390 RenderNodeDrawable backdropNode(nodes[0].get(), canvas);
391 if (content.right < backdrop.right) {
392 // draw backdrop to right side of content
393 SkAutoCanvasRestore acr(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700394 canvas->clipRect(SkRect::MakeLTRB(content.right, backdrop.top, backdrop.right,
395 backdrop.bottom));
Stan Iliev52771272016-11-17 09:54:38 -0500396 backdropNode.draw(canvas);
397 }
398 if (content.bottom < backdrop.bottom) {
399 // draw backdrop to bottom of content
400 // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill
401 SkAutoCanvasRestore acr(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700402 canvas->clipRect(SkRect::MakeLTRB(content.left, content.bottom, content.right,
403 backdrop.bottom));
Stan Iliev52771272016-11-17 09:54:38 -0500404 backdropNode.draw(canvas);
405 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400406 }
407
Stan Iliev52771272016-11-17 09:54:38 -0500408 RenderNodeDrawable contentNode(nodes[1].get(), canvas);
409 if (!backdrop.isEmpty()) {
410 // content node translation to catch up with backdrop
411 float dx = backdrop.left - contentDrawBounds.left;
412 float dy = backdrop.top - contentDrawBounds.top;
413
414 SkAutoCanvasRestore acr(canvas, true);
415 canvas->translate(dx, dy);
John Reck1bcacfd2017-11-03 10:12:19 -0700416 const SkRect contentLocalClip =
417 SkRect::MakeXYWH(contentDrawBounds.left, contentDrawBounds.top,
418 backdrop.getWidth(), backdrop.getHeight());
Stan Iliev52771272016-11-17 09:54:38 -0500419 canvas->clipRect(contentLocalClip);
420 contentNode.draw(canvas);
421 } else {
422 SkAutoCanvasRestore acr(canvas, true);
423 contentNode.draw(canvas);
424 }
425
426 // remaining overlay nodes, simply defer
427 for (size_t index = 2; index < nodes.size(); index++) {
428 if (!nodes[index]->nothingToDraw()) {
429 SkAutoCanvasRestore acr(canvas, true);
430 RenderNodeDrawable overlayNode(nodes[index].get(), canvas);
431 overlayNode.draw(canvas);
432 }
433 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400434 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400435}
436
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500437void SkiaPipeline::dumpResourceCacheUsage() const {
438 int resources, maxResources;
439 size_t bytes, maxBytes;
440 mRenderThread.getGrContext()->getResourceCacheUsage(&resources, &bytes);
441 mRenderThread.getGrContext()->getResourceCacheLimits(&maxResources, &maxBytes);
442
443 SkString log("Resource Cache Usage:\n");
444 log.appendf("%8d items out of %d maximum items\n", resources, maxResources);
John Reck1bcacfd2017-11-03 10:12:19 -0700445 log.appendf("%8zu bytes (%.2f MB) out of %.2f MB maximum\n", bytes,
446 bytes * (1.0f / (1024.0f * 1024.0f)), maxBytes * (1.0f / (1024.0f * 1024.0f)));
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500447
448 ALOGD("%s", log.c_str());
449}
450
Matt Sarettf58cc922016-11-14 18:33:38 -0500451// Overdraw debugging
452
453// These colors should be kept in sync with Caches::getOverdrawColor() with a few differences.
454// This implementation:
455// (1) Requires transparent entries for "no overdraw" and "single draws".
456// (2) Requires premul colors (instead of unpremul).
457// (3) Requires RGBA colors (instead of BGRA).
458static const uint32_t kOverdrawColors[2][6] = {
John Reck1bcacfd2017-11-03 10:12:19 -0700459 {
460 0x00000000, 0x00000000, 0x2f2f0000, 0x2f002f00, 0x3f00003f, 0x7f00007f,
461 },
462 {
463 0x00000000, 0x00000000, 0x2f2f0000, 0x4f004f4f, 0x5f50335f, 0x7f00007f,
464 },
Matt Sarettf58cc922016-11-14 18:33:38 -0500465};
466
467void SkiaPipeline::renderOverdraw(const LayerUpdateQueue& layers, const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700468 const std::vector<sp<RenderNode>>& nodes,
469 const Rect& contentDrawBounds, sk_sp<SkSurface> surface) {
Matt Sarettf58cc922016-11-14 18:33:38 -0500470 // Set up the overdraw canvas.
471 SkImageInfo offscreenInfo = SkImageInfo::MakeA8(surface->width(), surface->height());
472 sk_sp<SkSurface> offscreen = surface->makeSurface(offscreenInfo);
473 SkOverdrawCanvas overdrawCanvas(offscreen->getCanvas());
474
475 // Fake a redraw to replay the draw commands. This will increment the alpha channel
476 // each time a pixel would have been drawn.
477 // Pass true for opaque so we skip the clear - the overdrawCanvas is already zero
478 // initialized.
Romain Guy07ae5052017-06-13 18:25:32 -0700479 renderFrameImpl(layers, clip, nodes, true, false, contentDrawBounds, &overdrawCanvas);
Matt Sarettf58cc922016-11-14 18:33:38 -0500480 sk_sp<SkImage> counts = offscreen->makeImageSnapshot();
481
482 // Draw overdraw colors to the canvas. The color filter will convert counts to colors.
483 SkPaint paint;
484 const SkPMColor* colors = kOverdrawColors[static_cast<int>(Properties::overdrawColorSet)];
485 paint.setColorFilter(SkOverdrawColorFilter::Make(colors));
486 surface->getCanvas()->drawImage(counts.get(), 0.0f, 0.0f, &paint);
487}
488
Stan Iliev500a0c32016-10-26 10:30:09 -0400489} /* namespace skiapipeline */
490} /* namespace uirenderer */
491} /* namespace android */