blob: 6c04d78629796f44fd7777612c19abdf7871d1dd [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 Reck1bcacfd2017-11-03 10:12:19 -070028void RenderNodeDrawable::drawBackwardsProjectedNodes(SkCanvas* canvas,
29 const SkiaDisplayList& displayList,
30 int nestLevel) {
Stan Ilievdb45a4b2016-11-08 14:18:31 -050031 LOG_ALWAYS_FATAL_IF(0 == nestLevel && !displayList.mProjectionReceiver);
32 for (auto& child : displayList.mChildNodes) {
33 const RenderProperties& childProperties = child.getNodeProperties();
34
John Reck1bcacfd2017-11-03 10:12:19 -070035 // immediate children cannot be projected on their parent
Stan Ilievdb45a4b2016-11-08 14:18:31 -050036 if (childProperties.getProjectBackwards() && nestLevel > 0) {
37 SkAutoCanvasRestore acr2(canvas, true);
John Reck1bcacfd2017-11-03 10:12:19 -070038 // Apply recorded matrix, which is a total matrix saved at recording time to avoid
39 // replaying all DL commands.
Stan Ilievdb45a4b2016-11-08 14:18:31 -050040 canvas->concat(child.getRecordedMatrix());
41 child.drawContent(canvas);
42 }
43
John Reck1bcacfd2017-11-03 10:12:19 -070044 // skip walking sub-nodes if current display list contains a receiver with exception of
45 // level 0, which is a known receiver
Stan Ilievdb45a4b2016-11-08 14:18:31 -050046 if (0 == nestLevel || !displayList.containsProjectionReceiver()) {
47 SkAutoCanvasRestore acr(canvas, true);
48 SkMatrix nodeMatrix;
49 mat4 hwuiMatrix(child.getRecordedMatrix());
50 auto childNode = child.getRenderNode();
51 childNode->applyViewPropertyTransforms(hwuiMatrix);
52 hwuiMatrix.copyTo(nodeMatrix);
53 canvas->concat(nodeMatrix);
54 SkiaDisplayList* childDisplayList = static_cast<SkiaDisplayList*>(
John Reck1bcacfd2017-11-03 10:12:19 -070055 (const_cast<DisplayList*>(childNode->getDisplayList())));
Stan Ilievdb45a4b2016-11-08 14:18:31 -050056 if (childDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -070057 drawBackwardsProjectedNodes(canvas, *childDisplayList, nestLevel + 1);
Stan Ilievdb45a4b2016-11-08 14:18:31 -050058 }
59 }
60 }
61}
62
Stan Iliev021693b2016-10-17 16:26:15 -040063static void clipOutline(const Outline& outline, SkCanvas* canvas, const SkRect* pendingClip) {
Stan Iliev021693b2016-10-17 16:26:15 -040064 Rect possibleRect;
65 float radius;
Derek Sollenbergerf209c062017-05-26 12:11:34 -040066
67 /* To match the existing HWUI behavior we only supports rectangles or
68 * rounded rectangles; passing in a more complicated outline fails silently.
69 */
70 if (!outline.getAsRoundRect(&possibleRect, &radius)) {
71 if (pendingClip) {
72 canvas->clipRect(*pendingClip);
73 }
74 return;
75 }
76
Stan Iliev021693b2016-10-17 16:26:15 -040077 SkRect rect = possibleRect.toSkRect();
78 if (radius != 0.0f) {
79 if (pendingClip && !pendingClip->contains(rect)) {
80 canvas->clipRect(*pendingClip);
81 }
Mike Reed6c67f1d2016-12-14 10:29:54 -050082 canvas->clipRRect(SkRRect::MakeRectXY(rect, radius, radius), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -040083 } else {
84 if (pendingClip) {
85 (void)rect.intersect(*pendingClip);
86 }
87 canvas->clipRect(rect);
88 }
89}
90
91const RenderProperties& RenderNodeDrawable::getNodeProperties() const {
92 return mRenderNode->properties();
93}
94
95void RenderNodeDrawable::onDraw(SkCanvas* canvas) {
John Reck1bcacfd2017-11-03 10:12:19 -070096 // negative and positive Z order are drawn out of order, if this render node drawable is in
97 // a reordering section
Stan Iliev2f06e8a2016-11-02 15:29:03 -040098 if ((!mInReorderingSection) || MathUtils::isZero(mRenderNode->properties().getZ())) {
Stan Iliev021693b2016-10-17 16:26:15 -040099 this->forceDraw(canvas);
100 }
101}
102
103void RenderNodeDrawable::forceDraw(SkCanvas* canvas) {
104 RenderNode* renderNode = mRenderNode.get();
Stan Ilieve9d00122017-09-19 12:07:10 -0400105 if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
Stan Iliev021693b2016-10-17 16:26:15 -0400106 SkRect dimensions = SkRect::MakeWH(renderNode->getWidth(), renderNode->getHeight());
107 canvas->drawAnnotation(dimensions, renderNode->getName(), nullptr);
108 }
109
110 // We only respect the nothingToDraw check when we are composing a layer. This
111 // ensures that we paint the layer even if it is not currently visible in the
112 // event that the properties change and it becomes visible.
Yuqian Li5ebbc8e2017-11-29 09:53:06 -0500113 if ((mProjectedDisplayList == nullptr && !renderNode->isRenderable()) ||
114 (renderNode->nothingToDraw() && mComposeLayer)) {
Stan Iliev021693b2016-10-17 16:26:15 -0400115 return;
116 }
117
118 SkASSERT(renderNode->getDisplayList()->isSkiaDL());
119 SkiaDisplayList* displayList = (SkiaDisplayList*)renderNode->getDisplayList();
120
121 SkAutoCanvasRestore acr(canvas, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400122 const RenderProperties& properties = this->getNodeProperties();
John Reck1bcacfd2017-11-03 10:12:19 -0700123 // pass this outline to the children that may clip backward projected nodes
124 displayList->mProjectedOutline =
125 displayList->containsProjectionReceiver() ? &properties.getOutline() : nullptr;
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500126 if (!properties.getProjectBackwards()) {
127 drawContent(canvas);
128 if (mProjectedDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700129 acr.restore(); // draw projected children using parent matrix
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500130 LOG_ALWAYS_FATAL_IF(!mProjectedDisplayList->mProjectedOutline);
131 const bool shouldClip = mProjectedDisplayList->mProjectedOutline->getPath();
132 SkAutoCanvasRestore acr2(canvas, shouldClip);
133 canvas->setMatrix(mProjectedDisplayList->mProjectedReceiverParentMatrix);
134 if (shouldClip) {
135 clipOutline(*mProjectedDisplayList->mProjectedOutline, canvas, nullptr);
136 }
137 drawBackwardsProjectedNodes(canvas, *mProjectedDisplayList);
Stan Iliev021693b2016-10-17 16:26:15 -0400138 }
Stan Iliev021693b2016-10-17 16:26:15 -0400139 }
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500140 displayList->mProjectedOutline = nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400141}
142
John Reck1bcacfd2017-11-03 10:12:19 -0700143static bool layerNeedsPaint(const LayerProperties& properties, float alphaMultiplier,
144 SkPaint* paint) {
Stan Iliev7e100e92018-01-22 10:36:33 -0500145 paint->setFilterQuality(kLow_SkFilterQuality);
John Reck1bcacfd2017-11-03 10:12:19 -0700146 if (alphaMultiplier < 1.0f || properties.alpha() < 255 ||
147 properties.xferMode() != SkBlendMode::kSrcOver || properties.colorFilter() != nullptr) {
Stan Iliev021693b2016-10-17 16:26:15 -0400148 paint->setAlpha(properties.alpha() * alphaMultiplier);
149 paint->setBlendMode(properties.xferMode());
Derek Sollenbergerf87da672016-11-02 11:34:27 -0400150 paint->setColorFilter(sk_ref_sp(properties.colorFilter()));
Stan Iliev021693b2016-10-17 16:26:15 -0400151 return true;
152 }
153 return false;
154}
155
Stan Iliev1843ac72017-09-20 18:05:35 -0400156class AlphaFilterCanvas : public SkPaintFilterCanvas {
157public:
158 AlphaFilterCanvas(SkCanvas* canvas, float alpha) : SkPaintFilterCanvas(canvas), mAlpha(alpha) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700159
Stan Iliev1843ac72017-09-20 18:05:35 -0400160protected:
161 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type t) const override {
162 SkTLazy<SkPaint> defaultPaint;
163 if (!*paint) {
164 paint->init(*defaultPaint.init());
165 }
John Reck1bcacfd2017-11-03 10:12:19 -0700166 paint->writable()->setAlpha((uint8_t)(*paint)->getAlpha() * mAlpha);
Stan Iliev1843ac72017-09-20 18:05:35 -0400167 return true;
168 }
169 void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override {
170 // We unroll the drawable using "this" canvas, so that draw calls contained inside will
171 // get their alpha applied. THe default SkPaintFilterCanvas::onDrawDrawable does not unroll.
172 drawable->draw(this, matrix);
173 }
John Reck1bcacfd2017-11-03 10:12:19 -0700174
Stan Iliev1843ac72017-09-20 18:05:35 -0400175private:
176 float mAlpha;
177};
178
Stan Iliev021693b2016-10-17 16:26:15 -0400179void RenderNodeDrawable::drawContent(SkCanvas* canvas) const {
180 RenderNode* renderNode = mRenderNode.get();
181 float alphaMultiplier = 1.0f;
182 const RenderProperties& properties = renderNode->properties();
183
184 // If we are drawing the contents of layer, we don't want to apply any of
185 // the RenderNode's properties during this pass. Those will all be applied
186 // when the layer is composited.
187 if (mComposeLayer) {
188 setViewProperties(properties, canvas, &alphaMultiplier);
189 }
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500190 SkiaDisplayList* displayList = (SkiaDisplayList*)mRenderNode->getDisplayList();
191 if (displayList->containsProjectionReceiver()) {
192 displayList->mProjectedReceiverParentMatrix = canvas->getTotalMatrix();
193 }
Stan Iliev021693b2016-10-17 16:26:15 -0400194
John Reck1bcacfd2017-11-03 10:12:19 -0700195 // TODO should we let the bound of the drawable do this for us?
Stan Iliev021693b2016-10-17 16:26:15 -0400196 const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
197 bool quickRejected = properties.getClipToBounds() && canvas->quickReject(bounds);
198 if (!quickRejected) {
199 SkiaDisplayList* displayList = (SkiaDisplayList*)renderNode->getDisplayList();
200 const LayerProperties& layerProperties = properties.layerProperties();
201 // composing a hardware layer
202 if (renderNode->getLayerSurface() && mComposeLayer) {
203 SkASSERT(properties.effectiveLayerType() == LayerType::RenderLayer);
Stan Iliev7e100e92018-01-22 10:36:33 -0500204 SkPaint paint;
205 layerNeedsPaint(layerProperties, alphaMultiplier, &paint);
Derek Sollenberger03e6cff72017-12-04 15:07:08 -0500206
207 // surfaces for layers are created on LAYER_SIZE boundaries (which are >= layer size) so
208 // we need to restrict the portion of the surface drawn to the size of the renderNode.
209 SkASSERT(renderNode->getLayerSurface()->width() >= bounds.width());
210 SkASSERT(renderNode->getLayerSurface()->height() >= bounds.height());
211 canvas->drawImageRect(renderNode->getLayerSurface()->makeImageSnapshot().get(),
Stan Iliev7e100e92018-01-22 10:36:33 -0500212 bounds, bounds, &paint);
Matt Sarett79756be2016-11-09 16:13:54 -0500213
Matt Sarettf58cc922016-11-14 18:33:38 -0500214 if (!renderNode->getSkiaLayer()->hasRenderedSinceRepaint) {
Matt Sarett79756be2016-11-09 16:13:54 -0500215 renderNode->getSkiaLayer()->hasRenderedSinceRepaint = true;
Matt Sarettf58cc922016-11-14 18:33:38 -0500216 if (CC_UNLIKELY(Properties::debugLayersUpdates)) {
217 SkPaint layerPaint;
218 layerPaint.setColor(0x7f00ff00);
219 canvas->drawRect(bounds, layerPaint);
220 } else if (CC_UNLIKELY(Properties::debugOverdraw)) {
221 // Render transparent rect to increment overdraw for repaint area.
222 // This can be "else if" because flashing green on layer updates
223 // will also increment the overdraw if it happens to be turned on.
224 SkPaint transparentPaint;
225 transparentPaint.setColor(SK_ColorTRANSPARENT);
226 canvas->drawRect(bounds, transparentPaint);
227 }
Matt Sarett79756be2016-11-09 16:13:54 -0500228 }
Stan Iliev021693b2016-10-17 16:26:15 -0400229 } else {
Stan Iliev1843ac72017-09-20 18:05:35 -0400230 if (alphaMultiplier < 1.0f) {
231 // Non-layer draw for a view with getHasOverlappingRendering=false, will apply
232 // the alpha to the paint of each nested draw.
233 AlphaFilterCanvas alphaCanvas(canvas, alphaMultiplier);
234 displayList->draw(&alphaCanvas);
235 } else {
236 displayList->draw(canvas);
237 }
Stan Iliev021693b2016-10-17 16:26:15 -0400238 }
239 }
240}
241
242void RenderNodeDrawable::setViewProperties(const RenderProperties& properties, SkCanvas* canvas,
John Reck1bcacfd2017-11-03 10:12:19 -0700243 float* alphaMultiplier) {
Stan Iliev021693b2016-10-17 16:26:15 -0400244 if (properties.getLeft() != 0 || properties.getTop() != 0) {
245 canvas->translate(properties.getLeft(), properties.getTop());
246 }
247 if (properties.getStaticMatrix()) {
248 canvas->concat(*properties.getStaticMatrix());
249 } else if (properties.getAnimationMatrix()) {
250 canvas->concat(*properties.getAnimationMatrix());
251 }
252 if (properties.hasTransformMatrix()) {
253 if (properties.isTransformTranslateOnly()) {
254 canvas->translate(properties.getTranslationX(), properties.getTranslationY());
255 } else {
256 canvas->concat(*properties.getTransformMatrix());
257 }
258 }
259 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
260 int clipFlags = properties.getClippingFlags();
261 if (properties.getAlpha() < 1) {
262 if (isLayer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700263 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
Stan Iliev021693b2016-10-17 16:26:15 -0400264 }
265 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
266 *alphaMultiplier = properties.getAlpha();
267 } else {
268 // savelayer needed to create an offscreen buffer
269 Rect layerBounds(0, 0, properties.getWidth(), properties.getHeight());
270 if (clipFlags) {
271 properties.getClippingRectForFlags(clipFlags, &layerBounds);
John Reck1bcacfd2017-11-03 10:12:19 -0700272 clipFlags = 0; // all clipping done by savelayer
Stan Iliev021693b2016-10-17 16:26:15 -0400273 }
John Reck1bcacfd2017-11-03 10:12:19 -0700274 SkRect bounds = SkRect::MakeLTRB(layerBounds.left, layerBounds.top, layerBounds.right,
275 layerBounds.bottom);
276 canvas->saveLayerAlpha(&bounds, (int)(properties.getAlpha() * 255));
Stan Iliev021693b2016-10-17 16:26:15 -0400277 }
278
279 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
280 // pretend alpha always causes savelayer to warn about
281 // performance problem affecting old versions
282 ATRACE_FORMAT("alpha caused saveLayer %dx%d", properties.getWidth(),
John Reck1bcacfd2017-11-03 10:12:19 -0700283 properties.getHeight());
Stan Iliev021693b2016-10-17 16:26:15 -0400284 }
285 }
286
287 const SkRect* pendingClip = nullptr;
288 SkRect clipRect;
289
290 if (clipFlags) {
291 Rect tmpRect;
292 properties.getClippingRectForFlags(clipFlags, &tmpRect);
293 clipRect = tmpRect.toSkRect();
294 pendingClip = &clipRect;
295 }
296
297 if (properties.getRevealClip().willClip()) {
Mike Reed6c67f1d2016-12-14 10:29:54 -0500298 canvas->clipPath(*properties.getRevealClip().getPath(), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400299 } else if (properties.getOutline().willClip()) {
300 clipOutline(properties.getOutline(), canvas, pendingClip);
301 pendingClip = nullptr;
302 }
303
304 if (pendingClip) {
305 canvas->clipRect(*pendingClip);
306 }
307}
308
John Reck1bcacfd2017-11-03 10:12:19 -0700309}; // namespace skiapipeline
310}; // namespace uirenderer
311}; // namespace android