blob: c195a8eee87019b07b94c165f9a5ba82fc78c4d7 [file] [log] [blame]
Stan Iliev021693b2016-10-17 16:26:15 -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 "RenderNodeDrawable.h"
John Reck1bcacfd2017-11-03 10:12:19 -070018#include <SkPaintFilterCanvas.h>
Stan Iliev021693b2016-10-17 16:26:15 -040019#include "RenderNode.h"
20#include "SkiaDisplayList.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040021#include "SkiaPipeline.h"
Stan Iliev021693b2016-10-17 16:26:15 -040022#include "utils/TraceUtils.h"
23
24namespace android {
25namespace uirenderer {
26namespace skiapipeline {
27
John Reckd9d7f122018-05-03 14:40:56 -070028RenderNodeDrawable::RenderNodeDrawable(RenderNode* node, SkCanvas* canvas, bool composeLayer,
29 bool inReorderingSection)
30 : mRenderNode(node)
31 , mRecordedTransform(canvas->getTotalMatrix())
32 , mComposeLayer(composeLayer)
33 , mInReorderingSection(inReorderingSection) {}
34
35RenderNodeDrawable::~RenderNodeDrawable() {
36 // Just here to move the destructor into the cpp file where we can access RenderNode.
37
38 // TODO: Detangle the header nightmare.
39}
40
John Reck1bcacfd2017-11-03 10:12:19 -070041void RenderNodeDrawable::drawBackwardsProjectedNodes(SkCanvas* canvas,
42 const SkiaDisplayList& displayList,
43 int nestLevel) {
Stan Ilievdb45a4b2016-11-08 14:18:31 -050044 LOG_ALWAYS_FATAL_IF(0 == nestLevel && !displayList.mProjectionReceiver);
45 for (auto& child : displayList.mChildNodes) {
46 const RenderProperties& childProperties = child.getNodeProperties();
47
John Reck1bcacfd2017-11-03 10:12:19 -070048 // immediate children cannot be projected on their parent
Stan Ilievdb45a4b2016-11-08 14:18:31 -050049 if (childProperties.getProjectBackwards() && nestLevel > 0) {
50 SkAutoCanvasRestore acr2(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -070051 // Apply recorded matrix, which is a total matrix saved at recording time to avoid
52 // replaying all DL commands.
Stan Ilievdb45a4b2016-11-08 14:18:31 -050053 canvas->concat(child.getRecordedMatrix());
54 child.drawContent(canvas);
55 }
56
John Reck1bcacfd2017-11-03 10:12:19 -070057 // skip walking sub-nodes if current display list contains a receiver with exception of
58 // level 0, which is a known receiver
Stan Ilievdb45a4b2016-11-08 14:18:31 -050059 if (0 == nestLevel || !displayList.containsProjectionReceiver()) {
60 SkAutoCanvasRestore acr(canvas, true);
61 SkMatrix nodeMatrix;
62 mat4 hwuiMatrix(child.getRecordedMatrix());
63 auto childNode = child.getRenderNode();
64 childNode->applyViewPropertyTransforms(hwuiMatrix);
65 hwuiMatrix.copyTo(nodeMatrix);
66 canvas->concat(nodeMatrix);
67 SkiaDisplayList* childDisplayList = static_cast<SkiaDisplayList*>(
John Reck1bcacfd2017-11-03 10:12:19 -070068 (const_cast<DisplayList*>(childNode->getDisplayList())));
Stan Ilievdb45a4b2016-11-08 14:18:31 -050069 if (childDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -070070 drawBackwardsProjectedNodes(canvas, *childDisplayList, nestLevel + 1);
Stan Ilievdb45a4b2016-11-08 14:18:31 -050071 }
72 }
73 }
74}
75
Stan Iliev021693b2016-10-17 16:26:15 -040076static void clipOutline(const Outline& outline, SkCanvas* canvas, const SkRect* pendingClip) {
Stan Iliev021693b2016-10-17 16:26:15 -040077 Rect possibleRect;
78 float radius;
Derek Sollenbergerf209c062017-05-26 12:11:34 -040079
80 /* To match the existing HWUI behavior we only supports rectangles or
81 * rounded rectangles; passing in a more complicated outline fails silently.
82 */
83 if (!outline.getAsRoundRect(&possibleRect, &radius)) {
84 if (pendingClip) {
85 canvas->clipRect(*pendingClip);
86 }
87 return;
88 }
89
Stan Iliev021693b2016-10-17 16:26:15 -040090 SkRect rect = possibleRect.toSkRect();
91 if (radius != 0.0f) {
92 if (pendingClip && !pendingClip->contains(rect)) {
93 canvas->clipRect(*pendingClip);
94 }
Mike Reed6c67f1d2016-12-14 10:29:54 -050095 canvas->clipRRect(SkRRect::MakeRectXY(rect, radius, radius), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -040096 } else {
97 if (pendingClip) {
98 (void)rect.intersect(*pendingClip);
99 }
100 canvas->clipRect(rect);
101 }
102}
103
104const RenderProperties& RenderNodeDrawable::getNodeProperties() const {
105 return mRenderNode->properties();
106}
107
108void RenderNodeDrawable::onDraw(SkCanvas* canvas) {
John Reck1bcacfd2017-11-03 10:12:19 -0700109 // negative and positive Z order are drawn out of order, if this render node drawable is in
110 // a reordering section
Stan Iliev2f06e8a2016-11-02 15:29:03 -0400111 if ((!mInReorderingSection) || MathUtils::isZero(mRenderNode->properties().getZ())) {
Stan Iliev021693b2016-10-17 16:26:15 -0400112 this->forceDraw(canvas);
113 }
114}
115
116void RenderNodeDrawable::forceDraw(SkCanvas* canvas) {
117 RenderNode* renderNode = mRenderNode.get();
Stan Ilieve9d00122017-09-19 12:07:10 -0400118 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
Stan Iliev021693b2016-10-17 16:26:15 -0400119 SkRect dimensions = SkRect::MakeWH(renderNode->getWidth(), renderNode->getHeight());
120 canvas->drawAnnotation(dimensions, renderNode->getName(), nullptr);
121 }
122
123 // We only respect the nothingToDraw check when we are composing a layer. This
124 // ensures that we paint the layer even if it is not currently visible in the
125 // event that the properties change and it becomes visible.
Yuqian Li5ebbc8e2017-11-29 09:53:06 -0500126 if ((mProjectedDisplayList == nullptr && !renderNode->isRenderable()) ||
127 (renderNode->nothingToDraw() && mComposeLayer)) {
Stan Iliev021693b2016-10-17 16:26:15 -0400128 return;
129 }
130
Stan Iliev021693b2016-10-17 16:26:15 -0400131 SkiaDisplayList* displayList = (SkiaDisplayList*)renderNode->getDisplayList();
132
133 SkAutoCanvasRestore acr(canvas, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400134 const RenderProperties& properties = this->getNodeProperties();
John Reck1bcacfd2017-11-03 10:12:19 -0700135 // pass this outline to the children that may clip backward projected nodes
136 displayList->mProjectedOutline =
137 displayList->containsProjectionReceiver() ? &properties.getOutline() : nullptr;
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500138 if (!properties.getProjectBackwards()) {
139 drawContent(canvas);
140 if (mProjectedDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700141 acr.restore(); // draw projected children using parent matrix
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500142 LOG_ALWAYS_FATAL_IF(!mProjectedDisplayList->mProjectedOutline);
143 const bool shouldClip = mProjectedDisplayList->mProjectedOutline->getPath();
144 SkAutoCanvasRestore acr2(canvas, shouldClip);
Stan Iliev54d70322018-06-14 18:00:10 -0400145 canvas->setMatrix(mProjectedDisplayList->mParentMatrix);
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500146 if (shouldClip) {
147 clipOutline(*mProjectedDisplayList->mProjectedOutline, canvas, nullptr);
148 }
149 drawBackwardsProjectedNodes(canvas, *mProjectedDisplayList);
Stan Iliev021693b2016-10-17 16:26:15 -0400150 }
Stan Iliev021693b2016-10-17 16:26:15 -0400151 }
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500152 displayList->mProjectedOutline = nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400153}
154
John Reck1bcacfd2017-11-03 10:12:19 -0700155static bool layerNeedsPaint(const LayerProperties& properties, float alphaMultiplier,
156 SkPaint* paint) {
Stan Iliev7e100e92018-01-22 10:36:33 -0500157 paint->setFilterQuality(kLow_SkFilterQuality);
John Reck1bcacfd2017-11-03 10:12:19 -0700158 if (alphaMultiplier < 1.0f || properties.alpha() < 255 ||
159 properties.xferMode() != SkBlendMode::kSrcOver || properties.colorFilter() != nullptr) {
Stan Iliev021693b2016-10-17 16:26:15 -0400160 paint->setAlpha(properties.alpha() * alphaMultiplier);
161 paint->setBlendMode(properties.xferMode());
Derek Sollenbergerf87da672016-11-02 11:34:27 -0400162 paint->setColorFilter(sk_ref_sp(properties.colorFilter()));
Stan Iliev021693b2016-10-17 16:26:15 -0400163 return true;
164 }
165 return false;
166}
167
Stan Iliev1843ac72017-09-20 18:05:35 -0400168class AlphaFilterCanvas : public SkPaintFilterCanvas {
169public:
170 AlphaFilterCanvas(SkCanvas* canvas, float alpha) : SkPaintFilterCanvas(canvas), mAlpha(alpha) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700171
Stan Iliev1843ac72017-09-20 18:05:35 -0400172protected:
173 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type t) const override {
174 SkTLazy<SkPaint> defaultPaint;
175 if (!*paint) {
176 paint->init(*defaultPaint.init());
177 }
John Reck1bcacfd2017-11-03 10:12:19 -0700178 paint->writable()->setAlpha((uint8_t)(*paint)->getAlpha() * mAlpha);
Stan Iliev1843ac72017-09-20 18:05:35 -0400179 return true;
180 }
181 void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override {
182 // We unroll the drawable using "this" canvas, so that draw calls contained inside will
183 // get their alpha applied. THe default SkPaintFilterCanvas::onDrawDrawable does not unroll.
184 drawable->draw(this, matrix);
185 }
John Reck1bcacfd2017-11-03 10:12:19 -0700186
Stan Iliev1843ac72017-09-20 18:05:35 -0400187private:
188 float mAlpha;
189};
190
Stan Iliev021693b2016-10-17 16:26:15 -0400191void RenderNodeDrawable::drawContent(SkCanvas* canvas) const {
192 RenderNode* renderNode = mRenderNode.get();
193 float alphaMultiplier = 1.0f;
194 const RenderProperties& properties = renderNode->properties();
195
196 // If we are drawing the contents of layer, we don't want to apply any of
197 // the RenderNode's properties during this pass. Those will all be applied
198 // when the layer is composited.
199 if (mComposeLayer) {
200 setViewProperties(properties, canvas, &alphaMultiplier);
201 }
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500202 SkiaDisplayList* displayList = (SkiaDisplayList*)mRenderNode->getDisplayList();
Stan Iliev54d70322018-06-14 18:00:10 -0400203 displayList->mParentMatrix = canvas->getTotalMatrix();
Stan Iliev021693b2016-10-17 16:26:15 -0400204
John Reck1bcacfd2017-11-03 10:12:19 -0700205 // TODO should we let the bound of the drawable do this for us?
Stan Iliev021693b2016-10-17 16:26:15 -0400206 const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
207 bool quickRejected = properties.getClipToBounds() && canvas->quickReject(bounds);
208 if (!quickRejected) {
209 SkiaDisplayList* displayList = (SkiaDisplayList*)renderNode->getDisplayList();
210 const LayerProperties& layerProperties = properties.layerProperties();
211 // composing a hardware layer
212 if (renderNode->getLayerSurface() && mComposeLayer) {
213 SkASSERT(properties.effectiveLayerType() == LayerType::RenderLayer);
Stan Iliev7e100e92018-01-22 10:36:33 -0500214 SkPaint paint;
215 layerNeedsPaint(layerProperties, alphaMultiplier, &paint);
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500216
217 // surfaces for layers are created on LAYER_SIZE boundaries (which are >= layer size) so
218 // we need to restrict the portion of the surface drawn to the size of the renderNode.
219 SkASSERT(renderNode->getLayerSurface()->width() >= bounds.width());
220 SkASSERT(renderNode->getLayerSurface()->height() >= bounds.height());
221 canvas->drawImageRect(renderNode->getLayerSurface()->makeImageSnapshot().get(),
Stan Iliev7e100e92018-01-22 10:36:33 -0500222 bounds, bounds, &paint);
Matt Sarett79756be2016-11-09 16:13:54 -0500223
Matt Sarettf58cc922016-11-14 18:33:38 -0500224 if (!renderNode->getSkiaLayer()->hasRenderedSinceRepaint) {
Matt Sarett79756be2016-11-09 16:13:54 -0500225 renderNode->getSkiaLayer()->hasRenderedSinceRepaint = true;
Matt Sarettf58cc922016-11-14 18:33:38 -0500226 if (CC_UNLIKELY(Properties::debugLayersUpdates)) {
227 SkPaint layerPaint;
228 layerPaint.setColor(0x7f00ff00);
229 canvas->drawRect(bounds, layerPaint);
230 } else if (CC_UNLIKELY(Properties::debugOverdraw)) {
231 // Render transparent rect to increment overdraw for repaint area.
232 // This can be "else if" because flashing green on layer updates
233 // will also increment the overdraw if it happens to be turned on.
234 SkPaint transparentPaint;
235 transparentPaint.setColor(SK_ColorTRANSPARENT);
236 canvas->drawRect(bounds, transparentPaint);
237 }
Matt Sarett79756be2016-11-09 16:13:54 -0500238 }
Stan Iliev021693b2016-10-17 16:26:15 -0400239 } else {
Stan Iliev1843ac72017-09-20 18:05:35 -0400240 if (alphaMultiplier < 1.0f) {
241 // Non-layer draw for a view with getHasOverlappingRendering=false, will apply
242 // the alpha to the paint of each nested draw.
243 AlphaFilterCanvas alphaCanvas(canvas, alphaMultiplier);
244 displayList->draw(&alphaCanvas);
245 } else {
246 displayList->draw(canvas);
247 }
Stan Iliev021693b2016-10-17 16:26:15 -0400248 }
249 }
250}
251
252void RenderNodeDrawable::setViewProperties(const RenderProperties& properties, SkCanvas* canvas,
John Reck1bcacfd2017-11-03 10:12:19 -0700253 float* alphaMultiplier) {
Stan Iliev021693b2016-10-17 16:26:15 -0400254 if (properties.getLeft() != 0 || properties.getTop() != 0) {
255 canvas->translate(properties.getLeft(), properties.getTop());
256 }
257 if (properties.getStaticMatrix()) {
258 canvas->concat(*properties.getStaticMatrix());
259 } else if (properties.getAnimationMatrix()) {
260 canvas->concat(*properties.getAnimationMatrix());
261 }
262 if (properties.hasTransformMatrix()) {
263 if (properties.isTransformTranslateOnly()) {
264 canvas->translate(properties.getTranslationX(), properties.getTranslationY());
265 } else {
266 canvas->concat(*properties.getTransformMatrix());
267 }
268 }
269 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
270 int clipFlags = properties.getClippingFlags();
271 if (properties.getAlpha() < 1) {
272 if (isLayer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700273 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
Stan Iliev021693b2016-10-17 16:26:15 -0400274 }
275 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
276 *alphaMultiplier = properties.getAlpha();
277 } else {
278 // savelayer needed to create an offscreen buffer
279 Rect layerBounds(0, 0, properties.getWidth(), properties.getHeight());
280 if (clipFlags) {
281 properties.getClippingRectForFlags(clipFlags, &layerBounds);
John Reck1bcacfd2017-11-03 10:12:19 -0700282 clipFlags = 0; // all clipping done by savelayer
Stan Iliev021693b2016-10-17 16:26:15 -0400283 }
John Reck1bcacfd2017-11-03 10:12:19 -0700284 SkRect bounds = SkRect::MakeLTRB(layerBounds.left, layerBounds.top, layerBounds.right,
285 layerBounds.bottom);
286 canvas->saveLayerAlpha(&bounds, (int)(properties.getAlpha() * 255));
Stan Iliev021693b2016-10-17 16:26:15 -0400287 }
288
289 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
290 // pretend alpha always causes savelayer to warn about
291 // performance problem affecting old versions
292 ATRACE_FORMAT("alpha caused saveLayer %dx%d", properties.getWidth(),
John Reck1bcacfd2017-11-03 10:12:19 -0700293 properties.getHeight());
Stan Iliev021693b2016-10-17 16:26:15 -0400294 }
295 }
296
297 const SkRect* pendingClip = nullptr;
298 SkRect clipRect;
299
300 if (clipFlags) {
301 Rect tmpRect;
302 properties.getClippingRectForFlags(clipFlags, &tmpRect);
303 clipRect = tmpRect.toSkRect();
304 pendingClip = &clipRect;
305 }
306
307 if (properties.getRevealClip().willClip()) {
Mike Reed6c67f1d2016-12-14 10:29:54 -0500308 canvas->clipPath(*properties.getRevealClip().getPath(), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400309 } else if (properties.getOutline().willClip()) {
310 clipOutline(properties.getOutline(), canvas, pendingClip);
311 pendingClip = nullptr;
312 }
313
314 if (pendingClip) {
315 canvas->clipRect(*pendingClip);
316 }
317}
318
John Reck1bcacfd2017-11-03 10:12:19 -0700319}; // namespace skiapipeline
320}; // namespace uirenderer
321}; // namespace android