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> |
| 25 | #include <SkPixelSerializer.h> |
| 26 | #include <SkStream.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 | 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 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 84 | void SkiaPipeline::renderLayers(const FrameBuilder::LightGeometry& lightGeometry, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 85 | LayerUpdateQueue* layerUpdateQueue, bool opaque, |
| 86 | bool wideColorGamut, const BakedOpRenderer::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()); |
| 106 | SkASSERT(layerNode->getDisplayList()->isSkiaDL()); |
| 107 | SkiaDisplayList* displayList = (SkiaDisplayList*)layerNode->getDisplayList(); |
| 108 | if (!displayList || displayList->isEmpty()) { |
Stan Iliev | 18b388d | 2017-07-18 16:41:48 -0400 | [diff] [blame] | 109 | SkDEBUGF(("%p drawLayers(%s) : missing drawable", layerNode, layerNode->getName())); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | |
| 113 | const Rect& layerDamage = layers.entries()[i].damage; |
| 114 | |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 115 | SkCanvas* layerCanvas = tryCapture(layerNode->getLayerSurface()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 116 | |
| 117 | int saveCount = layerCanvas->save(); |
| 118 | SkASSERT(saveCount == 1); |
| 119 | |
Stan Iliev | b66b8bb | 2016-12-15 18:17:42 -0500 | [diff] [blame] | 120 | layerCanvas->androidFramework_setDeviceClipRestriction(layerDamage.toSkIRect()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 121 | |
| 122 | auto savedLightCenter = mLightCenter; |
| 123 | // map current light center into RenderNode's coordinate space |
| 124 | layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(mLightCenter); |
| 125 | |
| 126 | const RenderProperties& properties = layerNode->properties(); |
| 127 | const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight()); |
| 128 | if (properties.getClipToBounds() && layerCanvas->quickReject(bounds)) { |
| 129 | return; |
| 130 | } |
| 131 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 132 | ATRACE_FORMAT("drawLayer [%s] %.1f x %.1f", layerNode->getName(), bounds.width(), |
| 133 | bounds.height()); |
Derek Sollenberger | 9552c2c | 2017-08-31 15:37:52 -0400 | [diff] [blame] | 134 | |
Matt Sarett | 79756be | 2016-11-09 16:13:54 -0500 | [diff] [blame] | 135 | layerNode->getSkiaLayer()->hasRenderedSinceRepaint = false; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 136 | layerCanvas->clear(SK_ColorTRANSPARENT); |
| 137 | |
| 138 | RenderNodeDrawable root(layerNode, layerCanvas, false); |
| 139 | root.forceDraw(layerCanvas); |
| 140 | layerCanvas->restoreToCount(saveCount); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 141 | mLightCenter = savedLightCenter; |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 142 | |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 143 | endCapture(layerNode->getLayerSurface()); |
| 144 | |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 145 | // cache the current context so that we can defer flushing it until |
| 146 | // either all the layers have been rendered or the context changes |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 147 | GrContext* currentContext = layerNode->getLayerSurface()->getCanvas()->getGrContext(); |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 148 | if (cachedContext.get() != currentContext) { |
| 149 | if (cachedContext.get()) { |
| 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()) { |
| 158 | cachedContext->flush(); |
| 159 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 160 | } |
| 161 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 162 | bool SkiaPipeline::createOrUpdateLayer(RenderNode* node, const DamageAccumulator& damageAccumulator, |
| 163 | bool wideColorGamut) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 164 | SkSurface* layer = node->getLayerSurface(); |
| 165 | if (!layer || layer->width() != node->getWidth() || layer->height() != node->getHeight()) { |
Stan Iliev | 08fc19a | 2017-07-24 10:20:33 -0400 | [diff] [blame] | 166 | SkImageInfo info; |
| 167 | if (wideColorGamut) { |
| 168 | info = SkImageInfo::Make(node->getWidth(), node->getHeight(), kRGBA_F16_SkColorType, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 169 | kPremul_SkAlphaType); |
Stan Iliev | 08fc19a | 2017-07-24 10:20:33 -0400 | [diff] [blame] | 170 | } else { |
| 171 | info = SkImageInfo::MakeN32Premul(node->getWidth(), node->getHeight()); |
| 172 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 173 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
| 174 | SkASSERT(mRenderThread.getGrContext() != nullptr); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 175 | node->setLayerSurface(SkSurface::MakeRenderTarget(mRenderThread.getGrContext(), |
| 176 | SkBudgeted::kYes, info, 0, &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; |
| 183 | } |
| 184 | return true; |
| 185 | } |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | void SkiaPipeline::destroyLayer(RenderNode* node) { |
| 190 | node->setLayerSurface(nullptr); |
| 191 | } |
| 192 | |
| 193 | void SkiaPipeline::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) { |
| 194 | GrContext* context = thread.getGrContext(); |
| 195 | if (context) { |
| 196 | ATRACE_FORMAT("Bitmap#prepareToDraw %dx%d", bitmap->width(), bitmap->height()); |
Derek Sollenberger | fb0c8fc | 2017-07-26 13:53:27 -0400 | [diff] [blame] | 197 | sk_sp<SkColorFilter> colorFilter; |
| 198 | auto image = bitmap->makeImage(&colorFilter); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 199 | if (image.get() && !bitmap->isHardware()) { |
| 200 | SkImage_pinAsTexture(image.get(), context); |
| 201 | SkImage_unpinAsTexture(image.get(), context); |
| 202 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| 206 | // Encodes to PNG, unless there is already encoded data, in which case that gets |
| 207 | // used. |
| 208 | class PngPixelSerializer : public SkPixelSerializer { |
| 209 | public: |
| 210 | bool onUseEncodedData(const void*, size_t) override { return true; } |
| 211 | SkData* onEncode(const SkPixmap& pixmap) override { |
Hal Canary | 10219fb | 2016-11-23 20:41:22 -0500 | [diff] [blame] | 212 | SkDynamicMemoryWStream buf; |
| 213 | return SkEncodeImage(&buf, pixmap, SkEncodedImageFormat::kPNG, 100) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 214 | ? buf.detachAsData().release() |
| 215 | : nullptr; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 216 | } |
| 217 | }; |
| 218 | |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 219 | void SkiaPipeline::renderVectorDrawableCache() { |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 220 | if (!mVectorDrawables.empty()) { |
| 221 | sp<VectorDrawableAtlas> atlas = mRenderThread.cacheManager().acquireVectorDrawableAtlas(); |
| 222 | auto grContext = mRenderThread.getGrContext(); |
| 223 | atlas->prepareForDraw(grContext); |
Derek Sollenberger | 9552c2c | 2017-08-31 15:37:52 -0400 | [diff] [blame] | 224 | ATRACE_NAME("Update VectorDrawables"); |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 225 | for (auto vd : mVectorDrawables) { |
| 226 | vd->updateCache(atlas, grContext); |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 227 | } |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 228 | mVectorDrawables.clear(); |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 229 | } |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 230 | } |
| 231 | |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 232 | class SkiaPipeline::SavePictureProcessor : public TaskProcessor<bool> { |
| 233 | public: |
| 234 | explicit SavePictureProcessor(TaskManager* taskManager) : TaskProcessor<bool>(taskManager) {} |
| 235 | |
| 236 | struct SavePictureTask : public Task<bool> { |
| 237 | sk_sp<SkData> data; |
| 238 | std::string filename; |
| 239 | }; |
| 240 | |
| 241 | void savePicture(const sk_sp<SkData>& data, const std::string& filename) { |
| 242 | sp<SavePictureTask> task(new SavePictureTask()); |
| 243 | task->data = data; |
| 244 | task->filename = filename; |
| 245 | TaskProcessor<bool>::add(task); |
| 246 | } |
| 247 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 248 | virtual void onProcess(const sp<Task<bool>>& task) override { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 249 | SavePictureTask* t = static_cast<SavePictureTask*>(task.get()); |
| 250 | |
| 251 | if (0 == access(t->filename.c_str(), F_OK)) { |
| 252 | task->setResult(false); |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | SkFILEWStream stream(t->filename.c_str()); |
| 257 | if (stream.isValid()) { |
| 258 | stream.write(t->data->data(), t->data->size()); |
| 259 | stream.flush(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 260 | SkDebugf("SKP Captured Drawing Output (%d bytes) for frame. %s", stream.bytesWritten(), |
| 261 | t->filename.c_str()); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | task->setResult(true); |
| 265 | } |
| 266 | }; |
| 267 | |
| 268 | SkCanvas* SkiaPipeline::tryCapture(SkSurface* surface) { |
| 269 | if (CC_UNLIKELY(Properties::skpCaptureEnabled)) { |
| 270 | bool recordingPicture = mCaptureSequence > 0; |
| 271 | char prop[PROPERTY_VALUE_MAX] = {'\0'}; |
| 272 | if (!recordingPicture) { |
| 273 | property_get(PROPERTY_CAPTURE_SKP_FILENAME, prop, "0"); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 274 | recordingPicture = prop[0] != '0' && |
| 275 | mCapturedFile != prop; // ensure we capture only once per filename |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 276 | if (recordingPicture) { |
| 277 | mCapturedFile = prop; |
| 278 | mCaptureSequence = property_get_int32(PROPERTY_CAPTURE_SKP_FRAMES, 1); |
| 279 | } |
| 280 | } |
| 281 | if (recordingPicture) { |
| 282 | mRecorder.reset(new SkPictureRecorder()); |
| 283 | return mRecorder->beginRecording(surface->width(), surface->height(), nullptr, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 284 | SkPictureRecorder::kPlaybackDrawPicture_RecordFlag); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | return surface->getCanvas(); |
| 288 | } |
| 289 | |
| 290 | void SkiaPipeline::endCapture(SkSurface* surface) { |
| 291 | if (CC_UNLIKELY(mRecorder.get())) { |
| 292 | sk_sp<SkPicture> picture = mRecorder->finishRecordingAsPicture(); |
| 293 | surface->getCanvas()->drawPicture(picture); |
| 294 | if (picture->approximateOpCount() > 0) { |
| 295 | SkDynamicMemoryWStream stream; |
| 296 | PngPixelSerializer serializer; |
| 297 | picture->serialize(&stream, &serializer); |
| 298 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 299 | // offload saving to file in a different thread |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 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(stream.detachAsData(), mCapturedFile); |
| 307 | } else { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 308 | mSavePictureProcessor->savePicture( |
| 309 | stream.detachAsData(), |
| 310 | mCapturedFile + "_" + std::to_string(mCaptureSequence)); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 311 | } |
| 312 | mCaptureSequence--; |
| 313 | } |
| 314 | mRecorder.reset(); |
| 315 | } |
| 316 | } |
| 317 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 318 | void SkiaPipeline::renderFrame(const LayerUpdateQueue& layers, const SkRect& clip, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 319 | const std::vector<sp<RenderNode>>& nodes, bool opaque, |
| 320 | bool wideColorGamut, const Rect& contentDrawBounds, |
| 321 | sk_sp<SkSurface> surface) { |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 322 | renderVectorDrawableCache(); |
| 323 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 324 | // draw all layers up front |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame] | 325 | renderLayersImpl(layers, opaque, wideColorGamut); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 326 | |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 327 | // initialize the canvas for the current frame, that might be a recording canvas if SKP |
| 328 | // capture is enabled. |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 329 | std::unique_ptr<SkPictureRecorder> recorder; |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 330 | SkCanvas* canvas = tryCapture(surface.get()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 331 | |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame] | 332 | renderFrameImpl(layers, clip, nodes, opaque, wideColorGamut, contentDrawBounds, canvas); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 333 | |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 334 | endCapture(surface.get()); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 335 | |
| 336 | if (CC_UNLIKELY(Properties::debugOverdraw)) { |
| 337 | renderOverdraw(layers, clip, nodes, contentDrawBounds, surface); |
| 338 | } |
| 339 | |
| 340 | ATRACE_NAME("flush commands"); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 341 | surface->getCanvas()->flush(); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 342 | } |
| 343 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 344 | namespace { |
| 345 | static Rect nodeBounds(RenderNode& node) { |
| 346 | auto& props = node.properties(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 347 | return Rect(props.getLeft(), props.getTop(), props.getRight(), props.getBottom()); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 351 | void SkiaPipeline::renderFrameImpl(const LayerUpdateQueue& layers, const SkRect& clip, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 352 | const std::vector<sp<RenderNode>>& nodes, bool opaque, |
| 353 | bool wideColorGamut, const Rect& contentDrawBounds, |
| 354 | SkCanvas* canvas) { |
Stan Iliev | b66b8bb | 2016-12-15 18:17:42 -0500 | [diff] [blame] | 355 | SkAutoCanvasRestore saver(canvas, true); |
| 356 | canvas->androidFramework_setDeviceClipRestriction(clip.roundOut()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 357 | |
| 358 | if (!opaque) { |
| 359 | canvas->clear(SK_ColorTRANSPARENT); |
| 360 | } |
| 361 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 362 | if (1 == nodes.size()) { |
| 363 | if (!nodes[0]->nothingToDraw()) { |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 364 | RenderNodeDrawable root(nodes[0].get(), canvas); |
| 365 | root.draw(canvas); |
| 366 | } |
| 367 | } else if (0 == nodes.size()) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 368 | // nothing to draw |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 369 | } else { |
| 370 | // It there are multiple render nodes, they are laid out as follows: |
| 371 | // #0 - backdrop (content + caption) |
| 372 | // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop) |
| 373 | // #2 - additional overlay nodes |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 374 | // Usually the backdrop cannot be seen since it will be entirely covered by the content. |
| 375 | // While |
| 376 | // resizing however it might become partially visible. The following render loop will crop |
| 377 | // the |
| 378 | // backdrop against the content and draw the remaining part of it. It will then draw the |
| 379 | // content |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 380 | // cropped to the backdrop (since that indicates a shrinking of the window). |
| 381 | // |
| 382 | // Additional nodes will be drawn on top with no particular clipping semantics. |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 383 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 384 | // Usually the contents bounds should be mContentDrawBounds - however - we will |
| 385 | // move it towards the fixed edge to give it a more stable appearance (for the moment). |
| 386 | // 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] | 387 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 388 | // Backdrop bounds in render target space |
| 389 | const Rect backdrop = nodeBounds(*nodes[0]); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 390 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 391 | // Bounds that content will fill in render target space (note content node bounds may be |
| 392 | // bigger) |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 393 | Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight()); |
| 394 | content.translate(backdrop.left, backdrop.top); |
| 395 | if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) { |
| 396 | // Content doesn't entirely overlap backdrop, so fill around content (right/bottom) |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 397 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 398 | // 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^] | 399 | // also fill left/top. Currently, both 2up and freeform position content at the top/left |
| 400 | // of |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 401 | // the backdrop, so this isn't necessary. |
| 402 | RenderNodeDrawable backdropNode(nodes[0].get(), canvas); |
| 403 | if (content.right < backdrop.right) { |
| 404 | // draw backdrop to right side of content |
| 405 | SkAutoCanvasRestore acr(canvas, true); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 406 | canvas->clipRect(SkRect::MakeLTRB(content.right, backdrop.top, backdrop.right, |
| 407 | backdrop.bottom)); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 408 | backdropNode.draw(canvas); |
| 409 | } |
| 410 | if (content.bottom < backdrop.bottom) { |
| 411 | // draw backdrop to bottom of content |
| 412 | // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill |
| 413 | SkAutoCanvasRestore acr(canvas, true); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 414 | canvas->clipRect(SkRect::MakeLTRB(content.left, content.bottom, content.right, |
| 415 | backdrop.bottom)); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 416 | backdropNode.draw(canvas); |
| 417 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 418 | } |
| 419 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 420 | RenderNodeDrawable contentNode(nodes[1].get(), canvas); |
| 421 | if (!backdrop.isEmpty()) { |
| 422 | // content node translation to catch up with backdrop |
| 423 | float dx = backdrop.left - contentDrawBounds.left; |
| 424 | float dy = backdrop.top - contentDrawBounds.top; |
| 425 | |
| 426 | SkAutoCanvasRestore acr(canvas, true); |
| 427 | canvas->translate(dx, dy); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 428 | const SkRect contentLocalClip = |
| 429 | SkRect::MakeXYWH(contentDrawBounds.left, contentDrawBounds.top, |
| 430 | backdrop.getWidth(), backdrop.getHeight()); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 431 | canvas->clipRect(contentLocalClip); |
| 432 | contentNode.draw(canvas); |
| 433 | } else { |
| 434 | SkAutoCanvasRestore acr(canvas, true); |
| 435 | contentNode.draw(canvas); |
| 436 | } |
| 437 | |
| 438 | // remaining overlay nodes, simply defer |
| 439 | for (size_t index = 2; index < nodes.size(); index++) { |
| 440 | if (!nodes[index]->nothingToDraw()) { |
| 441 | SkAutoCanvasRestore acr(canvas, true); |
| 442 | RenderNodeDrawable overlayNode(nodes[index].get(), canvas); |
| 443 | overlayNode.draw(canvas); |
| 444 | } |
| 445 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 446 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 447 | } |
| 448 | |
Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 449 | void SkiaPipeline::dumpResourceCacheUsage() const { |
| 450 | int resources, maxResources; |
| 451 | size_t bytes, maxBytes; |
| 452 | mRenderThread.getGrContext()->getResourceCacheUsage(&resources, &bytes); |
| 453 | mRenderThread.getGrContext()->getResourceCacheLimits(&maxResources, &maxBytes); |
| 454 | |
| 455 | SkString log("Resource Cache Usage:\n"); |
| 456 | log.appendf("%8d items out of %d maximum items\n", resources, maxResources); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 457 | log.appendf("%8zu bytes (%.2f MB) out of %.2f MB maximum\n", bytes, |
| 458 | 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] | 459 | |
| 460 | ALOGD("%s", log.c_str()); |
| 461 | } |
| 462 | |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 463 | // Overdraw debugging |
| 464 | |
| 465 | // These colors should be kept in sync with Caches::getOverdrawColor() with a few differences. |
| 466 | // This implementation: |
| 467 | // (1) Requires transparent entries for "no overdraw" and "single draws". |
| 468 | // (2) Requires premul colors (instead of unpremul). |
| 469 | // (3) Requires RGBA colors (instead of BGRA). |
| 470 | static const uint32_t kOverdrawColors[2][6] = { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 471 | { |
| 472 | 0x00000000, 0x00000000, 0x2f2f0000, 0x2f002f00, 0x3f00003f, 0x7f00007f, |
| 473 | }, |
| 474 | { |
| 475 | 0x00000000, 0x00000000, 0x2f2f0000, 0x4f004f4f, 0x5f50335f, 0x7f00007f, |
| 476 | }, |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 477 | }; |
| 478 | |
| 479 | void SkiaPipeline::renderOverdraw(const LayerUpdateQueue& layers, const SkRect& clip, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 480 | const std::vector<sp<RenderNode>>& nodes, |
| 481 | const Rect& contentDrawBounds, sk_sp<SkSurface> surface) { |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 482 | // Set up the overdraw canvas. |
| 483 | SkImageInfo offscreenInfo = SkImageInfo::MakeA8(surface->width(), surface->height()); |
| 484 | sk_sp<SkSurface> offscreen = surface->makeSurface(offscreenInfo); |
| 485 | SkOverdrawCanvas overdrawCanvas(offscreen->getCanvas()); |
| 486 | |
| 487 | // Fake a redraw to replay the draw commands. This will increment the alpha channel |
| 488 | // each time a pixel would have been drawn. |
| 489 | // Pass true for opaque so we skip the clear - the overdrawCanvas is already zero |
| 490 | // initialized. |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame] | 491 | renderFrameImpl(layers, clip, nodes, true, false, contentDrawBounds, &overdrawCanvas); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 492 | sk_sp<SkImage> counts = offscreen->makeImageSnapshot(); |
| 493 | |
| 494 | // Draw overdraw colors to the canvas. The color filter will convert counts to colors. |
| 495 | SkPaint paint; |
| 496 | const SkPMColor* colors = kOverdrawColors[static_cast<int>(Properties::overdrawColorSet)]; |
| 497 | paint.setColorFilter(SkOverdrawColorFilter::Make(colors)); |
| 498 | surface->getCanvas()->drawImage(counts.get(), 0.0f, 0.0f, &paint); |
| 499 | } |
| 500 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 501 | } /* namespace skiapipeline */ |
| 502 | } /* namespace uirenderer */ |
| 503 | } /* namespace android */ |