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