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> |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 22 | #include <SkOverdrawCanvas.h> |
| 23 | #include <SkOverdrawColorFilter.h> |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 24 | #include <SkPicture.h> |
| 25 | #include <SkPictureRecorder.h> |
Stan Iliev | 216b157 | 2018-03-26 14:29:50 -0400 | [diff] [blame] | 26 | #include "TreeInfo.h" |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 27 | #include "VectorDrawable.h" |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 28 | #include "thread/CommonPool.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 29 | #include "utils/TraceUtils.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 30 | |
Ben Wagner | 3aeda5c | 2017-03-17 17:22:01 -0400 | [diff] [blame] | 31 | #include <unistd.h> |
| 32 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 33 | using namespace android::uirenderer::renderthread; |
| 34 | |
| 35 | namespace android { |
| 36 | namespace uirenderer { |
| 37 | namespace skiapipeline { |
| 38 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 39 | float SkiaPipeline::mLightRadius = 0; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 40 | uint8_t SkiaPipeline::mAmbientShadowAlpha = 0; |
| 41 | uint8_t SkiaPipeline::mSpotShadowAlpha = 0; |
| 42 | |
| 43 | Vector3 SkiaPipeline::mLightCenter = {FLT_MIN, FLT_MIN, FLT_MIN}; |
| 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) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 85 | updateLighting(lightGeometry, lightInfo); |
| 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()) { |
Stan Iliev | 18b388d | 2017-07-18 16:41:48 -0400 | [diff] [blame] | 105 | SkDEBUGF(("%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 | |
| 118 | auto savedLightCenter = mLightCenter; |
| 119 | // map current light center into RenderNode's coordinate space |
| 120 | layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(mLightCenter); |
| 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 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 128 | ATRACE_FORMAT("drawLayer [%s] %.1f x %.1f", layerNode->getName(), bounds.width(), |
| 129 | bounds.height()); |
Derek Sollenberger | 9552c2c | 2017-08-31 15:37:52 -0400 | [diff] [blame] | 130 | |
Matt Sarett | 79756be | 2016-11-09 16:13:54 -0500 | [diff] [blame] | 131 | layerNode->getSkiaLayer()->hasRenderedSinceRepaint = false; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 132 | layerCanvas->clear(SK_ColorTRANSPARENT); |
| 133 | |
| 134 | RenderNodeDrawable root(layerNode, layerCanvas, false); |
| 135 | root.forceDraw(layerCanvas); |
| 136 | layerCanvas->restoreToCount(saveCount); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 137 | mLightCenter = savedLightCenter; |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 138 | |
| 139 | // cache the current context so that we can defer flushing it until |
| 140 | // either all the layers have been rendered or the context changes |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 141 | GrContext* currentContext = layerNode->getLayerSurface()->getCanvas()->getGrContext(); |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 142 | if (cachedContext.get() != currentContext) { |
| 143 | if (cachedContext.get()) { |
Derek Sollenberger | be3876c | 2018-04-20 16:13:31 -0400 | [diff] [blame] | 144 | ATRACE_NAME("flush layers (context changed)"); |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 145 | cachedContext->flush(); |
| 146 | } |
| 147 | cachedContext.reset(SkSafeRef(currentContext)); |
| 148 | } |
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); |
| 179 | node->getSkiaLayer()->inverseTransformInWindow = 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 | |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 212 | void SkiaPipeline::renderVectorDrawableCache() { |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 213 | if (!mVectorDrawables.empty()) { |
| 214 | sp<VectorDrawableAtlas> atlas = mRenderThread.cacheManager().acquireVectorDrawableAtlas(); |
| 215 | auto grContext = mRenderThread.getGrContext(); |
| 216 | atlas->prepareForDraw(grContext); |
Derek Sollenberger | 9552c2c | 2017-08-31 15:37:52 -0400 | [diff] [blame] | 217 | ATRACE_NAME("Update VectorDrawables"); |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 218 | for (auto vd : mVectorDrawables) { |
| 219 | vd->updateCache(atlas, grContext); |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 220 | } |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 221 | mVectorDrawables.clear(); |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 222 | } |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 223 | } |
| 224 | |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 225 | static void savePictureAsync(const sk_sp<SkData>& data, const std::string& filename) { |
| 226 | CommonPool::post([data, filename] { |
| 227 | if (0 == access(filename.c_str(), F_OK)) { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 228 | return; |
| 229 | } |
| 230 | |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 231 | SkFILEWStream stream(filename.c_str()); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 232 | if (stream.isValid()) { |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 233 | stream.write(data->data(), data->size()); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 234 | stream.flush(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 235 | SkDebugf("SKP Captured Drawing Output (%d bytes) for frame. %s", stream.bytesWritten(), |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 236 | filename.c_str()); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 237 | } |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 238 | }); |
| 239 | } |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 240 | |
| 241 | SkCanvas* SkiaPipeline::tryCapture(SkSurface* surface) { |
| 242 | if (CC_UNLIKELY(Properties::skpCaptureEnabled)) { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 243 | char prop[PROPERTY_VALUE_MAX] = {'\0'}; |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 244 | if (mCaptureSequence <= 0) { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 245 | property_get(PROPERTY_CAPTURE_SKP_FILENAME, prop, "0"); |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 246 | if (prop[0] != '0' && mCapturedFile != prop) { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 247 | mCapturedFile = prop; |
| 248 | mCaptureSequence = property_get_int32(PROPERTY_CAPTURE_SKP_FRAMES, 1); |
| 249 | } |
| 250 | } |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 251 | if (mCaptureSequence > 0 || mPictureCapturedCallback) { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 252 | mRecorder.reset(new SkPictureRecorder()); |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame^] | 253 | SkCanvas* pictureCanvas = |
| 254 | mRecorder->beginRecording(surface->width(), surface->height(), nullptr, |
| 255 | SkPictureRecorder::kPlaybackDrawPicture_RecordFlag); |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 256 | mNwayCanvas = std::make_unique<SkNWayCanvas>(surface->width(), surface->height()); |
| 257 | mNwayCanvas->addCanvas(surface->getCanvas()); |
| 258 | mNwayCanvas->addCanvas(pictureCanvas); |
| 259 | return mNwayCanvas.get(); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | return surface->getCanvas(); |
| 263 | } |
| 264 | |
| 265 | void SkiaPipeline::endCapture(SkSurface* surface) { |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 266 | mNwayCanvas.reset(); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 267 | if (CC_UNLIKELY(mRecorder.get())) { |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 268 | ATRACE_CALL(); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 269 | sk_sp<SkPicture> picture = mRecorder->finishRecordingAsPicture(); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 270 | if (picture->approximateOpCount() > 0) { |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 271 | if (mCaptureSequence > 0) { |
| 272 | ATRACE_BEGIN("picture->serialize"); |
| 273 | auto data = picture->serialize(); |
| 274 | ATRACE_END(); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 275 | |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 276 | // offload saving to file in a different thread |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 277 | if (1 == mCaptureSequence) { |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 278 | savePictureAsync(data, mCapturedFile); |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 279 | } else { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame^] | 280 | savePictureAsync(data, mCapturedFile + "_" + std::to_string(mCaptureSequence)); |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 281 | } |
| 282 | mCaptureSequence--; |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 283 | } |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 284 | if (mPictureCapturedCallback) { |
| 285 | std::invoke(mPictureCapturedCallback, std::move(picture)); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 286 | } |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 287 | } |
| 288 | mRecorder.reset(); |
| 289 | } |
| 290 | } |
| 291 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 292 | void SkiaPipeline::renderFrame(const LayerUpdateQueue& layers, const SkRect& clip, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 293 | const std::vector<sp<RenderNode>>& nodes, bool opaque, |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 294 | const Rect& contentDrawBounds, sk_sp<SkSurface> surface, |
| 295 | const SkMatrix& preTransform) { |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 296 | bool previousSkpEnabled = Properties::skpCaptureEnabled; |
| 297 | if (mPictureCapturedCallback) { |
| 298 | Properties::skpCaptureEnabled = true; |
| 299 | } |
| 300 | |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 301 | renderVectorDrawableCache(); |
| 302 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 303 | // draw all layers up front |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 304 | renderLayersImpl(layers, opaque); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 305 | |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 306 | // initialize the canvas for the current frame, that might be a recording canvas if SKP |
| 307 | // capture is enabled. |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 308 | std::unique_ptr<SkPictureRecorder> recorder; |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 309 | SkCanvas* canvas = tryCapture(surface.get()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 310 | |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 311 | renderFrameImpl(layers, clip, nodes, opaque, contentDrawBounds, canvas, preTransform); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 312 | |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 313 | endCapture(surface.get()); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 314 | |
| 315 | if (CC_UNLIKELY(Properties::debugOverdraw)) { |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 316 | renderOverdraw(layers, clip, nodes, contentDrawBounds, surface, preTransform); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | ATRACE_NAME("flush commands"); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 320 | surface->getCanvas()->flush(); |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 321 | |
| 322 | Properties::skpCaptureEnabled = previousSkpEnabled; |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 323 | } |
| 324 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 325 | namespace { |
| 326 | static Rect nodeBounds(RenderNode& node) { |
| 327 | auto& props = node.properties(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 328 | return Rect(props.getLeft(), props.getTop(), props.getRight(), props.getBottom()); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 329 | } |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame^] | 330 | } // namespace |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 331 | |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 332 | void SkiaPipeline::renderFrameImpl(const LayerUpdateQueue& layers, const SkRect& clip, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 333 | const std::vector<sp<RenderNode>>& nodes, bool opaque, |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 334 | const Rect& contentDrawBounds, SkCanvas* canvas, |
| 335 | const SkMatrix& preTransform) { |
Stan Iliev | b66b8bb | 2016-12-15 18:17:42 -0500 | [diff] [blame] | 336 | SkAutoCanvasRestore saver(canvas, true); |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 337 | canvas->androidFramework_setDeviceClipRestriction(preTransform.mapRect(clip).roundOut()); |
| 338 | canvas->concat(preTransform); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 339 | |
Stan Iliev | aadc032 | 2018-03-23 10:50:11 -0400 | [diff] [blame] | 340 | // 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] | 341 | if (!opaque || getSurfaceColorType() == kRGBA_F16_SkColorType) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 342 | canvas->clear(SK_ColorTRANSPARENT); |
| 343 | } |
| 344 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 345 | if (1 == nodes.size()) { |
| 346 | if (!nodes[0]->nothingToDraw()) { |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 347 | RenderNodeDrawable root(nodes[0].get(), canvas); |
| 348 | root.draw(canvas); |
| 349 | } |
| 350 | } else if (0 == nodes.size()) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 351 | // nothing to draw |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 352 | } else { |
| 353 | // It there are multiple render nodes, they are laid out as follows: |
| 354 | // #0 - backdrop (content + caption) |
| 355 | // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop) |
| 356 | // #2 - additional overlay nodes |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 357 | // Usually the backdrop cannot be seen since it will be entirely covered by the content. |
| 358 | // While |
| 359 | // resizing however it might become partially visible. The following render loop will crop |
| 360 | // the |
| 361 | // backdrop against the content and draw the remaining part of it. It will then draw the |
| 362 | // content |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 363 | // cropped to the backdrop (since that indicates a shrinking of the window). |
| 364 | // |
| 365 | // Additional nodes will be drawn on top with no particular clipping semantics. |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 366 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 367 | // Usually the contents bounds should be mContentDrawBounds - however - we will |
| 368 | // move it towards the fixed edge to give it a more stable appearance (for the moment). |
| 369 | // 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] | 370 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 371 | // Backdrop bounds in render target space |
| 372 | const Rect backdrop = nodeBounds(*nodes[0]); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 373 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 374 | // Bounds that content will fill in render target space (note content node bounds may be |
| 375 | // bigger) |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 376 | Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight()); |
| 377 | content.translate(backdrop.left, backdrop.top); |
| 378 | if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) { |
| 379 | // Content doesn't entirely overlap backdrop, so fill around content (right/bottom) |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 380 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 381 | // 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] | 382 | // also fill left/top. Currently, both 2up and freeform position content at the top/left |
| 383 | // of |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 384 | // the backdrop, so this isn't necessary. |
| 385 | RenderNodeDrawable backdropNode(nodes[0].get(), canvas); |
| 386 | if (content.right < backdrop.right) { |
| 387 | // draw backdrop to right side of content |
| 388 | SkAutoCanvasRestore acr(canvas, true); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 389 | canvas->clipRect(SkRect::MakeLTRB(content.right, backdrop.top, backdrop.right, |
| 390 | backdrop.bottom)); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 391 | backdropNode.draw(canvas); |
| 392 | } |
| 393 | if (content.bottom < backdrop.bottom) { |
| 394 | // draw backdrop to bottom of content |
| 395 | // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill |
| 396 | SkAutoCanvasRestore acr(canvas, true); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 397 | canvas->clipRect(SkRect::MakeLTRB(content.left, content.bottom, content.right, |
| 398 | backdrop.bottom)); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 399 | backdropNode.draw(canvas); |
| 400 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 401 | } |
| 402 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 403 | RenderNodeDrawable contentNode(nodes[1].get(), canvas); |
| 404 | if (!backdrop.isEmpty()) { |
| 405 | // content node translation to catch up with backdrop |
| 406 | float dx = backdrop.left - contentDrawBounds.left; |
| 407 | float dy = backdrop.top - contentDrawBounds.top; |
| 408 | |
| 409 | SkAutoCanvasRestore acr(canvas, true); |
| 410 | canvas->translate(dx, dy); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 411 | const SkRect contentLocalClip = |
| 412 | SkRect::MakeXYWH(contentDrawBounds.left, contentDrawBounds.top, |
| 413 | backdrop.getWidth(), backdrop.getHeight()); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 414 | canvas->clipRect(contentLocalClip); |
| 415 | contentNode.draw(canvas); |
| 416 | } else { |
| 417 | SkAutoCanvasRestore acr(canvas, true); |
| 418 | contentNode.draw(canvas); |
| 419 | } |
| 420 | |
| 421 | // remaining overlay nodes, simply defer |
| 422 | for (size_t index = 2; index < nodes.size(); index++) { |
| 423 | if (!nodes[index]->nothingToDraw()) { |
| 424 | SkAutoCanvasRestore acr(canvas, true); |
| 425 | RenderNodeDrawable overlayNode(nodes[index].get(), canvas); |
| 426 | overlayNode.draw(canvas); |
| 427 | } |
| 428 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 429 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 430 | } |
| 431 | |
Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 432 | void SkiaPipeline::dumpResourceCacheUsage() const { |
| 433 | int resources, maxResources; |
| 434 | size_t bytes, maxBytes; |
| 435 | mRenderThread.getGrContext()->getResourceCacheUsage(&resources, &bytes); |
| 436 | mRenderThread.getGrContext()->getResourceCacheLimits(&maxResources, &maxBytes); |
| 437 | |
| 438 | SkString log("Resource Cache Usage:\n"); |
| 439 | log.appendf("%8d items out of %d maximum items\n", resources, maxResources); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 440 | log.appendf("%8zu bytes (%.2f MB) out of %.2f MB maximum\n", bytes, |
| 441 | 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] | 442 | |
| 443 | ALOGD("%s", log.c_str()); |
| 444 | } |
| 445 | |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 446 | void SkiaPipeline::setSurfaceColorProperties(ColorMode colorMode) { |
| 447 | if (colorMode == ColorMode::SRGB) { |
| 448 | mSurfaceColorType = SkColorType::kN32_SkColorType; |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 449 | mSurfaceColorSpace = SkColorSpace::MakeSRGB(); |
| 450 | } else if (colorMode == ColorMode::WideColorGamut) { |
| 451 | mSurfaceColorType = DeviceInfo::get()->getWideColorType(); |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 452 | mSurfaceColorSpace = DeviceInfo::get()->getWideColorSpace(); |
| 453 | } else { |
| 454 | LOG_ALWAYS_FATAL("Unreachable: unsupported color mode."); |
| 455 | } |
| 456 | } |
| 457 | |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 458 | // Overdraw debugging |
| 459 | |
| 460 | // These colors should be kept in sync with Caches::getOverdrawColor() with a few differences. |
| 461 | // This implementation: |
| 462 | // (1) Requires transparent entries for "no overdraw" and "single draws". |
| 463 | // (2) Requires premul colors (instead of unpremul). |
| 464 | // (3) Requires RGBA colors (instead of BGRA). |
| 465 | static const uint32_t kOverdrawColors[2][6] = { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 466 | { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame^] | 467 | 0x00000000, |
| 468 | 0x00000000, |
| 469 | 0x2f2f0000, |
| 470 | 0x2f002f00, |
| 471 | 0x3f00003f, |
| 472 | 0x7f00007f, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 473 | }, |
| 474 | { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame^] | 475 | 0x00000000, |
| 476 | 0x00000000, |
| 477 | 0x2f2f0000, |
| 478 | 0x4f004f4f, |
| 479 | 0x5f50335f, |
| 480 | 0x7f00007f, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 481 | }, |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 482 | }; |
| 483 | |
| 484 | void SkiaPipeline::renderOverdraw(const LayerUpdateQueue& layers, const SkRect& clip, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 485 | const std::vector<sp<RenderNode>>& nodes, |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 486 | const Rect& contentDrawBounds, sk_sp<SkSurface> surface, |
| 487 | const SkMatrix& preTransform) { |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 488 | // Set up the overdraw canvas. |
| 489 | SkImageInfo offscreenInfo = SkImageInfo::MakeA8(surface->width(), surface->height()); |
| 490 | sk_sp<SkSurface> offscreen = surface->makeSurface(offscreenInfo); |
| 491 | SkOverdrawCanvas overdrawCanvas(offscreen->getCanvas()); |
| 492 | |
| 493 | // Fake a redraw to replay the draw commands. This will increment the alpha channel |
| 494 | // each time a pixel would have been drawn. |
| 495 | // Pass true for opaque so we skip the clear - the overdrawCanvas is already zero |
| 496 | // initialized. |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 497 | renderFrameImpl(layers, clip, nodes, true, contentDrawBounds, &overdrawCanvas, preTransform); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 498 | sk_sp<SkImage> counts = offscreen->makeImageSnapshot(); |
| 499 | |
| 500 | // Draw overdraw colors to the canvas. The color filter will convert counts to colors. |
| 501 | SkPaint paint; |
| 502 | const SkPMColor* colors = kOverdrawColors[static_cast<int>(Properties::overdrawColorSet)]; |
| 503 | paint.setColorFilter(SkOverdrawColorFilter::Make(colors)); |
| 504 | surface->getCanvas()->drawImage(counts.get(), 0.0f, 0.0f, &paint); |
| 505 | } |
| 506 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 507 | } /* namespace skiapipeline */ |
| 508 | } /* namespace uirenderer */ |
| 509 | } /* namespace android */ |