blob: 69e603b6c927ec51d1cd776a0e898fbad77051cf [file] [log] [blame]
Stan Iliev500a0c32016-10-26 10:30:09 -04001/*
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
26using namespace android::uirenderer::renderthread;
27
28namespace android {
29namespace uirenderer {
30namespace skiapipeline {
31
32float SkiaPipeline::mLightRadius = 0;
33uint8_t SkiaPipeline::mAmbientShadowAlpha = 0;
34uint8_t SkiaPipeline::mSpotShadowAlpha = 0;
35
36Vector3 SkiaPipeline::mLightCenter = {FLT_MIN, FLT_MIN, FLT_MIN};
37
38SkiaPipeline::SkiaPipeline(RenderThread& thread) : mRenderThread(thread) { }
39
40TaskManager* SkiaPipeline::getTaskManager() {
41 return &mTaskManager;
42}
43
44void 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 Sollenbergerb7d34b62016-11-04 10:46:18 -040049bool 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
58void SkiaPipeline::unpinImages() {
59 for (auto& image : mPinnedImages) {
60 SkImage_unpinAsTexture(image.get(), mRenderThread.getGrContext());
61 }
62 mPinnedImages.clear();
63}
64
Stan Iliev500a0c32016-10-26 10:30:09 -040065void 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
74void 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
109 layerCanvas->clear(SK_ColorTRANSPARENT);
110
111 RenderNodeDrawable root(layerNode, layerCanvas, false);
112 root.forceDraw(layerCanvas);
113 layerCanvas->restoreToCount(saveCount);
114 layerCanvas->flush();
115 mLightCenter = savedLightCenter;
116 }
117 }
118}
119
120bool SkiaPipeline::createOrUpdateLayer(RenderNode* node,
121 const DamageAccumulator& damageAccumulator) {
122 SkSurface* layer = node->getLayerSurface();
123 if (!layer || layer->width() != node->getWidth() || layer->height() != node->getHeight()) {
124 SkImageInfo info = SkImageInfo::MakeN32Premul(node->getWidth(), node->getHeight());
125 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
126 SkASSERT(mRenderThread.getGrContext() != nullptr);
127 node->setLayerSurface(
128 SkSurface::MakeRenderTarget(mRenderThread.getGrContext(), SkBudgeted::kYes,
129 info, 0, &props));
130 if (node->getLayerSurface()) {
131 // update the transform in window of the layer to reset its origin wrt light source
132 // position
133 Matrix4 windowTransform;
134 damageAccumulator.computeCurrentTransform(&windowTransform);
135 node->getSkiaLayer()->inverseTransformInWindow = windowTransform;
136 }
137 return true;
138 }
139 return false;
140}
141
142void SkiaPipeline::destroyLayer(RenderNode* node) {
143 node->setLayerSurface(nullptr);
144}
145
146void SkiaPipeline::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) {
147 GrContext* context = thread.getGrContext();
148 if (context) {
149 ATRACE_FORMAT("Bitmap#prepareToDraw %dx%d", bitmap->width(), bitmap->height());
150 SkBitmap skiaBitmap;
151 bitmap->getSkBitmap(&skiaBitmap);
152 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(skiaBitmap, kNever_SkCopyPixelsMode);
153 SkImage_pinAsTexture(image.get(), context);
154 SkImage_unpinAsTexture(image.get(), context);
155 }
156}
157
158// Encodes to PNG, unless there is already encoded data, in which case that gets
159// used.
160class PngPixelSerializer : public SkPixelSerializer {
161public:
162 bool onUseEncodedData(const void*, size_t) override { return true; }
163 SkData* onEncode(const SkPixmap& pixmap) override {
164 return SkImageEncoder::EncodeData(pixmap.info(), pixmap.addr(), pixmap.rowBytes(),
165 SkImageEncoder::kPNG_Type, 100);
166 }
167};
168
169void SkiaPipeline::renderFrame(const LayerUpdateQueue& layers, const SkRect& clip,
170 const std::vector<sp<RenderNode>>& nodes, bool opaque, const Rect &contentDrawBounds,
171 sk_sp<SkSurface> surface) {
172
Stan Iliev500a0c32016-10-26 10:30:09 -0400173 // draw all layers up front
174 renderLayersImpl(layers, opaque);
175
176 // initialize the canvas for the current frame
177 SkCanvas* canvas = surface->getCanvas();
178
179 std::unique_ptr<SkPictureRecorder> recorder;
180 bool recordingPicture = false;
181 char prop[PROPERTY_VALUE_MAX];
182 if (skpCaptureEnabled()) {
183 property_get("debug.hwui.capture_frame_as_skp", prop, "0");
184 recordingPicture = prop[0] != '0' && !sk_exists(prop);
185 if (recordingPicture) {
186 recorder.reset(new SkPictureRecorder());
187 canvas = recorder->beginRecording(surface->width(), surface->height(),
188 nullptr, SkPictureRecorder::kPlaybackDrawPicture_RecordFlag);
189 }
190 }
191
192 canvas->clipRect(clip, SkRegion::kReplace_Op);
193
194 if (!opaque) {
195 canvas->clear(SK_ColorTRANSPARENT);
196 }
197
198 // If there are multiple render nodes, they are laid out as follows:
199 // #0 - backdrop (content + caption)
200 // #1 - content (positioned at (0,0) and clipped to - its bounds mContentDrawBounds)
201 // #2 - additional overlay nodes
202 // Usually the backdrop cannot be seen since it will be entirely covered by the content. While
203 // resizing however it might become partially visible. The following render loop will crop the
204 // backdrop against the content and draw the remaining part of it. It will then draw the content
205 // cropped to the backdrop (since that indicates a shrinking of the window).
206 //
207 // Additional nodes will be drawn on top with no particular clipping semantics.
208
209 // The bounds of the backdrop against which the content should be clipped.
210 Rect backdropBounds = contentDrawBounds;
211 // Usually the contents bounds should be mContentDrawBounds - however - we will
212 // move it towards the fixed edge to give it a more stable appearance (for the moment).
213 // If there is no content bounds we ignore the layering as stated above and start with 2.
214 int layer = (contentDrawBounds.isEmpty() || nodes.size() == 1) ? 2 : 0;
215
216 for (const sp<RenderNode>& node : nodes) {
217 if (node->nothingToDraw()) continue;
218
219 SkASSERT(node->getDisplayList()->isSkiaDL());
220
221 int count = canvas->save();
222
223 if (layer == 0) {
224 const RenderProperties& properties = node->properties();
225 Rect targetBounds(properties.getLeft(), properties.getTop(),
226 properties.getRight(), properties.getBottom());
227 // Move the content bounds towards the fixed corner of the backdrop.
228 const int x = targetBounds.left;
229 const int y = targetBounds.top;
230 // Remember the intersection of the target bounds and the intersection bounds against
231 // which we have to crop the content.
232 backdropBounds.set(x, y, x + backdropBounds.getWidth(), y + backdropBounds.getHeight());
233 backdropBounds.doIntersect(targetBounds);
234 } else if (layer == 1) {
235 // We shift and clip the content to match its final location in the window.
236 const SkRect clip = SkRect::MakeXYWH(contentDrawBounds.left, contentDrawBounds.top,
237 backdropBounds.getWidth(), backdropBounds.getHeight());
238 const float dx = backdropBounds.left - contentDrawBounds.left;
239 const float dy = backdropBounds.top - contentDrawBounds.top;
240 canvas->translate(dx, dy);
241 // It gets cropped against the bounds of the backdrop to stay inside.
242 canvas->clipRect(clip, SkRegion::kIntersect_Op);
243 }
244
245 RenderNodeDrawable root(node.get(), canvas);
246 root.draw(canvas);
247 canvas->restoreToCount(count);
248 layer++;
249 }
250
251 if (skpCaptureEnabled() && recordingPicture) {
252 sk_sp<SkPicture> picture = recorder->finishRecordingAsPicture();
253 if (picture->approximateOpCount() > 0) {
254 SkFILEWStream stream(prop);
255 if (stream.isValid()) {
256 PngPixelSerializer serializer;
257 picture->serialize(&stream, &serializer);
258 stream.flush();
259 SkDebugf("Captured Drawing Output (%d bytes) for frame. %s", stream.bytesWritten(), prop);
260 }
261 }
262 surface->getCanvas()->drawPicture(picture);
263 }
264
265 ATRACE_NAME("flush commands");
266 canvas->flush();
267}
268
269} /* namespace skiapipeline */
270} /* namespace uirenderer */
271} /* namespace android */