blob: ca93925c3c04df016013d46333b34b12b7da4e7b [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"
18#include "RenderNode.h"
19#include "SkiaDisplayList.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040020#include "SkiaPipeline.h"
Stan Iliev021693b2016-10-17 16:26:15 -040021#include "utils/TraceUtils.h"
Stan Iliev1843ac72017-09-20 18:05:35 -040022#include <SkPaintFilterCanvas.h>
Stan Iliev021693b2016-10-17 16:26:15 -040023
24namespace android {
25namespace uirenderer {
26namespace skiapipeline {
27
Stan Ilievdb45a4b2016-11-08 14:18:31 -050028void RenderNodeDrawable::drawBackwardsProjectedNodes(SkCanvas* canvas, const SkiaDisplayList& displayList,
29 int nestLevel) {
30 LOG_ALWAYS_FATAL_IF(0 == nestLevel && !displayList.mProjectionReceiver);
31 for (auto& child : displayList.mChildNodes) {
32 const RenderProperties& childProperties = child.getNodeProperties();
33
34 //immediate children cannot be projected on their parent
35 if (childProperties.getProjectBackwards() && nestLevel > 0) {
36 SkAutoCanvasRestore acr2(canvas, true);
37 //Apply recorded matrix, which is a total matrix saved at recording time to avoid
38 //replaying all DL commands.
39 canvas->concat(child.getRecordedMatrix());
40 child.drawContent(canvas);
41 }
42
43 //skip walking sub-nodes if current display list contains a receiver with exception of
44 //level 0, which is a known receiver
45 if (0 == nestLevel || !displayList.containsProjectionReceiver()) {
46 SkAutoCanvasRestore acr(canvas, true);
47 SkMatrix nodeMatrix;
48 mat4 hwuiMatrix(child.getRecordedMatrix());
49 auto childNode = child.getRenderNode();
50 childNode->applyViewPropertyTransforms(hwuiMatrix);
51 hwuiMatrix.copyTo(nodeMatrix);
52 canvas->concat(nodeMatrix);
53 SkiaDisplayList* childDisplayList = static_cast<SkiaDisplayList*>(
54 (const_cast<DisplayList*>(childNode->getDisplayList())));
55 if (childDisplayList) {
56 drawBackwardsProjectedNodes(canvas, *childDisplayList, nestLevel+1);
57 }
58 }
59 }
60}
61
Stan Iliev021693b2016-10-17 16:26:15 -040062static void clipOutline(const Outline& outline, SkCanvas* canvas, const SkRect* pendingClip) {
Stan Iliev021693b2016-10-17 16:26:15 -040063 Rect possibleRect;
64 float radius;
Derek Sollenbergerf209c062017-05-26 12:11:34 -040065
66 /* To match the existing HWUI behavior we only supports rectangles or
67 * rounded rectangles; passing in a more complicated outline fails silently.
68 */
69 if (!outline.getAsRoundRect(&possibleRect, &radius)) {
70 if (pendingClip) {
71 canvas->clipRect(*pendingClip);
72 }
73 return;
74 }
75
Stan Iliev021693b2016-10-17 16:26:15 -040076 SkRect rect = possibleRect.toSkRect();
77 if (radius != 0.0f) {
78 if (pendingClip && !pendingClip->contains(rect)) {
79 canvas->clipRect(*pendingClip);
80 }
Mike Reed6c67f1d2016-12-14 10:29:54 -050081 canvas->clipRRect(SkRRect::MakeRectXY(rect, radius, radius), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -040082 } else {
83 if (pendingClip) {
84 (void)rect.intersect(*pendingClip);
85 }
86 canvas->clipRect(rect);
87 }
88}
89
90const RenderProperties& RenderNodeDrawable::getNodeProperties() const {
91 return mRenderNode->properties();
92}
93
94void RenderNodeDrawable::onDraw(SkCanvas* canvas) {
Stan Iliev2f06e8a2016-11-02 15:29:03 -040095 //negative and positive Z order are drawn out of order, if this render node drawable is in
96 //a reordering section
97 if ((!mInReorderingSection) || MathUtils::isZero(mRenderNode->properties().getZ())) {
Stan Iliev021693b2016-10-17 16:26:15 -040098 this->forceDraw(canvas);
99 }
100}
101
102void RenderNodeDrawable::forceDraw(SkCanvas* canvas) {
103 RenderNode* renderNode = mRenderNode.get();
Stan Ilieve9d00122017-09-19 12:07:10 -0400104 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
Stan Iliev021693b2016-10-17 16:26:15 -0400105 SkRect dimensions = SkRect::MakeWH(renderNode->getWidth(), renderNode->getHeight());
106 canvas->drawAnnotation(dimensions, renderNode->getName(), nullptr);
107 }
108
109 // We only respect the nothingToDraw check when we are composing a layer. This
110 // ensures that we paint the layer even if it is not currently visible in the
111 // event that the properties change and it becomes visible.
112 if (!renderNode->isRenderable() || (renderNode->nothingToDraw() && mComposeLayer)) {
113 return;
114 }
115
116 SkASSERT(renderNode->getDisplayList()->isSkiaDL());
117 SkiaDisplayList* displayList = (SkiaDisplayList*)renderNode->getDisplayList();
118
119 SkAutoCanvasRestore acr(canvas, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400120 const RenderProperties& properties = this->getNodeProperties();
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500121 //pass this outline to the children that may clip backward projected nodes
122 displayList->mProjectedOutline = displayList->containsProjectionReceiver()
123 ? &properties.getOutline() : nullptr;
124 if (!properties.getProjectBackwards()) {
125 drawContent(canvas);
126 if (mProjectedDisplayList) {
127 acr.restore(); //draw projected children using parent matrix
128 LOG_ALWAYS_FATAL_IF(!mProjectedDisplayList->mProjectedOutline);
129 const bool shouldClip = mProjectedDisplayList->mProjectedOutline->getPath();
130 SkAutoCanvasRestore acr2(canvas, shouldClip);
131 canvas->setMatrix(mProjectedDisplayList->mProjectedReceiverParentMatrix);
132 if (shouldClip) {
133 clipOutline(*mProjectedDisplayList->mProjectedOutline, canvas, nullptr);
134 }
135 drawBackwardsProjectedNodes(canvas, *mProjectedDisplayList);
Stan Iliev021693b2016-10-17 16:26:15 -0400136 }
Stan Iliev021693b2016-10-17 16:26:15 -0400137 }
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500138 displayList->mProjectedOutline = nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400139}
140
141static bool layerNeedsPaint(const LayerProperties& properties,
142 float alphaMultiplier, SkPaint* paint) {
143 if (alphaMultiplier < 1.0f
144 || properties.alpha() < 255
145 || properties.xferMode() != SkBlendMode::kSrcOver
146 || properties.colorFilter() != nullptr) {
147 paint->setAlpha(properties.alpha() * alphaMultiplier);
148 paint->setBlendMode(properties.xferMode());
Derek Sollenbergerf87da672016-11-02 11:34:27 -0400149 paint->setColorFilter(sk_ref_sp(properties.colorFilter()));
Stan Iliev021693b2016-10-17 16:26:15 -0400150 return true;
151 }
152 return false;
153}
154
Stan Iliev1843ac72017-09-20 18:05:35 -0400155class AlphaFilterCanvas : public SkPaintFilterCanvas {
156public:
157 AlphaFilterCanvas(SkCanvas* canvas, float alpha) : SkPaintFilterCanvas(canvas), mAlpha(alpha) {}
158protected:
159 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type t) const override {
160 SkTLazy<SkPaint> defaultPaint;
161 if (!*paint) {
162 paint->init(*defaultPaint.init());
163 }
164 paint->writable()->setAlpha((uint8_t)(*paint)->getAlpha()*mAlpha);
165 return true;
166 }
167 void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override {
168 // We unroll the drawable using "this" canvas, so that draw calls contained inside will
169 // get their alpha applied. THe default SkPaintFilterCanvas::onDrawDrawable does not unroll.
170 drawable->draw(this, matrix);
171 }
172private:
173 float mAlpha;
174};
175
Stan Iliev021693b2016-10-17 16:26:15 -0400176void RenderNodeDrawable::drawContent(SkCanvas* canvas) const {
177 RenderNode* renderNode = mRenderNode.get();
178 float alphaMultiplier = 1.0f;
179 const RenderProperties& properties = renderNode->properties();
180
181 // If we are drawing the contents of layer, we don't want to apply any of
182 // the RenderNode's properties during this pass. Those will all be applied
183 // when the layer is composited.
184 if (mComposeLayer) {
185 setViewProperties(properties, canvas, &alphaMultiplier);
186 }
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500187 SkiaDisplayList* displayList = (SkiaDisplayList*)mRenderNode->getDisplayList();
188 if (displayList->containsProjectionReceiver()) {
189 displayList->mProjectedReceiverParentMatrix = canvas->getTotalMatrix();
190 }
Stan Iliev021693b2016-10-17 16:26:15 -0400191
192 //TODO should we let the bound of the drawable do this for us?
193 const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
194 bool quickRejected = properties.getClipToBounds() && canvas->quickReject(bounds);
195 if (!quickRejected) {
196 SkiaDisplayList* displayList = (SkiaDisplayList*)renderNode->getDisplayList();
197 const LayerProperties& layerProperties = properties.layerProperties();
198 // composing a hardware layer
199 if (renderNode->getLayerSurface() && mComposeLayer) {
200 SkASSERT(properties.effectiveLayerType() == LayerType::RenderLayer);
201 SkPaint* paint = nullptr;
202 SkPaint tmpPaint;
203 if (layerNeedsPaint(layerProperties, alphaMultiplier, &tmpPaint)) {
204 paint = &tmpPaint;
205 }
206 renderNode->getLayerSurface()->draw(canvas, 0, 0, paint);
Matt Sarett79756be2016-11-09 16:13:54 -0500207
Matt Sarettf58cc922016-11-14 18:33:38 -0500208 if (!renderNode->getSkiaLayer()->hasRenderedSinceRepaint) {
Matt Sarett79756be2016-11-09 16:13:54 -0500209 renderNode->getSkiaLayer()->hasRenderedSinceRepaint = true;
Matt Sarettf58cc922016-11-14 18:33:38 -0500210 if (CC_UNLIKELY(Properties::debugLayersUpdates)) {
211 SkPaint layerPaint;
212 layerPaint.setColor(0x7f00ff00);
213 canvas->drawRect(bounds, layerPaint);
214 } else if (CC_UNLIKELY(Properties::debugOverdraw)) {
215 // Render transparent rect to increment overdraw for repaint area.
216 // This can be "else if" because flashing green on layer updates
217 // will also increment the overdraw if it happens to be turned on.
218 SkPaint transparentPaint;
219 transparentPaint.setColor(SK_ColorTRANSPARENT);
220 canvas->drawRect(bounds, transparentPaint);
221 }
Matt Sarett79756be2016-11-09 16:13:54 -0500222 }
Matt Sarettf58cc922016-11-14 18:33:38 -0500223
Stan Iliev021693b2016-10-17 16:26:15 -0400224 // composing a software layer with alpha
225 } else if (properties.effectiveLayerType() == LayerType::Software) {
226 SkPaint paint;
227 bool needsLayer = layerNeedsPaint(layerProperties, alphaMultiplier, &paint);
228 if (needsLayer) {
229 canvas->saveLayer(bounds, &paint);
230 }
Derek Sollenbergerea1fe9b2017-03-01 13:02:43 -0500231 displayList->draw(canvas);
Stan Iliev021693b2016-10-17 16:26:15 -0400232 if (needsLayer) {
233 canvas->restore();
234 }
235 } else {
Stan Iliev1843ac72017-09-20 18:05:35 -0400236 if (alphaMultiplier < 1.0f) {
237 // Non-layer draw for a view with getHasOverlappingRendering=false, will apply
238 // the alpha to the paint of each nested draw.
239 AlphaFilterCanvas alphaCanvas(canvas, alphaMultiplier);
240 displayList->draw(&alphaCanvas);
241 } else {
242 displayList->draw(canvas);
243 }
Stan Iliev021693b2016-10-17 16:26:15 -0400244 }
245 }
246}
247
248void RenderNodeDrawable::setViewProperties(const RenderProperties& properties, SkCanvas* canvas,
249 float* alphaMultiplier) {
250 if (properties.getLeft() != 0 || properties.getTop() != 0) {
251 canvas->translate(properties.getLeft(), properties.getTop());
252 }
253 if (properties.getStaticMatrix()) {
254 canvas->concat(*properties.getStaticMatrix());
255 } else if (properties.getAnimationMatrix()) {
256 canvas->concat(*properties.getAnimationMatrix());
257 }
258 if (properties.hasTransformMatrix()) {
259 if (properties.isTransformTranslateOnly()) {
260 canvas->translate(properties.getTranslationX(), properties.getTranslationY());
261 } else {
262 canvas->concat(*properties.getTransformMatrix());
263 }
264 }
265 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
266 int clipFlags = properties.getClippingFlags();
267 if (properties.getAlpha() < 1) {
268 if (isLayer) {
269 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
270 }
271 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
272 *alphaMultiplier = properties.getAlpha();
273 } else {
274 // savelayer needed to create an offscreen buffer
275 Rect layerBounds(0, 0, properties.getWidth(), properties.getHeight());
276 if (clipFlags) {
277 properties.getClippingRectForFlags(clipFlags, &layerBounds);
278 clipFlags = 0; // all clipping done by savelayer
279 }
280 SkRect bounds = SkRect::MakeLTRB(layerBounds.left, layerBounds.top,
281 layerBounds.right, layerBounds.bottom);
282 canvas->saveLayerAlpha(&bounds, (int) (properties.getAlpha() * 255));
283 }
284
285 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
286 // pretend alpha always causes savelayer to warn about
287 // performance problem affecting old versions
288 ATRACE_FORMAT("alpha caused saveLayer %dx%d", properties.getWidth(),
289 properties.getHeight());
290 }
291 }
292
293 const SkRect* pendingClip = nullptr;
294 SkRect clipRect;
295
296 if (clipFlags) {
297 Rect tmpRect;
298 properties.getClippingRectForFlags(clipFlags, &tmpRect);
299 clipRect = tmpRect.toSkRect();
300 pendingClip = &clipRect;
301 }
302
303 if (properties.getRevealClip().willClip()) {
Mike Reed6c67f1d2016-12-14 10:29:54 -0500304 canvas->clipPath(*properties.getRevealClip().getPath(), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400305 } else if (properties.getOutline().willClip()) {
306 clipOutline(properties.getOutline(), canvas, pendingClip);
307 pendingClip = nullptr;
308 }
309
310 if (pendingClip) {
311 canvas->clipRect(*pendingClip);
312 }
313}
314
315}; // namespace skiapipeline
316}; // namespace uirenderer
317}; // namespace android