SkiaPipeline.cpp revision 9a648a1c74f39b8aca525ae3787d379cb4c76971
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 <SkImageEncoder.h>
21#include <SkOSFile.h>
22#include <SkOverdrawCanvas.h>
23#include <SkOverdrawColorFilter.h>
24#include <SkPicture.h>
25#include <SkPictureRecorder.h>
26#include <SkPixelSerializer.h>
27#include <SkStream.h>
28
29using namespace android::uirenderer::renderthread;
30
31namespace android {
32namespace uirenderer {
33namespace skiapipeline {
34
35float   SkiaPipeline::mLightRadius = 0;
36uint8_t SkiaPipeline::mAmbientShadowAlpha = 0;
37uint8_t SkiaPipeline::mSpotShadowAlpha = 0;
38
39Vector3 SkiaPipeline::mLightCenter = {FLT_MIN, FLT_MIN, FLT_MIN};
40
41SkiaPipeline::SkiaPipeline(RenderThread& thread) :  mRenderThread(thread) { }
42
43TaskManager* SkiaPipeline::getTaskManager() {
44    return &mTaskManager;
45}
46
47void SkiaPipeline::onDestroyHardwareResources() {
48    // No need to flush the caches here. There is a timer
49    // which will flush temporary resources over time.
50}
51
52bool SkiaPipeline::pinImages(std::vector<SkImage*>& mutableImages) {
53    for (SkImage* image : mutableImages) {
54        if (SkImage_pinAsTexture(image, mRenderThread.getGrContext())) {
55            mPinnedImages.emplace_back(sk_ref_sp(image));
56        } else {
57            return false;
58        }
59    }
60    return true;
61}
62
63void SkiaPipeline::unpinImages() {
64    for (auto& image : mPinnedImages) {
65        SkImage_unpinAsTexture(image.get(), mRenderThread.getGrContext());
66    }
67    mPinnedImages.clear();
68}
69
70void SkiaPipeline::renderLayers(const FrameBuilder::LightGeometry& lightGeometry,
71        LayerUpdateQueue* layerUpdateQueue, bool opaque,
72        const BakedOpRenderer::LightInfo& lightInfo) {
73    updateLighting(lightGeometry, lightInfo);
74    ATRACE_NAME("draw layers");
75    renderLayersImpl(*layerUpdateQueue, opaque);
76    layerUpdateQueue->clear();
77}
78
79void SkiaPipeline::renderLayersImpl(const LayerUpdateQueue& layers, bool opaque) {
80    // Render all layers that need to be updated, in order.
81    for (size_t i = 0; i < layers.entries().size(); i++) {
82        RenderNode* layerNode = layers.entries()[i].renderNode;
83        // only schedule repaint if node still on layer - possible it may have been
84        // removed during a dropped frame, but layers may still remain scheduled so
85        // as not to lose info on what portion is damaged
86        if (CC_LIKELY(layerNode->getLayerSurface() != nullptr)) {
87            SkASSERT(layerNode->getLayerSurface());
88            SkASSERT(layerNode->getDisplayList()->isSkiaDL());
89            SkiaDisplayList* displayList = (SkiaDisplayList*)layerNode->getDisplayList();
90            if (!displayList || displayList->isEmpty()) {
91                SkDEBUGF(("%p drawLayers(%s) : missing drawable", this, layerNode->getName()));
92                return;
93            }
94
95            const Rect& layerDamage = layers.entries()[i].damage;
96
97            SkCanvas* layerCanvas = layerNode->getLayerSurface()->getCanvas();
98
99            int saveCount = layerCanvas->save();
100            SkASSERT(saveCount == 1);
101
102            layerCanvas->clipRect(layerDamage.toSkRect(), kReplace_SkClipOp);
103
104            auto savedLightCenter = mLightCenter;
105            // map current light center into RenderNode's coordinate space
106            layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(mLightCenter);
107
108            const RenderProperties& properties = layerNode->properties();
109            const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
110            if (properties.getClipToBounds() && layerCanvas->quickReject(bounds)) {
111                return;
112            }
113
114            layerNode->getSkiaLayer()->hasRenderedSinceRepaint = false;
115            layerCanvas->clear(SK_ColorTRANSPARENT);
116
117            RenderNodeDrawable root(layerNode, layerCanvas, false);
118            root.forceDraw(layerCanvas);
119            layerCanvas->restoreToCount(saveCount);
120            layerCanvas->flush();
121            mLightCenter = savedLightCenter;
122        }
123    }
124}
125
126bool SkiaPipeline::createOrUpdateLayer(RenderNode* node,
127        const DamageAccumulator& damageAccumulator) {
128    SkSurface* layer = node->getLayerSurface();
129    if (!layer || layer->width() != node->getWidth() || layer->height() != node->getHeight()) {
130        SkImageInfo info = SkImageInfo::MakeN32Premul(node->getWidth(), node->getHeight());
131        SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
132        SkASSERT(mRenderThread.getGrContext() != nullptr);
133        node->setLayerSurface(
134                SkSurface::MakeRenderTarget(mRenderThread.getGrContext(), SkBudgeted::kYes,
135                        info, 0, &props));
136        if (node->getLayerSurface()) {
137            // update the transform in window of the layer to reset its origin wrt light source
138            // position
139            Matrix4 windowTransform;
140            damageAccumulator.computeCurrentTransform(&windowTransform);
141            node->getSkiaLayer()->inverseTransformInWindow = windowTransform;
142        }
143        return true;
144    }
145    return false;
146}
147
148void SkiaPipeline::destroyLayer(RenderNode* node) {
149    node->setLayerSurface(nullptr);
150}
151
152void SkiaPipeline::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) {
153    GrContext* context = thread.getGrContext();
154    if (context) {
155        ATRACE_FORMAT("Bitmap#prepareToDraw %dx%d", bitmap->width(), bitmap->height());
156        SkBitmap skiaBitmap;
157        bitmap->getSkBitmap(&skiaBitmap);
158        sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(skiaBitmap, kNever_SkCopyPixelsMode);
159        SkImage_pinAsTexture(image.get(), context);
160        SkImage_unpinAsTexture(image.get(), context);
161    }
162}
163
164// Encodes to PNG, unless there is already encoded data, in which case that gets
165// used.
166class PngPixelSerializer : public SkPixelSerializer {
167public:
168    bool onUseEncodedData(const void*, size_t) override { return true; }
169    SkData* onEncode(const SkPixmap& pixmap) override {
170        SkDynamicMemoryWStream buf;
171        return SkEncodeImage(&buf, pixmap, SkEncodedImageFormat::kPNG, 100)
172               ? buf.detachAsData().release()
173               : nullptr;
174    }
175};
176
177void SkiaPipeline::renderFrame(const LayerUpdateQueue& layers, const SkRect& clip,
178        const std::vector<sp<RenderNode>>& nodes, bool opaque, const Rect &contentDrawBounds,
179        sk_sp<SkSurface> surface) {
180
181    // draw all layers up front
182    renderLayersImpl(layers, opaque);
183
184    // initialize the canvas for the current frame
185    SkCanvas* canvas = surface->getCanvas();
186
187    std::unique_ptr<SkPictureRecorder> recorder;
188    bool recordingPicture = false;
189    char prop[PROPERTY_VALUE_MAX];
190    if (skpCaptureEnabled()) {
191        property_get("debug.hwui.capture_frame_as_skp", prop, "0");
192        recordingPicture = prop[0] != '0' && !sk_exists(prop);
193        if (recordingPicture) {
194            recorder.reset(new SkPictureRecorder());
195            canvas = recorder->beginRecording(surface->width(), surface->height(),
196                    nullptr, SkPictureRecorder::kPlaybackDrawPicture_RecordFlag);
197        }
198    }
199
200    renderFrameImpl(layers, clip, nodes, opaque, contentDrawBounds, canvas);
201
202    if (skpCaptureEnabled() && recordingPicture) {
203        sk_sp<SkPicture> picture = recorder->finishRecordingAsPicture();
204        if (picture->approximateOpCount() > 0) {
205            SkFILEWStream stream(prop);
206            if (stream.isValid()) {
207                PngPixelSerializer serializer;
208                picture->serialize(&stream, &serializer);
209                stream.flush();
210                SkDebugf("Captured Drawing Output (%d bytes) for frame. %s", stream.bytesWritten(), prop);
211            }
212        }
213        surface->getCanvas()->drawPicture(picture);
214    }
215
216    if (CC_UNLIKELY(Properties::debugOverdraw)) {
217        renderOverdraw(layers, clip, nodes, contentDrawBounds, surface);
218    }
219
220    ATRACE_NAME("flush commands");
221    canvas->flush();
222}
223
224void SkiaPipeline::renderFrameImpl(const LayerUpdateQueue& layers, const SkRect& clip,
225        const std::vector<sp<RenderNode>>& nodes, bool opaque, const Rect &contentDrawBounds,
226        SkCanvas* canvas) {
227
228    canvas->clipRect(clip, kReplace_SkClipOp);
229
230    if (!opaque) {
231        canvas->clear(SK_ColorTRANSPARENT);
232    }
233
234    // If there are multiple render nodes, they are laid out as follows:
235    // #0 - backdrop (content + caption)
236    // #1 - content (positioned at (0,0) and clipped to - its bounds mContentDrawBounds)
237    // #2 - additional overlay nodes
238    // Usually the backdrop cannot be seen since it will be entirely covered by the content. While
239    // resizing however it might become partially visible. The following render loop will crop the
240    // backdrop against the content and draw the remaining part of it. It will then draw the content
241    // cropped to the backdrop (since that indicates a shrinking of the window).
242    //
243    // Additional nodes will be drawn on top with no particular clipping semantics.
244
245    // The bounds of the backdrop against which the content should be clipped.
246    Rect backdropBounds = contentDrawBounds;
247    // Usually the contents bounds should be mContentDrawBounds - however - we will
248    // move it towards the fixed edge to give it a more stable appearance (for the moment).
249    // If there is no content bounds we ignore the layering as stated above and start with 2.
250    int layer = (contentDrawBounds.isEmpty() || nodes.size() == 1) ? 2 : 0;
251
252    for (const sp<RenderNode>& node : nodes) {
253        if (node->nothingToDraw()) continue;
254
255        SkASSERT(node->getDisplayList()->isSkiaDL());
256
257        int count = canvas->save();
258
259        if (layer == 0) {
260            const RenderProperties& properties = node->properties();
261            Rect targetBounds(properties.getLeft(), properties.getTop(),
262                              properties.getRight(), properties.getBottom());
263            // Move the content bounds towards the fixed corner of the backdrop.
264            const int x = targetBounds.left;
265            const int y = targetBounds.top;
266            // Remember the intersection of the target bounds and the intersection bounds against
267            // which we have to crop the content.
268            backdropBounds.set(x, y, x + backdropBounds.getWidth(), y + backdropBounds.getHeight());
269            backdropBounds.doIntersect(targetBounds);
270        } else if (layer == 1) {
271            // We shift and clip the content to match its final location in the window.
272            const SkRect clip = SkRect::MakeXYWH(contentDrawBounds.left, contentDrawBounds.top,
273                                                 backdropBounds.getWidth(), backdropBounds.getHeight());
274            const float dx = backdropBounds.left - contentDrawBounds.left;
275            const float dy = backdropBounds.top - contentDrawBounds.top;
276            canvas->translate(dx, dy);
277            // It gets cropped against the bounds of the backdrop to stay inside.
278            canvas->clipRect(clip);
279        }
280
281        RenderNodeDrawable root(node.get(), canvas);
282        root.draw(canvas);
283        canvas->restoreToCount(count);
284        layer++;
285    }
286}
287
288void SkiaPipeline::dumpResourceCacheUsage() const {
289    int resources, maxResources;
290    size_t bytes, maxBytes;
291    mRenderThread.getGrContext()->getResourceCacheUsage(&resources, &bytes);
292    mRenderThread.getGrContext()->getResourceCacheLimits(&maxResources, &maxBytes);
293
294    SkString log("Resource Cache Usage:\n");
295    log.appendf("%8d items out of %d maximum items\n", resources, maxResources);
296    log.appendf("%8zu bytes (%.2f MB) out of %.2f MB maximum\n",
297            bytes, bytes * (1.0f / (1024.0f * 1024.0f)), maxBytes * (1.0f / (1024.0f * 1024.0f)));
298
299    ALOGD("%s", log.c_str());
300}
301
302// Overdraw debugging
303
304// These colors should be kept in sync with Caches::getOverdrawColor() with a few differences.
305// This implementation:
306// (1) Requires transparent entries for "no overdraw" and "single draws".
307// (2) Requires premul colors (instead of unpremul).
308// (3) Requires RGBA colors (instead of BGRA).
309static const uint32_t kOverdrawColors[2][6] = {
310        { 0x00000000, 0x00000000, 0x2f2f0000, 0x2f002f00, 0x3f00003f, 0x7f00007f, },
311        { 0x00000000, 0x00000000, 0x2f2f0000, 0x4f004f4f, 0x5f50335f, 0x7f00007f, },
312};
313
314void SkiaPipeline::renderOverdraw(const LayerUpdateQueue& layers, const SkRect& clip,
315        const std::vector<sp<RenderNode>>& nodes, const Rect &contentDrawBounds,
316        sk_sp<SkSurface> surface) {
317    // Set up the overdraw canvas.
318    SkImageInfo offscreenInfo = SkImageInfo::MakeA8(surface->width(), surface->height());
319    sk_sp<SkSurface> offscreen = surface->makeSurface(offscreenInfo);
320    SkOverdrawCanvas overdrawCanvas(offscreen->getCanvas());
321
322    // Fake a redraw to replay the draw commands.  This will increment the alpha channel
323    // each time a pixel would have been drawn.
324    // Pass true for opaque so we skip the clear - the overdrawCanvas is already zero
325    // initialized.
326    renderFrameImpl(layers, clip, nodes, true, contentDrawBounds, &overdrawCanvas);
327    sk_sp<SkImage> counts = offscreen->makeImageSnapshot();
328
329    // Draw overdraw colors to the canvas.  The color filter will convert counts to colors.
330    SkPaint paint;
331    const SkPMColor* colors = kOverdrawColors[static_cast<int>(Properties::overdrawColorSet)];
332    paint.setColorFilter(SkOverdrawColorFilter::Make(colors));
333    surface->getCanvas()->drawImage(counts.get(), 0.0f, 0.0f, &paint);
334}
335
336} /* namespace skiapipeline */
337} /* namespace uirenderer */
338} /* namespace android */
339