blob: 07052cdab48bbc2d307182f51ac5a44b0c8d784e [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 Iliev216b1572018-03-26 14:29:50 -040025#include "TreeInfo.h"
Stan Iliev23c38a92017-03-23 00:12:50 -040026#include "VectorDrawable.h"
John Reck1bcacfd2017-11-03 10:12:19 -070027#include "utils/TraceUtils.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040028
Ben Wagner3aeda5c2017-03-17 17:22:01 -040029#include <unistd.h>
30
Stan Iliev500a0c32016-10-26 10:30:09 -040031using namespace android::uirenderer::renderthread;
32
33namespace android {
34namespace uirenderer {
35namespace skiapipeline {
36
John Reck1bcacfd2017-11-03 10:12:19 -070037float SkiaPipeline::mLightRadius = 0;
Stan Iliev500a0c32016-10-26 10:30:09 -040038uint8_t SkiaPipeline::mAmbientShadowAlpha = 0;
39uint8_t SkiaPipeline::mSpotShadowAlpha = 0;
40
41Vector3 SkiaPipeline::mLightCenter = {FLT_MIN, FLT_MIN, FLT_MIN};
42
John Reck1bcacfd2017-11-03 10:12:19 -070043SkiaPipeline::SkiaPipeline(RenderThread& thread) : mRenderThread(thread) {
Stan Iliev23c38a92017-03-23 00:12:50 -040044 mVectorDrawables.reserve(30);
45}
Stan Iliev500a0c32016-10-26 10:30:09 -040046
Stan Iliev232f3622017-08-23 17:15:09 -040047SkiaPipeline::~SkiaPipeline() {
48 unpinImages();
49}
50
Stan Iliev500a0c32016-10-26 10:30:09 -040051TaskManager* SkiaPipeline::getTaskManager() {
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040052 return mRenderThread.cacheManager().getTaskManager();
Stan Iliev500a0c32016-10-26 10:30:09 -040053}
54
55void SkiaPipeline::onDestroyHardwareResources() {
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
Stan Iliev47fed6ba2017-10-18 17:56:43 -040077void SkiaPipeline::onPrepareTree() {
78 // The only time mVectorDrawables is not empty is if prepare tree was called 2 times without
79 // a renderFrame in the middle.
80 mVectorDrawables.clear();
81}
82
Stan Iliev500a0c32016-10-26 10:30:09 -040083void SkiaPipeline::renderLayers(const FrameBuilder::LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070084 LayerUpdateQueue* layerUpdateQueue, bool opaque,
85 bool wideColorGamut, const BakedOpRenderer::LightInfo& lightInfo) {
Stan Iliev500a0c32016-10-26 10:30:09 -040086 updateLighting(lightGeometry, lightInfo);
87 ATRACE_NAME("draw layers");
Stan Iliev23c38a92017-03-23 00:12:50 -040088 renderVectorDrawableCache();
Romain Guy07ae5052017-06-13 18:25:32 -070089 renderLayersImpl(*layerUpdateQueue, opaque, wideColorGamut);
Stan Iliev500a0c32016-10-26 10:30:09 -040090 layerUpdateQueue->clear();
91}
92
John Reck1bcacfd2017-11-03 10:12:19 -070093void SkiaPipeline::renderLayersImpl(const LayerUpdateQueue& layers, bool opaque,
94 bool wideColorGamut) {
Derek Sollenbergerf7df1842017-09-05 11:15:58 -040095 sk_sp<GrContext> cachedContext;
96
Stan Iliev500a0c32016-10-26 10:30:09 -040097 // Render all layers that need to be updated, in order.
98 for (size_t i = 0; i < layers.entries().size(); i++) {
John Reckfc29f7acd2017-03-02 13:23:16 -080099 RenderNode* layerNode = layers.entries()[i].renderNode.get();
Stan Iliev500a0c32016-10-26 10:30:09 -0400100 // only schedule repaint if node still on layer - possible it may have been
101 // removed during a dropped frame, but layers may still remain scheduled so
102 // as not to lose info on what portion is damaged
103 if (CC_LIKELY(layerNode->getLayerSurface() != nullptr)) {
104 SkASSERT(layerNode->getLayerSurface());
105 SkASSERT(layerNode->getDisplayList()->isSkiaDL());
106 SkiaDisplayList* displayList = (SkiaDisplayList*)layerNode->getDisplayList();
107 if (!displayList || displayList->isEmpty()) {
Stan Iliev18b388d2017-07-18 16:41:48 -0400108 SkDEBUGF(("%p drawLayers(%s) : missing drawable", layerNode, layerNode->getName()));
Stan Iliev500a0c32016-10-26 10:30:09 -0400109 return;
110 }
111
112 const Rect& layerDamage = layers.entries()[i].damage;
113
Stan Ilieve9d00122017-09-19 12:07:10 -0400114 SkCanvas* layerCanvas = tryCapture(layerNode->getLayerSurface());
Stan Iliev500a0c32016-10-26 10:30:09 -0400115
116 int saveCount = layerCanvas->save();
117 SkASSERT(saveCount == 1);
118
Stan Ilievb66b8bb2016-12-15 18:17:42 -0500119 layerCanvas->androidFramework_setDeviceClipRestriction(layerDamage.toSkIRect());
Stan Iliev500a0c32016-10-26 10:30:09 -0400120
121 auto savedLightCenter = mLightCenter;
122 // map current light center into RenderNode's coordinate space
123 layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(mLightCenter);
124
125 const RenderProperties& properties = layerNode->properties();
126 const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
127 if (properties.getClipToBounds() && layerCanvas->quickReject(bounds)) {
128 return;
129 }
130
John Reck1bcacfd2017-11-03 10:12:19 -0700131 ATRACE_FORMAT("drawLayer [%s] %.1f x %.1f", layerNode->getName(), bounds.width(),
132 bounds.height());
Derek Sollenberger9552c2c2017-08-31 15:37:52 -0400133
Matt Sarett79756be2016-11-09 16:13:54 -0500134 layerNode->getSkiaLayer()->hasRenderedSinceRepaint = false;
Stan Iliev500a0c32016-10-26 10:30:09 -0400135 layerCanvas->clear(SK_ColorTRANSPARENT);
136
137 RenderNodeDrawable root(layerNode, layerCanvas, false);
138 root.forceDraw(layerCanvas);
139 layerCanvas->restoreToCount(saveCount);
Stan Iliev500a0c32016-10-26 10:30:09 -0400140 mLightCenter = savedLightCenter;
Derek Sollenbergerf7df1842017-09-05 11:15:58 -0400141
Stan Ilieve9d00122017-09-19 12:07:10 -0400142 endCapture(layerNode->getLayerSurface());
143
Derek Sollenbergerf7df1842017-09-05 11:15:58 -0400144 // cache the current context so that we can defer flushing it until
145 // either all the layers have been rendered or the context changes
Stan Ilieve9d00122017-09-19 12:07:10 -0400146 GrContext* currentContext = layerNode->getLayerSurface()->getCanvas()->getGrContext();
Derek Sollenbergerf7df1842017-09-05 11:15:58 -0400147 if (cachedContext.get() != currentContext) {
148 if (cachedContext.get()) {
149 cachedContext->flush();
150 }
151 cachedContext.reset(SkSafeRef(currentContext));
152 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400153 }
154 }
Derek Sollenbergerf7df1842017-09-05 11:15:58 -0400155
156 if (cachedContext.get()) {
157 cachedContext->flush();
158 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400159}
160
John Reck1bcacfd2017-11-03 10:12:19 -0700161bool SkiaPipeline::createOrUpdateLayer(RenderNode* node, const DamageAccumulator& damageAccumulator,
Stan Iliev216b1572018-03-26 14:29:50 -0400162 bool wideColorGamut, ErrorHandler* errorHandler) {
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500163 // compute the size of the surface (i.e. texture) to be allocated for this layer
164 const int surfaceWidth = ceilf(node->getWidth() / float(LAYER_SIZE)) * LAYER_SIZE;
165 const int surfaceHeight = ceilf(node->getHeight() / float(LAYER_SIZE)) * LAYER_SIZE;
166
Stan Iliev500a0c32016-10-26 10:30:09 -0400167 SkSurface* layer = node->getLayerSurface();
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500168 if (!layer || layer->width() != surfaceWidth || layer->height() != surfaceHeight) {
Stan Iliev08fc19a2017-07-24 10:20:33 -0400169 SkImageInfo info;
170 if (wideColorGamut) {
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500171 info = SkImageInfo::Make(surfaceWidth, surfaceHeight, kRGBA_F16_SkColorType,
John Reck1bcacfd2017-11-03 10:12:19 -0700172 kPremul_SkAlphaType);
Stan Iliev08fc19a2017-07-24 10:20:33 -0400173 } else {
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500174 info = SkImageInfo::MakeN32Premul(surfaceWidth, surfaceHeight);
Stan Iliev08fc19a2017-07-24 10:20:33 -0400175 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400176 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
177 SkASSERT(mRenderThread.getGrContext() != nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -0700178 node->setLayerSurface(SkSurface::MakeRenderTarget(mRenderThread.getGrContext(),
179 SkBudgeted::kYes, info, 0, &props));
Stan Iliev500a0c32016-10-26 10:30:09 -0400180 if (node->getLayerSurface()) {
181 // update the transform in window of the layer to reset its origin wrt light source
182 // position
183 Matrix4 windowTransform;
184 damageAccumulator.computeCurrentTransform(&windowTransform);
185 node->getSkiaLayer()->inverseTransformInWindow = windowTransform;
Stan Iliev216b1572018-03-26 14:29:50 -0400186 } else {
187 String8 cachesOutput;
188 mRenderThread.cacheManager().dumpMemoryUsage(cachesOutput,
189 &mRenderThread.renderState());
190 ALOGE("%s", cachesOutput.string());
191 if (errorHandler) {
192 std::ostringstream err;
193 err << "Unable to create layer for " << node->getName();
194 const int maxTextureSize = DeviceInfo::get()->maxTextureSize();
195 err << ", size " << info.width() << "x" << info.height() << " max size "
196 << maxTextureSize << " color type " << (int)info.colorType()
197 << " has context " << (int)(mRenderThread.getGrContext() != nullptr);
198 errorHandler->onError(err.str());
199 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400200 }
201 return true;
202 }
203 return false;
204}
205
206void SkiaPipeline::destroyLayer(RenderNode* node) {
207 node->setLayerSurface(nullptr);
208}
209
210void SkiaPipeline::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) {
211 GrContext* context = thread.getGrContext();
212 if (context) {
213 ATRACE_FORMAT("Bitmap#prepareToDraw %dx%d", bitmap->width(), bitmap->height());
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400214 sk_sp<SkColorFilter> colorFilter;
215 auto image = bitmap->makeImage(&colorFilter);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400216 if (image.get() && !bitmap->isHardware()) {
217 SkImage_pinAsTexture(image.get(), context);
218 SkImage_unpinAsTexture(image.get(), context);
219 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400220 }
221}
222
Stan Iliev23c38a92017-03-23 00:12:50 -0400223void SkiaPipeline::renderVectorDrawableCache() {
Stan Iliev3310fb12017-03-23 16:56:51 -0400224 if (!mVectorDrawables.empty()) {
225 sp<VectorDrawableAtlas> atlas = mRenderThread.cacheManager().acquireVectorDrawableAtlas();
226 auto grContext = mRenderThread.getGrContext();
227 atlas->prepareForDraw(grContext);
Derek Sollenberger9552c2c2017-08-31 15:37:52 -0400228 ATRACE_NAME("Update VectorDrawables");
Stan Iliev3310fb12017-03-23 16:56:51 -0400229 for (auto vd : mVectorDrawables) {
230 vd->updateCache(atlas, grContext);
Stan Iliev23c38a92017-03-23 00:12:50 -0400231 }
Stan Iliev3310fb12017-03-23 16:56:51 -0400232 mVectorDrawables.clear();
Stan Iliev23c38a92017-03-23 00:12:50 -0400233 }
Stan Iliev23c38a92017-03-23 00:12:50 -0400234}
235
Stan Ilieve9d00122017-09-19 12:07:10 -0400236class SkiaPipeline::SavePictureProcessor : public TaskProcessor<bool> {
237public:
238 explicit SavePictureProcessor(TaskManager* taskManager) : TaskProcessor<bool>(taskManager) {}
239
240 struct SavePictureTask : public Task<bool> {
241 sk_sp<SkData> data;
242 std::string filename;
243 };
244
245 void savePicture(const sk_sp<SkData>& data, const std::string& filename) {
246 sp<SavePictureTask> task(new SavePictureTask());
247 task->data = data;
248 task->filename = filename;
249 TaskProcessor<bool>::add(task);
250 }
251
John Reck1bcacfd2017-11-03 10:12:19 -0700252 virtual void onProcess(const sp<Task<bool>>& task) override {
Stan Ilieve9d00122017-09-19 12:07:10 -0400253 SavePictureTask* t = static_cast<SavePictureTask*>(task.get());
254
255 if (0 == access(t->filename.c_str(), F_OK)) {
256 task->setResult(false);
257 return;
258 }
259
260 SkFILEWStream stream(t->filename.c_str());
261 if (stream.isValid()) {
262 stream.write(t->data->data(), t->data->size());
263 stream.flush();
John Reck1bcacfd2017-11-03 10:12:19 -0700264 SkDebugf("SKP Captured Drawing Output (%d bytes) for frame. %s", stream.bytesWritten(),
265 t->filename.c_str());
Stan Ilieve9d00122017-09-19 12:07:10 -0400266 }
267
268 task->setResult(true);
269 }
270};
271
272SkCanvas* SkiaPipeline::tryCapture(SkSurface* surface) {
273 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
274 bool recordingPicture = mCaptureSequence > 0;
275 char prop[PROPERTY_VALUE_MAX] = {'\0'};
276 if (!recordingPicture) {
277 property_get(PROPERTY_CAPTURE_SKP_FILENAME, prop, "0");
John Reck1bcacfd2017-11-03 10:12:19 -0700278 recordingPicture = prop[0] != '0' &&
279 mCapturedFile != prop; // ensure we capture only once per filename
Stan Ilieve9d00122017-09-19 12:07:10 -0400280 if (recordingPicture) {
281 mCapturedFile = prop;
282 mCaptureSequence = property_get_int32(PROPERTY_CAPTURE_SKP_FRAMES, 1);
283 }
284 }
285 if (recordingPicture) {
286 mRecorder.reset(new SkPictureRecorder());
287 return mRecorder->beginRecording(surface->width(), surface->height(), nullptr,
John Reck1bcacfd2017-11-03 10:12:19 -0700288 SkPictureRecorder::kPlaybackDrawPicture_RecordFlag);
Stan Ilieve9d00122017-09-19 12:07:10 -0400289 }
290 }
291 return surface->getCanvas();
292}
293
294void SkiaPipeline::endCapture(SkSurface* surface) {
295 if (CC_UNLIKELY(mRecorder.get())) {
296 sk_sp<SkPicture> picture = mRecorder->finishRecordingAsPicture();
297 surface->getCanvas()->drawPicture(picture);
298 if (picture->approximateOpCount() > 0) {
Mike Reedebf96fb2017-12-13 13:43:16 -0500299 auto data = picture->serialize();
Stan Ilieve9d00122017-09-19 12:07:10 -0400300
John Reck1bcacfd2017-11-03 10:12:19 -0700301 // offload saving to file in a different thread
Stan Ilieve9d00122017-09-19 12:07:10 -0400302 if (!mSavePictureProcessor.get()) {
303 TaskManager* taskManager = getTaskManager();
304 mSavePictureProcessor = new SavePictureProcessor(
305 taskManager->canRunTasks() ? taskManager : nullptr);
306 }
307 if (1 == mCaptureSequence) {
Mike Reedebf96fb2017-12-13 13:43:16 -0500308 mSavePictureProcessor->savePicture(data, mCapturedFile);
Stan Ilieve9d00122017-09-19 12:07:10 -0400309 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700310 mSavePictureProcessor->savePicture(
Mike Reedebf96fb2017-12-13 13:43:16 -0500311 data,
John Reck1bcacfd2017-11-03 10:12:19 -0700312 mCapturedFile + "_" + std::to_string(mCaptureSequence));
Stan Ilieve9d00122017-09-19 12:07:10 -0400313 }
314 mCaptureSequence--;
315 }
316 mRecorder.reset();
317 }
318}
319
Stan Iliev500a0c32016-10-26 10:30:09 -0400320void SkiaPipeline::renderFrame(const LayerUpdateQueue& layers, const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700321 const std::vector<sp<RenderNode>>& nodes, bool opaque,
322 bool wideColorGamut, const Rect& contentDrawBounds,
323 sk_sp<SkSurface> surface) {
Stan Iliev23c38a92017-03-23 00:12:50 -0400324 renderVectorDrawableCache();
325
Stan Iliev500a0c32016-10-26 10:30:09 -0400326 // draw all layers up front
Romain Guy07ae5052017-06-13 18:25:32 -0700327 renderLayersImpl(layers, opaque, wideColorGamut);
Stan Iliev500a0c32016-10-26 10:30:09 -0400328
Stan Ilieve9d00122017-09-19 12:07:10 -0400329 // initialize the canvas for the current frame, that might be a recording canvas if SKP
330 // capture is enabled.
Stan Iliev500a0c32016-10-26 10:30:09 -0400331 std::unique_ptr<SkPictureRecorder> recorder;
Stan Ilieve9d00122017-09-19 12:07:10 -0400332 SkCanvas* canvas = tryCapture(surface.get());
Stan Iliev500a0c32016-10-26 10:30:09 -0400333
Romain Guy07ae5052017-06-13 18:25:32 -0700334 renderFrameImpl(layers, clip, nodes, opaque, wideColorGamut, contentDrawBounds, canvas);
Matt Sarettf58cc922016-11-14 18:33:38 -0500335
Stan Ilieve9d00122017-09-19 12:07:10 -0400336 endCapture(surface.get());
Matt Sarettf58cc922016-11-14 18:33:38 -0500337
338 if (CC_UNLIKELY(Properties::debugOverdraw)) {
339 renderOverdraw(layers, clip, nodes, contentDrawBounds, surface);
340 }
341
342 ATRACE_NAME("flush commands");
Stan Ilieve9d00122017-09-19 12:07:10 -0400343 surface->getCanvas()->flush();
Matt Sarettf58cc922016-11-14 18:33:38 -0500344}
345
Stan Iliev52771272016-11-17 09:54:38 -0500346namespace {
347static Rect nodeBounds(RenderNode& node) {
348 auto& props = node.properties();
John Reck1bcacfd2017-11-03 10:12:19 -0700349 return Rect(props.getLeft(), props.getTop(), props.getRight(), props.getBottom());
Stan Iliev52771272016-11-17 09:54:38 -0500350}
351}
352
Matt Sarettf58cc922016-11-14 18:33:38 -0500353void SkiaPipeline::renderFrameImpl(const LayerUpdateQueue& layers, const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700354 const std::vector<sp<RenderNode>>& nodes, bool opaque,
355 bool wideColorGamut, const Rect& contentDrawBounds,
356 SkCanvas* canvas) {
Stan Ilievb66b8bb2016-12-15 18:17:42 -0500357 SkAutoCanvasRestore saver(canvas, true);
358 canvas->androidFramework_setDeviceClipRestriction(clip.roundOut());
Stan Iliev500a0c32016-10-26 10:30:09 -0400359
Stan Ilievaadc0322018-03-23 10:50:11 -0400360 // STOPSHIP: Revert, temporary workaround to clear always F16 frame buffer for b/74976293
361 if (!opaque || wideColorGamut) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400362 canvas->clear(SK_ColorTRANSPARENT);
363 }
364
Stan Iliev52771272016-11-17 09:54:38 -0500365 if (1 == nodes.size()) {
366 if (!nodes[0]->nothingToDraw()) {
Stan Iliev52771272016-11-17 09:54:38 -0500367 RenderNodeDrawable root(nodes[0].get(), canvas);
368 root.draw(canvas);
369 }
370 } else if (0 == nodes.size()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700371 // nothing to draw
Stan Iliev52771272016-11-17 09:54:38 -0500372 } else {
373 // It there are multiple render nodes, they are laid out as follows:
374 // #0 - backdrop (content + caption)
375 // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop)
376 // #2 - additional overlay nodes
John Reck1bcacfd2017-11-03 10:12:19 -0700377 // Usually the backdrop cannot be seen since it will be entirely covered by the content.
378 // While
379 // resizing however it might become partially visible. The following render loop will crop
380 // the
381 // backdrop against the content and draw the remaining part of it. It will then draw the
382 // content
Stan Iliev52771272016-11-17 09:54:38 -0500383 // cropped to the backdrop (since that indicates a shrinking of the window).
384 //
385 // Additional nodes will be drawn on top with no particular clipping semantics.
Stan Iliev500a0c32016-10-26 10:30:09 -0400386
Stan Iliev52771272016-11-17 09:54:38 -0500387 // Usually the contents bounds should be mContentDrawBounds - however - we will
388 // move it towards the fixed edge to give it a more stable appearance (for the moment).
389 // If there is no content bounds we ignore the layering as stated above and start with 2.
Stan Iliev500a0c32016-10-26 10:30:09 -0400390
Stan Iliev52771272016-11-17 09:54:38 -0500391 // Backdrop bounds in render target space
392 const Rect backdrop = nodeBounds(*nodes[0]);
Stan Iliev500a0c32016-10-26 10:30:09 -0400393
John Reck1bcacfd2017-11-03 10:12:19 -0700394 // Bounds that content will fill in render target space (note content node bounds may be
395 // bigger)
Stan Iliev52771272016-11-17 09:54:38 -0500396 Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight());
397 content.translate(backdrop.left, backdrop.top);
398 if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) {
399 // Content doesn't entirely overlap backdrop, so fill around content (right/bottom)
Stan Iliev500a0c32016-10-26 10:30:09 -0400400
Stan Iliev52771272016-11-17 09:54:38 -0500401 // 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 -0700402 // also fill left/top. Currently, both 2up and freeform position content at the top/left
403 // of
Stan Iliev52771272016-11-17 09:54:38 -0500404 // the backdrop, so this isn't necessary.
405 RenderNodeDrawable backdropNode(nodes[0].get(), canvas);
406 if (content.right < backdrop.right) {
407 // draw backdrop to right side of content
408 SkAutoCanvasRestore acr(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700409 canvas->clipRect(SkRect::MakeLTRB(content.right, backdrop.top, backdrop.right,
410 backdrop.bottom));
Stan Iliev52771272016-11-17 09:54:38 -0500411 backdropNode.draw(canvas);
412 }
413 if (content.bottom < backdrop.bottom) {
414 // draw backdrop to bottom of content
415 // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill
416 SkAutoCanvasRestore acr(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700417 canvas->clipRect(SkRect::MakeLTRB(content.left, content.bottom, content.right,
418 backdrop.bottom));
Stan Iliev52771272016-11-17 09:54:38 -0500419 backdropNode.draw(canvas);
420 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400421 }
422
Stan Iliev52771272016-11-17 09:54:38 -0500423 RenderNodeDrawable contentNode(nodes[1].get(), canvas);
424 if (!backdrop.isEmpty()) {
425 // content node translation to catch up with backdrop
426 float dx = backdrop.left - contentDrawBounds.left;
427 float dy = backdrop.top - contentDrawBounds.top;
428
429 SkAutoCanvasRestore acr(canvas, true);
430 canvas->translate(dx, dy);
John Reck1bcacfd2017-11-03 10:12:19 -0700431 const SkRect contentLocalClip =
432 SkRect::MakeXYWH(contentDrawBounds.left, contentDrawBounds.top,
433 backdrop.getWidth(), backdrop.getHeight());
Stan Iliev52771272016-11-17 09:54:38 -0500434 canvas->clipRect(contentLocalClip);
435 contentNode.draw(canvas);
436 } else {
437 SkAutoCanvasRestore acr(canvas, true);
438 contentNode.draw(canvas);
439 }
440
441 // remaining overlay nodes, simply defer
442 for (size_t index = 2; index < nodes.size(); index++) {
443 if (!nodes[index]->nothingToDraw()) {
444 SkAutoCanvasRestore acr(canvas, true);
445 RenderNodeDrawable overlayNode(nodes[index].get(), canvas);
446 overlayNode.draw(canvas);
447 }
448 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400449 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400450}
451
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500452void SkiaPipeline::dumpResourceCacheUsage() const {
453 int resources, maxResources;
454 size_t bytes, maxBytes;
455 mRenderThread.getGrContext()->getResourceCacheUsage(&resources, &bytes);
456 mRenderThread.getGrContext()->getResourceCacheLimits(&maxResources, &maxBytes);
457
458 SkString log("Resource Cache Usage:\n");
459 log.appendf("%8d items out of %d maximum items\n", resources, maxResources);
John Reck1bcacfd2017-11-03 10:12:19 -0700460 log.appendf("%8zu bytes (%.2f MB) out of %.2f MB maximum\n", bytes,
461 bytes * (1.0f / (1024.0f * 1024.0f)), maxBytes * (1.0f / (1024.0f * 1024.0f)));
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500462
463 ALOGD("%s", log.c_str());
464}
465
Matt Sarettf58cc922016-11-14 18:33:38 -0500466// Overdraw debugging
467
468// These colors should be kept in sync with Caches::getOverdrawColor() with a few differences.
469// This implementation:
470// (1) Requires transparent entries for "no overdraw" and "single draws".
471// (2) Requires premul colors (instead of unpremul).
472// (3) Requires RGBA colors (instead of BGRA).
473static const uint32_t kOverdrawColors[2][6] = {
John Reck1bcacfd2017-11-03 10:12:19 -0700474 {
475 0x00000000, 0x00000000, 0x2f2f0000, 0x2f002f00, 0x3f00003f, 0x7f00007f,
476 },
477 {
478 0x00000000, 0x00000000, 0x2f2f0000, 0x4f004f4f, 0x5f50335f, 0x7f00007f,
479 },
Matt Sarettf58cc922016-11-14 18:33:38 -0500480};
481
482void SkiaPipeline::renderOverdraw(const LayerUpdateQueue& layers, const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700483 const std::vector<sp<RenderNode>>& nodes,
484 const Rect& contentDrawBounds, sk_sp<SkSurface> surface) {
Matt Sarettf58cc922016-11-14 18:33:38 -0500485 // Set up the overdraw canvas.
486 SkImageInfo offscreenInfo = SkImageInfo::MakeA8(surface->width(), surface->height());
487 sk_sp<SkSurface> offscreen = surface->makeSurface(offscreenInfo);
488 SkOverdrawCanvas overdrawCanvas(offscreen->getCanvas());
489
490 // Fake a redraw to replay the draw commands. This will increment the alpha channel
491 // each time a pixel would have been drawn.
492 // Pass true for opaque so we skip the clear - the overdrawCanvas is already zero
493 // initialized.
Romain Guy07ae5052017-06-13 18:25:32 -0700494 renderFrameImpl(layers, clip, nodes, true, false, contentDrawBounds, &overdrawCanvas);
Matt Sarettf58cc922016-11-14 18:33:38 -0500495 sk_sp<SkImage> counts = offscreen->makeImageSnapshot();
496
497 // Draw overdraw colors to the canvas. The color filter will convert counts to colors.
498 SkPaint paint;
499 const SkPMColor* colors = kOverdrawColors[static_cast<int>(Properties::overdrawColorSet)];
500 paint.setColorFilter(SkOverdrawColorFilter::Make(colors));
501 surface->getCanvas()->drawImage(counts.get(), 0.0f, 0.0f, &paint);
502}
503
Stan Iliev500a0c32016-10-26 10:30:09 -0400504} /* namespace skiapipeline */
505} /* namespace uirenderer */
506} /* namespace android */