blob: 845acc0fc0b2ecd14b478bdc946de5b55116d344 [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) {
145 if (alphaMultiplier < 1.0f || properties.alpha() < 255 ||
146 properties.xferMode() != SkBlendMode::kSrcOver || properties.colorFilter() != nullptr) {
Stan Iliev021693b2016-10-17 16:26:15 -0400147 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) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700158
Stan Iliev1843ac72017-09-20 18:05:35 -0400159protected:
160 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type t) const override {
161 SkTLazy<SkPaint> defaultPaint;
162 if (!*paint) {
163 paint->init(*defaultPaint.init());
164 }
John Reck1bcacfd2017-11-03 10:12:19 -0700165 paint->writable()->setAlpha((uint8_t)(*paint)->getAlpha() * mAlpha);
Stan Iliev1843ac72017-09-20 18:05:35 -0400166 return true;
167 }
168 void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override {
169 // We unroll the drawable using "this" canvas, so that draw calls contained inside will
170 // get their alpha applied. THe default SkPaintFilterCanvas::onDrawDrawable does not unroll.
171 drawable->draw(this, matrix);
172 }
John Reck1bcacfd2017-11-03 10:12:19 -0700173
Stan Iliev1843ac72017-09-20 18:05:35 -0400174private:
175 float mAlpha;
176};
177
Stan Iliev021693b2016-10-17 16:26:15 -0400178void RenderNodeDrawable::drawContent(SkCanvas* canvas) const {
179 RenderNode* renderNode = mRenderNode.get();
180 float alphaMultiplier = 1.0f;
181 const RenderProperties& properties = renderNode->properties();
182
183 // If we are drawing the contents of layer, we don't want to apply any of
184 // the RenderNode's properties during this pass. Those will all be applied
185 // when the layer is composited.
186 if (mComposeLayer) {
187 setViewProperties(properties, canvas, &alphaMultiplier);
188 }
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500189 SkiaDisplayList* displayList = (SkiaDisplayList*)mRenderNode->getDisplayList();
190 if (displayList->containsProjectionReceiver()) {
191 displayList->mProjectedReceiverParentMatrix = canvas->getTotalMatrix();
192 }
Stan Iliev021693b2016-10-17 16:26:15 -0400193
John Reck1bcacfd2017-11-03 10:12:19 -0700194 // TODO should we let the bound of the drawable do this for us?
Stan Iliev021693b2016-10-17 16:26:15 -0400195 const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
196 bool quickRejected = properties.getClipToBounds() && canvas->quickReject(bounds);
197 if (!quickRejected) {
198 SkiaDisplayList* displayList = (SkiaDisplayList*)renderNode->getDisplayList();
199 const LayerProperties& layerProperties = properties.layerProperties();
200 // composing a hardware layer
201 if (renderNode->getLayerSurface() && mComposeLayer) {
202 SkASSERT(properties.effectiveLayerType() == LayerType::RenderLayer);
203 SkPaint* paint = nullptr;
204 SkPaint tmpPaint;
205 if (layerNeedsPaint(layerProperties, alphaMultiplier, &tmpPaint)) {
206 paint = &tmpPaint;
207 }
208 renderNode->getLayerSurface()->draw(canvas, 0, 0, paint);
Matt Sarett79756be2016-11-09 16:13:54 -0500209
Matt Sarettf58cc922016-11-14 18:33:38 -0500210 if (!renderNode->getSkiaLayer()->hasRenderedSinceRepaint) {
Matt Sarett79756be2016-11-09 16:13:54 -0500211 renderNode->getSkiaLayer()->hasRenderedSinceRepaint = true;
Matt Sarettf58cc922016-11-14 18:33:38 -0500212 if (CC_UNLIKELY(Properties::debugLayersUpdates)) {
213 SkPaint layerPaint;
214 layerPaint.setColor(0x7f00ff00);
215 canvas->drawRect(bounds, layerPaint);
216 } else if (CC_UNLIKELY(Properties::debugOverdraw)) {
217 // Render transparent rect to increment overdraw for repaint area.
218 // This can be "else if" because flashing green on layer updates
219 // will also increment the overdraw if it happens to be turned on.
220 SkPaint transparentPaint;
221 transparentPaint.setColor(SK_ColorTRANSPARENT);
222 canvas->drawRect(bounds, transparentPaint);
223 }
Matt Sarett79756be2016-11-09 16:13:54 -0500224 }
Matt Sarettf58cc922016-11-14 18:33:38 -0500225
John Reck1bcacfd2017-11-03 10:12:19 -0700226 // composing a software layer with alpha
Stan Iliev021693b2016-10-17 16:26:15 -0400227 } else if (properties.effectiveLayerType() == LayerType::Software) {
228 SkPaint paint;
229 bool needsLayer = layerNeedsPaint(layerProperties, alphaMultiplier, &paint);
230 if (needsLayer) {
231 canvas->saveLayer(bounds, &paint);
232 }
Derek Sollenbergerea1fe9b2017-03-01 13:02:43 -0500233 displayList->draw(canvas);
Stan Iliev021693b2016-10-17 16:26:15 -0400234 if (needsLayer) {
235 canvas->restore();
236 }
237 } else {
Stan Iliev1843ac72017-09-20 18:05:35 -0400238 if (alphaMultiplier < 1.0f) {
239 // Non-layer draw for a view with getHasOverlappingRendering=false, will apply
240 // the alpha to the paint of each nested draw.
241 AlphaFilterCanvas alphaCanvas(canvas, alphaMultiplier);
242 displayList->draw(&alphaCanvas);
243 } else {
244 displayList->draw(canvas);
245 }
Stan Iliev021693b2016-10-17 16:26:15 -0400246 }
247 }
248}
249
250void RenderNodeDrawable::setViewProperties(const RenderProperties& properties, SkCanvas* canvas,
John Reck1bcacfd2017-11-03 10:12:19 -0700251 float* alphaMultiplier) {
Stan Iliev021693b2016-10-17 16:26:15 -0400252 if (properties.getLeft() != 0 || properties.getTop() != 0) {
253 canvas->translate(properties.getLeft(), properties.getTop());
254 }
255 if (properties.getStaticMatrix()) {
256 canvas->concat(*properties.getStaticMatrix());
257 } else if (properties.getAnimationMatrix()) {
258 canvas->concat(*properties.getAnimationMatrix());
259 }
260 if (properties.hasTransformMatrix()) {
261 if (properties.isTransformTranslateOnly()) {
262 canvas->translate(properties.getTranslationX(), properties.getTranslationY());
263 } else {
264 canvas->concat(*properties.getTransformMatrix());
265 }
266 }
267 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
268 int clipFlags = properties.getClippingFlags();
269 if (properties.getAlpha() < 1) {
270 if (isLayer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700271 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
Stan Iliev021693b2016-10-17 16:26:15 -0400272 }
273 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
274 *alphaMultiplier = properties.getAlpha();
275 } else {
276 // savelayer needed to create an offscreen buffer
277 Rect layerBounds(0, 0, properties.getWidth(), properties.getHeight());
278 if (clipFlags) {
279 properties.getClippingRectForFlags(clipFlags, &layerBounds);
John Reck1bcacfd2017-11-03 10:12:19 -0700280 clipFlags = 0; // all clipping done by savelayer
Stan Iliev021693b2016-10-17 16:26:15 -0400281 }
John Reck1bcacfd2017-11-03 10:12:19 -0700282 SkRect bounds = SkRect::MakeLTRB(layerBounds.left, layerBounds.top, layerBounds.right,
283 layerBounds.bottom);
284 canvas->saveLayerAlpha(&bounds, (int)(properties.getAlpha() * 255));
Stan Iliev021693b2016-10-17 16:26:15 -0400285 }
286
287 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
288 // pretend alpha always causes savelayer to warn about
289 // performance problem affecting old versions
290 ATRACE_FORMAT("alpha caused saveLayer %dx%d", properties.getWidth(),
John Reck1bcacfd2017-11-03 10:12:19 -0700291 properties.getHeight());
Stan Iliev021693b2016-10-17 16:26:15 -0400292 }
293 }
294
295 const SkRect* pendingClip = nullptr;
296 SkRect clipRect;
297
298 if (clipFlags) {
299 Rect tmpRect;
300 properties.getClippingRectForFlags(clipFlags, &tmpRect);
301 clipRect = tmpRect.toSkRect();
302 pendingClip = &clipRect;
303 }
304
305 if (properties.getRevealClip().willClip()) {
Mike Reed6c67f1d2016-12-14 10:29:54 -0500306 canvas->clipPath(*properties.getRevealClip().getPath(), SkClipOp::kIntersect, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400307 } else if (properties.getOutline().willClip()) {
308 clipOutline(properties.getOutline(), canvas, pendingClip);
309 pendingClip = nullptr;
310 }
311
312 if (pendingClip) {
313 canvas->clipRect(*pendingClip);
314 }
315}
316
John Reck1bcacfd2017-11-03 10:12:19 -0700317}; // namespace skiapipeline
318}; // namespace uirenderer
319}; // namespace android