blob: d7faaf707aa864b8470fe788707600958c8b7cea [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>
Matt Sarettf58cc922016-11-14 18:33:38 -050022#include <SkOverdrawCanvas.h>
23#include <SkOverdrawColorFilter.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040024#include <SkPicture.h>
25#include <SkPictureRecorder.h>
Stan Iliev216b1572018-03-26 14:29:50 -040026#include "TreeInfo.h"
Stan Iliev23c38a92017-03-23 00:12:50 -040027#include "VectorDrawable.h"
John Reck1bcacfd2017-11-03 10:12:19 -070028#include "utils/TraceUtils.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040029
Ben Wagner3aeda5c2017-03-17 17:22:01 -040030#include <unistd.h>
31
Stan Iliev500a0c32016-10-26 10:30:09 -040032using namespace android::uirenderer::renderthread;
33
34namespace android {
35namespace uirenderer {
36namespace skiapipeline {
37
John Reck1bcacfd2017-11-03 10:12:19 -070038float SkiaPipeline::mLightRadius = 0;
Stan Iliev500a0c32016-10-26 10:30:09 -040039uint8_t SkiaPipeline::mAmbientShadowAlpha = 0;
40uint8_t SkiaPipeline::mSpotShadowAlpha = 0;
41
42Vector3 SkiaPipeline::mLightCenter = {FLT_MIN, FLT_MIN, FLT_MIN};
43
John Reck1bcacfd2017-11-03 10:12:19 -070044SkiaPipeline::SkiaPipeline(RenderThread& thread) : mRenderThread(thread) {
Stan Iliev23c38a92017-03-23 00:12:50 -040045 mVectorDrawables.reserve(30);
46}
Stan Iliev500a0c32016-10-26 10:30:09 -040047
Stan Iliev232f3622017-08-23 17:15:09 -040048SkiaPipeline::~SkiaPipeline() {
49 unpinImages();
50}
51
Stan Iliev500a0c32016-10-26 10:30:09 -040052TaskManager* SkiaPipeline::getTaskManager() {
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040053 return mRenderThread.cacheManager().getTaskManager();
Stan Iliev500a0c32016-10-26 10:30:09 -040054}
55
56void SkiaPipeline::onDestroyHardwareResources() {
Derek Sollenberger92a9eb92018-04-12 13:42:19 -040057 unpinImages();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040058 mRenderThread.cacheManager().trimStaleResources();
Stan Iliev500a0c32016-10-26 10:30:09 -040059}
60
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -040061bool SkiaPipeline::pinImages(std::vector<SkImage*>& mutableImages) {
62 for (SkImage* image : mutableImages) {
Derek Sollenberger189e8742016-11-16 16:00:17 -050063 if (SkImage_pinAsTexture(image, mRenderThread.getGrContext())) {
64 mPinnedImages.emplace_back(sk_ref_sp(image));
65 } else {
66 return false;
67 }
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -040068 }
69 return true;
70}
71
72void SkiaPipeline::unpinImages() {
73 for (auto& image : mPinnedImages) {
74 SkImage_unpinAsTexture(image.get(), mRenderThread.getGrContext());
75 }
76 mPinnedImages.clear();
77}
78
Stan Iliev47fed6ba2017-10-18 17:56:43 -040079void SkiaPipeline::onPrepareTree() {
80 // The only time mVectorDrawables is not empty is if prepare tree was called 2 times without
81 // a renderFrame in the middle.
82 mVectorDrawables.clear();
83}
84
John Reckd9d7f122018-05-03 14:40:56 -070085void SkiaPipeline::renderLayers(const LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070086 LayerUpdateQueue* layerUpdateQueue, bool opaque,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070087 const LightInfo& lightInfo) {
Stan Iliev500a0c32016-10-26 10:30:09 -040088 updateLighting(lightGeometry, lightInfo);
89 ATRACE_NAME("draw layers");
Stan Iliev23c38a92017-03-23 00:12:50 -040090 renderVectorDrawableCache();
Peiyong Lin1f6aa122018-09-10 16:28:08 -070091 renderLayersImpl(*layerUpdateQueue, opaque);
Stan Iliev500a0c32016-10-26 10:30:09 -040092 layerUpdateQueue->clear();
93}
94
Peiyong Lin1f6aa122018-09-10 16:28:08 -070095void SkiaPipeline::renderLayersImpl(const LayerUpdateQueue& layers, bool opaque) {
Derek Sollenbergerf7df1842017-09-05 11:15:58 -040096 sk_sp<GrContext> cachedContext;
97
Stan Iliev500a0c32016-10-26 10:30:09 -040098 // Render all layers that need to be updated, in order.
99 for (size_t i = 0; i < layers.entries().size(); i++) {
John Reckfc29f7acd2017-03-02 13:23:16 -0800100 RenderNode* layerNode = layers.entries()[i].renderNode.get();
Stan Iliev500a0c32016-10-26 10:30:09 -0400101 // only schedule repaint if node still on layer - possible it may have been
102 // removed during a dropped frame, but layers may still remain scheduled so
103 // as not to lose info on what portion is damaged
104 if (CC_LIKELY(layerNode->getLayerSurface() != nullptr)) {
105 SkASSERT(layerNode->getLayerSurface());
Stan Iliev500a0c32016-10-26 10:30:09 -0400106 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()) {
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -0400149 ATRACE_NAME("flush layers (context changed)");
Derek Sollenbergerf7df1842017-09-05 11:15:58 -0400150 cachedContext->flush();
151 }
152 cachedContext.reset(SkSafeRef(currentContext));
153 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400154 }
155 }
Derek Sollenbergerf7df1842017-09-05 11:15:58 -0400156
157 if (cachedContext.get()) {
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -0400158 ATRACE_NAME("flush layers");
Derek Sollenbergerf7df1842017-09-05 11:15:58 -0400159 cachedContext->flush();
160 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400161}
162
John Reck1bcacfd2017-11-03 10:12:19 -0700163bool SkiaPipeline::createOrUpdateLayer(RenderNode* node, const DamageAccumulator& damageAccumulator,
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700164 ErrorHandler* errorHandler) {
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500165 // compute the size of the surface (i.e. texture) to be allocated for this layer
166 const int surfaceWidth = ceilf(node->getWidth() / float(LAYER_SIZE)) * LAYER_SIZE;
167 const int surfaceHeight = ceilf(node->getHeight() / float(LAYER_SIZE)) * LAYER_SIZE;
168
Stan Iliev500a0c32016-10-26 10:30:09 -0400169 SkSurface* layer = node->getLayerSurface();
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500170 if (!layer || layer->width() != surfaceWidth || layer->height() != surfaceHeight) {
Stan Iliev08fc19a2017-07-24 10:20:33 -0400171 SkImageInfo info;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700172 info = SkImageInfo::Make(surfaceWidth, surfaceHeight, getSurfaceColorType(),
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400173 kPremul_SkAlphaType, getSurfaceColorSpace());
Stan Iliev500a0c32016-10-26 10:30:09 -0400174 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
175 SkASSERT(mRenderThread.getGrContext() != nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -0700176 node->setLayerSurface(SkSurface::MakeRenderTarget(mRenderThread.getGrContext(),
Derek Sollenberger25833d22019-01-14 13:55:55 -0500177 SkBudgeted::kYes, info, 0,
178 this->getSurfaceOrigin(), &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;
Stan Iliev216b1572018-03-26 14:29:50 -0400185 } else {
186 String8 cachesOutput;
187 mRenderThread.cacheManager().dumpMemoryUsage(cachesOutput,
John Reck283bb462018-12-13 16:40:14 -0800188 &mRenderThread.renderState());
Stan Iliev216b1572018-03-26 14:29:50 -0400189 ALOGE("%s", cachesOutput.string());
190 if (errorHandler) {
191 std::ostringstream err;
192 err << "Unable to create layer for " << node->getName();
193 const int maxTextureSize = DeviceInfo::get()->maxTextureSize();
194 err << ", size " << info.width() << "x" << info.height() << " max size "
John Reck283bb462018-12-13 16:40:14 -0800195 << maxTextureSize << " color type " << (int)info.colorType() << " has context "
196 << (int)(mRenderThread.getGrContext() != nullptr);
Stan Iliev216b1572018-03-26 14:29:50 -0400197 errorHandler->onError(err.str());
198 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400199 }
200 return true;
201 }
202 return false;
203}
204
Stan Iliev500a0c32016-10-26 10:30:09 -0400205void SkiaPipeline::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) {
206 GrContext* context = thread.getGrContext();
207 if (context) {
208 ATRACE_FORMAT("Bitmap#prepareToDraw %dx%d", bitmap->width(), bitmap->height());
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400209 auto image = bitmap->makeImage();
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400210 if (image.get() && !bitmap->isHardware()) {
211 SkImage_pinAsTexture(image.get(), context);
212 SkImage_unpinAsTexture(image.get(), context);
213 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400214 }
215}
216
Stan Iliev23c38a92017-03-23 00:12:50 -0400217void SkiaPipeline::renderVectorDrawableCache() {
Stan Iliev3310fb12017-03-23 16:56:51 -0400218 if (!mVectorDrawables.empty()) {
219 sp<VectorDrawableAtlas> atlas = mRenderThread.cacheManager().acquireVectorDrawableAtlas();
220 auto grContext = mRenderThread.getGrContext();
221 atlas->prepareForDraw(grContext);
Derek Sollenberger9552c2c2017-08-31 15:37:52 -0400222 ATRACE_NAME("Update VectorDrawables");
Stan Iliev3310fb12017-03-23 16:56:51 -0400223 for (auto vd : mVectorDrawables) {
224 vd->updateCache(atlas, grContext);
Stan Iliev23c38a92017-03-23 00:12:50 -0400225 }
Stan Iliev3310fb12017-03-23 16:56:51 -0400226 mVectorDrawables.clear();
Stan Iliev23c38a92017-03-23 00:12:50 -0400227 }
Stan Iliev23c38a92017-03-23 00:12:50 -0400228}
229
Stan Ilieve9d00122017-09-19 12:07:10 -0400230class SkiaPipeline::SavePictureProcessor : public TaskProcessor<bool> {
231public:
232 explicit SavePictureProcessor(TaskManager* taskManager) : TaskProcessor<bool>(taskManager) {}
233
234 struct SavePictureTask : public Task<bool> {
235 sk_sp<SkData> data;
236 std::string filename;
237 };
238
239 void savePicture(const sk_sp<SkData>& data, const std::string& filename) {
240 sp<SavePictureTask> task(new SavePictureTask());
241 task->data = data;
242 task->filename = filename;
243 TaskProcessor<bool>::add(task);
244 }
245
John Reck1bcacfd2017-11-03 10:12:19 -0700246 virtual void onProcess(const sp<Task<bool>>& task) override {
Stan Ilieve9d00122017-09-19 12:07:10 -0400247 SavePictureTask* t = static_cast<SavePictureTask*>(task.get());
248
249 if (0 == access(t->filename.c_str(), F_OK)) {
250 task->setResult(false);
251 return;
252 }
253
254 SkFILEWStream stream(t->filename.c_str());
255 if (stream.isValid()) {
256 stream.write(t->data->data(), t->data->size());
257 stream.flush();
John Reck1bcacfd2017-11-03 10:12:19 -0700258 SkDebugf("SKP Captured Drawing Output (%d bytes) for frame. %s", stream.bytesWritten(),
259 t->filename.c_str());
Stan Ilieve9d00122017-09-19 12:07:10 -0400260 }
261
262 task->setResult(true);
263 }
264};
265
266SkCanvas* SkiaPipeline::tryCapture(SkSurface* surface) {
267 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
268 bool recordingPicture = mCaptureSequence > 0;
269 char prop[PROPERTY_VALUE_MAX] = {'\0'};
270 if (!recordingPicture) {
271 property_get(PROPERTY_CAPTURE_SKP_FILENAME, prop, "0");
John Reck1bcacfd2017-11-03 10:12:19 -0700272 recordingPicture = prop[0] != '0' &&
273 mCapturedFile != prop; // ensure we capture only once per filename
Stan Ilieve9d00122017-09-19 12:07:10 -0400274 if (recordingPicture) {
275 mCapturedFile = prop;
276 mCaptureSequence = property_get_int32(PROPERTY_CAPTURE_SKP_FRAMES, 1);
277 }
278 }
279 if (recordingPicture) {
280 mRecorder.reset(new SkPictureRecorder());
281 return mRecorder->beginRecording(surface->width(), surface->height(), nullptr,
John Reck1bcacfd2017-11-03 10:12:19 -0700282 SkPictureRecorder::kPlaybackDrawPicture_RecordFlag);
Stan Ilieve9d00122017-09-19 12:07:10 -0400283 }
284 }
285 return surface->getCanvas();
286}
287
288void SkiaPipeline::endCapture(SkSurface* surface) {
289 if (CC_UNLIKELY(mRecorder.get())) {
290 sk_sp<SkPicture> picture = mRecorder->finishRecordingAsPicture();
291 surface->getCanvas()->drawPicture(picture);
292 if (picture->approximateOpCount() > 0) {
Mike Reedebf96fb2017-12-13 13:43:16 -0500293 auto data = picture->serialize();
Stan Ilieve9d00122017-09-19 12:07:10 -0400294
John Reck1bcacfd2017-11-03 10:12:19 -0700295 // offload saving to file in a different thread
Stan Ilieve9d00122017-09-19 12:07:10 -0400296 if (!mSavePictureProcessor.get()) {
297 TaskManager* taskManager = getTaskManager();
298 mSavePictureProcessor = new SavePictureProcessor(
299 taskManager->canRunTasks() ? taskManager : nullptr);
300 }
301 if (1 == mCaptureSequence) {
Mike Reedebf96fb2017-12-13 13:43:16 -0500302 mSavePictureProcessor->savePicture(data, mCapturedFile);
Stan Ilieve9d00122017-09-19 12:07:10 -0400303 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700304 mSavePictureProcessor->savePicture(
John Reck283bb462018-12-13 16:40:14 -0800305 data, mCapturedFile + "_" + std::to_string(mCaptureSequence));
Stan Ilieve9d00122017-09-19 12:07:10 -0400306 }
307 mCaptureSequence--;
308 }
309 mRecorder.reset();
310 }
311}
312
Stan Iliev500a0c32016-10-26 10:30:09 -0400313void SkiaPipeline::renderFrame(const LayerUpdateQueue& layers, const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700314 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700315 const Rect& contentDrawBounds, sk_sp<SkSurface> surface) {
Stan Iliev23c38a92017-03-23 00:12:50 -0400316 renderVectorDrawableCache();
317
Stan Iliev500a0c32016-10-26 10:30:09 -0400318 // draw all layers up front
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700319 renderLayersImpl(layers, opaque);
Stan Iliev500a0c32016-10-26 10:30:09 -0400320
Stan Ilieve9d00122017-09-19 12:07:10 -0400321 // initialize the canvas for the current frame, that might be a recording canvas if SKP
322 // capture is enabled.
Stan Iliev500a0c32016-10-26 10:30:09 -0400323 std::unique_ptr<SkPictureRecorder> recorder;
Stan Ilieve9d00122017-09-19 12:07:10 -0400324 SkCanvas* canvas = tryCapture(surface.get());
Stan Iliev500a0c32016-10-26 10:30:09 -0400325
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700326 renderFrameImpl(layers, clip, nodes, opaque, contentDrawBounds, canvas);
Matt Sarettf58cc922016-11-14 18:33:38 -0500327
Stan Ilieve9d00122017-09-19 12:07:10 -0400328 endCapture(surface.get());
Matt Sarettf58cc922016-11-14 18:33:38 -0500329
330 if (CC_UNLIKELY(Properties::debugOverdraw)) {
331 renderOverdraw(layers, clip, nodes, contentDrawBounds, surface);
332 }
333
334 ATRACE_NAME("flush commands");
Stan Ilieve9d00122017-09-19 12:07:10 -0400335 surface->getCanvas()->flush();
Matt Sarettf58cc922016-11-14 18:33:38 -0500336}
337
Stan Iliev52771272016-11-17 09:54:38 -0500338namespace {
339static Rect nodeBounds(RenderNode& node) {
340 auto& props = node.properties();
John Reck1bcacfd2017-11-03 10:12:19 -0700341 return Rect(props.getLeft(), props.getTop(), props.getRight(), props.getBottom());
Stan Iliev52771272016-11-17 09:54:38 -0500342}
343}
344
Matt Sarettf58cc922016-11-14 18:33:38 -0500345void SkiaPipeline::renderFrameImpl(const LayerUpdateQueue& layers, const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700346 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700347 const Rect& contentDrawBounds, SkCanvas* canvas) {
Stan Ilievb66b8bb2016-12-15 18:17:42 -0500348 SkAutoCanvasRestore saver(canvas, true);
349 canvas->androidFramework_setDeviceClipRestriction(clip.roundOut());
Stan Iliev500a0c32016-10-26 10:30:09 -0400350
Stan Ilievaadc0322018-03-23 10:50:11 -0400351 // STOPSHIP: Revert, temporary workaround to clear always F16 frame buffer for b/74976293
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700352 if (!opaque || getSurfaceColorType() == kRGBA_F16_SkColorType) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400353 canvas->clear(SK_ColorTRANSPARENT);
354 }
355
Stan Iliev52771272016-11-17 09:54:38 -0500356 if (1 == nodes.size()) {
357 if (!nodes[0]->nothingToDraw()) {
Stan Iliev52771272016-11-17 09:54:38 -0500358 RenderNodeDrawable root(nodes[0].get(), canvas);
359 root.draw(canvas);
360 }
361 } else if (0 == nodes.size()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700362 // nothing to draw
Stan Iliev52771272016-11-17 09:54:38 -0500363 } else {
364 // It there are multiple render nodes, they are laid out as follows:
365 // #0 - backdrop (content + caption)
366 // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop)
367 // #2 - additional overlay nodes
John Reck1bcacfd2017-11-03 10:12:19 -0700368 // Usually the backdrop cannot be seen since it will be entirely covered by the content.
369 // While
370 // resizing however it might become partially visible. The following render loop will crop
371 // the
372 // backdrop against the content and draw the remaining part of it. It will then draw the
373 // content
Stan Iliev52771272016-11-17 09:54:38 -0500374 // cropped to the backdrop (since that indicates a shrinking of the window).
375 //
376 // Additional nodes will be drawn on top with no particular clipping semantics.
Stan Iliev500a0c32016-10-26 10:30:09 -0400377
Stan Iliev52771272016-11-17 09:54:38 -0500378 // Usually the contents bounds should be mContentDrawBounds - however - we will
379 // move it towards the fixed edge to give it a more stable appearance (for the moment).
380 // If there is no content bounds we ignore the layering as stated above and start with 2.
Stan Iliev500a0c32016-10-26 10:30:09 -0400381
Stan Iliev52771272016-11-17 09:54:38 -0500382 // Backdrop bounds in render target space
383 const Rect backdrop = nodeBounds(*nodes[0]);
Stan Iliev500a0c32016-10-26 10:30:09 -0400384
John Reck1bcacfd2017-11-03 10:12:19 -0700385 // Bounds that content will fill in render target space (note content node bounds may be
386 // bigger)
Stan Iliev52771272016-11-17 09:54:38 -0500387 Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight());
388 content.translate(backdrop.left, backdrop.top);
389 if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) {
390 // Content doesn't entirely overlap backdrop, so fill around content (right/bottom)
Stan Iliev500a0c32016-10-26 10:30:09 -0400391
Stan Iliev52771272016-11-17 09:54:38 -0500392 // 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 -0700393 // also fill left/top. Currently, both 2up and freeform position content at the top/left
394 // of
Stan Iliev52771272016-11-17 09:54:38 -0500395 // the backdrop, so this isn't necessary.
396 RenderNodeDrawable backdropNode(nodes[0].get(), canvas);
397 if (content.right < backdrop.right) {
398 // draw backdrop to right side of content
399 SkAutoCanvasRestore acr(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700400 canvas->clipRect(SkRect::MakeLTRB(content.right, backdrop.top, backdrop.right,
401 backdrop.bottom));
Stan Iliev52771272016-11-17 09:54:38 -0500402 backdropNode.draw(canvas);
403 }
404 if (content.bottom < backdrop.bottom) {
405 // draw backdrop to bottom of content
406 // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill
407 SkAutoCanvasRestore acr(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700408 canvas->clipRect(SkRect::MakeLTRB(content.left, content.bottom, content.right,
409 backdrop.bottom));
Stan Iliev52771272016-11-17 09:54:38 -0500410 backdropNode.draw(canvas);
411 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400412 }
413
Stan Iliev52771272016-11-17 09:54:38 -0500414 RenderNodeDrawable contentNode(nodes[1].get(), canvas);
415 if (!backdrop.isEmpty()) {
416 // content node translation to catch up with backdrop
417 float dx = backdrop.left - contentDrawBounds.left;
418 float dy = backdrop.top - contentDrawBounds.top;
419
420 SkAutoCanvasRestore acr(canvas, true);
421 canvas->translate(dx, dy);
John Reck1bcacfd2017-11-03 10:12:19 -0700422 const SkRect contentLocalClip =
423 SkRect::MakeXYWH(contentDrawBounds.left, contentDrawBounds.top,
424 backdrop.getWidth(), backdrop.getHeight());
Stan Iliev52771272016-11-17 09:54:38 -0500425 canvas->clipRect(contentLocalClip);
426 contentNode.draw(canvas);
427 } else {
428 SkAutoCanvasRestore acr(canvas, true);
429 contentNode.draw(canvas);
430 }
431
432 // remaining overlay nodes, simply defer
433 for (size_t index = 2; index < nodes.size(); index++) {
434 if (!nodes[index]->nothingToDraw()) {
435 SkAutoCanvasRestore acr(canvas, true);
436 RenderNodeDrawable overlayNode(nodes[index].get(), canvas);
437 overlayNode.draw(canvas);
438 }
439 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400440 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400441}
442
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500443void SkiaPipeline::dumpResourceCacheUsage() const {
444 int resources, maxResources;
445 size_t bytes, maxBytes;
446 mRenderThread.getGrContext()->getResourceCacheUsage(&resources, &bytes);
447 mRenderThread.getGrContext()->getResourceCacheLimits(&maxResources, &maxBytes);
448
449 SkString log("Resource Cache Usage:\n");
450 log.appendf("%8d items out of %d maximum items\n", resources, maxResources);
John Reck1bcacfd2017-11-03 10:12:19 -0700451 log.appendf("%8zu bytes (%.2f MB) out of %.2f MB maximum\n", bytes,
452 bytes * (1.0f / (1024.0f * 1024.0f)), maxBytes * (1.0f / (1024.0f * 1024.0f)));
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500453
454 ALOGD("%s", log.c_str());
455}
456
Peiyong Lin3bff1352018-12-11 07:56:07 -0800457void SkiaPipeline::setSurfaceColorProperties(ColorMode colorMode) {
458 if (colorMode == ColorMode::SRGB) {
459 mSurfaceColorType = SkColorType::kN32_SkColorType;
460 mSurfaceColorGamut = SkColorSpace::Gamut::kSRGB_Gamut;
461 mSurfaceColorSpace = SkColorSpace::MakeSRGB();
462 } else if (colorMode == ColorMode::WideColorGamut) {
463 mSurfaceColorType = DeviceInfo::get()->getWideColorType();
464 mSurfaceColorGamut = DeviceInfo::get()->getWideColorGamut();
465 mSurfaceColorSpace = DeviceInfo::get()->getWideColorSpace();
466 } else {
467 LOG_ALWAYS_FATAL("Unreachable: unsupported color mode.");
468 }
469}
470
Matt Sarettf58cc922016-11-14 18:33:38 -0500471// Overdraw debugging
472
473// These colors should be kept in sync with Caches::getOverdrawColor() with a few differences.
474// This implementation:
475// (1) Requires transparent entries for "no overdraw" and "single draws".
476// (2) Requires premul colors (instead of unpremul).
477// (3) Requires RGBA colors (instead of BGRA).
478static const uint32_t kOverdrawColors[2][6] = {
John Reck1bcacfd2017-11-03 10:12:19 -0700479 {
480 0x00000000, 0x00000000, 0x2f2f0000, 0x2f002f00, 0x3f00003f, 0x7f00007f,
481 },
482 {
483 0x00000000, 0x00000000, 0x2f2f0000, 0x4f004f4f, 0x5f50335f, 0x7f00007f,
484 },
Matt Sarettf58cc922016-11-14 18:33:38 -0500485};
486
487void SkiaPipeline::renderOverdraw(const LayerUpdateQueue& layers, const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700488 const std::vector<sp<RenderNode>>& nodes,
489 const Rect& contentDrawBounds, sk_sp<SkSurface> surface) {
Matt Sarettf58cc922016-11-14 18:33:38 -0500490 // Set up the overdraw canvas.
491 SkImageInfo offscreenInfo = SkImageInfo::MakeA8(surface->width(), surface->height());
492 sk_sp<SkSurface> offscreen = surface->makeSurface(offscreenInfo);
493 SkOverdrawCanvas overdrawCanvas(offscreen->getCanvas());
494
495 // Fake a redraw to replay the draw commands. This will increment the alpha channel
496 // each time a pixel would have been drawn.
497 // Pass true for opaque so we skip the clear - the overdrawCanvas is already zero
498 // initialized.
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700499 renderFrameImpl(layers, clip, nodes, true, contentDrawBounds, &overdrawCanvas);
Matt Sarettf58cc922016-11-14 18:33:38 -0500500 sk_sp<SkImage> counts = offscreen->makeImageSnapshot();
501
502 // Draw overdraw colors to the canvas. The color filter will convert counts to colors.
503 SkPaint paint;
504 const SkPMColor* colors = kOverdrawColors[static_cast<int>(Properties::overdrawColorSet)];
505 paint.setColorFilter(SkOverdrawColorFilter::Make(colors));
506 surface->getCanvas()->drawImage(counts.get(), 0.0f, 0.0f, &paint);
507}
508
Stan Iliev500a0c32016-10-26 10:30:09 -0400509} /* namespace skiapipeline */
510} /* namespace uirenderer */
511} /* namespace android */