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" |
| 20 | #include <SkOSFile.h> |
| 21 | #include <SkPicture.h> |
| 22 | #include <SkPictureRecorder.h> |
| 23 | #include <SkPixelSerializer.h> |
| 24 | #include <SkStream.h> |
| 25 | |
| 26 | using namespace android::uirenderer::renderthread; |
| 27 | |
| 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | namespace skiapipeline { |
| 31 | |
| 32 | float SkiaPipeline::mLightRadius = 0; |
| 33 | uint8_t SkiaPipeline::mAmbientShadowAlpha = 0; |
| 34 | uint8_t SkiaPipeline::mSpotShadowAlpha = 0; |
| 35 | |
| 36 | Vector3 SkiaPipeline::mLightCenter = {FLT_MIN, FLT_MIN, FLT_MIN}; |
| 37 | |
| 38 | SkiaPipeline::SkiaPipeline(RenderThread& thread) : mRenderThread(thread) { } |
| 39 | |
| 40 | TaskManager* SkiaPipeline::getTaskManager() { |
| 41 | return &mTaskManager; |
| 42 | } |
| 43 | |
| 44 | void SkiaPipeline::onDestroyHardwareResources() { |
| 45 | // No need to flush the caches here. There is a timer |
| 46 | // which will flush temporary resources over time. |
| 47 | } |
| 48 | |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 49 | bool SkiaPipeline::pinImages(std::vector<SkImage*>& mutableImages) { |
| 50 | for (SkImage* image : mutableImages) { |
| 51 | mPinnedImages.emplace_back(sk_ref_sp(image)); |
| 52 | // TODO: return false if texture creation fails (see b/32691999) |
| 53 | SkImage_pinAsTexture(image, mRenderThread.getGrContext()); |
| 54 | } |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | void SkiaPipeline::unpinImages() { |
| 59 | for (auto& image : mPinnedImages) { |
| 60 | SkImage_unpinAsTexture(image.get(), mRenderThread.getGrContext()); |
| 61 | } |
| 62 | mPinnedImages.clear(); |
| 63 | } |
| 64 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 65 | void SkiaPipeline::renderLayers(const FrameBuilder::LightGeometry& lightGeometry, |
| 66 | LayerUpdateQueue* layerUpdateQueue, bool opaque, |
| 67 | const BakedOpRenderer::LightInfo& lightInfo) { |
| 68 | updateLighting(lightGeometry, lightInfo); |
| 69 | ATRACE_NAME("draw layers"); |
| 70 | renderLayersImpl(*layerUpdateQueue, opaque); |
| 71 | layerUpdateQueue->clear(); |
| 72 | } |
| 73 | |
| 74 | void SkiaPipeline::renderLayersImpl(const LayerUpdateQueue& layers, bool opaque) { |
| 75 | // Render all layers that need to be updated, in order. |
| 76 | for (size_t i = 0; i < layers.entries().size(); i++) { |
| 77 | RenderNode* layerNode = layers.entries()[i].renderNode; |
| 78 | // only schedule repaint if node still on layer - possible it may have been |
| 79 | // removed during a dropped frame, but layers may still remain scheduled so |
| 80 | // as not to lose info on what portion is damaged |
| 81 | if (CC_LIKELY(layerNode->getLayerSurface() != nullptr)) { |
| 82 | SkASSERT(layerNode->getLayerSurface()); |
| 83 | SkASSERT(layerNode->getDisplayList()->isSkiaDL()); |
| 84 | SkiaDisplayList* displayList = (SkiaDisplayList*)layerNode->getDisplayList(); |
| 85 | if (!displayList || displayList->isEmpty()) { |
| 86 | SkDEBUGF(("%p drawLayers(%s) : missing drawable", this, layerNode->getName())); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | const Rect& layerDamage = layers.entries()[i].damage; |
| 91 | |
| 92 | SkCanvas* layerCanvas = layerNode->getLayerSurface()->getCanvas(); |
| 93 | |
| 94 | int saveCount = layerCanvas->save(); |
| 95 | SkASSERT(saveCount == 1); |
| 96 | |
| 97 | layerCanvas->clipRect(layerDamage.toSkRect(), SkRegion::kReplace_Op); |
| 98 | |
| 99 | auto savedLightCenter = mLightCenter; |
| 100 | // map current light center into RenderNode's coordinate space |
| 101 | layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(mLightCenter); |
| 102 | |
| 103 | const RenderProperties& properties = layerNode->properties(); |
| 104 | const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight()); |
| 105 | if (properties.getClipToBounds() && layerCanvas->quickReject(bounds)) { |
| 106 | return; |
| 107 | } |
| 108 | |
Matt Sarett | 79756be | 2016-11-09 16:13:54 -0500 | [diff] [blame] | 109 | layerNode->getSkiaLayer()->hasRenderedSinceRepaint = false; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 110 | layerCanvas->clear(SK_ColorTRANSPARENT); |
| 111 | |
| 112 | RenderNodeDrawable root(layerNode, layerCanvas, false); |
| 113 | root.forceDraw(layerCanvas); |
| 114 | layerCanvas->restoreToCount(saveCount); |
| 115 | layerCanvas->flush(); |
| 116 | mLightCenter = savedLightCenter; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | bool SkiaPipeline::createOrUpdateLayer(RenderNode* node, |
| 122 | const DamageAccumulator& damageAccumulator) { |
| 123 | SkSurface* layer = node->getLayerSurface(); |
| 124 | if (!layer || layer->width() != node->getWidth() || layer->height() != node->getHeight()) { |
| 125 | SkImageInfo info = SkImageInfo::MakeN32Premul(node->getWidth(), node->getHeight()); |
| 126 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
| 127 | SkASSERT(mRenderThread.getGrContext() != nullptr); |
| 128 | node->setLayerSurface( |
| 129 | SkSurface::MakeRenderTarget(mRenderThread.getGrContext(), SkBudgeted::kYes, |
| 130 | info, 0, &props)); |
| 131 | if (node->getLayerSurface()) { |
| 132 | // update the transform in window of the layer to reset its origin wrt light source |
| 133 | // position |
| 134 | Matrix4 windowTransform; |
| 135 | damageAccumulator.computeCurrentTransform(&windowTransform); |
| 136 | node->getSkiaLayer()->inverseTransformInWindow = windowTransform; |
| 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | void SkiaPipeline::destroyLayer(RenderNode* node) { |
| 144 | node->setLayerSurface(nullptr); |
| 145 | } |
| 146 | |
| 147 | void SkiaPipeline::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) { |
| 148 | GrContext* context = thread.getGrContext(); |
| 149 | if (context) { |
| 150 | ATRACE_FORMAT("Bitmap#prepareToDraw %dx%d", bitmap->width(), bitmap->height()); |
| 151 | SkBitmap skiaBitmap; |
| 152 | bitmap->getSkBitmap(&skiaBitmap); |
| 153 | sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(skiaBitmap, kNever_SkCopyPixelsMode); |
| 154 | SkImage_pinAsTexture(image.get(), context); |
| 155 | SkImage_unpinAsTexture(image.get(), context); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // Encodes to PNG, unless there is already encoded data, in which case that gets |
| 160 | // used. |
| 161 | class PngPixelSerializer : public SkPixelSerializer { |
| 162 | public: |
| 163 | bool onUseEncodedData(const void*, size_t) override { return true; } |
| 164 | SkData* onEncode(const SkPixmap& pixmap) override { |
| 165 | return SkImageEncoder::EncodeData(pixmap.info(), pixmap.addr(), pixmap.rowBytes(), |
| 166 | SkImageEncoder::kPNG_Type, 100); |
| 167 | } |
| 168 | }; |
| 169 | |
| 170 | void SkiaPipeline::renderFrame(const LayerUpdateQueue& layers, const SkRect& clip, |
| 171 | const std::vector<sp<RenderNode>>& nodes, bool opaque, const Rect &contentDrawBounds, |
| 172 | sk_sp<SkSurface> surface) { |
| 173 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 174 | // draw all layers up front |
| 175 | renderLayersImpl(layers, opaque); |
| 176 | |
| 177 | // initialize the canvas for the current frame |
| 178 | SkCanvas* canvas = surface->getCanvas(); |
| 179 | |
| 180 | std::unique_ptr<SkPictureRecorder> recorder; |
| 181 | bool recordingPicture = false; |
| 182 | char prop[PROPERTY_VALUE_MAX]; |
| 183 | if (skpCaptureEnabled()) { |
| 184 | property_get("debug.hwui.capture_frame_as_skp", prop, "0"); |
| 185 | recordingPicture = prop[0] != '0' && !sk_exists(prop); |
| 186 | if (recordingPicture) { |
| 187 | recorder.reset(new SkPictureRecorder()); |
| 188 | canvas = recorder->beginRecording(surface->width(), surface->height(), |
| 189 | nullptr, SkPictureRecorder::kPlaybackDrawPicture_RecordFlag); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | canvas->clipRect(clip, SkRegion::kReplace_Op); |
| 194 | |
| 195 | if (!opaque) { |
| 196 | canvas->clear(SK_ColorTRANSPARENT); |
| 197 | } |
| 198 | |
| 199 | // If there are multiple render nodes, they are laid out as follows: |
| 200 | // #0 - backdrop (content + caption) |
| 201 | // #1 - content (positioned at (0,0) and clipped to - its bounds mContentDrawBounds) |
| 202 | // #2 - additional overlay nodes |
| 203 | // Usually the backdrop cannot be seen since it will be entirely covered by the content. While |
| 204 | // resizing however it might become partially visible. The following render loop will crop the |
| 205 | // backdrop against the content and draw the remaining part of it. It will then draw the content |
| 206 | // cropped to the backdrop (since that indicates a shrinking of the window). |
| 207 | // |
| 208 | // Additional nodes will be drawn on top with no particular clipping semantics. |
| 209 | |
| 210 | // The bounds of the backdrop against which the content should be clipped. |
| 211 | Rect backdropBounds = contentDrawBounds; |
| 212 | // Usually the contents bounds should be mContentDrawBounds - however - we will |
| 213 | // move it towards the fixed edge to give it a more stable appearance (for the moment). |
| 214 | // If there is no content bounds we ignore the layering as stated above and start with 2. |
| 215 | int layer = (contentDrawBounds.isEmpty() || nodes.size() == 1) ? 2 : 0; |
| 216 | |
| 217 | for (const sp<RenderNode>& node : nodes) { |
| 218 | if (node->nothingToDraw()) continue; |
| 219 | |
| 220 | SkASSERT(node->getDisplayList()->isSkiaDL()); |
| 221 | |
| 222 | int count = canvas->save(); |
| 223 | |
| 224 | if (layer == 0) { |
| 225 | const RenderProperties& properties = node->properties(); |
| 226 | Rect targetBounds(properties.getLeft(), properties.getTop(), |
| 227 | properties.getRight(), properties.getBottom()); |
| 228 | // Move the content bounds towards the fixed corner of the backdrop. |
| 229 | const int x = targetBounds.left; |
| 230 | const int y = targetBounds.top; |
| 231 | // Remember the intersection of the target bounds and the intersection bounds against |
| 232 | // which we have to crop the content. |
| 233 | backdropBounds.set(x, y, x + backdropBounds.getWidth(), y + backdropBounds.getHeight()); |
| 234 | backdropBounds.doIntersect(targetBounds); |
| 235 | } else if (layer == 1) { |
| 236 | // We shift and clip the content to match its final location in the window. |
| 237 | const SkRect clip = SkRect::MakeXYWH(contentDrawBounds.left, contentDrawBounds.top, |
| 238 | backdropBounds.getWidth(), backdropBounds.getHeight()); |
| 239 | const float dx = backdropBounds.left - contentDrawBounds.left; |
| 240 | const float dy = backdropBounds.top - contentDrawBounds.top; |
| 241 | canvas->translate(dx, dy); |
| 242 | // It gets cropped against the bounds of the backdrop to stay inside. |
| 243 | canvas->clipRect(clip, SkRegion::kIntersect_Op); |
| 244 | } |
| 245 | |
| 246 | RenderNodeDrawable root(node.get(), canvas); |
| 247 | root.draw(canvas); |
| 248 | canvas->restoreToCount(count); |
| 249 | layer++; |
| 250 | } |
| 251 | |
| 252 | if (skpCaptureEnabled() && recordingPicture) { |
| 253 | sk_sp<SkPicture> picture = recorder->finishRecordingAsPicture(); |
| 254 | if (picture->approximateOpCount() > 0) { |
| 255 | SkFILEWStream stream(prop); |
| 256 | if (stream.isValid()) { |
| 257 | PngPixelSerializer serializer; |
| 258 | picture->serialize(&stream, &serializer); |
| 259 | stream.flush(); |
| 260 | SkDebugf("Captured Drawing Output (%d bytes) for frame. %s", stream.bytesWritten(), prop); |
| 261 | } |
| 262 | } |
| 263 | surface->getCanvas()->drawPicture(picture); |
| 264 | } |
| 265 | |
| 266 | ATRACE_NAME("flush commands"); |
| 267 | canvas->flush(); |
| 268 | } |
| 269 | |
Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 270 | void SkiaPipeline::dumpResourceCacheUsage() const { |
| 271 | int resources, maxResources; |
| 272 | size_t bytes, maxBytes; |
| 273 | mRenderThread.getGrContext()->getResourceCacheUsage(&resources, &bytes); |
| 274 | mRenderThread.getGrContext()->getResourceCacheLimits(&maxResources, &maxBytes); |
| 275 | |
| 276 | SkString log("Resource Cache Usage:\n"); |
| 277 | log.appendf("%8d items out of %d maximum items\n", resources, maxResources); |
| 278 | log.appendf("%8zu bytes (%.2f MB) out of %.2f MB maximum\n", |
| 279 | bytes, bytes * (1.0f / (1024.0f * 1024.0f)), maxBytes * (1.0f / (1024.0f * 1024.0f))); |
| 280 | |
| 281 | ALOGD("%s", log.c_str()); |
| 282 | } |
| 283 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 284 | } /* namespace skiapipeline */ |
| 285 | } /* namespace uirenderer */ |
| 286 | } /* namespace android */ |