Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
Kevin Lubick | 6817fc4 | 2023-05-12 19:27:20 +0000 | [diff] [blame] | 19 | #include <include/android/SkSurfaceAndroid.h> |
| 20 | #include <include/gpu/ganesh/SkSurfaceGanesh.h> |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 21 | #include <SkCanvas.h> |
| 22 | #include <SkColor.h> |
| 23 | #include <SkColorSpace.h> |
| 24 | #include <SkData.h> |
| 25 | #include <SkImage.h> |
Kevin Lubick | 9367108 | 2023-03-13 18:30:55 +0000 | [diff] [blame] | 26 | #include <SkImageAndroid.h> |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 27 | #include <SkImageInfo.h> |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 28 | #include <SkMatrix.h> |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 29 | #include <SkMultiPictureDocument.h> |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 30 | #include <SkOverdrawCanvas.h> |
| 31 | #include <SkOverdrawColorFilter.h> |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 32 | #include <SkPicture.h> |
| 33 | #include <SkPictureRecorder.h> |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 34 | #include <SkRect.h> |
| 35 | #include <SkRefCnt.h> |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 36 | #include <SkSerialProcs.h> |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 37 | #include <SkStream.h> |
| 38 | #include <SkString.h> |
Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame] | 39 | #include <SkTypeface.h> |
| 40 | #include <android-base/properties.h> |
John Reck | 29b1ee0 | 2023-04-04 17:44:23 -0400 | [diff] [blame] | 41 | #include <gui/TraceUtils.h> |
Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame] | 42 | #include <unistd.h> |
| 43 | |
| 44 | #include <sstream> |
| 45 | |
Fedor Kudasov | 90df056 | 2019-06-19 11:41:34 +0100 | [diff] [blame] | 46 | #include "LightingInfo.h" |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 47 | #include "VectorDrawable.h" |
John Reck | 29b1ee0 | 2023-04-04 17:44:23 -0400 | [diff] [blame] | 48 | #include "include/gpu/GpuTypes.h" // from Skia |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 49 | #include "thread/CommonPool.h" |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 50 | #include "tools/SkSharingProc.h" |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 51 | #include "utils/Color.h" |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 52 | #include "utils/String8.h" |
Jerome Gaillard | a02a12d | 2019-05-28 18:07:56 +0100 | [diff] [blame] | 53 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 54 | using namespace android::uirenderer::renderthread; |
| 55 | |
| 56 | namespace android { |
| 57 | namespace uirenderer { |
| 58 | namespace skiapipeline { |
| 59 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 60 | SkiaPipeline::SkiaPipeline(RenderThread& thread) : mRenderThread(thread) { |
Derek Sollenberger | 1863d94 | 2020-02-05 15:41:51 -0500 | [diff] [blame] | 61 | setSurfaceColorProperties(mColorMode); |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 62 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 63 | |
Stan Iliev | 232f362 | 2017-08-23 17:15:09 -0400 | [diff] [blame] | 64 | SkiaPipeline::~SkiaPipeline() { |
| 65 | unpinImages(); |
| 66 | } |
| 67 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 68 | void SkiaPipeline::onDestroyHardwareResources() { |
Derek Sollenberger | 92a9eb9 | 2018-04-12 13:42:19 -0400 | [diff] [blame] | 69 | unpinImages(); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 70 | mRenderThread.cacheManager().trimStaleResources(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 73 | bool SkiaPipeline::pinImages(std::vector<SkImage*>& mutableImages) { |
Greg Daniel | f8cb525 | 2021-06-30 10:40:34 -0400 | [diff] [blame] | 74 | if (!mRenderThread.getGrContext()) { |
| 75 | ALOGD("Trying to pin an image with an invalid GrContext"); |
| 76 | return false; |
| 77 | } |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 78 | for (SkImage* image : mutableImages) { |
Kevin Lubick | 9367108 | 2023-03-13 18:30:55 +0000 | [diff] [blame] | 79 | if (skgpu::ganesh::PinAsTexture(mRenderThread.getGrContext(), image)) { |
Derek Sollenberger | 189e874 | 2016-11-16 16:00:17 -0500 | [diff] [blame] | 80 | mPinnedImages.emplace_back(sk_ref_sp(image)); |
| 81 | } else { |
| 82 | return false; |
| 83 | } |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 84 | } |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | void SkiaPipeline::unpinImages() { |
| 89 | for (auto& image : mPinnedImages) { |
Kevin Lubick | 9367108 | 2023-03-13 18:30:55 +0000 | [diff] [blame] | 90 | skgpu::ganesh::UnpinTexture(mRenderThread.getGrContext(), image.get()); |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 91 | } |
| 92 | mPinnedImages.clear(); |
| 93 | } |
| 94 | |
John Reck | d9d7f12 | 2018-05-03 14:40:56 -0700 | [diff] [blame] | 95 | void SkiaPipeline::renderLayers(const LightGeometry& lightGeometry, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 96 | LayerUpdateQueue* layerUpdateQueue, bool opaque, |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 97 | const LightInfo& lightInfo) { |
Fedor Kudasov | 90df056 | 2019-06-19 11:41:34 +0100 | [diff] [blame] | 98 | LightingInfo::updateLighting(lightGeometry, lightInfo); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 99 | ATRACE_NAME("draw layers"); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 100 | renderLayersImpl(*layerUpdateQueue, opaque); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 101 | layerUpdateQueue->clear(); |
| 102 | } |
| 103 | |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 104 | void SkiaPipeline::renderLayersImpl(const LayerUpdateQueue& layers, bool opaque) { |
Adlai Holler | ab76215 | 2020-09-16 10:37:19 -0400 | [diff] [blame] | 105 | sk_sp<GrDirectContext> cachedContext; |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 106 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 107 | // Render all layers that need to be updated, in order. |
| 108 | for (size_t i = 0; i < layers.entries().size(); i++) { |
John Reck | fc29f7acd | 2017-03-02 13:23:16 -0800 | [diff] [blame] | 109 | RenderNode* layerNode = layers.entries()[i].renderNode.get(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 110 | // only schedule repaint if node still on layer - possible it may have been |
| 111 | // removed during a dropped frame, but layers may still remain scheduled so |
| 112 | // as not to lose info on what portion is damaged |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 113 | if (CC_UNLIKELY(layerNode->getLayerSurface() == nullptr)) { |
| 114 | continue; |
| 115 | } |
| 116 | SkASSERT(layerNode->getLayerSurface()); |
John Reck | be67195 | 2021-01-13 22:39:32 -0500 | [diff] [blame] | 117 | SkiaDisplayList* displayList = layerNode->getDisplayList().asSkiaDl(); |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 118 | if (!displayList || displayList->isEmpty()) { |
| 119 | ALOGE("%p drawLayers(%s) : missing drawable", layerNode, layerNode->getName()); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | const Rect& layerDamage = layers.entries()[i].damage; |
| 124 | |
| 125 | SkCanvas* layerCanvas = layerNode->getLayerSurface()->getCanvas(); |
| 126 | |
| 127 | int saveCount = layerCanvas->save(); |
| 128 | SkASSERT(saveCount == 1); |
| 129 | |
| 130 | layerCanvas->androidFramework_setDeviceClipRestriction(layerDamage.toSkIRect()); |
| 131 | |
| 132 | // TODO: put localized light center calculation and storage to a drawable related code. |
| 133 | // It does not seem right to store something localized in a global state |
| 134 | // fix here and in recordLayers |
| 135 | const Vector3 savedLightCenter(LightingInfo::getLightCenterRaw()); |
| 136 | Vector3 transformedLightCenter(savedLightCenter); |
| 137 | // map current light center into RenderNode's coordinate space |
| 138 | layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(transformedLightCenter); |
| 139 | LightingInfo::setLightCenterRaw(transformedLightCenter); |
| 140 | |
| 141 | const RenderProperties& properties = layerNode->properties(); |
| 142 | const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight()); |
| 143 | if (properties.getClipToBounds() && layerCanvas->quickReject(bounds)) { |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | ATRACE_FORMAT("drawLayer [%s] %.1f x %.1f", layerNode->getName(), bounds.width(), |
| 148 | bounds.height()); |
| 149 | |
| 150 | layerNode->getSkiaLayer()->hasRenderedSinceRepaint = false; |
| 151 | layerCanvas->clear(SK_ColorTRANSPARENT); |
| 152 | |
| 153 | RenderNodeDrawable root(layerNode, layerCanvas, false); |
| 154 | root.forceDraw(layerCanvas); |
| 155 | layerCanvas->restoreToCount(saveCount); |
| 156 | |
| 157 | LightingInfo::setLightCenterRaw(savedLightCenter); |
| 158 | |
| 159 | // cache the current context so that we can defer flushing it until |
| 160 | // either all the layers have been rendered or the context changes |
Adlai Holler | ab76215 | 2020-09-16 10:37:19 -0400 | [diff] [blame] | 161 | GrDirectContext* currentContext = |
| 162 | GrAsDirectContext(layerNode->getLayerSurface()->getCanvas()->recordingContext()); |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 163 | if (cachedContext.get() != currentContext) { |
| 164 | if (cachedContext.get()) { |
| 165 | ATRACE_NAME("flush layers (context changed)"); |
Greg Daniel | c7ad408 | 2020-05-14 15:38:26 -0400 | [diff] [blame] | 166 | cachedContext->flushAndSubmit(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 167 | } |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 168 | cachedContext.reset(SkSafeRef(currentContext)); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 169 | } |
| 170 | } |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 171 | |
| 172 | if (cachedContext.get()) { |
Derek Sollenberger | be3876c | 2018-04-20 16:13:31 -0400 | [diff] [blame] | 173 | ATRACE_NAME("flush layers"); |
Greg Daniel | c7ad408 | 2020-05-14 15:38:26 -0400 | [diff] [blame] | 174 | cachedContext->flushAndSubmit(); |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 175 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 176 | } |
| 177 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 178 | bool SkiaPipeline::createOrUpdateLayer(RenderNode* node, const DamageAccumulator& damageAccumulator, |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 179 | ErrorHandler* errorHandler) { |
Derek Sollenberger | 03e6cff7 | 2017-12-04 15:07:08 -0500 | [diff] [blame] | 180 | // compute the size of the surface (i.e. texture) to be allocated for this layer |
| 181 | const int surfaceWidth = ceilf(node->getWidth() / float(LAYER_SIZE)) * LAYER_SIZE; |
| 182 | const int surfaceHeight = ceilf(node->getHeight() / float(LAYER_SIZE)) * LAYER_SIZE; |
| 183 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 184 | SkSurface* layer = node->getLayerSurface(); |
Derek Sollenberger | 03e6cff7 | 2017-12-04 15:07:08 -0500 | [diff] [blame] | 185 | if (!layer || layer->width() != surfaceWidth || layer->height() != surfaceHeight) { |
Stan Iliev | 08fc19a | 2017-07-24 10:20:33 -0400 | [diff] [blame] | 186 | SkImageInfo info; |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 187 | info = SkImageInfo::Make(surfaceWidth, surfaceHeight, getSurfaceColorType(), |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 188 | kPremul_SkAlphaType, getSurfaceColorSpace()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 189 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
| 190 | SkASSERT(mRenderThread.getGrContext() != nullptr); |
Kevin Lubick | 6817fc4 | 2023-05-12 19:27:20 +0000 | [diff] [blame] | 191 | node->setLayerSurface(SkSurfaces::RenderTarget(mRenderThread.getGrContext(), |
| 192 | skgpu::Budgeted::kYes, info, 0, |
| 193 | this->getSurfaceOrigin(), &props)); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 194 | if (node->getLayerSurface()) { |
| 195 | // update the transform in window of the layer to reset its origin wrt light source |
| 196 | // position |
| 197 | Matrix4 windowTransform; |
| 198 | damageAccumulator.computeCurrentTransform(&windowTransform); |
Stan Iliev | da7c19c | 2019-05-22 14:43:44 -0400 | [diff] [blame] | 199 | node->getSkiaLayer()->inverseTransformInWindow.loadInverse(windowTransform); |
Stan Iliev | 216b157 | 2018-03-26 14:29:50 -0400 | [diff] [blame] | 200 | } else { |
| 201 | String8 cachesOutput; |
| 202 | mRenderThread.cacheManager().dumpMemoryUsage(cachesOutput, |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 203 | &mRenderThread.renderState()); |
Tomasz Wasilczyk | 3f9f863 | 2023-08-10 23:54:44 +0000 | [diff] [blame^] | 204 | ALOGE("%s", cachesOutput.c_str()); |
Stan Iliev | 216b157 | 2018-03-26 14:29:50 -0400 | [diff] [blame] | 205 | if (errorHandler) { |
| 206 | std::ostringstream err; |
| 207 | err << "Unable to create layer for " << node->getName(); |
| 208 | const int maxTextureSize = DeviceInfo::get()->maxTextureSize(); |
| 209 | err << ", size " << info.width() << "x" << info.height() << " max size " |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 210 | << maxTextureSize << " color type " << (int)info.colorType() << " has context " |
| 211 | << (int)(mRenderThread.getGrContext() != nullptr); |
Stan Iliev | 216b157 | 2018-03-26 14:29:50 -0400 | [diff] [blame] | 212 | errorHandler->onError(err.str()); |
| 213 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 214 | } |
| 215 | return true; |
| 216 | } |
| 217 | return false; |
| 218 | } |
| 219 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 220 | void SkiaPipeline::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) { |
Adlai Holler | ab76215 | 2020-09-16 10:37:19 -0400 | [diff] [blame] | 221 | GrDirectContext* context = thread.getGrContext(); |
John Reck | cf1170f | 2021-07-14 15:52:19 -0400 | [diff] [blame] | 222 | if (context && !bitmap->isHardware()) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 223 | ATRACE_FORMAT("Bitmap#prepareToDraw %dx%d", bitmap->width(), bitmap->height()); |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 224 | auto image = bitmap->makeImage(); |
John Reck | cf1170f | 2021-07-14 15:52:19 -0400 | [diff] [blame] | 225 | if (image.get()) { |
Kevin Lubick | 9367108 | 2023-03-13 18:30:55 +0000 | [diff] [blame] | 226 | skgpu::ganesh::PinAsTexture(context, image.get()); |
| 227 | skgpu::ganesh::UnpinTexture(context, image.get()); |
John Reck | cf1170f | 2021-07-14 15:52:19 -0400 | [diff] [blame] | 228 | // A submit is necessary as there may not be a frame coming soon, so without a call |
| 229 | // to submit these texture uploads can just sit in the queue building up until |
| 230 | // we run out of RAM |
| 231 | context->flushAndSubmit(); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 232 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 236 | static void savePictureAsync(const sk_sp<SkData>& data, const std::string& filename) { |
| 237 | CommonPool::post([data, filename] { |
| 238 | if (0 == access(filename.c_str(), F_OK)) { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 239 | return; |
| 240 | } |
| 241 | |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 242 | SkFILEWStream stream(filename.c_str()); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 243 | if (stream.isValid()) { |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 244 | stream.write(data->data(), data->size()); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 245 | stream.flush(); |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 246 | ALOGD("SKP Captured Drawing Output (%zu bytes) for frame. %s", stream.bytesWritten(), |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 247 | filename.c_str()); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 248 | } |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 249 | }); |
| 250 | } |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 251 | |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 252 | // Note multiple SkiaPipeline instances may be loaded if more than one app is visible. |
| 253 | // Each instance may observe the filename changing and try to record to a file of the same name. |
| 254 | // Only the first one will succeed. There is no scope available here where we could coordinate |
| 255 | // to cause this function to return true for only one of the instances. |
| 256 | bool SkiaPipeline::shouldStartNewFileCapture() { |
| 257 | // Don't start a new file based capture if one is currently ongoing. |
| 258 | if (mCaptureMode != CaptureMode::None) { return false; } |
| 259 | |
| 260 | // A new capture is started when the filename property changes. |
| 261 | // Read the filename property. |
| 262 | std::string prop = base::GetProperty(PROPERTY_CAPTURE_SKP_FILENAME, "0"); |
| 263 | // if the filename property changed to a valid value |
| 264 | if (prop[0] != '0' && mCapturedFile != prop) { |
| 265 | // remember this new filename |
| 266 | mCapturedFile = prop; |
| 267 | // and get a property indicating how many frames to capture. |
| 268 | mCaptureSequence = base::GetIntProperty(PROPERTY_CAPTURE_SKP_FRAMES, 1); |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 269 | if (mCaptureSequence <= 0) { |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 270 | return false; |
| 271 | } else if (mCaptureSequence == 1) { |
| 272 | mCaptureMode = CaptureMode::SingleFrameSKP; |
| 273 | } else { |
| 274 | mCaptureMode = CaptureMode::MultiFrameSKP; |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 275 | } |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 276 | return true; |
| 277 | } |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | // performs the first-frame work of a multi frame SKP capture. Returns true if successful. |
| 282 | bool SkiaPipeline::setupMultiFrameCapture() { |
| 283 | ALOGD("Set up multi-frame capture, frames = %d", mCaptureSequence); |
| 284 | // We own this stream and need to hold it until close() finishes. |
| 285 | auto stream = std::make_unique<SkFILEWStream>(mCapturedFile.c_str()); |
| 286 | if (stream->isValid()) { |
| 287 | mOpenMultiPicStream = std::move(stream); |
| 288 | mSerialContext.reset(new SkSharingSerialContext()); |
| 289 | SkSerialProcs procs; |
| 290 | procs.fImageProc = SkSharingSerialContext::serializeImage; |
| 291 | procs.fImageCtx = mSerialContext.get(); |
Nathaniel Nifong | 429fff4 | 2020-01-30 14:38:21 -0500 | [diff] [blame] | 292 | procs.fTypefaceProc = [](SkTypeface* tf, void* ctx){ |
| 293 | return tf->serialize(SkTypeface::SerializeBehavior::kDoIncludeData); |
| 294 | }; |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 295 | // SkDocuments don't take owership of the streams they write. |
| 296 | // we need to keep it until after mMultiPic.close() |
| 297 | // procs is passed as a pointer, but just as a method of having an optional default. |
| 298 | // procs doesn't need to outlive this Make call. |
Nathaniel Nifong | 7c21677 | 2021-01-22 13:23:25 -0500 | [diff] [blame] | 299 | mMultiPic = SkMakeMultiPictureDocument(mOpenMultiPicStream.get(), &procs, |
| 300 | [sharingCtx = mSerialContext.get()](const SkPicture* pic) { |
| 301 | SkSharingSerialContext::collectNonTextureImagesFromPicture(pic, sharingCtx); |
| 302 | }); |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 303 | return true; |
| 304 | } else { |
| 305 | ALOGE("Could not open \"%s\" for writing.", mCapturedFile.c_str()); |
| 306 | mCaptureSequence = 0; |
| 307 | mCaptureMode = CaptureMode::None; |
| 308 | return false; |
| 309 | } |
| 310 | } |
| 311 | |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 312 | // recurse through the rendernode's children, add any nodes which are layers to the queue. |
| 313 | static void collectLayers(RenderNode* node, LayerUpdateQueue* layers) { |
John Reck | be67195 | 2021-01-13 22:39:32 -0500 | [diff] [blame] | 314 | SkiaDisplayList* dl = node->getDisplayList().asSkiaDl(); |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 315 | if (dl) { |
| 316 | const auto& prop = node->properties(); |
| 317 | if (node->hasLayer()) { |
| 318 | layers->enqueueLayerWithDamage(node, Rect(prop.getWidth(), prop.getHeight())); |
| 319 | } |
| 320 | // The way to recurse through rendernodes is to call this with a lambda. |
| 321 | dl->updateChildren([&](RenderNode* child) { collectLayers(child, layers); }); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | // record the provided layers to the provided canvas as self-contained skpictures. |
| 326 | static void recordLayers(const LayerUpdateQueue& layers, |
| 327 | SkCanvas* mskpCanvas) { |
| 328 | const Vector3 savedLightCenter(LightingInfo::getLightCenterRaw()); |
| 329 | // Record the commands to re-draw each dirty layer into an SkPicture |
| 330 | for (size_t i = 0; i < layers.entries().size(); i++) { |
| 331 | RenderNode* layerNode = layers.entries()[i].renderNode.get(); |
| 332 | const Rect& layerDamage = layers.entries()[i].damage; |
| 333 | const RenderProperties& properties = layerNode->properties(); |
| 334 | |
| 335 | // Temporarily map current light center into RenderNode's coordinate space |
| 336 | Vector3 transformedLightCenter(savedLightCenter); |
| 337 | layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(transformedLightCenter); |
| 338 | LightingInfo::setLightCenterRaw(transformedLightCenter); |
| 339 | |
| 340 | SkPictureRecorder layerRec; |
| 341 | auto* recCanvas = layerRec.beginRecording(properties.getWidth(), |
| 342 | properties.getHeight()); |
| 343 | // This is not recorded but still causes clipping. |
| 344 | recCanvas->androidFramework_setDeviceClipRestriction(layerDamage.toSkIRect()); |
| 345 | RenderNodeDrawable root(layerNode, recCanvas, false); |
| 346 | root.forceDraw(recCanvas); |
| 347 | // Now write this picture into the SKP canvas with an annotation indicating what it is |
| 348 | mskpCanvas->drawAnnotation(layerDamage.toSkRect(), String8::format( |
| 349 | "OffscreenLayerDraw|%" PRId64, layerNode->uniqueId()).c_str(), nullptr); |
| 350 | mskpCanvas->drawPicture(layerRec.finishRecordingAsPicture()); |
| 351 | } |
| 352 | LightingInfo::setLightCenterRaw(savedLightCenter); |
| 353 | } |
| 354 | |
| 355 | SkCanvas* SkiaPipeline::tryCapture(SkSurface* surface, RenderNode* root, |
| 356 | const LayerUpdateQueue& dirtyLayers) { |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 357 | if (CC_LIKELY(!Properties::skpCaptureEnabled)) { |
| 358 | return surface->getCanvas(); // Bail out early when capture is not turned on. |
| 359 | } |
| 360 | // Note that shouldStartNewFileCapture tells us if this is the *first* frame of a capture. |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 361 | bool firstFrameOfAnim = false; |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 362 | if (shouldStartNewFileCapture() && mCaptureMode == CaptureMode::MultiFrameSKP) { |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 363 | // set a reminder to record every layer near the end of this method, after we have set up |
| 364 | // the nway canvas. |
| 365 | firstFrameOfAnim = true; |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 366 | if (!setupMultiFrameCapture()) { |
| 367 | return surface->getCanvas(); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 368 | } |
| 369 | } |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 370 | |
| 371 | // Create a canvas pointer, fill it depending on what kind of capture is requested (if any) |
| 372 | SkCanvas* pictureCanvas = nullptr; |
| 373 | switch (mCaptureMode) { |
| 374 | case CaptureMode::CallbackAPI: |
| 375 | case CaptureMode::SingleFrameSKP: |
| 376 | mRecorder.reset(new SkPictureRecorder()); |
Nathaniel Nifong | 31fee3a | 2019-07-11 16:27:14 -0400 | [diff] [blame] | 377 | pictureCanvas = mRecorder->beginRecording(surface->width(), surface->height()); |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 378 | break; |
| 379 | case CaptureMode::MultiFrameSKP: |
| 380 | // If a multi frame recording is active, initialize recording for a single frame of a |
| 381 | // multi frame file. |
| 382 | pictureCanvas = mMultiPic->beginPage(surface->width(), surface->height()); |
| 383 | break; |
| 384 | case CaptureMode::None: |
| 385 | // Returning here in the non-capture case means we can count on pictureCanvas being |
| 386 | // non-null below. |
| 387 | return surface->getCanvas(); |
| 388 | } |
| 389 | |
| 390 | // Setting up an nway canvas is common to any kind of capture. |
| 391 | mNwayCanvas = std::make_unique<SkNWayCanvas>(surface->width(), surface->height()); |
| 392 | mNwayCanvas->addCanvas(surface->getCanvas()); |
| 393 | mNwayCanvas->addCanvas(pictureCanvas); |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 394 | |
| 395 | if (firstFrameOfAnim) { |
| 396 | // On the first frame of any mskp capture we want to record any layers that are needed in |
| 397 | // frame but may have been rendered offscreen before recording began. |
| 398 | // We do not maintain a list of all layers, since it isn't needed outside this rare, |
| 399 | // recording use case. Traverse the tree to find them and put them in this LayerUpdateQueue. |
| 400 | LayerUpdateQueue luq; |
| 401 | collectLayers(root, &luq); |
| 402 | recordLayers(luq, mNwayCanvas.get()); |
| 403 | } else { |
| 404 | // on non-first frames, we record any normal layer draws (dirty regions) |
| 405 | recordLayers(dirtyLayers, mNwayCanvas.get()); |
| 406 | } |
| 407 | |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 408 | return mNwayCanvas.get(); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | void SkiaPipeline::endCapture(SkSurface* surface) { |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 412 | if (CC_LIKELY(mCaptureMode == CaptureMode::None)) { return; } |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 413 | mNwayCanvas.reset(); |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 414 | ATRACE_CALL(); |
| 415 | if (mCaptureSequence > 0 && mCaptureMode == CaptureMode::MultiFrameSKP) { |
| 416 | mMultiPic->endPage(); |
| 417 | mCaptureSequence--; |
| 418 | if (mCaptureSequence == 0) { |
| 419 | mCaptureMode = CaptureMode::None; |
| 420 | // Pass mMultiPic and mOpenMultiPicStream to a background thread, which will handle |
| 421 | // the heavyweight serialization work and destroy them. mOpenMultiPicStream is released |
| 422 | // to a bare pointer because keeping it in a smart pointer makes the lambda |
| 423 | // non-copyable. The lambda is only called once, so this is safe. |
| 424 | SkFILEWStream* stream = mOpenMultiPicStream.release(); |
| 425 | CommonPool::post([doc = std::move(mMultiPic), stream]{ |
| 426 | ALOGD("Finalizing multi frame SKP"); |
| 427 | doc->close(); |
| 428 | delete stream; |
| 429 | ALOGD("Multi frame SKP complete."); |
| 430 | }); |
| 431 | } |
| 432 | } else { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 433 | sk_sp<SkPicture> picture = mRecorder->finishRecordingAsPicture(); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 434 | if (picture->approximateOpCount() > 0) { |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 435 | if (mPictureCapturedCallback) { |
| 436 | std::invoke(mPictureCapturedCallback, std::move(picture)); |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 437 | } else { |
| 438 | // single frame skp to file |
Nathaniel Nifong | 429fff4 | 2020-01-30 14:38:21 -0500 | [diff] [blame] | 439 | SkSerialProcs procs; |
| 440 | procs.fTypefaceProc = [](SkTypeface* tf, void* ctx){ |
| 441 | return tf->serialize(SkTypeface::SerializeBehavior::kDoIncludeData); |
| 442 | }; |
John Reck | 7600518 | 2021-06-09 22:43:05 -0400 | [diff] [blame] | 443 | auto data = picture->serialize(&procs); |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 444 | savePictureAsync(data, mCapturedFile); |
| 445 | mCaptureSequence = 0; |
Derek Sollenberger | 5f9753d | 2020-04-01 15:59:02 -0400 | [diff] [blame] | 446 | mCaptureMode = CaptureMode::None; |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 447 | } |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 448 | } |
| 449 | mRecorder.reset(); |
| 450 | } |
| 451 | } |
| 452 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 453 | void SkiaPipeline::renderFrame(const LayerUpdateQueue& layers, const SkRect& clip, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 454 | const std::vector<sp<RenderNode>>& nodes, bool opaque, |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 455 | const Rect& contentDrawBounds, sk_sp<SkSurface> surface, |
| 456 | const SkMatrix& preTransform) { |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 457 | bool previousSkpEnabled = Properties::skpCaptureEnabled; |
| 458 | if (mPictureCapturedCallback) { |
| 459 | Properties::skpCaptureEnabled = true; |
| 460 | } |
| 461 | |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 462 | // Initialize the canvas for the current frame, that might be a recording canvas if SKP |
| 463 | // capture is enabled. |
| 464 | SkCanvas* canvas = tryCapture(surface.get(), nodes[0].get(), layers); |
| 465 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 466 | // draw all layers up front |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 467 | renderLayersImpl(layers, opaque); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 468 | |
Nathaniel Nifong | dc19a65 | 2019-11-11 11:47:50 -0500 | [diff] [blame] | 469 | renderFrameImpl(clip, nodes, opaque, contentDrawBounds, canvas, preTransform); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 470 | |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 471 | endCapture(surface.get()); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 472 | |
| 473 | if (CC_UNLIKELY(Properties::debugOverdraw)) { |
Nathaniel Nifong | dc19a65 | 2019-11-11 11:47:50 -0500 | [diff] [blame] | 474 | renderOverdraw(clip, nodes, contentDrawBounds, surface, preTransform); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 475 | } |
| 476 | |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 477 | Properties::skpCaptureEnabled = previousSkpEnabled; |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 478 | } |
| 479 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 480 | namespace { |
| 481 | static Rect nodeBounds(RenderNode& node) { |
| 482 | auto& props = node.properties(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 483 | return Rect(props.getLeft(), props.getTop(), props.getRight(), props.getBottom()); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 484 | } |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 485 | } // namespace |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 486 | |
Nathaniel Nifong | dc19a65 | 2019-11-11 11:47:50 -0500 | [diff] [blame] | 487 | void SkiaPipeline::renderFrameImpl(const SkRect& clip, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 488 | const std::vector<sp<RenderNode>>& nodes, bool opaque, |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 489 | const Rect& contentDrawBounds, SkCanvas* canvas, |
| 490 | const SkMatrix& preTransform) { |
Stan Iliev | b66b8bb | 2016-12-15 18:17:42 -0500 | [diff] [blame] | 491 | SkAutoCanvasRestore saver(canvas, true); |
Nathaniel Nifong | cff969f | 2020-01-09 14:03:49 -0500 | [diff] [blame] | 492 | auto clipRestriction = preTransform.mapRect(clip).roundOut(); |
John Reck | 7600518 | 2021-06-09 22:43:05 -0400 | [diff] [blame] | 493 | if (CC_UNLIKELY(isCapturingSkp())) { |
Nathaniel Nifong | 89a5dc5 | 2020-01-17 15:32:17 -0500 | [diff] [blame] | 494 | canvas->drawAnnotation(SkRect::Make(clipRestriction), "AndroidDeviceClipRestriction", |
| 495 | nullptr); |
| 496 | } else { |
| 497 | // clip drawing to dirty region only when not recording SKP files (which should contain all |
| 498 | // draw ops on every frame) |
| 499 | canvas->androidFramework_setDeviceClipRestriction(clipRestriction); |
| 500 | } |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 501 | canvas->concat(preTransform); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 502 | |
Nader Jawad | f5ce452 | 2023-03-27 13:02:41 -0700 | [diff] [blame] | 503 | if (!opaque) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 504 | canvas->clear(SK_ColorTRANSPARENT); |
| 505 | } |
| 506 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 507 | if (1 == nodes.size()) { |
| 508 | if (!nodes[0]->nothingToDraw()) { |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 509 | RenderNodeDrawable root(nodes[0].get(), canvas); |
| 510 | root.draw(canvas); |
| 511 | } |
| 512 | } else if (0 == nodes.size()) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 513 | // nothing to draw |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 514 | } else { |
| 515 | // It there are multiple render nodes, they are laid out as follows: |
| 516 | // #0 - backdrop (content + caption) |
| 517 | // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop) |
| 518 | // #2 - additional overlay nodes |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 519 | // Usually the backdrop cannot be seen since it will be entirely covered by the content. |
| 520 | // While |
| 521 | // resizing however it might become partially visible. The following render loop will crop |
| 522 | // the |
| 523 | // backdrop against the content and draw the remaining part of it. It will then draw the |
| 524 | // content |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 525 | // cropped to the backdrop (since that indicates a shrinking of the window). |
| 526 | // |
| 527 | // Additional nodes will be drawn on top with no particular clipping semantics. |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 528 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 529 | // Usually the contents bounds should be mContentDrawBounds - however - we will |
| 530 | // move it towards the fixed edge to give it a more stable appearance (for the moment). |
| 531 | // If there is no content bounds we ignore the layering as stated above and start with 2. |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 532 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 533 | // Backdrop bounds in render target space |
| 534 | const Rect backdrop = nodeBounds(*nodes[0]); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 535 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 536 | // Bounds that content will fill in render target space (note content node bounds may be |
| 537 | // bigger) |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 538 | Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight()); |
| 539 | content.translate(backdrop.left, backdrop.top); |
| 540 | if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) { |
| 541 | // Content doesn't entirely overlap backdrop, so fill around content (right/bottom) |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 542 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 543 | // Note: in the future, if content doesn't snap to backdrop's left/top, this may need to |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 544 | // also fill left/top. Currently, both 2up and freeform position content at the top/left |
| 545 | // of |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 546 | // the backdrop, so this isn't necessary. |
| 547 | RenderNodeDrawable backdropNode(nodes[0].get(), canvas); |
| 548 | if (content.right < backdrop.right) { |
| 549 | // draw backdrop to right side of content |
| 550 | SkAutoCanvasRestore acr(canvas, true); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 551 | canvas->clipRect(SkRect::MakeLTRB(content.right, backdrop.top, backdrop.right, |
| 552 | backdrop.bottom)); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 553 | backdropNode.draw(canvas); |
| 554 | } |
| 555 | if (content.bottom < backdrop.bottom) { |
| 556 | // draw backdrop to bottom of content |
| 557 | // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill |
| 558 | SkAutoCanvasRestore acr(canvas, true); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 559 | canvas->clipRect(SkRect::MakeLTRB(content.left, content.bottom, content.right, |
| 560 | backdrop.bottom)); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 561 | backdropNode.draw(canvas); |
| 562 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 563 | } |
| 564 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 565 | RenderNodeDrawable contentNode(nodes[1].get(), canvas); |
| 566 | if (!backdrop.isEmpty()) { |
| 567 | // content node translation to catch up with backdrop |
| 568 | float dx = backdrop.left - contentDrawBounds.left; |
| 569 | float dy = backdrop.top - contentDrawBounds.top; |
| 570 | |
| 571 | SkAutoCanvasRestore acr(canvas, true); |
| 572 | canvas->translate(dx, dy); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 573 | const SkRect contentLocalClip = |
| 574 | SkRect::MakeXYWH(contentDrawBounds.left, contentDrawBounds.top, |
| 575 | backdrop.getWidth(), backdrop.getHeight()); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 576 | canvas->clipRect(contentLocalClip); |
| 577 | contentNode.draw(canvas); |
| 578 | } else { |
| 579 | SkAutoCanvasRestore acr(canvas, true); |
| 580 | contentNode.draw(canvas); |
| 581 | } |
| 582 | |
| 583 | // remaining overlay nodes, simply defer |
| 584 | for (size_t index = 2; index < nodes.size(); index++) { |
| 585 | if (!nodes[index]->nothingToDraw()) { |
| 586 | SkAutoCanvasRestore acr(canvas, true); |
| 587 | RenderNodeDrawable overlayNode(nodes[index].get(), canvas); |
| 588 | overlayNode.draw(canvas); |
| 589 | } |
| 590 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 591 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 592 | } |
| 593 | |
Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 594 | void SkiaPipeline::dumpResourceCacheUsage() const { |
Robert Phillips | 57bb0bf | 2019-09-06 13:18:17 -0400 | [diff] [blame] | 595 | int resources; |
| 596 | size_t bytes; |
Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 597 | mRenderThread.getGrContext()->getResourceCacheUsage(&resources, &bytes); |
Robert Phillips | 57bb0bf | 2019-09-06 13:18:17 -0400 | [diff] [blame] | 598 | size_t maxBytes = mRenderThread.getGrContext()->getResourceCacheLimit(); |
Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 599 | |
| 600 | SkString log("Resource Cache Usage:\n"); |
Robert Phillips | 57bb0bf | 2019-09-06 13:18:17 -0400 | [diff] [blame] | 601 | log.appendf("%8d items\n", resources); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 602 | log.appendf("%8zu bytes (%.2f MB) out of %.2f MB maximum\n", bytes, |
| 603 | bytes * (1.0f / (1024.0f * 1024.0f)), maxBytes * (1.0f / (1024.0f * 1024.0f))); |
Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 604 | |
| 605 | ALOGD("%s", log.c_str()); |
| 606 | } |
| 607 | |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame] | 608 | void SkiaPipeline::setHardwareBuffer(AHardwareBuffer* buffer) { |
| 609 | if (mHardwareBuffer) { |
| 610 | AHardwareBuffer_release(mHardwareBuffer); |
| 611 | mHardwareBuffer = nullptr; |
| 612 | } |
| 613 | |
| 614 | if (buffer) { |
| 615 | AHardwareBuffer_acquire(buffer); |
| 616 | mHardwareBuffer = buffer; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | sk_sp<SkSurface> SkiaPipeline::getBufferSkSurface( |
| 621 | const renderthread::HardwareBufferRenderParams& bufferParams) { |
| 622 | auto bufferColorSpace = bufferParams.getColorSpace(); |
| 623 | if (mBufferSurface == nullptr || mBufferColorSpace == nullptr || |
| 624 | !SkColorSpace::Equals(mBufferColorSpace.get(), bufferColorSpace.get())) { |
Kevin Lubick | 6817fc4 | 2023-05-12 19:27:20 +0000 | [diff] [blame] | 625 | mBufferSurface = SkSurfaces::WrapAndroidHardwareBuffer( |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame] | 626 | mRenderThread.getGrContext(), mHardwareBuffer, kTopLeft_GrSurfaceOrigin, |
| 627 | bufferColorSpace, nullptr, true); |
| 628 | mBufferColorSpace = bufferColorSpace; |
| 629 | } |
| 630 | return mBufferSurface; |
| 631 | } |
| 632 | |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 633 | void SkiaPipeline::setSurfaceColorProperties(ColorMode colorMode) { |
Derek Sollenberger | 1863d94 | 2020-02-05 15:41:51 -0500 | [diff] [blame] | 634 | mColorMode = colorMode; |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 635 | switch (colorMode) { |
| 636 | case ColorMode::Default: |
| 637 | mSurfaceColorType = SkColorType::kN32_SkColorType; |
| 638 | mSurfaceColorSpace = SkColorSpace::MakeSRGB(); |
| 639 | break; |
| 640 | case ColorMode::WideColorGamut: |
| 641 | mSurfaceColorType = DeviceInfo::get()->getWideColorType(); |
| 642 | mSurfaceColorSpace = DeviceInfo::get()->getWideColorSpace(); |
| 643 | break; |
| 644 | case ColorMode::Hdr: |
John Reck | 5588776 | 2023-01-25 16:51:18 -0500 | [diff] [blame] | 645 | mSurfaceColorType = SkColorType::kN32_SkColorType; |
| 646 | mSurfaceColorSpace = SkColorSpace::MakeRGB( |
| 647 | GetExtendedTransferFunction(mTargetSdrHdrRatio), SkNamedGamut::kDisplayP3); |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 648 | break; |
John Reck | 0b3f331 | 2023-01-31 16:21:28 -0500 | [diff] [blame] | 649 | case ColorMode::Hdr10: |
| 650 | mSurfaceColorType = SkColorType::kRGBA_1010102_SkColorType; |
| 651 | mSurfaceColorSpace = SkColorSpace::MakeRGB( |
| 652 | GetExtendedTransferFunction(mTargetSdrHdrRatio), SkNamedGamut::kDisplayP3); |
| 653 | break; |
Leon Scroggins III | cbdbb66 | 2021-11-30 13:59:00 -0500 | [diff] [blame] | 654 | case ColorMode::A8: |
| 655 | mSurfaceColorType = SkColorType::kAlpha_8_SkColorType; |
| 656 | mSurfaceColorSpace = nullptr; |
| 657 | break; |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
John Reck | 5588776 | 2023-01-25 16:51:18 -0500 | [diff] [blame] | 661 | void SkiaPipeline::setTargetSdrHdrRatio(float ratio) { |
John Reck | 0b3f331 | 2023-01-31 16:21:28 -0500 | [diff] [blame] | 662 | if (mColorMode == ColorMode::Hdr || mColorMode == ColorMode::Hdr10) { |
John Reck | 5588776 | 2023-01-25 16:51:18 -0500 | [diff] [blame] | 663 | mTargetSdrHdrRatio = ratio; |
| 664 | mSurfaceColorSpace = SkColorSpace::MakeRGB(GetExtendedTransferFunction(mTargetSdrHdrRatio), |
| 665 | SkNamedGamut::kDisplayP3); |
| 666 | } else { |
| 667 | mTargetSdrHdrRatio = 1.f; |
| 668 | } |
| 669 | } |
| 670 | |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 671 | // Overdraw debugging |
| 672 | |
| 673 | // These colors should be kept in sync with Caches::getOverdrawColor() with a few differences. |
Mike Reed | 331c4e1 | 2020-02-13 10:21:54 -0500 | [diff] [blame] | 674 | // This implementation requires transparent entries for "no overdraw" and "single draws". |
| 675 | static const SkColor kOverdrawColors[2][6] = { |
| 676 | { |
| 677 | 0x00000000, |
| 678 | 0x00000000, |
| 679 | 0x2f0000ff, |
| 680 | 0x2f00ff00, |
| 681 | 0x3fff0000, |
| 682 | 0x7fff0000, |
| 683 | }, |
| 684 | { |
| 685 | 0x00000000, |
| 686 | 0x00000000, |
| 687 | 0x2f0000ff, |
| 688 | 0x4fffff00, |
| 689 | 0x5fff89d7, |
| 690 | 0x7fff0000, |
| 691 | }, |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 692 | }; |
| 693 | |
Nathaniel Nifong | dc19a65 | 2019-11-11 11:47:50 -0500 | [diff] [blame] | 694 | void SkiaPipeline::renderOverdraw(const SkRect& clip, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 695 | const std::vector<sp<RenderNode>>& nodes, |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 696 | const Rect& contentDrawBounds, sk_sp<SkSurface> surface, |
| 697 | const SkMatrix& preTransform) { |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 698 | // Set up the overdraw canvas. |
| 699 | SkImageInfo offscreenInfo = SkImageInfo::MakeA8(surface->width(), surface->height()); |
| 700 | sk_sp<SkSurface> offscreen = surface->makeSurface(offscreenInfo); |
Nathaniel Nifong | fe05b7c | 2019-09-17 12:52:52 -0400 | [diff] [blame] | 701 | LOG_ALWAYS_FATAL_IF(!offscreen, "Failed to create offscreen SkSurface for overdraw viz."); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 702 | SkOverdrawCanvas overdrawCanvas(offscreen->getCanvas()); |
| 703 | |
| 704 | // Fake a redraw to replay the draw commands. This will increment the alpha channel |
| 705 | // each time a pixel would have been drawn. |
| 706 | // Pass true for opaque so we skip the clear - the overdrawCanvas is already zero |
| 707 | // initialized. |
Nathaniel Nifong | dc19a65 | 2019-11-11 11:47:50 -0500 | [diff] [blame] | 708 | renderFrameImpl(clip, nodes, true, contentDrawBounds, &overdrawCanvas, preTransform); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 709 | sk_sp<SkImage> counts = offscreen->makeImageSnapshot(); |
| 710 | |
| 711 | // Draw overdraw colors to the canvas. The color filter will convert counts to colors. |
| 712 | SkPaint paint; |
Mike Reed | 331c4e1 | 2020-02-13 10:21:54 -0500 | [diff] [blame] | 713 | const SkColor* colors = kOverdrawColors[static_cast<int>(Properties::overdrawColorSet)]; |
| 714 | paint.setColorFilter(SkOverdrawColorFilter::MakeWithSkColors(colors)); |
Mike Reed | 7994a31 | 2021-01-28 18:06:26 -0500 | [diff] [blame] | 715 | surface->getCanvas()->drawImage(counts.get(), 0.0f, 0.0f, SkSamplingOptions(), &paint); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 716 | } |
| 717 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 718 | } /* namespace skiapipeline */ |
| 719 | } /* namespace uirenderer */ |
| 720 | } /* namespace android */ |