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