blob: 14cc44973c8fb9fced63f7ca010a9855f7910074 [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"
22
23namespace android {
24namespace uirenderer {
25namespace skiapipeline {
26
Stan Ilievdb45a4b2016-11-08 14:18:31 -050027void RenderNodeDrawable::drawBackwardsProjectedNodes(SkCanvas* canvas, const SkiaDisplayList& displayList,
28 int nestLevel) {
29 LOG_ALWAYS_FATAL_IF(0 == nestLevel && !displayList.mProjectionReceiver);
30 for (auto& child : displayList.mChildNodes) {
31 const RenderProperties& childProperties = child.getNodeProperties();
32
33 //immediate children cannot be projected on their parent
34 if (childProperties.getProjectBackwards() && nestLevel > 0) {
35 SkAutoCanvasRestore acr2(canvas, true);
36 //Apply recorded matrix, which is a total matrix saved at recording time to avoid
37 //replaying all DL commands.
38 canvas->concat(child.getRecordedMatrix());
39 child.drawContent(canvas);
40 }
41
42 //skip walking sub-nodes if current display list contains a receiver with exception of
43 //level 0, which is a known receiver
44 if (0 == nestLevel || !displayList.containsProjectionReceiver()) {
45 SkAutoCanvasRestore acr(canvas, true);
46 SkMatrix nodeMatrix;
47 mat4 hwuiMatrix(child.getRecordedMatrix());
48 auto childNode = child.getRenderNode();
49 childNode->applyViewPropertyTransforms(hwuiMatrix);
50 hwuiMatrix.copyTo(nodeMatrix);
51 canvas->concat(nodeMatrix);
52 SkiaDisplayList* childDisplayList = static_cast<SkiaDisplayList*>(
53 (const_cast<DisplayList*>(childNode->getDisplayList())));
54 if (childDisplayList) {
55 drawBackwardsProjectedNodes(canvas, *childDisplayList, nestLevel+1);
56 }
57 }
58 }
59}
60
Stan Iliev021693b2016-10-17 16:26:15 -040061static void clipOutline(const Outline& outline, SkCanvas* canvas, const SkRect* pendingClip) {
Stan Iliev021693b2016-10-17 16:26:15 -040062 Rect possibleRect;
63 float radius;
64 LOG_ALWAYS_FATAL_IF(!outline.getAsRoundRect(&possibleRect, &radius),
65 "clipping outlines should be at most roundedRects");
66 SkRect rect = possibleRect.toSkRect();
67 if (radius != 0.0f) {
68 if (pendingClip && !pendingClip->contains(rect)) {
69 canvas->clipRect(*pendingClip);
70 }
Mike Reed6e49c9f2016-12-02 15:36:59 -050071 canvas->clipRRect(SkRRect::MakeRectXY(rect, radius, radius), kIntersect_SkClipOp, true);
Stan Iliev021693b2016-10-17 16:26:15 -040072 } else {
73 if (pendingClip) {
74 (void)rect.intersect(*pendingClip);
75 }
76 canvas->clipRect(rect);
77 }
78}
79
80const RenderProperties& RenderNodeDrawable::getNodeProperties() const {
81 return mRenderNode->properties();
82}
83
84void RenderNodeDrawable::onDraw(SkCanvas* canvas) {
Stan Iliev2f06e8a2016-11-02 15:29:03 -040085 //negative and positive Z order are drawn out of order, if this render node drawable is in
86 //a reordering section
87 if ((!mInReorderingSection) || MathUtils::isZero(mRenderNode->properties().getZ())) {
Stan Iliev021693b2016-10-17 16:26:15 -040088 this->forceDraw(canvas);
89 }
90}
91
92void RenderNodeDrawable::forceDraw(SkCanvas* canvas) {
93 RenderNode* renderNode = mRenderNode.get();
Stan Iliev500a0c32016-10-26 10:30:09 -040094 if (SkiaPipeline::skpCaptureEnabled()) {
Stan Iliev021693b2016-10-17 16:26:15 -040095 SkRect dimensions = SkRect::MakeWH(renderNode->getWidth(), renderNode->getHeight());
96 canvas->drawAnnotation(dimensions, renderNode->getName(), nullptr);
97 }
98
99 // We only respect the nothingToDraw check when we are composing a layer. This
100 // ensures that we paint the layer even if it is not currently visible in the
101 // event that the properties change and it becomes visible.
102 if (!renderNode->isRenderable() || (renderNode->nothingToDraw() && mComposeLayer)) {
103 return;
104 }
105
106 SkASSERT(renderNode->getDisplayList()->isSkiaDL());
107 SkiaDisplayList* displayList = (SkiaDisplayList*)renderNode->getDisplayList();
108
109 SkAutoCanvasRestore acr(canvas, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400110 const RenderProperties& properties = this->getNodeProperties();
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500111 //pass this outline to the children that may clip backward projected nodes
112 displayList->mProjectedOutline = displayList->containsProjectionReceiver()
113 ? &properties.getOutline() : nullptr;
114 if (!properties.getProjectBackwards()) {
115 drawContent(canvas);
116 if (mProjectedDisplayList) {
117 acr.restore(); //draw projected children using parent matrix
118 LOG_ALWAYS_FATAL_IF(!mProjectedDisplayList->mProjectedOutline);
119 const bool shouldClip = mProjectedDisplayList->mProjectedOutline->getPath();
120 SkAutoCanvasRestore acr2(canvas, shouldClip);
121 canvas->setMatrix(mProjectedDisplayList->mProjectedReceiverParentMatrix);
122 if (shouldClip) {
123 clipOutline(*mProjectedDisplayList->mProjectedOutline, canvas, nullptr);
124 }
125 drawBackwardsProjectedNodes(canvas, *mProjectedDisplayList);
Stan Iliev021693b2016-10-17 16:26:15 -0400126 }
Stan Iliev021693b2016-10-17 16:26:15 -0400127 }
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500128 displayList->mProjectedOutline = nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400129}
130
131static bool layerNeedsPaint(const LayerProperties& properties,
132 float alphaMultiplier, SkPaint* paint) {
133 if (alphaMultiplier < 1.0f
134 || properties.alpha() < 255
135 || properties.xferMode() != SkBlendMode::kSrcOver
136 || properties.colorFilter() != nullptr) {
137 paint->setAlpha(properties.alpha() * alphaMultiplier);
138 paint->setBlendMode(properties.xferMode());
Derek Sollenbergerf87da672016-11-02 11:34:27 -0400139 paint->setColorFilter(sk_ref_sp(properties.colorFilter()));
Stan Iliev021693b2016-10-17 16:26:15 -0400140 return true;
141 }
142 return false;
143}
144
145void RenderNodeDrawable::drawContent(SkCanvas* canvas) const {
146 RenderNode* renderNode = mRenderNode.get();
147 float alphaMultiplier = 1.0f;
148 const RenderProperties& properties = renderNode->properties();
149
150 // If we are drawing the contents of layer, we don't want to apply any of
151 // the RenderNode's properties during this pass. Those will all be applied
152 // when the layer is composited.
153 if (mComposeLayer) {
154 setViewProperties(properties, canvas, &alphaMultiplier);
155 }
Stan Ilievdb45a4b2016-11-08 14:18:31 -0500156 SkiaDisplayList* displayList = (SkiaDisplayList*)mRenderNode->getDisplayList();
157 if (displayList->containsProjectionReceiver()) {
158 displayList->mProjectedReceiverParentMatrix = canvas->getTotalMatrix();
159 }
Stan Iliev021693b2016-10-17 16:26:15 -0400160
161 //TODO should we let the bound of the drawable do this for us?
162 const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
163 bool quickRejected = properties.getClipToBounds() && canvas->quickReject(bounds);
164 if (!quickRejected) {
165 SkiaDisplayList* displayList = (SkiaDisplayList*)renderNode->getDisplayList();
166 const LayerProperties& layerProperties = properties.layerProperties();
167 // composing a hardware layer
168 if (renderNode->getLayerSurface() && mComposeLayer) {
169 SkASSERT(properties.effectiveLayerType() == LayerType::RenderLayer);
170 SkPaint* paint = nullptr;
171 SkPaint tmpPaint;
172 if (layerNeedsPaint(layerProperties, alphaMultiplier, &tmpPaint)) {
173 paint = &tmpPaint;
174 }
175 renderNode->getLayerSurface()->draw(canvas, 0, 0, paint);
Matt Sarett79756be2016-11-09 16:13:54 -0500176
Matt Sarettf58cc922016-11-14 18:33:38 -0500177 if (!renderNode->getSkiaLayer()->hasRenderedSinceRepaint) {
Matt Sarett79756be2016-11-09 16:13:54 -0500178 renderNode->getSkiaLayer()->hasRenderedSinceRepaint = true;
Matt Sarettf58cc922016-11-14 18:33:38 -0500179 if (CC_UNLIKELY(Properties::debugLayersUpdates)) {
180 SkPaint layerPaint;
181 layerPaint.setColor(0x7f00ff00);
182 canvas->drawRect(bounds, layerPaint);
183 } else if (CC_UNLIKELY(Properties::debugOverdraw)) {
184 // Render transparent rect to increment overdraw for repaint area.
185 // This can be "else if" because flashing green on layer updates
186 // will also increment the overdraw if it happens to be turned on.
187 SkPaint transparentPaint;
188 transparentPaint.setColor(SK_ColorTRANSPARENT);
189 canvas->drawRect(bounds, transparentPaint);
190 }
Matt Sarett79756be2016-11-09 16:13:54 -0500191 }
Matt Sarettf58cc922016-11-14 18:33:38 -0500192
Stan Iliev021693b2016-10-17 16:26:15 -0400193 // composing a software layer with alpha
194 } else if (properties.effectiveLayerType() == LayerType::Software) {
195 SkPaint paint;
196 bool needsLayer = layerNeedsPaint(layerProperties, alphaMultiplier, &paint);
197 if (needsLayer) {
198 canvas->saveLayer(bounds, &paint);
199 }
200 canvas->drawDrawable(displayList->mDrawable.get());
201 if (needsLayer) {
202 canvas->restore();
203 }
204 } else {
205 canvas->drawDrawable(displayList->mDrawable.get());
206 }
207 }
208}
209
210void RenderNodeDrawable::setViewProperties(const RenderProperties& properties, SkCanvas* canvas,
211 float* alphaMultiplier) {
212 if (properties.getLeft() != 0 || properties.getTop() != 0) {
213 canvas->translate(properties.getLeft(), properties.getTop());
214 }
215 if (properties.getStaticMatrix()) {
216 canvas->concat(*properties.getStaticMatrix());
217 } else if (properties.getAnimationMatrix()) {
218 canvas->concat(*properties.getAnimationMatrix());
219 }
220 if (properties.hasTransformMatrix()) {
221 if (properties.isTransformTranslateOnly()) {
222 canvas->translate(properties.getTranslationX(), properties.getTranslationY());
223 } else {
224 canvas->concat(*properties.getTransformMatrix());
225 }
226 }
227 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
228 int clipFlags = properties.getClippingFlags();
229 if (properties.getAlpha() < 1) {
230 if (isLayer) {
231 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
232 }
233 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
234 *alphaMultiplier = properties.getAlpha();
235 } else {
236 // savelayer needed to create an offscreen buffer
237 Rect layerBounds(0, 0, properties.getWidth(), properties.getHeight());
238 if (clipFlags) {
239 properties.getClippingRectForFlags(clipFlags, &layerBounds);
240 clipFlags = 0; // all clipping done by savelayer
241 }
242 SkRect bounds = SkRect::MakeLTRB(layerBounds.left, layerBounds.top,
243 layerBounds.right, layerBounds.bottom);
244 canvas->saveLayerAlpha(&bounds, (int) (properties.getAlpha() * 255));
245 }
246
247 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
248 // pretend alpha always causes savelayer to warn about
249 // performance problem affecting old versions
250 ATRACE_FORMAT("alpha caused saveLayer %dx%d", properties.getWidth(),
251 properties.getHeight());
252 }
253 }
254
255 const SkRect* pendingClip = nullptr;
256 SkRect clipRect;
257
258 if (clipFlags) {
259 Rect tmpRect;
260 properties.getClippingRectForFlags(clipFlags, &tmpRect);
261 clipRect = tmpRect.toSkRect();
262 pendingClip = &clipRect;
263 }
264
265 if (properties.getRevealClip().willClip()) {
Mike Reed6e49c9f2016-12-02 15:36:59 -0500266 canvas->clipPath(*properties.getRevealClip().getPath(), kIntersect_SkClipOp, true);
Stan Iliev021693b2016-10-17 16:26:15 -0400267 } else if (properties.getOutline().willClip()) {
268 clipOutline(properties.getOutline(), canvas, pendingClip);
269 pendingClip = nullptr;
270 }
271
272 if (pendingClip) {
273 canvas->clipRect(*pendingClip);
274 }
275}
276
277}; // namespace skiapipeline
278}; // namespace uirenderer
279}; // namespace android