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 | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 28 | #include "utils/TraceUtils.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 29 | |
Ben Wagner | 3aeda5c | 2017-03-17 17:22:01 -0400 | [diff] [blame] | 30 | #include <unistd.h> |
| 31 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 32 | using namespace android::uirenderer::renderthread; |
| 33 | |
| 34 | namespace android { |
| 35 | namespace uirenderer { |
| 36 | namespace skiapipeline { |
| 37 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 38 | float SkiaPipeline::mLightRadius = 0; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 39 | uint8_t SkiaPipeline::mAmbientShadowAlpha = 0; |
| 40 | uint8_t SkiaPipeline::mSpotShadowAlpha = 0; |
| 41 | |
| 42 | Vector3 SkiaPipeline::mLightCenter = {FLT_MIN, FLT_MIN, FLT_MIN}; |
| 43 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 44 | SkiaPipeline::SkiaPipeline(RenderThread& thread) : mRenderThread(thread) { |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 45 | mVectorDrawables.reserve(30); |
| 46 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 47 | |
Stan Iliev | 232f362 | 2017-08-23 17:15:09 -0400 | [diff] [blame] | 48 | SkiaPipeline::~SkiaPipeline() { |
| 49 | unpinImages(); |
| 50 | } |
| 51 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 52 | TaskManager* SkiaPipeline::getTaskManager() { |
Derek Sollenberger | 8ec9e88 | 2017-08-24 16:36:08 -0400 | [diff] [blame] | 53 | return mRenderThread.cacheManager().getTaskManager(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void SkiaPipeline::onDestroyHardwareResources() { |
Derek Sollenberger | 92a9eb9 | 2018-04-12 13:42:19 -0400 | [diff] [blame] | 57 | unpinImages(); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 58 | mRenderThread.cacheManager().trimStaleResources(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 59 | } |
| 60 | |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 61 | bool SkiaPipeline::pinImages(std::vector<SkImage*>& mutableImages) { |
| 62 | for (SkImage* image : mutableImages) { |
Derek Sollenberger | 189e874 | 2016-11-16 16:00:17 -0500 | [diff] [blame] | 63 | if (SkImage_pinAsTexture(image, mRenderThread.getGrContext())) { |
| 64 | mPinnedImages.emplace_back(sk_ref_sp(image)); |
| 65 | } else { |
| 66 | return false; |
| 67 | } |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | void SkiaPipeline::unpinImages() { |
| 73 | for (auto& image : mPinnedImages) { |
| 74 | SkImage_unpinAsTexture(image.get(), mRenderThread.getGrContext()); |
| 75 | } |
| 76 | mPinnedImages.clear(); |
| 77 | } |
| 78 | |
Stan Iliev | 47fed6ba | 2017-10-18 17:56:43 -0400 | [diff] [blame] | 79 | void SkiaPipeline::onPrepareTree() { |
| 80 | // The only time mVectorDrawables is not empty is if prepare tree was called 2 times without |
| 81 | // a renderFrame in the middle. |
| 82 | mVectorDrawables.clear(); |
| 83 | } |
| 84 | |
John Reck | d9d7f12 | 2018-05-03 14:40:56 -0700 | [diff] [blame] | 85 | void SkiaPipeline::renderLayers(const LightGeometry& lightGeometry, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 86 | LayerUpdateQueue* layerUpdateQueue, bool opaque, |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 87 | const LightInfo& lightInfo) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 88 | updateLighting(lightGeometry, lightInfo); |
| 89 | ATRACE_NAME("draw layers"); |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 90 | renderVectorDrawableCache(); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 91 | renderLayersImpl(*layerUpdateQueue, opaque); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 92 | layerUpdateQueue->clear(); |
| 93 | } |
| 94 | |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 95 | void SkiaPipeline::renderLayersImpl(const LayerUpdateQueue& layers, bool opaque) { |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 96 | sk_sp<GrContext> cachedContext; |
| 97 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 98 | // Render all layers that need to be updated, in order. |
| 99 | for (size_t i = 0; i < layers.entries().size(); i++) { |
John Reck | fc29f7acd | 2017-03-02 13:23:16 -0800 | [diff] [blame] | 100 | RenderNode* layerNode = layers.entries()[i].renderNode.get(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 101 | // only schedule repaint if node still on layer - possible it may have been |
| 102 | // removed during a dropped frame, but layers may still remain scheduled so |
| 103 | // as not to lose info on what portion is damaged |
| 104 | if (CC_LIKELY(layerNode->getLayerSurface() != nullptr)) { |
| 105 | SkASSERT(layerNode->getLayerSurface()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 106 | SkiaDisplayList* displayList = (SkiaDisplayList*)layerNode->getDisplayList(); |
| 107 | if (!displayList || displayList->isEmpty()) { |
Stan Iliev | 18b388d | 2017-07-18 16:41:48 -0400 | [diff] [blame] | 108 | SkDEBUGF(("%p drawLayers(%s) : missing drawable", layerNode, layerNode->getName())); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 109 | return; |
| 110 | } |
| 111 | |
| 112 | const Rect& layerDamage = layers.entries()[i].damage; |
| 113 | |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame^] | 114 | SkCanvas* layerCanvas = layerNode->getLayerSurface()->getCanvas(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 115 | |
| 116 | int saveCount = layerCanvas->save(); |
| 117 | SkASSERT(saveCount == 1); |
| 118 | |
Stan Iliev | b66b8bb | 2016-12-15 18:17:42 -0500 | [diff] [blame] | 119 | layerCanvas->androidFramework_setDeviceClipRestriction(layerDamage.toSkIRect()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 120 | |
| 121 | auto savedLightCenter = mLightCenter; |
| 122 | // map current light center into RenderNode's coordinate space |
| 123 | layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(mLightCenter); |
| 124 | |
| 125 | const RenderProperties& properties = layerNode->properties(); |
| 126 | const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight()); |
| 127 | if (properties.getClipToBounds() && layerCanvas->quickReject(bounds)) { |
| 128 | return; |
| 129 | } |
| 130 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 131 | ATRACE_FORMAT("drawLayer [%s] %.1f x %.1f", layerNode->getName(), bounds.width(), |
| 132 | bounds.height()); |
Derek Sollenberger | 9552c2c | 2017-08-31 15:37:52 -0400 | [diff] [blame] | 133 | |
Matt Sarett | 79756be | 2016-11-09 16:13:54 -0500 | [diff] [blame] | 134 | layerNode->getSkiaLayer()->hasRenderedSinceRepaint = false; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 135 | layerCanvas->clear(SK_ColorTRANSPARENT); |
| 136 | |
| 137 | RenderNodeDrawable root(layerNode, layerCanvas, false); |
| 138 | root.forceDraw(layerCanvas); |
| 139 | layerCanvas->restoreToCount(saveCount); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 140 | mLightCenter = savedLightCenter; |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 141 | |
| 142 | // cache the current context so that we can defer flushing it until |
| 143 | // either all the layers have been rendered or the context changes |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 144 | GrContext* currentContext = layerNode->getLayerSurface()->getCanvas()->getGrContext(); |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 145 | if (cachedContext.get() != currentContext) { |
| 146 | if (cachedContext.get()) { |
Derek Sollenberger | be3876c | 2018-04-20 16:13:31 -0400 | [diff] [blame] | 147 | ATRACE_NAME("flush layers (context changed)"); |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 148 | cachedContext->flush(); |
| 149 | } |
| 150 | cachedContext.reset(SkSafeRef(currentContext)); |
| 151 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 152 | } |
| 153 | } |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 154 | |
| 155 | if (cachedContext.get()) { |
Derek Sollenberger | be3876c | 2018-04-20 16:13:31 -0400 | [diff] [blame] | 156 | ATRACE_NAME("flush layers"); |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 157 | cachedContext->flush(); |
| 158 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 159 | } |
| 160 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 161 | bool SkiaPipeline::createOrUpdateLayer(RenderNode* node, const DamageAccumulator& damageAccumulator, |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 162 | ErrorHandler* errorHandler) { |
Derek Sollenberger | 03e6cff7 | 2017-12-04 15:07:08 -0500 | [diff] [blame] | 163 | // compute the size of the surface (i.e. texture) to be allocated for this layer |
| 164 | const int surfaceWidth = ceilf(node->getWidth() / float(LAYER_SIZE)) * LAYER_SIZE; |
| 165 | const int surfaceHeight = ceilf(node->getHeight() / float(LAYER_SIZE)) * LAYER_SIZE; |
| 166 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 167 | SkSurface* layer = node->getLayerSurface(); |
Derek Sollenberger | 03e6cff7 | 2017-12-04 15:07:08 -0500 | [diff] [blame] | 168 | if (!layer || layer->width() != surfaceWidth || layer->height() != surfaceHeight) { |
Stan Iliev | 08fc19a | 2017-07-24 10:20:33 -0400 | [diff] [blame] | 169 | SkImageInfo info; |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 170 | info = SkImageInfo::Make(surfaceWidth, surfaceHeight, getSurfaceColorType(), |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 171 | kPremul_SkAlphaType, getSurfaceColorSpace()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 172 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
| 173 | SkASSERT(mRenderThread.getGrContext() != nullptr); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 174 | node->setLayerSurface(SkSurface::MakeRenderTarget(mRenderThread.getGrContext(), |
Derek Sollenberger | 25833d2 | 2019-01-14 13:55:55 -0500 | [diff] [blame] | 175 | SkBudgeted::kYes, info, 0, |
| 176 | this->getSurfaceOrigin(), &props)); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 177 | if (node->getLayerSurface()) { |
| 178 | // update the transform in window of the layer to reset its origin wrt light source |
| 179 | // position |
| 180 | Matrix4 windowTransform; |
| 181 | damageAccumulator.computeCurrentTransform(&windowTransform); |
| 182 | node->getSkiaLayer()->inverseTransformInWindow = windowTransform; |
Stan Iliev | 216b157 | 2018-03-26 14:29:50 -0400 | [diff] [blame] | 183 | } else { |
| 184 | String8 cachesOutput; |
| 185 | mRenderThread.cacheManager().dumpMemoryUsage(cachesOutput, |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 186 | &mRenderThread.renderState()); |
Stan Iliev | 216b157 | 2018-03-26 14:29:50 -0400 | [diff] [blame] | 187 | ALOGE("%s", cachesOutput.string()); |
| 188 | if (errorHandler) { |
| 189 | std::ostringstream err; |
| 190 | err << "Unable to create layer for " << node->getName(); |
| 191 | const int maxTextureSize = DeviceInfo::get()->maxTextureSize(); |
| 192 | err << ", size " << info.width() << "x" << info.height() << " max size " |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 193 | << maxTextureSize << " color type " << (int)info.colorType() << " has context " |
| 194 | << (int)(mRenderThread.getGrContext() != nullptr); |
Stan Iliev | 216b157 | 2018-03-26 14:29:50 -0400 | [diff] [blame] | 195 | errorHandler->onError(err.str()); |
| 196 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 197 | } |
| 198 | return true; |
| 199 | } |
| 200 | return false; |
| 201 | } |
| 202 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 203 | void SkiaPipeline::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) { |
| 204 | GrContext* context = thread.getGrContext(); |
| 205 | if (context) { |
| 206 | ATRACE_FORMAT("Bitmap#prepareToDraw %dx%d", bitmap->width(), bitmap->height()); |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 207 | auto image = bitmap->makeImage(); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 208 | if (image.get() && !bitmap->isHardware()) { |
| 209 | SkImage_pinAsTexture(image.get(), context); |
| 210 | SkImage_unpinAsTexture(image.get(), context); |
| 211 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 215 | void SkiaPipeline::renderVectorDrawableCache() { |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 216 | if (!mVectorDrawables.empty()) { |
| 217 | sp<VectorDrawableAtlas> atlas = mRenderThread.cacheManager().acquireVectorDrawableAtlas(); |
| 218 | auto grContext = mRenderThread.getGrContext(); |
| 219 | atlas->prepareForDraw(grContext); |
Derek Sollenberger | 9552c2c | 2017-08-31 15:37:52 -0400 | [diff] [blame] | 220 | ATRACE_NAME("Update VectorDrawables"); |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 221 | for (auto vd : mVectorDrawables) { |
| 222 | vd->updateCache(atlas, grContext); |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 223 | } |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 224 | mVectorDrawables.clear(); |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 225 | } |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 226 | } |
| 227 | |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 228 | class SkiaPipeline::SavePictureProcessor : public TaskProcessor<bool> { |
| 229 | public: |
| 230 | explicit SavePictureProcessor(TaskManager* taskManager) : TaskProcessor<bool>(taskManager) {} |
| 231 | |
| 232 | struct SavePictureTask : public Task<bool> { |
| 233 | sk_sp<SkData> data; |
| 234 | std::string filename; |
| 235 | }; |
| 236 | |
| 237 | void savePicture(const sk_sp<SkData>& data, const std::string& filename) { |
| 238 | sp<SavePictureTask> task(new SavePictureTask()); |
| 239 | task->data = data; |
| 240 | task->filename = filename; |
| 241 | TaskProcessor<bool>::add(task); |
| 242 | } |
| 243 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 244 | virtual void onProcess(const sp<Task<bool>>& task) override { |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame^] | 245 | ATRACE_NAME("SavePictureTask"); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 246 | SavePictureTask* t = static_cast<SavePictureTask*>(task.get()); |
| 247 | |
| 248 | if (0 == access(t->filename.c_str(), F_OK)) { |
| 249 | task->setResult(false); |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | SkFILEWStream stream(t->filename.c_str()); |
| 254 | if (stream.isValid()) { |
| 255 | stream.write(t->data->data(), t->data->size()); |
| 256 | stream.flush(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 257 | SkDebugf("SKP Captured Drawing Output (%d bytes) for frame. %s", stream.bytesWritten(), |
| 258 | t->filename.c_str()); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | task->setResult(true); |
| 262 | } |
| 263 | }; |
| 264 | |
| 265 | SkCanvas* SkiaPipeline::tryCapture(SkSurface* surface) { |
| 266 | if (CC_UNLIKELY(Properties::skpCaptureEnabled)) { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 267 | char prop[PROPERTY_VALUE_MAX] = {'\0'}; |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame^] | 268 | if (mCaptureSequence <= 0) { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 269 | property_get(PROPERTY_CAPTURE_SKP_FILENAME, prop, "0"); |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame^] | 270 | if (prop[0] != '0' && mCapturedFile != prop) { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 271 | mCapturedFile = prop; |
| 272 | mCaptureSequence = property_get_int32(PROPERTY_CAPTURE_SKP_FRAMES, 1); |
| 273 | } |
| 274 | } |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame^] | 275 | if (mCaptureSequence > 0 || mPictureCapturedCallback) { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 276 | mRecorder.reset(new SkPictureRecorder()); |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame^] | 277 | SkCanvas* pictureCanvas = mRecorder->beginRecording(surface->width(), surface->height(), nullptr, |
| 278 | SkPictureRecorder::kPlaybackDrawPicture_RecordFlag); |
| 279 | mNwayCanvas = std::make_unique<SkNWayCanvas>(surface->width(), surface->height()); |
| 280 | mNwayCanvas->addCanvas(surface->getCanvas()); |
| 281 | mNwayCanvas->addCanvas(pictureCanvas); |
| 282 | return mNwayCanvas.get(); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | return surface->getCanvas(); |
| 286 | } |
| 287 | |
| 288 | void SkiaPipeline::endCapture(SkSurface* surface) { |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame^] | 289 | mNwayCanvas.reset(); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 290 | if (CC_UNLIKELY(mRecorder.get())) { |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame^] | 291 | ATRACE_CALL(); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 292 | sk_sp<SkPicture> picture = mRecorder->finishRecordingAsPicture(); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 293 | if (picture->approximateOpCount() > 0) { |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame^] | 294 | if (mCaptureSequence > 0) { |
| 295 | ATRACE_BEGIN("picture->serialize"); |
| 296 | auto data = picture->serialize(); |
| 297 | ATRACE_END(); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 298 | |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame^] | 299 | // offload saving to file in a different thread |
| 300 | if (!mSavePictureProcessor.get()) { |
| 301 | TaskManager* taskManager = getTaskManager(); |
| 302 | mSavePictureProcessor = new SavePictureProcessor( |
| 303 | taskManager->canRunTasks() ? taskManager : nullptr); |
| 304 | } |
| 305 | if (1 == mCaptureSequence) { |
| 306 | mSavePictureProcessor->savePicture(data, mCapturedFile); |
| 307 | } else { |
| 308 | mSavePictureProcessor->savePicture( |
| 309 | data, |
| 310 | mCapturedFile + "_" + std::to_string(mCaptureSequence)); |
| 311 | } |
| 312 | mCaptureSequence--; |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 313 | } |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame^] | 314 | if (mPictureCapturedCallback) { |
| 315 | std::invoke(mPictureCapturedCallback, std::move(picture)); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 316 | } |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 317 | } |
| 318 | mRecorder.reset(); |
| 319 | } |
| 320 | } |
| 321 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 322 | void SkiaPipeline::renderFrame(const LayerUpdateQueue& layers, const SkRect& clip, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 323 | const std::vector<sp<RenderNode>>& nodes, bool opaque, |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 324 | const Rect& contentDrawBounds, sk_sp<SkSurface> surface, |
| 325 | const SkMatrix& preTransform) { |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame^] | 326 | bool previousSkpEnabled = Properties::skpCaptureEnabled; |
| 327 | if (mPictureCapturedCallback) { |
| 328 | Properties::skpCaptureEnabled = true; |
| 329 | } |
| 330 | |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 331 | renderVectorDrawableCache(); |
| 332 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 333 | // draw all layers up front |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 334 | renderLayersImpl(layers, opaque); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 335 | |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 336 | // initialize the canvas for the current frame, that might be a recording canvas if SKP |
| 337 | // capture is enabled. |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 338 | std::unique_ptr<SkPictureRecorder> recorder; |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 339 | SkCanvas* canvas = tryCapture(surface.get()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 340 | |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 341 | renderFrameImpl(layers, clip, nodes, opaque, contentDrawBounds, canvas, preTransform); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 342 | |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 343 | endCapture(surface.get()); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 344 | |
| 345 | if (CC_UNLIKELY(Properties::debugOverdraw)) { |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 346 | renderOverdraw(layers, clip, nodes, contentDrawBounds, surface, preTransform); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | ATRACE_NAME("flush commands"); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 350 | surface->getCanvas()->flush(); |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame^] | 351 | |
| 352 | Properties::skpCaptureEnabled = previousSkpEnabled; |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 353 | } |
| 354 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 355 | namespace { |
| 356 | static Rect nodeBounds(RenderNode& node) { |
| 357 | auto& props = node.properties(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 358 | return Rect(props.getLeft(), props.getTop(), props.getRight(), props.getBottom()); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 362 | void SkiaPipeline::renderFrameImpl(const LayerUpdateQueue& layers, const SkRect& clip, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 363 | const std::vector<sp<RenderNode>>& nodes, bool opaque, |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 364 | const Rect& contentDrawBounds, SkCanvas* canvas, |
| 365 | const SkMatrix& preTransform) { |
Stan Iliev | b66b8bb | 2016-12-15 18:17:42 -0500 | [diff] [blame] | 366 | SkAutoCanvasRestore saver(canvas, true); |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 367 | canvas->androidFramework_setDeviceClipRestriction(preTransform.mapRect(clip).roundOut()); |
| 368 | canvas->concat(preTransform); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 369 | |
Stan Iliev | aadc032 | 2018-03-23 10:50:11 -0400 | [diff] [blame] | 370 | // 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] | 371 | if (!opaque || getSurfaceColorType() == kRGBA_F16_SkColorType) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 372 | canvas->clear(SK_ColorTRANSPARENT); |
| 373 | } |
| 374 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 375 | if (1 == nodes.size()) { |
| 376 | if (!nodes[0]->nothingToDraw()) { |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 377 | RenderNodeDrawable root(nodes[0].get(), canvas); |
| 378 | root.draw(canvas); |
| 379 | } |
| 380 | } else if (0 == nodes.size()) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 381 | // nothing to draw |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 382 | } else { |
| 383 | // It there are multiple render nodes, they are laid out as follows: |
| 384 | // #0 - backdrop (content + caption) |
| 385 | // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop) |
| 386 | // #2 - additional overlay nodes |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 387 | // Usually the backdrop cannot be seen since it will be entirely covered by the content. |
| 388 | // While |
| 389 | // resizing however it might become partially visible. The following render loop will crop |
| 390 | // the |
| 391 | // backdrop against the content and draw the remaining part of it. It will then draw the |
| 392 | // content |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 393 | // cropped to the backdrop (since that indicates a shrinking of the window). |
| 394 | // |
| 395 | // Additional nodes will be drawn on top with no particular clipping semantics. |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 396 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 397 | // Usually the contents bounds should be mContentDrawBounds - however - we will |
| 398 | // move it towards the fixed edge to give it a more stable appearance (for the moment). |
| 399 | // 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] | 400 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 401 | // Backdrop bounds in render target space |
| 402 | const Rect backdrop = nodeBounds(*nodes[0]); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 403 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 404 | // Bounds that content will fill in render target space (note content node bounds may be |
| 405 | // bigger) |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 406 | Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight()); |
| 407 | content.translate(backdrop.left, backdrop.top); |
| 408 | if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) { |
| 409 | // Content doesn't entirely overlap backdrop, so fill around content (right/bottom) |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 410 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 411 | // 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] | 412 | // also fill left/top. Currently, both 2up and freeform position content at the top/left |
| 413 | // of |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 414 | // the backdrop, so this isn't necessary. |
| 415 | RenderNodeDrawable backdropNode(nodes[0].get(), canvas); |
| 416 | if (content.right < backdrop.right) { |
| 417 | // draw backdrop to right side of content |
| 418 | SkAutoCanvasRestore acr(canvas, true); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 419 | canvas->clipRect(SkRect::MakeLTRB(content.right, backdrop.top, backdrop.right, |
| 420 | backdrop.bottom)); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 421 | backdropNode.draw(canvas); |
| 422 | } |
| 423 | if (content.bottom < backdrop.bottom) { |
| 424 | // draw backdrop to bottom of content |
| 425 | // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill |
| 426 | SkAutoCanvasRestore acr(canvas, true); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 427 | canvas->clipRect(SkRect::MakeLTRB(content.left, content.bottom, content.right, |
| 428 | backdrop.bottom)); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 429 | backdropNode.draw(canvas); |
| 430 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 431 | } |
| 432 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 433 | RenderNodeDrawable contentNode(nodes[1].get(), canvas); |
| 434 | if (!backdrop.isEmpty()) { |
| 435 | // content node translation to catch up with backdrop |
| 436 | float dx = backdrop.left - contentDrawBounds.left; |
| 437 | float dy = backdrop.top - contentDrawBounds.top; |
| 438 | |
| 439 | SkAutoCanvasRestore acr(canvas, true); |
| 440 | canvas->translate(dx, dy); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 441 | const SkRect contentLocalClip = |
| 442 | SkRect::MakeXYWH(contentDrawBounds.left, contentDrawBounds.top, |
| 443 | backdrop.getWidth(), backdrop.getHeight()); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 444 | canvas->clipRect(contentLocalClip); |
| 445 | contentNode.draw(canvas); |
| 446 | } else { |
| 447 | SkAutoCanvasRestore acr(canvas, true); |
| 448 | contentNode.draw(canvas); |
| 449 | } |
| 450 | |
| 451 | // remaining overlay nodes, simply defer |
| 452 | for (size_t index = 2; index < nodes.size(); index++) { |
| 453 | if (!nodes[index]->nothingToDraw()) { |
| 454 | SkAutoCanvasRestore acr(canvas, true); |
| 455 | RenderNodeDrawable overlayNode(nodes[index].get(), canvas); |
| 456 | overlayNode.draw(canvas); |
| 457 | } |
| 458 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 459 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 460 | } |
| 461 | |
Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 462 | void SkiaPipeline::dumpResourceCacheUsage() const { |
| 463 | int resources, maxResources; |
| 464 | size_t bytes, maxBytes; |
| 465 | mRenderThread.getGrContext()->getResourceCacheUsage(&resources, &bytes); |
| 466 | mRenderThread.getGrContext()->getResourceCacheLimits(&maxResources, &maxBytes); |
| 467 | |
| 468 | SkString log("Resource Cache Usage:\n"); |
| 469 | log.appendf("%8d items out of %d maximum items\n", resources, maxResources); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 470 | log.appendf("%8zu bytes (%.2f MB) out of %.2f MB maximum\n", bytes, |
| 471 | 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] | 472 | |
| 473 | ALOGD("%s", log.c_str()); |
| 474 | } |
| 475 | |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 476 | void SkiaPipeline::setSurfaceColorProperties(ColorMode colorMode) { |
| 477 | if (colorMode == ColorMode::SRGB) { |
| 478 | mSurfaceColorType = SkColorType::kN32_SkColorType; |
| 479 | mSurfaceColorGamut = SkColorSpace::Gamut::kSRGB_Gamut; |
| 480 | mSurfaceColorSpace = SkColorSpace::MakeSRGB(); |
| 481 | } else if (colorMode == ColorMode::WideColorGamut) { |
| 482 | mSurfaceColorType = DeviceInfo::get()->getWideColorType(); |
| 483 | mSurfaceColorGamut = DeviceInfo::get()->getWideColorGamut(); |
| 484 | mSurfaceColorSpace = DeviceInfo::get()->getWideColorSpace(); |
| 485 | } else { |
| 486 | LOG_ALWAYS_FATAL("Unreachable: unsupported color mode."); |
| 487 | } |
| 488 | } |
| 489 | |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 490 | // Overdraw debugging |
| 491 | |
| 492 | // These colors should be kept in sync with Caches::getOverdrawColor() with a few differences. |
| 493 | // This implementation: |
| 494 | // (1) Requires transparent entries for "no overdraw" and "single draws". |
| 495 | // (2) Requires premul colors (instead of unpremul). |
| 496 | // (3) Requires RGBA colors (instead of BGRA). |
| 497 | static const uint32_t kOverdrawColors[2][6] = { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 498 | { |
| 499 | 0x00000000, 0x00000000, 0x2f2f0000, 0x2f002f00, 0x3f00003f, 0x7f00007f, |
| 500 | }, |
| 501 | { |
| 502 | 0x00000000, 0x00000000, 0x2f2f0000, 0x4f004f4f, 0x5f50335f, 0x7f00007f, |
| 503 | }, |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 504 | }; |
| 505 | |
| 506 | void SkiaPipeline::renderOverdraw(const LayerUpdateQueue& layers, const SkRect& clip, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 507 | const std::vector<sp<RenderNode>>& nodes, |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 508 | const Rect& contentDrawBounds, sk_sp<SkSurface> surface, |
| 509 | const SkMatrix& preTransform) { |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 510 | // Set up the overdraw canvas. |
| 511 | SkImageInfo offscreenInfo = SkImageInfo::MakeA8(surface->width(), surface->height()); |
| 512 | sk_sp<SkSurface> offscreen = surface->makeSurface(offscreenInfo); |
| 513 | SkOverdrawCanvas overdrawCanvas(offscreen->getCanvas()); |
| 514 | |
| 515 | // Fake a redraw to replay the draw commands. This will increment the alpha channel |
| 516 | // each time a pixel would have been drawn. |
| 517 | // Pass true for opaque so we skip the clear - the overdrawCanvas is already zero |
| 518 | // initialized. |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 519 | renderFrameImpl(layers, clip, nodes, true, contentDrawBounds, &overdrawCanvas, preTransform); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 520 | sk_sp<SkImage> counts = offscreen->makeImageSnapshot(); |
| 521 | |
| 522 | // Draw overdraw colors to the canvas. The color filter will convert counts to colors. |
| 523 | SkPaint paint; |
| 524 | const SkPMColor* colors = kOverdrawColors[static_cast<int>(Properties::overdrawColorSet)]; |
| 525 | paint.setColorFilter(SkOverdrawColorFilter::Make(colors)); |
| 526 | surface->getCanvas()->drawImage(counts.get(), 0.0f, 0.0f, &paint); |
| 527 | } |
| 528 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 529 | } /* namespace skiapipeline */ |
| 530 | } /* namespace uirenderer */ |
| 531 | } /* namespace android */ |