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