blob: 066828190627637ab03469b33804d2d13ec98504 [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 Reck322b8ab2019-03-14 13:15:28 -070028#include "thread/CommonPool.h"
John Reck1bcacfd2017-11-03 10:12:19 -070029#include "utils/TraceUtils.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040030
Ben Wagner3aeda5c2017-03-17 17:22:01 -040031#include <unistd.h>
32
Jerome Gaillarda02a12d2019-05-28 18:07:56 +010033#include <android-base/properties.h>
34
Stan Iliev500a0c32016-10-26 10:30:09 -040035using namespace android::uirenderer::renderthread;
36
37namespace android {
38namespace uirenderer {
39namespace skiapipeline {
40
John Reck1bcacfd2017-11-03 10:12:19 -070041float SkiaPipeline::mLightRadius = 0;
Stan Iliev500a0c32016-10-26 10:30:09 -040042uint8_t SkiaPipeline::mAmbientShadowAlpha = 0;
43uint8_t SkiaPipeline::mSpotShadowAlpha = 0;
44
45Vector3 SkiaPipeline::mLightCenter = {FLT_MIN, FLT_MIN, FLT_MIN};
46
John Reck1bcacfd2017-11-03 10:12:19 -070047SkiaPipeline::SkiaPipeline(RenderThread& thread) : mRenderThread(thread) {
Stan Iliev23c38a92017-03-23 00:12:50 -040048 mVectorDrawables.reserve(30);
49}
Stan Iliev500a0c32016-10-26 10:30:09 -040050
Stan Iliev232f3622017-08-23 17:15:09 -040051SkiaPipeline::~SkiaPipeline() {
52 unpinImages();
53}
54
Stan Iliev500a0c32016-10-26 10:30:09 -040055void SkiaPipeline::onDestroyHardwareResources() {
Derek Sollenberger92a9eb92018-04-12 13:42:19 -040056 unpinImages();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040057 mRenderThread.cacheManager().trimStaleResources();
Stan Iliev500a0c32016-10-26 10:30:09 -040058}
59
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -040060bool SkiaPipeline::pinImages(std::vector<SkImage*>& mutableImages) {
61 for (SkImage* image : mutableImages) {
Derek Sollenberger189e8742016-11-16 16:00:17 -050062 if (SkImage_pinAsTexture(image, mRenderThread.getGrContext())) {
63 mPinnedImages.emplace_back(sk_ref_sp(image));
64 } else {
65 return false;
66 }
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -040067 }
68 return true;
69}
70
71void SkiaPipeline::unpinImages() {
72 for (auto& image : mPinnedImages) {
73 SkImage_unpinAsTexture(image.get(), mRenderThread.getGrContext());
74 }
75 mPinnedImages.clear();
76}
77
Stan Iliev47fed6ba2017-10-18 17:56:43 -040078void SkiaPipeline::onPrepareTree() {
79 // The only time mVectorDrawables is not empty is if prepare tree was called 2 times without
80 // a renderFrame in the middle.
81 mVectorDrawables.clear();
82}
83
John Reckd9d7f122018-05-03 14:40:56 -070084void SkiaPipeline::renderLayers(const LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070085 LayerUpdateQueue* layerUpdateQueue, bool opaque,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070086 const LightInfo& lightInfo) {
Stan Iliev500a0c32016-10-26 10:30:09 -040087 updateLighting(lightGeometry, lightInfo);
88 ATRACE_NAME("draw layers");
Stan Iliev23c38a92017-03-23 00:12:50 -040089 renderVectorDrawableCache();
Peiyong Lin1f6aa122018-09-10 16:28:08 -070090 renderLayersImpl(*layerUpdateQueue, opaque);
Stan Iliev500a0c32016-10-26 10:30:09 -040091 layerUpdateQueue->clear();
92}
93
Peiyong Lin1f6aa122018-09-10 16:28:08 -070094void SkiaPipeline::renderLayersImpl(const LayerUpdateQueue& layers, bool opaque) {
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());
Stan Iliev500a0c32016-10-26 10:30:09 -0400105 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
John Reck5cca8f22018-12-10 17:06:22 -0800113 SkCanvas* layerCanvas = layerNode->getLayerSurface()->getCanvas();
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
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
Stan Ilieve9d00122017-09-19 12:07:10 -0400143 GrContext* currentContext = layerNode->getLayerSurface()->getCanvas()->getGrContext();
Derek Sollenbergerf7df1842017-09-05 11:15:58 -0400144 if (cachedContext.get() != currentContext) {
145 if (cachedContext.get()) {
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -0400146 ATRACE_NAME("flush layers (context changed)");
Derek Sollenbergerf7df1842017-09-05 11:15:58 -0400147 cachedContext->flush();
148 }
149 cachedContext.reset(SkSafeRef(currentContext));
150 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400151 }
152 }
Derek Sollenbergerf7df1842017-09-05 11:15:58 -0400153
154 if (cachedContext.get()) {
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -0400155 ATRACE_NAME("flush layers");
Derek Sollenbergerf7df1842017-09-05 11:15:58 -0400156 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,
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700161 ErrorHandler* errorHandler) {
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;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700169 info = SkImageInfo::Make(surfaceWidth, surfaceHeight, getSurfaceColorType(),
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400170 kPremul_SkAlphaType, getSurfaceColorSpace());
Stan Iliev500a0c32016-10-26 10:30:09 -0400171 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
172 SkASSERT(mRenderThread.getGrContext() != nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -0700173 node->setLayerSurface(SkSurface::MakeRenderTarget(mRenderThread.getGrContext(),
Derek Sollenberger25833d22019-01-14 13:55:55 -0500174 SkBudgeted::kYes, info, 0,
175 this->getSurfaceOrigin(), &props));
Stan Iliev500a0c32016-10-26 10:30:09 -0400176 if (node->getLayerSurface()) {
177 // update the transform in window of the layer to reset its origin wrt light source
178 // position
179 Matrix4 windowTransform;
180 damageAccumulator.computeCurrentTransform(&windowTransform);
Stan Ilievda7c19c2019-05-22 14:43:44 -0400181 node->getSkiaLayer()->inverseTransformInWindow.loadInverse(windowTransform);
Stan Iliev216b1572018-03-26 14:29:50 -0400182 } else {
183 String8 cachesOutput;
184 mRenderThread.cacheManager().dumpMemoryUsage(cachesOutput,
John Reck283bb462018-12-13 16:40:14 -0800185 &mRenderThread.renderState());
Stan Iliev216b1572018-03-26 14:29:50 -0400186 ALOGE("%s", cachesOutput.string());
187 if (errorHandler) {
188 std::ostringstream err;
189 err << "Unable to create layer for " << node->getName();
190 const int maxTextureSize = DeviceInfo::get()->maxTextureSize();
191 err << ", size " << info.width() << "x" << info.height() << " max size "
John Reck283bb462018-12-13 16:40:14 -0800192 << maxTextureSize << " color type " << (int)info.colorType() << " has context "
193 << (int)(mRenderThread.getGrContext() != nullptr);
Stan Iliev216b1572018-03-26 14:29:50 -0400194 errorHandler->onError(err.str());
195 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400196 }
197 return true;
198 }
199 return false;
200}
201
Stan Iliev500a0c32016-10-26 10:30:09 -0400202void SkiaPipeline::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) {
203 GrContext* context = thread.getGrContext();
204 if (context) {
205 ATRACE_FORMAT("Bitmap#prepareToDraw %dx%d", bitmap->width(), bitmap->height());
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400206 auto image = bitmap->makeImage();
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400207 if (image.get() && !bitmap->isHardware()) {
208 SkImage_pinAsTexture(image.get(), context);
209 SkImage_unpinAsTexture(image.get(), context);
210 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400211 }
212}
213
Stan Iliev23c38a92017-03-23 00:12:50 -0400214void SkiaPipeline::renderVectorDrawableCache() {
Stan Iliev3310fb12017-03-23 16:56:51 -0400215 if (!mVectorDrawables.empty()) {
216 sp<VectorDrawableAtlas> atlas = mRenderThread.cacheManager().acquireVectorDrawableAtlas();
217 auto grContext = mRenderThread.getGrContext();
218 atlas->prepareForDraw(grContext);
Derek Sollenberger9552c2c2017-08-31 15:37:52 -0400219 ATRACE_NAME("Update VectorDrawables");
Stan Iliev3310fb12017-03-23 16:56:51 -0400220 for (auto vd : mVectorDrawables) {
221 vd->updateCache(atlas, grContext);
Stan Iliev23c38a92017-03-23 00:12:50 -0400222 }
Stan Iliev3310fb12017-03-23 16:56:51 -0400223 mVectorDrawables.clear();
Stan Iliev23c38a92017-03-23 00:12:50 -0400224 }
Stan Iliev23c38a92017-03-23 00:12:50 -0400225}
226
John Reck322b8ab2019-03-14 13:15:28 -0700227static void savePictureAsync(const sk_sp<SkData>& data, const std::string& filename) {
228 CommonPool::post([data, filename] {
229 if (0 == access(filename.c_str(), F_OK)) {
Stan Ilieve9d00122017-09-19 12:07:10 -0400230 return;
231 }
232
John Reck322b8ab2019-03-14 13:15:28 -0700233 SkFILEWStream stream(filename.c_str());
Stan Ilieve9d00122017-09-19 12:07:10 -0400234 if (stream.isValid()) {
John Reck322b8ab2019-03-14 13:15:28 -0700235 stream.write(data->data(), data->size());
Stan Ilieve9d00122017-09-19 12:07:10 -0400236 stream.flush();
John Reck1bcacfd2017-11-03 10:12:19 -0700237 SkDebugf("SKP Captured Drawing Output (%d bytes) for frame. %s", stream.bytesWritten(),
John Reck322b8ab2019-03-14 13:15:28 -0700238 filename.c_str());
Stan Ilieve9d00122017-09-19 12:07:10 -0400239 }
John Reck322b8ab2019-03-14 13:15:28 -0700240 });
241}
Stan Ilieve9d00122017-09-19 12:07:10 -0400242
243SkCanvas* SkiaPipeline::tryCapture(SkSurface* surface) {
244 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
John Reck5cca8f22018-12-10 17:06:22 -0800245 if (mCaptureSequence <= 0) {
Jerome Gaillarda02a12d2019-05-28 18:07:56 +0100246 std::string prop = base::GetProperty(PROPERTY_CAPTURE_SKP_FILENAME, "0");
John Reck5cca8f22018-12-10 17:06:22 -0800247 if (prop[0] != '0' && mCapturedFile != prop) {
Stan Ilieve9d00122017-09-19 12:07:10 -0400248 mCapturedFile = prop;
Jerome Gaillarda02a12d2019-05-28 18:07:56 +0100249 mCaptureSequence = base::GetIntProperty(PROPERTY_CAPTURE_SKP_FRAMES, 1);
Stan Ilieve9d00122017-09-19 12:07:10 -0400250 }
251 }
John Reck5cca8f22018-12-10 17:06:22 -0800252 if (mCaptureSequence > 0 || mPictureCapturedCallback) {
Stan Ilieve9d00122017-09-19 12:07:10 -0400253 mRecorder.reset(new SkPictureRecorder());
John Reck0fa0cbc2019-04-05 16:57:46 -0700254 SkCanvas* pictureCanvas =
255 mRecorder->beginRecording(surface->width(), surface->height(), nullptr,
256 SkPictureRecorder::kPlaybackDrawPicture_RecordFlag);
John Reck5cca8f22018-12-10 17:06:22 -0800257 mNwayCanvas = std::make_unique<SkNWayCanvas>(surface->width(), surface->height());
258 mNwayCanvas->addCanvas(surface->getCanvas());
259 mNwayCanvas->addCanvas(pictureCanvas);
260 return mNwayCanvas.get();
Stan Ilieve9d00122017-09-19 12:07:10 -0400261 }
262 }
263 return surface->getCanvas();
264}
265
266void SkiaPipeline::endCapture(SkSurface* surface) {
John Reck5cca8f22018-12-10 17:06:22 -0800267 mNwayCanvas.reset();
Stan Ilieve9d00122017-09-19 12:07:10 -0400268 if (CC_UNLIKELY(mRecorder.get())) {
John Reck5cca8f22018-12-10 17:06:22 -0800269 ATRACE_CALL();
Stan Ilieve9d00122017-09-19 12:07:10 -0400270 sk_sp<SkPicture> picture = mRecorder->finishRecordingAsPicture();
Stan Ilieve9d00122017-09-19 12:07:10 -0400271 if (picture->approximateOpCount() > 0) {
John Reck5cca8f22018-12-10 17:06:22 -0800272 if (mCaptureSequence > 0) {
273 ATRACE_BEGIN("picture->serialize");
274 auto data = picture->serialize();
275 ATRACE_END();
Stan Ilieve9d00122017-09-19 12:07:10 -0400276
John Reck5cca8f22018-12-10 17:06:22 -0800277 // offload saving to file in a different thread
John Reck5cca8f22018-12-10 17:06:22 -0800278 if (1 == mCaptureSequence) {
John Reck322b8ab2019-03-14 13:15:28 -0700279 savePictureAsync(data, mCapturedFile);
John Reck5cca8f22018-12-10 17:06:22 -0800280 } else {
John Reck0fa0cbc2019-04-05 16:57:46 -0700281 savePictureAsync(data, mCapturedFile + "_" + std::to_string(mCaptureSequence));
John Reck5cca8f22018-12-10 17:06:22 -0800282 }
283 mCaptureSequence--;
Stan Ilieve9d00122017-09-19 12:07:10 -0400284 }
John Reck5cca8f22018-12-10 17:06:22 -0800285 if (mPictureCapturedCallback) {
286 std::invoke(mPictureCapturedCallback, std::move(picture));
Stan Ilieve9d00122017-09-19 12:07:10 -0400287 }
Stan Ilieve9d00122017-09-19 12:07:10 -0400288 }
289 mRecorder.reset();
290 }
291}
292
Stan Iliev500a0c32016-10-26 10:30:09 -0400293void SkiaPipeline::renderFrame(const LayerUpdateQueue& layers, const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700294 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Greg Danielc4076782019-01-08 16:01:18 -0500295 const Rect& contentDrawBounds, sk_sp<SkSurface> surface,
296 const SkMatrix& preTransform) {
John Reck5cca8f22018-12-10 17:06:22 -0800297 bool previousSkpEnabled = Properties::skpCaptureEnabled;
298 if (mPictureCapturedCallback) {
299 Properties::skpCaptureEnabled = true;
300 }
301
Stan Iliev23c38a92017-03-23 00:12:50 -0400302 renderVectorDrawableCache();
303
Stan Iliev500a0c32016-10-26 10:30:09 -0400304 // draw all layers up front
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700305 renderLayersImpl(layers, opaque);
Stan Iliev500a0c32016-10-26 10:30:09 -0400306
Stan Ilieve9d00122017-09-19 12:07:10 -0400307 // initialize the canvas for the current frame, that might be a recording canvas if SKP
308 // capture is enabled.
Stan Iliev500a0c32016-10-26 10:30:09 -0400309 std::unique_ptr<SkPictureRecorder> recorder;
Stan Ilieve9d00122017-09-19 12:07:10 -0400310 SkCanvas* canvas = tryCapture(surface.get());
Stan Iliev500a0c32016-10-26 10:30:09 -0400311
Greg Danielc4076782019-01-08 16:01:18 -0500312 renderFrameImpl(layers, clip, nodes, opaque, contentDrawBounds, canvas, preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -0500313
Stan Ilieve9d00122017-09-19 12:07:10 -0400314 endCapture(surface.get());
Matt Sarettf58cc922016-11-14 18:33:38 -0500315
316 if (CC_UNLIKELY(Properties::debugOverdraw)) {
Greg Danielc4076782019-01-08 16:01:18 -0500317 renderOverdraw(layers, clip, nodes, contentDrawBounds, surface, preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -0500318 }
319
320 ATRACE_NAME("flush commands");
Stan Ilieve9d00122017-09-19 12:07:10 -0400321 surface->getCanvas()->flush();
John Reck5cca8f22018-12-10 17:06:22 -0800322
323 Properties::skpCaptureEnabled = previousSkpEnabled;
Matt Sarettf58cc922016-11-14 18:33:38 -0500324}
325
Stan Iliev52771272016-11-17 09:54:38 -0500326namespace {
327static Rect nodeBounds(RenderNode& node) {
328 auto& props = node.properties();
John Reck1bcacfd2017-11-03 10:12:19 -0700329 return Rect(props.getLeft(), props.getTop(), props.getRight(), props.getBottom());
Stan Iliev52771272016-11-17 09:54:38 -0500330}
John Reck0fa0cbc2019-04-05 16:57:46 -0700331} // namespace
Stan Iliev52771272016-11-17 09:54:38 -0500332
Matt Sarettf58cc922016-11-14 18:33:38 -0500333void SkiaPipeline::renderFrameImpl(const LayerUpdateQueue& layers, const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700334 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Greg Danielc4076782019-01-08 16:01:18 -0500335 const Rect& contentDrawBounds, SkCanvas* canvas,
336 const SkMatrix& preTransform) {
Stan Ilievb66b8bb2016-12-15 18:17:42 -0500337 SkAutoCanvasRestore saver(canvas, true);
Greg Danielc4076782019-01-08 16:01:18 -0500338 canvas->androidFramework_setDeviceClipRestriction(preTransform.mapRect(clip).roundOut());
339 canvas->concat(preTransform);
Stan Iliev500a0c32016-10-26 10:30:09 -0400340
Stan Ilievaadc0322018-03-23 10:50:11 -0400341 // STOPSHIP: Revert, temporary workaround to clear always F16 frame buffer for b/74976293
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700342 if (!opaque || getSurfaceColorType() == kRGBA_F16_SkColorType) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400343 canvas->clear(SK_ColorTRANSPARENT);
344 }
345
Stan Iliev52771272016-11-17 09:54:38 -0500346 if (1 == nodes.size()) {
347 if (!nodes[0]->nothingToDraw()) {
Stan Iliev52771272016-11-17 09:54:38 -0500348 RenderNodeDrawable root(nodes[0].get(), canvas);
349 root.draw(canvas);
350 }
351 } else if (0 == nodes.size()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700352 // nothing to draw
Stan Iliev52771272016-11-17 09:54:38 -0500353 } else {
354 // It there are multiple render nodes, they are laid out as follows:
355 // #0 - backdrop (content + caption)
356 // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop)
357 // #2 - additional overlay nodes
John Reck1bcacfd2017-11-03 10:12:19 -0700358 // Usually the backdrop cannot be seen since it will be entirely covered by the content.
359 // While
360 // resizing however it might become partially visible. The following render loop will crop
361 // the
362 // backdrop against the content and draw the remaining part of it. It will then draw the
363 // content
Stan Iliev52771272016-11-17 09:54:38 -0500364 // cropped to the backdrop (since that indicates a shrinking of the window).
365 //
366 // Additional nodes will be drawn on top with no particular clipping semantics.
Stan Iliev500a0c32016-10-26 10:30:09 -0400367
Stan Iliev52771272016-11-17 09:54:38 -0500368 // Usually the contents bounds should be mContentDrawBounds - however - we will
369 // move it towards the fixed edge to give it a more stable appearance (for the moment).
370 // If there is no content bounds we ignore the layering as stated above and start with 2.
Stan Iliev500a0c32016-10-26 10:30:09 -0400371
Stan Iliev52771272016-11-17 09:54:38 -0500372 // Backdrop bounds in render target space
373 const Rect backdrop = nodeBounds(*nodes[0]);
Stan Iliev500a0c32016-10-26 10:30:09 -0400374
John Reck1bcacfd2017-11-03 10:12:19 -0700375 // Bounds that content will fill in render target space (note content node bounds may be
376 // bigger)
Stan Iliev52771272016-11-17 09:54:38 -0500377 Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight());
378 content.translate(backdrop.left, backdrop.top);
379 if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) {
380 // Content doesn't entirely overlap backdrop, so fill around content (right/bottom)
Stan Iliev500a0c32016-10-26 10:30:09 -0400381
Stan Iliev52771272016-11-17 09:54:38 -0500382 // 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 -0700383 // also fill left/top. Currently, both 2up and freeform position content at the top/left
384 // of
Stan Iliev52771272016-11-17 09:54:38 -0500385 // the backdrop, so this isn't necessary.
386 RenderNodeDrawable backdropNode(nodes[0].get(), canvas);
387 if (content.right < backdrop.right) {
388 // draw backdrop to right side of content
389 SkAutoCanvasRestore acr(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700390 canvas->clipRect(SkRect::MakeLTRB(content.right, backdrop.top, backdrop.right,
391 backdrop.bottom));
Stan Iliev52771272016-11-17 09:54:38 -0500392 backdropNode.draw(canvas);
393 }
394 if (content.bottom < backdrop.bottom) {
395 // draw backdrop to bottom of content
396 // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill
397 SkAutoCanvasRestore acr(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700398 canvas->clipRect(SkRect::MakeLTRB(content.left, content.bottom, content.right,
399 backdrop.bottom));
Stan Iliev52771272016-11-17 09:54:38 -0500400 backdropNode.draw(canvas);
401 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400402 }
403
Stan Iliev52771272016-11-17 09:54:38 -0500404 RenderNodeDrawable contentNode(nodes[1].get(), canvas);
405 if (!backdrop.isEmpty()) {
406 // content node translation to catch up with backdrop
407 float dx = backdrop.left - contentDrawBounds.left;
408 float dy = backdrop.top - contentDrawBounds.top;
409
410 SkAutoCanvasRestore acr(canvas, true);
411 canvas->translate(dx, dy);
John Reck1bcacfd2017-11-03 10:12:19 -0700412 const SkRect contentLocalClip =
413 SkRect::MakeXYWH(contentDrawBounds.left, contentDrawBounds.top,
414 backdrop.getWidth(), backdrop.getHeight());
Stan Iliev52771272016-11-17 09:54:38 -0500415 canvas->clipRect(contentLocalClip);
416 contentNode.draw(canvas);
417 } else {
418 SkAutoCanvasRestore acr(canvas, true);
419 contentNode.draw(canvas);
420 }
421
422 // remaining overlay nodes, simply defer
423 for (size_t index = 2; index < nodes.size(); index++) {
424 if (!nodes[index]->nothingToDraw()) {
425 SkAutoCanvasRestore acr(canvas, true);
426 RenderNodeDrawable overlayNode(nodes[index].get(), canvas);
427 overlayNode.draw(canvas);
428 }
429 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400430 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400431}
432
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500433void SkiaPipeline::dumpResourceCacheUsage() const {
434 int resources, maxResources;
435 size_t bytes, maxBytes;
436 mRenderThread.getGrContext()->getResourceCacheUsage(&resources, &bytes);
437 mRenderThread.getGrContext()->getResourceCacheLimits(&maxResources, &maxBytes);
438
439 SkString log("Resource Cache Usage:\n");
440 log.appendf("%8d items out of %d maximum items\n", resources, maxResources);
John Reck1bcacfd2017-11-03 10:12:19 -0700441 log.appendf("%8zu bytes (%.2f MB) out of %.2f MB maximum\n", bytes,
442 bytes * (1.0f / (1024.0f * 1024.0f)), maxBytes * (1.0f / (1024.0f * 1024.0f)));
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500443
444 ALOGD("%s", log.c_str());
445}
446
Peiyong Lin3bff1352018-12-11 07:56:07 -0800447void SkiaPipeline::setSurfaceColorProperties(ColorMode colorMode) {
448 if (colorMode == ColorMode::SRGB) {
449 mSurfaceColorType = SkColorType::kN32_SkColorType;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800450 mSurfaceColorSpace = SkColorSpace::MakeSRGB();
451 } else if (colorMode == ColorMode::WideColorGamut) {
452 mSurfaceColorType = DeviceInfo::get()->getWideColorType();
Peiyong Lin3bff1352018-12-11 07:56:07 -0800453 mSurfaceColorSpace = DeviceInfo::get()->getWideColorSpace();
454 } else {
455 LOG_ALWAYS_FATAL("Unreachable: unsupported color mode.");
456 }
457}
458
Matt Sarettf58cc922016-11-14 18:33:38 -0500459// Overdraw debugging
460
461// These colors should be kept in sync with Caches::getOverdrawColor() with a few differences.
462// This implementation:
463// (1) Requires transparent entries for "no overdraw" and "single draws".
464// (2) Requires premul colors (instead of unpremul).
465// (3) Requires RGBA colors (instead of BGRA).
466static const uint32_t kOverdrawColors[2][6] = {
John Reck1bcacfd2017-11-03 10:12:19 -0700467 {
John Reck0fa0cbc2019-04-05 16:57:46 -0700468 0x00000000,
469 0x00000000,
470 0x2f2f0000,
471 0x2f002f00,
472 0x3f00003f,
473 0x7f00007f,
John Reck1bcacfd2017-11-03 10:12:19 -0700474 },
475 {
John Reck0fa0cbc2019-04-05 16:57:46 -0700476 0x00000000,
477 0x00000000,
478 0x2f2f0000,
479 0x4f004f4f,
480 0x5f50335f,
481 0x7f00007f,
John Reck1bcacfd2017-11-03 10:12:19 -0700482 },
Matt Sarettf58cc922016-11-14 18:33:38 -0500483};
484
485void SkiaPipeline::renderOverdraw(const LayerUpdateQueue& layers, const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700486 const std::vector<sp<RenderNode>>& nodes,
Greg Danielc4076782019-01-08 16:01:18 -0500487 const Rect& contentDrawBounds, sk_sp<SkSurface> surface,
488 const SkMatrix& preTransform) {
Matt Sarettf58cc922016-11-14 18:33:38 -0500489 // Set up the overdraw canvas.
490 SkImageInfo offscreenInfo = SkImageInfo::MakeA8(surface->width(), surface->height());
491 sk_sp<SkSurface> offscreen = surface->makeSurface(offscreenInfo);
492 SkOverdrawCanvas overdrawCanvas(offscreen->getCanvas());
493
494 // Fake a redraw to replay the draw commands. This will increment the alpha channel
495 // each time a pixel would have been drawn.
496 // Pass true for opaque so we skip the clear - the overdrawCanvas is already zero
497 // initialized.
Greg Danielc4076782019-01-08 16:01:18 -0500498 renderFrameImpl(layers, clip, nodes, true, contentDrawBounds, &overdrawCanvas, preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -0500499 sk_sp<SkImage> counts = offscreen->makeImageSnapshot();
500
501 // Draw overdraw colors to the canvas. The color filter will convert counts to colors.
502 SkPaint paint;
503 const SkPMColor* colors = kOverdrawColors[static_cast<int>(Properties::overdrawColorSet)];
504 paint.setColorFilter(SkOverdrawColorFilter::Make(colors));
505 surface->getCanvas()->drawImage(counts.get(), 0.0f, 0.0f, &paint);
506}
507
Stan Iliev500a0c32016-10-26 10:30:09 -0400508} /* namespace skiapipeline */
509} /* namespace uirenderer */
510} /* namespace android */