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 | |
Hal Canary | 10219fb | 2016-11-23 20:41:22 -0500 | [diff] [blame] | 19 | #include <SkImageEncoder.h> |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 20 | #include <SkImageInfo.h> |
Leon Scroggins III | ee708fa | 2016-12-12 15:31:39 -0500 | [diff] [blame] | 21 | #include <SkImagePriv.h> |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 22 | #include <SkMultiPictureDocument.h> |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 23 | #include <SkOverdrawCanvas.h> |
| 24 | #include <SkOverdrawColorFilter.h> |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 25 | #include <SkPicture.h> |
| 26 | #include <SkPictureRecorder.h> |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 27 | #include <SkSerialProcs.h> |
Fedor Kudasov | 90df056 | 2019-06-19 11:41:34 +0100 | [diff] [blame] | 28 | #include "LightingInfo.h" |
Stan Iliev | 216b157 | 2018-03-26 14:29:50 -0400 | [diff] [blame] | 29 | #include "TreeInfo.h" |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 30 | #include "VectorDrawable.h" |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 31 | #include "thread/CommonPool.h" |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 32 | #include "tools/SkSharingProc.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 33 | #include "utils/TraceUtils.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 34 | |
Ben Wagner | 3aeda5c | 2017-03-17 17:22:01 -0400 | [diff] [blame] | 35 | #include <unistd.h> |
| 36 | |
Jerome Gaillard | a02a12d | 2019-05-28 18:07:56 +0100 | [diff] [blame] | 37 | #include <android-base/properties.h> |
| 38 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 39 | using namespace android::uirenderer::renderthread; |
| 40 | |
| 41 | namespace android { |
| 42 | namespace uirenderer { |
| 43 | namespace skiapipeline { |
| 44 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 45 | SkiaPipeline::SkiaPipeline(RenderThread& thread) : mRenderThread(thread) { |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 46 | mVectorDrawables.reserve(30); |
| 47 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 48 | |
Stan Iliev | 232f362 | 2017-08-23 17:15:09 -0400 | [diff] [blame] | 49 | SkiaPipeline::~SkiaPipeline() { |
| 50 | unpinImages(); |
| 51 | } |
| 52 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 53 | void SkiaPipeline::onDestroyHardwareResources() { |
Derek Sollenberger | 92a9eb9 | 2018-04-12 13:42:19 -0400 | [diff] [blame] | 54 | unpinImages(); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 55 | mRenderThread.cacheManager().trimStaleResources(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 56 | } |
| 57 | |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 58 | bool SkiaPipeline::pinImages(std::vector<SkImage*>& mutableImages) { |
| 59 | for (SkImage* image : mutableImages) { |
Derek Sollenberger | 189e874 | 2016-11-16 16:00:17 -0500 | [diff] [blame] | 60 | if (SkImage_pinAsTexture(image, mRenderThread.getGrContext())) { |
| 61 | mPinnedImages.emplace_back(sk_ref_sp(image)); |
| 62 | } else { |
| 63 | return false; |
| 64 | } |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 65 | } |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | void SkiaPipeline::unpinImages() { |
| 70 | for (auto& image : mPinnedImages) { |
| 71 | SkImage_unpinAsTexture(image.get(), mRenderThread.getGrContext()); |
| 72 | } |
| 73 | mPinnedImages.clear(); |
| 74 | } |
| 75 | |
Stan Iliev | 47fed6ba | 2017-10-18 17:56:43 -0400 | [diff] [blame] | 76 | void SkiaPipeline::onPrepareTree() { |
| 77 | // The only time mVectorDrawables is not empty is if prepare tree was called 2 times without |
| 78 | // a renderFrame in the middle. |
| 79 | mVectorDrawables.clear(); |
| 80 | } |
| 81 | |
John Reck | d9d7f12 | 2018-05-03 14:40:56 -0700 | [diff] [blame] | 82 | void SkiaPipeline::renderLayers(const LightGeometry& lightGeometry, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 83 | LayerUpdateQueue* layerUpdateQueue, bool opaque, |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 84 | const LightInfo& lightInfo) { |
Fedor Kudasov | 90df056 | 2019-06-19 11:41:34 +0100 | [diff] [blame] | 85 | LightingInfo::updateLighting(lightGeometry, lightInfo); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 86 | ATRACE_NAME("draw layers"); |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 87 | renderVectorDrawableCache(); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 88 | renderLayersImpl(*layerUpdateQueue, opaque); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 89 | layerUpdateQueue->clear(); |
| 90 | } |
| 91 | |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 92 | void SkiaPipeline::renderLayersImpl(const LayerUpdateQueue& layers, bool opaque) { |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 93 | sk_sp<GrContext> cachedContext; |
| 94 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 95 | // Render all layers that need to be updated, in order. |
| 96 | for (size_t i = 0; i < layers.entries().size(); i++) { |
John Reck | fc29f7acd | 2017-03-02 13:23:16 -0800 | [diff] [blame] | 97 | RenderNode* layerNode = layers.entries()[i].renderNode.get(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 98 | // only schedule repaint if node still on layer - possible it may have been |
| 99 | // removed during a dropped frame, but layers may still remain scheduled so |
| 100 | // as not to lose info on what portion is damaged |
| 101 | if (CC_LIKELY(layerNode->getLayerSurface() != nullptr)) { |
| 102 | SkASSERT(layerNode->getLayerSurface()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 103 | SkiaDisplayList* displayList = (SkiaDisplayList*)layerNode->getDisplayList(); |
| 104 | if (!displayList || displayList->isEmpty()) { |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 105 | ALOGE("%p drawLayers(%s) : missing drawable", layerNode, layerNode->getName()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 106 | return; |
| 107 | } |
| 108 | |
| 109 | const Rect& layerDamage = layers.entries()[i].damage; |
| 110 | |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 111 | SkCanvas* layerCanvas = layerNode->getLayerSurface()->getCanvas(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 112 | |
| 113 | int saveCount = layerCanvas->save(); |
| 114 | SkASSERT(saveCount == 1); |
| 115 | |
Stan Iliev | b66b8bb | 2016-12-15 18:17:42 -0500 | [diff] [blame] | 116 | layerCanvas->androidFramework_setDeviceClipRestriction(layerDamage.toSkIRect()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 117 | |
Fedor Kudasov | 90df056 | 2019-06-19 11:41:34 +0100 | [diff] [blame] | 118 | // TODO: put localized light center calculation and storage to a drawable related code. |
| 119 | // It does not seem right to store something localized in a global state |
| 120 | const Vector3 savedLightCenter(LightingInfo::getLightCenterRaw()); |
| 121 | Vector3 transformedLightCenter(savedLightCenter); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 122 | // map current light center into RenderNode's coordinate space |
Fedor Kudasov | 90df056 | 2019-06-19 11:41:34 +0100 | [diff] [blame] | 123 | layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(transformedLightCenter); |
| 124 | LightingInfo::setLightCenterRaw(transformedLightCenter); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 125 | |
| 126 | const RenderProperties& properties = layerNode->properties(); |
| 127 | const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight()); |
| 128 | if (properties.getClipToBounds() && layerCanvas->quickReject(bounds)) { |
| 129 | return; |
| 130 | } |
| 131 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 132 | ATRACE_FORMAT("drawLayer [%s] %.1f x %.1f", layerNode->getName(), bounds.width(), |
| 133 | bounds.height()); |
Derek Sollenberger | 9552c2c | 2017-08-31 15:37:52 -0400 | [diff] [blame] | 134 | |
Matt Sarett | 79756be | 2016-11-09 16:13:54 -0500 | [diff] [blame] | 135 | layerNode->getSkiaLayer()->hasRenderedSinceRepaint = false; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 136 | layerCanvas->clear(SK_ColorTRANSPARENT); |
| 137 | |
| 138 | RenderNodeDrawable root(layerNode, layerCanvas, false); |
| 139 | root.forceDraw(layerCanvas); |
| 140 | layerCanvas->restoreToCount(saveCount); |
Fedor Kudasov | 90df056 | 2019-06-19 11:41:34 +0100 | [diff] [blame] | 141 | LightingInfo::setLightCenterRaw(savedLightCenter); |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 142 | |
| 143 | // cache the current context so that we can defer flushing it until |
| 144 | // either all the layers have been rendered or the context changes |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 145 | GrContext* currentContext = layerNode->getLayerSurface()->getCanvas()->getGrContext(); |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 146 | if (cachedContext.get() != currentContext) { |
| 147 | if (cachedContext.get()) { |
Derek Sollenberger | be3876c | 2018-04-20 16:13:31 -0400 | [diff] [blame] | 148 | ATRACE_NAME("flush layers (context changed)"); |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 149 | cachedContext->flush(); |
| 150 | } |
| 151 | cachedContext.reset(SkSafeRef(currentContext)); |
| 152 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 153 | } |
| 154 | } |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 155 | |
| 156 | if (cachedContext.get()) { |
Derek Sollenberger | be3876c | 2018-04-20 16:13:31 -0400 | [diff] [blame] | 157 | ATRACE_NAME("flush layers"); |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 158 | cachedContext->flush(); |
| 159 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 160 | } |
| 161 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 162 | bool SkiaPipeline::createOrUpdateLayer(RenderNode* node, const DamageAccumulator& damageAccumulator, |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 163 | ErrorHandler* errorHandler) { |
Derek Sollenberger | 03e6cff7 | 2017-12-04 15:07:08 -0500 | [diff] [blame] | 164 | // compute the size of the surface (i.e. texture) to be allocated for this layer |
| 165 | const int surfaceWidth = ceilf(node->getWidth() / float(LAYER_SIZE)) * LAYER_SIZE; |
| 166 | const int surfaceHeight = ceilf(node->getHeight() / float(LAYER_SIZE)) * LAYER_SIZE; |
| 167 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 168 | SkSurface* layer = node->getLayerSurface(); |
Derek Sollenberger | 03e6cff7 | 2017-12-04 15:07:08 -0500 | [diff] [blame] | 169 | if (!layer || layer->width() != surfaceWidth || layer->height() != surfaceHeight) { |
Stan Iliev | 08fc19a | 2017-07-24 10:20:33 -0400 | [diff] [blame] | 170 | SkImageInfo info; |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 171 | info = SkImageInfo::Make(surfaceWidth, surfaceHeight, getSurfaceColorType(), |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 172 | kPremul_SkAlphaType, getSurfaceColorSpace()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 173 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
| 174 | SkASSERT(mRenderThread.getGrContext() != nullptr); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 175 | node->setLayerSurface(SkSurface::MakeRenderTarget(mRenderThread.getGrContext(), |
Derek Sollenberger | 25833d2 | 2019-01-14 13:55:55 -0500 | [diff] [blame] | 176 | SkBudgeted::kYes, info, 0, |
| 177 | this->getSurfaceOrigin(), &props)); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 178 | if (node->getLayerSurface()) { |
| 179 | // update the transform in window of the layer to reset its origin wrt light source |
| 180 | // position |
| 181 | Matrix4 windowTransform; |
| 182 | damageAccumulator.computeCurrentTransform(&windowTransform); |
Stan Iliev | da7c19c | 2019-05-22 14:43:44 -0400 | [diff] [blame] | 183 | node->getSkiaLayer()->inverseTransformInWindow.loadInverse(windowTransform); |
Stan Iliev | 216b157 | 2018-03-26 14:29:50 -0400 | [diff] [blame] | 184 | } else { |
| 185 | String8 cachesOutput; |
| 186 | mRenderThread.cacheManager().dumpMemoryUsage(cachesOutput, |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 187 | &mRenderThread.renderState()); |
Stan Iliev | 216b157 | 2018-03-26 14:29:50 -0400 | [diff] [blame] | 188 | ALOGE("%s", cachesOutput.string()); |
| 189 | if (errorHandler) { |
| 190 | std::ostringstream err; |
| 191 | err << "Unable to create layer for " << node->getName(); |
| 192 | const int maxTextureSize = DeviceInfo::get()->maxTextureSize(); |
| 193 | err << ", size " << info.width() << "x" << info.height() << " max size " |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 194 | << maxTextureSize << " color type " << (int)info.colorType() << " has context " |
| 195 | << (int)(mRenderThread.getGrContext() != nullptr); |
Stan Iliev | 216b157 | 2018-03-26 14:29:50 -0400 | [diff] [blame] | 196 | errorHandler->onError(err.str()); |
| 197 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 198 | } |
| 199 | return true; |
| 200 | } |
| 201 | return false; |
| 202 | } |
| 203 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 204 | void SkiaPipeline::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) { |
| 205 | GrContext* context = thread.getGrContext(); |
| 206 | if (context) { |
| 207 | ATRACE_FORMAT("Bitmap#prepareToDraw %dx%d", bitmap->width(), bitmap->height()); |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 208 | auto image = bitmap->makeImage(); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 209 | if (image.get() && !bitmap->isHardware()) { |
| 210 | SkImage_pinAsTexture(image.get(), context); |
| 211 | SkImage_unpinAsTexture(image.get(), context); |
| 212 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 216 | void SkiaPipeline::renderVectorDrawableCache() { |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 217 | if (!mVectorDrawables.empty()) { |
| 218 | sp<VectorDrawableAtlas> atlas = mRenderThread.cacheManager().acquireVectorDrawableAtlas(); |
| 219 | auto grContext = mRenderThread.getGrContext(); |
| 220 | atlas->prepareForDraw(grContext); |
Derek Sollenberger | 9552c2c | 2017-08-31 15:37:52 -0400 | [diff] [blame] | 221 | ATRACE_NAME("Update VectorDrawables"); |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 222 | for (auto vd : mVectorDrawables) { |
| 223 | vd->updateCache(atlas, grContext); |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 224 | } |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 225 | mVectorDrawables.clear(); |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 226 | } |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 227 | } |
| 228 | |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 229 | static void savePictureAsync(const sk_sp<SkData>& data, const std::string& filename) { |
| 230 | CommonPool::post([data, filename] { |
| 231 | if (0 == access(filename.c_str(), F_OK)) { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 232 | return; |
| 233 | } |
| 234 | |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 235 | SkFILEWStream stream(filename.c_str()); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 236 | if (stream.isValid()) { |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 237 | stream.write(data->data(), data->size()); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 238 | stream.flush(); |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 239 | ALOGD("SKP Captured Drawing Output (%zu bytes) for frame. %s", stream.bytesWritten(), |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 240 | filename.c_str()); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 241 | } |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 242 | }); |
| 243 | } |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 244 | |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 245 | // Note multiple SkiaPipeline instances may be loaded if more than one app is visible. |
| 246 | // Each instance may observe the filename changing and try to record to a file of the same name. |
| 247 | // Only the first one will succeed. There is no scope available here where we could coordinate |
| 248 | // to cause this function to return true for only one of the instances. |
| 249 | bool SkiaPipeline::shouldStartNewFileCapture() { |
| 250 | // Don't start a new file based capture if one is currently ongoing. |
| 251 | if (mCaptureMode != CaptureMode::None) { return false; } |
| 252 | |
| 253 | // A new capture is started when the filename property changes. |
| 254 | // Read the filename property. |
| 255 | std::string prop = base::GetProperty(PROPERTY_CAPTURE_SKP_FILENAME, "0"); |
| 256 | // if the filename property changed to a valid value |
| 257 | if (prop[0] != '0' && mCapturedFile != prop) { |
| 258 | // remember this new filename |
| 259 | mCapturedFile = prop; |
| 260 | // and get a property indicating how many frames to capture. |
| 261 | mCaptureSequence = base::GetIntProperty(PROPERTY_CAPTURE_SKP_FRAMES, 1); |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 262 | if (mCaptureSequence <= 0) { |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 263 | return false; |
| 264 | } else if (mCaptureSequence == 1) { |
| 265 | mCaptureMode = CaptureMode::SingleFrameSKP; |
| 266 | } else { |
| 267 | mCaptureMode = CaptureMode::MultiFrameSKP; |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 268 | } |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 269 | return true; |
| 270 | } |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | // performs the first-frame work of a multi frame SKP capture. Returns true if successful. |
| 275 | bool SkiaPipeline::setupMultiFrameCapture() { |
| 276 | ALOGD("Set up multi-frame capture, frames = %d", mCaptureSequence); |
| 277 | // We own this stream and need to hold it until close() finishes. |
| 278 | auto stream = std::make_unique<SkFILEWStream>(mCapturedFile.c_str()); |
| 279 | if (stream->isValid()) { |
| 280 | mOpenMultiPicStream = std::move(stream); |
| 281 | mSerialContext.reset(new SkSharingSerialContext()); |
| 282 | SkSerialProcs procs; |
| 283 | procs.fImageProc = SkSharingSerialContext::serializeImage; |
| 284 | procs.fImageCtx = mSerialContext.get(); |
| 285 | // SkDocuments don't take owership of the streams they write. |
| 286 | // we need to keep it until after mMultiPic.close() |
| 287 | // procs is passed as a pointer, but just as a method of having an optional default. |
| 288 | // procs doesn't need to outlive this Make call. |
| 289 | mMultiPic = SkMakeMultiPictureDocument(mOpenMultiPicStream.get(), &procs); |
| 290 | return true; |
| 291 | } else { |
| 292 | ALOGE("Could not open \"%s\" for writing.", mCapturedFile.c_str()); |
| 293 | mCaptureSequence = 0; |
| 294 | mCaptureMode = CaptureMode::None; |
| 295 | return false; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | SkCanvas* SkiaPipeline::tryCapture(SkSurface* surface) { |
| 300 | if (CC_LIKELY(!Properties::skpCaptureEnabled)) { |
| 301 | return surface->getCanvas(); // Bail out early when capture is not turned on. |
| 302 | } |
| 303 | // Note that shouldStartNewFileCapture tells us if this is the *first* frame of a capture. |
| 304 | if (shouldStartNewFileCapture() && mCaptureMode == CaptureMode::MultiFrameSKP) { |
| 305 | if (!setupMultiFrameCapture()) { |
| 306 | return surface->getCanvas(); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 307 | } |
| 308 | } |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 309 | |
| 310 | // Create a canvas pointer, fill it depending on what kind of capture is requested (if any) |
| 311 | SkCanvas* pictureCanvas = nullptr; |
| 312 | switch (mCaptureMode) { |
| 313 | case CaptureMode::CallbackAPI: |
| 314 | case CaptureMode::SingleFrameSKP: |
| 315 | mRecorder.reset(new SkPictureRecorder()); |
Nathaniel Nifong | 31fee3a | 2019-07-11 16:27:14 -0400 | [diff] [blame] | 316 | pictureCanvas = mRecorder->beginRecording(surface->width(), surface->height()); |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 317 | break; |
| 318 | case CaptureMode::MultiFrameSKP: |
| 319 | // If a multi frame recording is active, initialize recording for a single frame of a |
| 320 | // multi frame file. |
| 321 | pictureCanvas = mMultiPic->beginPage(surface->width(), surface->height()); |
| 322 | break; |
| 323 | case CaptureMode::None: |
| 324 | // Returning here in the non-capture case means we can count on pictureCanvas being |
| 325 | // non-null below. |
| 326 | return surface->getCanvas(); |
| 327 | } |
| 328 | |
| 329 | // Setting up an nway canvas is common to any kind of capture. |
| 330 | mNwayCanvas = std::make_unique<SkNWayCanvas>(surface->width(), surface->height()); |
| 331 | mNwayCanvas->addCanvas(surface->getCanvas()); |
| 332 | mNwayCanvas->addCanvas(pictureCanvas); |
| 333 | return mNwayCanvas.get(); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | void SkiaPipeline::endCapture(SkSurface* surface) { |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 337 | if (CC_LIKELY(mCaptureMode == CaptureMode::None)) { return; } |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 338 | mNwayCanvas.reset(); |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 339 | ATRACE_CALL(); |
| 340 | if (mCaptureSequence > 0 && mCaptureMode == CaptureMode::MultiFrameSKP) { |
| 341 | mMultiPic->endPage(); |
| 342 | mCaptureSequence--; |
| 343 | if (mCaptureSequence == 0) { |
| 344 | mCaptureMode = CaptureMode::None; |
| 345 | // Pass mMultiPic and mOpenMultiPicStream to a background thread, which will handle |
| 346 | // the heavyweight serialization work and destroy them. mOpenMultiPicStream is released |
| 347 | // to a bare pointer because keeping it in a smart pointer makes the lambda |
| 348 | // non-copyable. The lambda is only called once, so this is safe. |
| 349 | SkFILEWStream* stream = mOpenMultiPicStream.release(); |
| 350 | CommonPool::post([doc = std::move(mMultiPic), stream]{ |
| 351 | ALOGD("Finalizing multi frame SKP"); |
| 352 | doc->close(); |
| 353 | delete stream; |
| 354 | ALOGD("Multi frame SKP complete."); |
| 355 | }); |
| 356 | } |
| 357 | } else { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 358 | sk_sp<SkPicture> picture = mRecorder->finishRecordingAsPicture(); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 359 | if (picture->approximateOpCount() > 0) { |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 360 | if (mPictureCapturedCallback) { |
| 361 | std::invoke(mPictureCapturedCallback, std::move(picture)); |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 362 | } else { |
| 363 | // single frame skp to file |
| 364 | auto data = picture->serialize(); |
| 365 | savePictureAsync(data, mCapturedFile); |
| 366 | mCaptureSequence = 0; |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 367 | } |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 368 | } |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 369 | mCaptureMode = CaptureMode::None; |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 370 | mRecorder.reset(); |
| 371 | } |
| 372 | } |
| 373 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 374 | void SkiaPipeline::renderFrame(const LayerUpdateQueue& layers, const SkRect& clip, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 375 | const std::vector<sp<RenderNode>>& nodes, bool opaque, |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 376 | const Rect& contentDrawBounds, sk_sp<SkSurface> surface, |
| 377 | const SkMatrix& preTransform) { |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 378 | bool previousSkpEnabled = Properties::skpCaptureEnabled; |
| 379 | if (mPictureCapturedCallback) { |
| 380 | Properties::skpCaptureEnabled = true; |
| 381 | } |
| 382 | |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 383 | renderVectorDrawableCache(); |
| 384 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 385 | // draw all layers up front |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 386 | renderLayersImpl(layers, opaque); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 387 | |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 388 | // initialize the canvas for the current frame, that might be a recording canvas if SKP |
| 389 | // capture is enabled. |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 390 | SkCanvas* canvas = tryCapture(surface.get()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 391 | |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 392 | renderFrameImpl(layers, clip, nodes, opaque, contentDrawBounds, canvas, preTransform); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 393 | |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 394 | endCapture(surface.get()); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 395 | |
| 396 | if (CC_UNLIKELY(Properties::debugOverdraw)) { |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 397 | renderOverdraw(layers, clip, nodes, contentDrawBounds, surface, preTransform); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | ATRACE_NAME("flush commands"); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 401 | surface->getCanvas()->flush(); |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 402 | |
| 403 | Properties::skpCaptureEnabled = previousSkpEnabled; |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 404 | } |
| 405 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 406 | namespace { |
| 407 | static Rect nodeBounds(RenderNode& node) { |
| 408 | auto& props = node.properties(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 409 | return Rect(props.getLeft(), props.getTop(), props.getRight(), props.getBottom()); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 410 | } |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 411 | } // namespace |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 412 | |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 413 | void SkiaPipeline::renderFrameImpl(const LayerUpdateQueue& layers, const SkRect& clip, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 414 | const std::vector<sp<RenderNode>>& nodes, bool opaque, |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 415 | const Rect& contentDrawBounds, SkCanvas* canvas, |
| 416 | const SkMatrix& preTransform) { |
Stan Iliev | b66b8bb | 2016-12-15 18:17:42 -0500 | [diff] [blame] | 417 | SkAutoCanvasRestore saver(canvas, true); |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 418 | canvas->androidFramework_setDeviceClipRestriction(preTransform.mapRect(clip).roundOut()); |
| 419 | canvas->concat(preTransform); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 420 | |
Stan Iliev | aadc032 | 2018-03-23 10:50:11 -0400 | [diff] [blame] | 421 | // STOPSHIP: Revert, temporary workaround to clear always F16 frame buffer for b/74976293 |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 422 | if (!opaque || getSurfaceColorType() == kRGBA_F16_SkColorType) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 423 | canvas->clear(SK_ColorTRANSPARENT); |
| 424 | } |
| 425 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 426 | if (1 == nodes.size()) { |
| 427 | if (!nodes[0]->nothingToDraw()) { |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 428 | RenderNodeDrawable root(nodes[0].get(), canvas); |
| 429 | root.draw(canvas); |
| 430 | } |
| 431 | } else if (0 == nodes.size()) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 432 | // nothing to draw |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 433 | } else { |
| 434 | // It there are multiple render nodes, they are laid out as follows: |
| 435 | // #0 - backdrop (content + caption) |
| 436 | // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop) |
| 437 | // #2 - additional overlay nodes |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 438 | // Usually the backdrop cannot be seen since it will be entirely covered by the content. |
| 439 | // While |
| 440 | // resizing however it might become partially visible. The following render loop will crop |
| 441 | // the |
| 442 | // backdrop against the content and draw the remaining part of it. It will then draw the |
| 443 | // content |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 444 | // cropped to the backdrop (since that indicates a shrinking of the window). |
| 445 | // |
| 446 | // Additional nodes will be drawn on top with no particular clipping semantics. |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 447 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 448 | // Usually the contents bounds should be mContentDrawBounds - however - we will |
| 449 | // move it towards the fixed edge to give it a more stable appearance (for the moment). |
| 450 | // 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] | 451 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 452 | // Backdrop bounds in render target space |
| 453 | const Rect backdrop = nodeBounds(*nodes[0]); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 454 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 455 | // Bounds that content will fill in render target space (note content node bounds may be |
| 456 | // bigger) |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 457 | Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight()); |
| 458 | content.translate(backdrop.left, backdrop.top); |
| 459 | if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) { |
| 460 | // Content doesn't entirely overlap backdrop, so fill around content (right/bottom) |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 461 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 462 | // 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] | 463 | // also fill left/top. Currently, both 2up and freeform position content at the top/left |
| 464 | // of |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 465 | // the backdrop, so this isn't necessary. |
| 466 | RenderNodeDrawable backdropNode(nodes[0].get(), canvas); |
| 467 | if (content.right < backdrop.right) { |
| 468 | // draw backdrop to right side of content |
| 469 | SkAutoCanvasRestore acr(canvas, true); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 470 | canvas->clipRect(SkRect::MakeLTRB(content.right, backdrop.top, backdrop.right, |
| 471 | backdrop.bottom)); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 472 | backdropNode.draw(canvas); |
| 473 | } |
| 474 | if (content.bottom < backdrop.bottom) { |
| 475 | // draw backdrop to bottom of content |
| 476 | // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill |
| 477 | SkAutoCanvasRestore acr(canvas, true); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 478 | canvas->clipRect(SkRect::MakeLTRB(content.left, content.bottom, content.right, |
| 479 | backdrop.bottom)); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 480 | backdropNode.draw(canvas); |
| 481 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 482 | } |
| 483 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 484 | RenderNodeDrawable contentNode(nodes[1].get(), canvas); |
| 485 | if (!backdrop.isEmpty()) { |
| 486 | // content node translation to catch up with backdrop |
| 487 | float dx = backdrop.left - contentDrawBounds.left; |
| 488 | float dy = backdrop.top - contentDrawBounds.top; |
| 489 | |
| 490 | SkAutoCanvasRestore acr(canvas, true); |
| 491 | canvas->translate(dx, dy); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 492 | const SkRect contentLocalClip = |
| 493 | SkRect::MakeXYWH(contentDrawBounds.left, contentDrawBounds.top, |
| 494 | backdrop.getWidth(), backdrop.getHeight()); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 495 | canvas->clipRect(contentLocalClip); |
| 496 | contentNode.draw(canvas); |
| 497 | } else { |
| 498 | SkAutoCanvasRestore acr(canvas, true); |
| 499 | contentNode.draw(canvas); |
| 500 | } |
| 501 | |
| 502 | // remaining overlay nodes, simply defer |
| 503 | for (size_t index = 2; index < nodes.size(); index++) { |
| 504 | if (!nodes[index]->nothingToDraw()) { |
| 505 | SkAutoCanvasRestore acr(canvas, true); |
| 506 | RenderNodeDrawable overlayNode(nodes[index].get(), canvas); |
| 507 | overlayNode.draw(canvas); |
| 508 | } |
| 509 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 510 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 511 | } |
| 512 | |
Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 513 | void SkiaPipeline::dumpResourceCacheUsage() const { |
| 514 | int resources, maxResources; |
| 515 | size_t bytes, maxBytes; |
| 516 | mRenderThread.getGrContext()->getResourceCacheUsage(&resources, &bytes); |
| 517 | mRenderThread.getGrContext()->getResourceCacheLimits(&maxResources, &maxBytes); |
| 518 | |
| 519 | SkString log("Resource Cache Usage:\n"); |
| 520 | log.appendf("%8d items out of %d maximum items\n", resources, maxResources); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 521 | log.appendf("%8zu bytes (%.2f MB) out of %.2f MB maximum\n", bytes, |
| 522 | 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] | 523 | |
| 524 | ALOGD("%s", log.c_str()); |
| 525 | } |
| 526 | |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 527 | void SkiaPipeline::setSurfaceColorProperties(ColorMode colorMode) { |
| 528 | if (colorMode == ColorMode::SRGB) { |
| 529 | mSurfaceColorType = SkColorType::kN32_SkColorType; |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 530 | mSurfaceColorSpace = SkColorSpace::MakeSRGB(); |
| 531 | } else if (colorMode == ColorMode::WideColorGamut) { |
| 532 | mSurfaceColorType = DeviceInfo::get()->getWideColorType(); |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 533 | mSurfaceColorSpace = DeviceInfo::get()->getWideColorSpace(); |
| 534 | } else { |
| 535 | LOG_ALWAYS_FATAL("Unreachable: unsupported color mode."); |
| 536 | } |
| 537 | } |
| 538 | |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 539 | // Overdraw debugging |
| 540 | |
| 541 | // These colors should be kept in sync with Caches::getOverdrawColor() with a few differences. |
| 542 | // This implementation: |
| 543 | // (1) Requires transparent entries for "no overdraw" and "single draws". |
| 544 | // (2) Requires premul colors (instead of unpremul). |
| 545 | // (3) Requires RGBA colors (instead of BGRA). |
| 546 | static const uint32_t kOverdrawColors[2][6] = { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 547 | { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 548 | 0x00000000, |
| 549 | 0x00000000, |
| 550 | 0x2f2f0000, |
| 551 | 0x2f002f00, |
| 552 | 0x3f00003f, |
| 553 | 0x7f00007f, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 554 | }, |
| 555 | { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 556 | 0x00000000, |
| 557 | 0x00000000, |
| 558 | 0x2f2f0000, |
| 559 | 0x4f004f4f, |
| 560 | 0x5f50335f, |
| 561 | 0x7f00007f, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 562 | }, |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 563 | }; |
| 564 | |
| 565 | void SkiaPipeline::renderOverdraw(const LayerUpdateQueue& layers, const SkRect& clip, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 566 | const std::vector<sp<RenderNode>>& nodes, |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 567 | const Rect& contentDrawBounds, sk_sp<SkSurface> surface, |
| 568 | const SkMatrix& preTransform) { |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 569 | // Set up the overdraw canvas. |
| 570 | SkImageInfo offscreenInfo = SkImageInfo::MakeA8(surface->width(), surface->height()); |
| 571 | sk_sp<SkSurface> offscreen = surface->makeSurface(offscreenInfo); |
| 572 | SkOverdrawCanvas overdrawCanvas(offscreen->getCanvas()); |
| 573 | |
| 574 | // Fake a redraw to replay the draw commands. This will increment the alpha channel |
| 575 | // each time a pixel would have been drawn. |
| 576 | // Pass true for opaque so we skip the clear - the overdrawCanvas is already zero |
| 577 | // initialized. |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 578 | renderFrameImpl(layers, clip, nodes, true, contentDrawBounds, &overdrawCanvas, preTransform); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 579 | sk_sp<SkImage> counts = offscreen->makeImageSnapshot(); |
| 580 | |
| 581 | // Draw overdraw colors to the canvas. The color filter will convert counts to colors. |
| 582 | SkPaint paint; |
| 583 | const SkPMColor* colors = kOverdrawColors[static_cast<int>(Properties::overdrawColorSet)]; |
| 584 | paint.setColorFilter(SkOverdrawColorFilter::Make(colors)); |
| 585 | surface->getCanvas()->drawImage(counts.get(), 0.0f, 0.0f, &paint); |
| 586 | } |
| 587 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 588 | } /* namespace skiapipeline */ |
| 589 | } /* namespace uirenderer */ |
| 590 | } /* namespace android */ |