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 | |
| 19 | #include "utils/TraceUtils.h" |
Hal Canary | 10219fb | 2016-11-23 20:41:22 -0500 | [diff] [blame] | 20 | #include <SkImageEncoder.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> |
| 26 | #include <SkPixelSerializer.h> |
| 27 | #include <SkStream.h> |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 28 | #include "VectorDrawable.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 | |
| 38 | float SkiaPipeline::mLightRadius = 0; |
| 39 | uint8_t SkiaPipeline::mAmbientShadowAlpha = 0; |
| 40 | uint8_t SkiaPipeline::mSpotShadowAlpha = 0; |
| 41 | |
| 42 | Vector3 SkiaPipeline::mLightCenter = {FLT_MIN, FLT_MIN, FLT_MIN}; |
| 43 | |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 44 | SkiaPipeline::SkiaPipeline(RenderThread& thread) : mRenderThread(thread) { |
| 45 | mVectorDrawables.reserve(30); |
| 46 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 47 | |
| 48 | TaskManager* SkiaPipeline::getTaskManager() { |
| 49 | return &mTaskManager; |
| 50 | } |
| 51 | |
| 52 | void SkiaPipeline::onDestroyHardwareResources() { |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 53 | mRenderThread.cacheManager().trimStaleResources(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 54 | } |
| 55 | |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 56 | bool SkiaPipeline::pinImages(std::vector<SkImage*>& mutableImages) { |
| 57 | for (SkImage* image : mutableImages) { |
Derek Sollenberger | 189e874 | 2016-11-16 16:00:17 -0500 | [diff] [blame] | 58 | if (SkImage_pinAsTexture(image, mRenderThread.getGrContext())) { |
| 59 | mPinnedImages.emplace_back(sk_ref_sp(image)); |
| 60 | } else { |
| 61 | return false; |
| 62 | } |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 63 | } |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | void SkiaPipeline::unpinImages() { |
| 68 | for (auto& image : mPinnedImages) { |
| 69 | SkImage_unpinAsTexture(image.get(), mRenderThread.getGrContext()); |
| 70 | } |
| 71 | mPinnedImages.clear(); |
| 72 | } |
| 73 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 74 | void SkiaPipeline::renderLayers(const FrameBuilder::LightGeometry& lightGeometry, |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame] | 75 | LayerUpdateQueue* layerUpdateQueue, bool opaque, bool wideColorGamut, |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 76 | const BakedOpRenderer::LightInfo& lightInfo) { |
| 77 | updateLighting(lightGeometry, lightInfo); |
| 78 | ATRACE_NAME("draw layers"); |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 79 | renderVectorDrawableCache(); |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame] | 80 | renderLayersImpl(*layerUpdateQueue, opaque, wideColorGamut); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 81 | layerUpdateQueue->clear(); |
| 82 | } |
| 83 | |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame] | 84 | void SkiaPipeline::renderLayersImpl(const LayerUpdateQueue& layers, |
| 85 | bool opaque, bool wideColorGamut) { |
| 86 | // TODO: Handle wide color gamut |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 87 | // Render all layers that need to be updated, in order. |
| 88 | for (size_t i = 0; i < layers.entries().size(); i++) { |
John Reck | fc29f7acd | 2017-03-02 13:23:16 -0800 | [diff] [blame] | 89 | RenderNode* layerNode = layers.entries()[i].renderNode.get(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 90 | // only schedule repaint if node still on layer - possible it may have been |
| 91 | // removed during a dropped frame, but layers may still remain scheduled so |
| 92 | // as not to lose info on what portion is damaged |
| 93 | if (CC_LIKELY(layerNode->getLayerSurface() != nullptr)) { |
| 94 | SkASSERT(layerNode->getLayerSurface()); |
| 95 | SkASSERT(layerNode->getDisplayList()->isSkiaDL()); |
| 96 | SkiaDisplayList* displayList = (SkiaDisplayList*)layerNode->getDisplayList(); |
| 97 | if (!displayList || displayList->isEmpty()) { |
Stan Iliev | 18b388d | 2017-07-18 16:41:48 -0400 | [diff] [blame] | 98 | SkDEBUGF(("%p drawLayers(%s) : missing drawable", layerNode, layerNode->getName())); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | |
| 102 | const Rect& layerDamage = layers.entries()[i].damage; |
| 103 | |
| 104 | SkCanvas* layerCanvas = layerNode->getLayerSurface()->getCanvas(); |
| 105 | |
| 106 | int saveCount = layerCanvas->save(); |
| 107 | SkASSERT(saveCount == 1); |
| 108 | |
Stan Iliev | b66b8bb | 2016-12-15 18:17:42 -0500 | [diff] [blame] | 109 | layerCanvas->androidFramework_setDeviceClipRestriction(layerDamage.toSkIRect()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 110 | |
| 111 | auto savedLightCenter = mLightCenter; |
| 112 | // map current light center into RenderNode's coordinate space |
| 113 | layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(mLightCenter); |
| 114 | |
| 115 | const RenderProperties& properties = layerNode->properties(); |
| 116 | const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight()); |
| 117 | if (properties.getClipToBounds() && layerCanvas->quickReject(bounds)) { |
| 118 | return; |
| 119 | } |
| 120 | |
Matt Sarett | 79756be | 2016-11-09 16:13:54 -0500 | [diff] [blame] | 121 | layerNode->getSkiaLayer()->hasRenderedSinceRepaint = false; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 122 | layerCanvas->clear(SK_ColorTRANSPARENT); |
| 123 | |
| 124 | RenderNodeDrawable root(layerNode, layerCanvas, false); |
| 125 | root.forceDraw(layerCanvas); |
| 126 | layerCanvas->restoreToCount(saveCount); |
| 127 | layerCanvas->flush(); |
| 128 | mLightCenter = savedLightCenter; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | bool SkiaPipeline::createOrUpdateLayer(RenderNode* node, |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame] | 134 | const DamageAccumulator& damageAccumulator, bool wideColorGamut) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 135 | SkSurface* layer = node->getLayerSurface(); |
| 136 | if (!layer || layer->width() != node->getWidth() || layer->height() != node->getHeight()) { |
Stan Iliev | 08fc19a | 2017-07-24 10:20:33 -0400 | [diff] [blame] | 137 | SkImageInfo info; |
| 138 | if (wideColorGamut) { |
| 139 | info = SkImageInfo::Make(node->getWidth(), node->getHeight(), kRGBA_F16_SkColorType, |
| 140 | kPremul_SkAlphaType); |
| 141 | } else { |
| 142 | info = SkImageInfo::MakeN32Premul(node->getWidth(), node->getHeight()); |
| 143 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 144 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
| 145 | SkASSERT(mRenderThread.getGrContext() != nullptr); |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame] | 146 | // TODO: Handle wide color gamut requests |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 147 | node->setLayerSurface( |
| 148 | SkSurface::MakeRenderTarget(mRenderThread.getGrContext(), SkBudgeted::kYes, |
| 149 | info, 0, &props)); |
| 150 | if (node->getLayerSurface()) { |
| 151 | // update the transform in window of the layer to reset its origin wrt light source |
| 152 | // position |
| 153 | Matrix4 windowTransform; |
| 154 | damageAccumulator.computeCurrentTransform(&windowTransform); |
| 155 | node->getSkiaLayer()->inverseTransformInWindow = windowTransform; |
| 156 | } |
| 157 | return true; |
| 158 | } |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | void SkiaPipeline::destroyLayer(RenderNode* node) { |
| 163 | node->setLayerSurface(nullptr); |
| 164 | } |
| 165 | |
| 166 | void SkiaPipeline::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) { |
| 167 | GrContext* context = thread.getGrContext(); |
| 168 | if (context) { |
| 169 | ATRACE_FORMAT("Bitmap#prepareToDraw %dx%d", bitmap->width(), bitmap->height()); |
Derek Sollenberger | fb0c8fc | 2017-07-26 13:53:27 -0400 | [diff] [blame] | 170 | sk_sp<SkColorFilter> colorFilter; |
| 171 | auto image = bitmap->makeImage(&colorFilter); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 172 | if (image.get() && !bitmap->isHardware()) { |
| 173 | SkImage_pinAsTexture(image.get(), context); |
| 174 | SkImage_unpinAsTexture(image.get(), context); |
| 175 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | // Encodes to PNG, unless there is already encoded data, in which case that gets |
| 180 | // used. |
| 181 | class PngPixelSerializer : public SkPixelSerializer { |
| 182 | public: |
| 183 | bool onUseEncodedData(const void*, size_t) override { return true; } |
| 184 | SkData* onEncode(const SkPixmap& pixmap) override { |
Hal Canary | 10219fb | 2016-11-23 20:41:22 -0500 | [diff] [blame] | 185 | SkDynamicMemoryWStream buf; |
| 186 | return SkEncodeImage(&buf, pixmap, SkEncodedImageFormat::kPNG, 100) |
| 187 | ? buf.detachAsData().release() |
| 188 | : nullptr; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 189 | } |
| 190 | }; |
| 191 | |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 192 | void SkiaPipeline::renderVectorDrawableCache() { |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 193 | if (!mVectorDrawables.empty()) { |
| 194 | sp<VectorDrawableAtlas> atlas = mRenderThread.cacheManager().acquireVectorDrawableAtlas(); |
| 195 | auto grContext = mRenderThread.getGrContext(); |
| 196 | atlas->prepareForDraw(grContext); |
| 197 | for (auto vd : mVectorDrawables) { |
| 198 | vd->updateCache(atlas, grContext); |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 199 | } |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 200 | grContext->flush(); |
| 201 | mVectorDrawables.clear(); |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 202 | } |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 203 | } |
| 204 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 205 | void SkiaPipeline::renderFrame(const LayerUpdateQueue& layers, const SkRect& clip, |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame] | 206 | const std::vector<sp<RenderNode>>& nodes, bool opaque, bool wideColorGamut, |
| 207 | const Rect &contentDrawBounds, sk_sp<SkSurface> surface) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 208 | |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 209 | renderVectorDrawableCache(); |
| 210 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 211 | // draw all layers up front |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame] | 212 | renderLayersImpl(layers, opaque, wideColorGamut); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 213 | |
| 214 | // initialize the canvas for the current frame |
| 215 | SkCanvas* canvas = surface->getCanvas(); |
| 216 | |
| 217 | std::unique_ptr<SkPictureRecorder> recorder; |
| 218 | bool recordingPicture = false; |
| 219 | char prop[PROPERTY_VALUE_MAX]; |
| 220 | if (skpCaptureEnabled()) { |
| 221 | property_get("debug.hwui.capture_frame_as_skp", prop, "0"); |
Ben Wagner | 3aeda5c | 2017-03-17 17:22:01 -0400 | [diff] [blame] | 222 | recordingPicture = prop[0] != '0' && access(prop, F_OK) != 0; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 223 | if (recordingPicture) { |
| 224 | recorder.reset(new SkPictureRecorder()); |
| 225 | canvas = recorder->beginRecording(surface->width(), surface->height(), |
| 226 | nullptr, SkPictureRecorder::kPlaybackDrawPicture_RecordFlag); |
| 227 | } |
| 228 | } |
| 229 | |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame] | 230 | renderFrameImpl(layers, clip, nodes, opaque, wideColorGamut, contentDrawBounds, canvas); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 231 | |
| 232 | if (skpCaptureEnabled() && recordingPicture) { |
| 233 | sk_sp<SkPicture> picture = recorder->finishRecordingAsPicture(); |
| 234 | if (picture->approximateOpCount() > 0) { |
| 235 | SkFILEWStream stream(prop); |
| 236 | if (stream.isValid()) { |
| 237 | PngPixelSerializer serializer; |
| 238 | picture->serialize(&stream, &serializer); |
| 239 | stream.flush(); |
| 240 | SkDebugf("Captured Drawing Output (%d bytes) for frame. %s", stream.bytesWritten(), prop); |
| 241 | } |
| 242 | } |
| 243 | surface->getCanvas()->drawPicture(picture); |
| 244 | } |
| 245 | |
| 246 | if (CC_UNLIKELY(Properties::debugOverdraw)) { |
| 247 | renderOverdraw(layers, clip, nodes, contentDrawBounds, surface); |
| 248 | } |
| 249 | |
| 250 | ATRACE_NAME("flush commands"); |
| 251 | canvas->flush(); |
| 252 | } |
| 253 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 254 | namespace { |
| 255 | static Rect nodeBounds(RenderNode& node) { |
| 256 | auto& props = node.properties(); |
| 257 | return Rect(props.getLeft(), props.getTop(), |
| 258 | props.getRight(), props.getBottom()); |
| 259 | } |
| 260 | } |
| 261 | |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 262 | void SkiaPipeline::renderFrameImpl(const LayerUpdateQueue& layers, const SkRect& clip, |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame] | 263 | const std::vector<sp<RenderNode>>& nodes, bool opaque, bool wideColorGamut, |
| 264 | const Rect &contentDrawBounds, SkCanvas* canvas) { |
Stan Iliev | b66b8bb | 2016-12-15 18:17:42 -0500 | [diff] [blame] | 265 | SkAutoCanvasRestore saver(canvas, true); |
| 266 | canvas->androidFramework_setDeviceClipRestriction(clip.roundOut()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 267 | |
| 268 | if (!opaque) { |
| 269 | canvas->clear(SK_ColorTRANSPARENT); |
| 270 | } |
| 271 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 272 | if (1 == nodes.size()) { |
| 273 | if (!nodes[0]->nothingToDraw()) { |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 274 | RenderNodeDrawable root(nodes[0].get(), canvas); |
| 275 | root.draw(canvas); |
| 276 | } |
| 277 | } else if (0 == nodes.size()) { |
| 278 | //nothing to draw |
| 279 | } else { |
| 280 | // It there are multiple render nodes, they are laid out as follows: |
| 281 | // #0 - backdrop (content + caption) |
| 282 | // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop) |
| 283 | // #2 - additional overlay nodes |
| 284 | // Usually the backdrop cannot be seen since it will be entirely covered by the content. While |
| 285 | // resizing however it might become partially visible. The following render loop will crop the |
| 286 | // backdrop against the content and draw the remaining part of it. It will then draw the content |
| 287 | // cropped to the backdrop (since that indicates a shrinking of the window). |
| 288 | // |
| 289 | // Additional nodes will be drawn on top with no particular clipping semantics. |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 290 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 291 | // Usually the contents bounds should be mContentDrawBounds - however - we will |
| 292 | // move it towards the fixed edge to give it a more stable appearance (for the moment). |
| 293 | // 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] | 294 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 295 | // Backdrop bounds in render target space |
| 296 | const Rect backdrop = nodeBounds(*nodes[0]); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 297 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 298 | // Bounds that content will fill in render target space (note content node bounds may be bigger) |
| 299 | Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight()); |
| 300 | content.translate(backdrop.left, backdrop.top); |
| 301 | if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) { |
| 302 | // Content doesn't entirely overlap backdrop, so fill around content (right/bottom) |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 303 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 304 | // Note: in the future, if content doesn't snap to backdrop's left/top, this may need to |
| 305 | // also fill left/top. Currently, both 2up and freeform position content at the top/left of |
| 306 | // the backdrop, so this isn't necessary. |
| 307 | RenderNodeDrawable backdropNode(nodes[0].get(), canvas); |
| 308 | if (content.right < backdrop.right) { |
| 309 | // draw backdrop to right side of content |
| 310 | SkAutoCanvasRestore acr(canvas, true); |
| 311 | canvas->clipRect(SkRect::MakeLTRB(content.right, backdrop.top, |
| 312 | backdrop.right, backdrop.bottom)); |
| 313 | backdropNode.draw(canvas); |
| 314 | } |
| 315 | if (content.bottom < backdrop.bottom) { |
| 316 | // draw backdrop to bottom of content |
| 317 | // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill |
| 318 | SkAutoCanvasRestore acr(canvas, true); |
| 319 | canvas->clipRect(SkRect::MakeLTRB(content.left, content.bottom, |
| 320 | content.right, backdrop.bottom)); |
| 321 | backdropNode.draw(canvas); |
| 322 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 323 | } |
| 324 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 325 | RenderNodeDrawable contentNode(nodes[1].get(), canvas); |
| 326 | if (!backdrop.isEmpty()) { |
| 327 | // content node translation to catch up with backdrop |
| 328 | float dx = backdrop.left - contentDrawBounds.left; |
| 329 | float dy = backdrop.top - contentDrawBounds.top; |
| 330 | |
| 331 | SkAutoCanvasRestore acr(canvas, true); |
| 332 | canvas->translate(dx, dy); |
| 333 | const SkRect contentLocalClip = SkRect::MakeXYWH(contentDrawBounds.left, |
| 334 | contentDrawBounds.top, backdrop.getWidth(), backdrop.getHeight()); |
| 335 | canvas->clipRect(contentLocalClip); |
| 336 | contentNode.draw(canvas); |
| 337 | } else { |
| 338 | SkAutoCanvasRestore acr(canvas, true); |
| 339 | contentNode.draw(canvas); |
| 340 | } |
| 341 | |
| 342 | // remaining overlay nodes, simply defer |
| 343 | for (size_t index = 2; index < nodes.size(); index++) { |
| 344 | if (!nodes[index]->nothingToDraw()) { |
| 345 | SkAutoCanvasRestore acr(canvas, true); |
| 346 | RenderNodeDrawable overlayNode(nodes[index].get(), canvas); |
| 347 | overlayNode.draw(canvas); |
| 348 | } |
| 349 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 350 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 351 | } |
| 352 | |
Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 353 | void SkiaPipeline::dumpResourceCacheUsage() const { |
| 354 | int resources, maxResources; |
| 355 | size_t bytes, maxBytes; |
| 356 | mRenderThread.getGrContext()->getResourceCacheUsage(&resources, &bytes); |
| 357 | mRenderThread.getGrContext()->getResourceCacheLimits(&maxResources, &maxBytes); |
| 358 | |
| 359 | SkString log("Resource Cache Usage:\n"); |
| 360 | log.appendf("%8d items out of %d maximum items\n", resources, maxResources); |
| 361 | log.appendf("%8zu bytes (%.2f MB) out of %.2f MB maximum\n", |
| 362 | bytes, bytes * (1.0f / (1024.0f * 1024.0f)), maxBytes * (1.0f / (1024.0f * 1024.0f))); |
| 363 | |
| 364 | ALOGD("%s", log.c_str()); |
| 365 | } |
| 366 | |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 367 | // Overdraw debugging |
| 368 | |
| 369 | // These colors should be kept in sync with Caches::getOverdrawColor() with a few differences. |
| 370 | // This implementation: |
| 371 | // (1) Requires transparent entries for "no overdraw" and "single draws". |
| 372 | // (2) Requires premul colors (instead of unpremul). |
| 373 | // (3) Requires RGBA colors (instead of BGRA). |
| 374 | static const uint32_t kOverdrawColors[2][6] = { |
| 375 | { 0x00000000, 0x00000000, 0x2f2f0000, 0x2f002f00, 0x3f00003f, 0x7f00007f, }, |
| 376 | { 0x00000000, 0x00000000, 0x2f2f0000, 0x4f004f4f, 0x5f50335f, 0x7f00007f, }, |
| 377 | }; |
| 378 | |
| 379 | void SkiaPipeline::renderOverdraw(const LayerUpdateQueue& layers, const SkRect& clip, |
| 380 | const std::vector<sp<RenderNode>>& nodes, const Rect &contentDrawBounds, |
| 381 | sk_sp<SkSurface> surface) { |
| 382 | // Set up the overdraw canvas. |
| 383 | SkImageInfo offscreenInfo = SkImageInfo::MakeA8(surface->width(), surface->height()); |
| 384 | sk_sp<SkSurface> offscreen = surface->makeSurface(offscreenInfo); |
| 385 | SkOverdrawCanvas overdrawCanvas(offscreen->getCanvas()); |
| 386 | |
| 387 | // Fake a redraw to replay the draw commands. This will increment the alpha channel |
| 388 | // each time a pixel would have been drawn. |
| 389 | // Pass true for opaque so we skip the clear - the overdrawCanvas is already zero |
| 390 | // initialized. |
Romain Guy | 07ae505 | 2017-06-13 18:25:32 -0700 | [diff] [blame] | 391 | renderFrameImpl(layers, clip, nodes, true, false, contentDrawBounds, &overdrawCanvas); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 392 | sk_sp<SkImage> counts = offscreen->makeImageSnapshot(); |
| 393 | |
| 394 | // Draw overdraw colors to the canvas. The color filter will convert counts to colors. |
| 395 | SkPaint paint; |
| 396 | const SkPMColor* colors = kOverdrawColors[static_cast<int>(Properties::overdrawColorSet)]; |
| 397 | paint.setColorFilter(SkOverdrawColorFilter::Make(colors)); |
| 398 | surface->getCanvas()->drawImage(counts.get(), 0.0f, 0.0f, &paint); |
| 399 | } |
| 400 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 401 | } /* namespace skiapipeline */ |
| 402 | } /* namespace uirenderer */ |
| 403 | } /* namespace android */ |