2016-10-17 16:26:15 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "RenderNodeDrawable.h"
|
2017-11-03 10:12:19 -07:00
|
|
|
#include <SkPaintFilterCanvas.h>
|
2016-10-17 16:26:15 -04:00
|
|
|
#include "RenderNode.h"
|
|
|
|
#include "SkiaDisplayList.h"
|
|
|
|
#include "utils/TraceUtils.h"
|
|
|
|
|
2018-08-20 18:36:05 -04:00
|
|
|
#include <optional>
|
|
|
|
|
2016-10-17 16:26:15 -04:00
|
|
|
namespace android {
|
|
|
|
namespace uirenderer {
|
|
|
|
namespace skiapipeline {
|
|
|
|
|
2018-05-03 14:40:56 -07:00
|
|
|
RenderNodeDrawable::RenderNodeDrawable(RenderNode* node, SkCanvas* canvas, bool composeLayer,
|
|
|
|
bool inReorderingSection)
|
|
|
|
: mRenderNode(node)
|
|
|
|
, mRecordedTransform(canvas->getTotalMatrix())
|
|
|
|
, mComposeLayer(composeLayer)
|
|
|
|
, mInReorderingSection(inReorderingSection) {}
|
|
|
|
|
|
|
|
RenderNodeDrawable::~RenderNodeDrawable() {
|
|
|
|
// Just here to move the destructor into the cpp file where we can access RenderNode.
|
|
|
|
|
|
|
|
// TODO: Detangle the header nightmare.
|
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
void RenderNodeDrawable::drawBackwardsProjectedNodes(SkCanvas* canvas,
|
|
|
|
const SkiaDisplayList& displayList,
|
2019-11-25 09:34:21 -05:00
|
|
|
int nestLevel) const {
|
2016-11-08 14:18:31 -05:00
|
|
|
LOG_ALWAYS_FATAL_IF(0 == nestLevel && !displayList.mProjectionReceiver);
|
|
|
|
for (auto& child : displayList.mChildNodes) {
|
|
|
|
const RenderProperties& childProperties = child.getNodeProperties();
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
// immediate children cannot be projected on their parent
|
2016-11-08 14:18:31 -05:00
|
|
|
if (childProperties.getProjectBackwards() && nestLevel > 0) {
|
|
|
|
SkAutoCanvasRestore acr2(canvas, true);
|
2017-11-03 10:12:19 -07:00
|
|
|
// Apply recorded matrix, which is a total matrix saved at recording time to avoid
|
|
|
|
// replaying all DL commands.
|
2016-11-08 14:18:31 -05:00
|
|
|
canvas->concat(child.getRecordedMatrix());
|
|
|
|
child.drawContent(canvas);
|
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
// skip walking sub-nodes if current display list contains a receiver with exception of
|
|
|
|
// level 0, which is a known receiver
|
2016-11-08 14:18:31 -05:00
|
|
|
if (0 == nestLevel || !displayList.containsProjectionReceiver()) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
SkMatrix nodeMatrix;
|
|
|
|
mat4 hwuiMatrix(child.getRecordedMatrix());
|
|
|
|
auto childNode = child.getRenderNode();
|
|
|
|
childNode->applyViewPropertyTransforms(hwuiMatrix);
|
|
|
|
hwuiMatrix.copyTo(nodeMatrix);
|
|
|
|
canvas->concat(nodeMatrix);
|
|
|
|
SkiaDisplayList* childDisplayList = static_cast<SkiaDisplayList*>(
|
2017-11-03 10:12:19 -07:00
|
|
|
(const_cast<DisplayList*>(childNode->getDisplayList())));
|
2016-11-08 14:18:31 -05:00
|
|
|
if (childDisplayList) {
|
2017-11-03 10:12:19 -07:00
|
|
|
drawBackwardsProjectedNodes(canvas, *childDisplayList, nestLevel + 1);
|
2016-11-08 14:18:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-17 16:26:15 -04:00
|
|
|
static void clipOutline(const Outline& outline, SkCanvas* canvas, const SkRect* pendingClip) {
|
|
|
|
Rect possibleRect;
|
|
|
|
float radius;
|
2017-05-26 12:11:34 -04:00
|
|
|
|
|
|
|
/* To match the existing HWUI behavior we only supports rectangles or
|
|
|
|
* rounded rectangles; passing in a more complicated outline fails silently.
|
|
|
|
*/
|
|
|
|
if (!outline.getAsRoundRect(&possibleRect, &radius)) {
|
|
|
|
if (pendingClip) {
|
|
|
|
canvas->clipRect(*pendingClip);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-17 16:26:15 -04:00
|
|
|
SkRect rect = possibleRect.toSkRect();
|
|
|
|
if (radius != 0.0f) {
|
|
|
|
if (pendingClip && !pendingClip->contains(rect)) {
|
|
|
|
canvas->clipRect(*pendingClip);
|
|
|
|
}
|
2016-12-14 10:29:54 -05:00
|
|
|
canvas->clipRRect(SkRRect::MakeRectXY(rect, radius, radius), SkClipOp::kIntersect, true);
|
2016-10-17 16:26:15 -04:00
|
|
|
} else {
|
|
|
|
if (pendingClip) {
|
|
|
|
(void)rect.intersect(*pendingClip);
|
|
|
|
}
|
|
|
|
canvas->clipRect(rect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const RenderProperties& RenderNodeDrawable::getNodeProperties() const {
|
|
|
|
return mRenderNode->properties();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderNodeDrawable::onDraw(SkCanvas* canvas) {
|
2017-11-03 10:12:19 -07:00
|
|
|
// negative and positive Z order are drawn out of order, if this render node drawable is in
|
|
|
|
// a reordering section
|
2016-11-02 15:29:03 -04:00
|
|
|
if ((!mInReorderingSection) || MathUtils::isZero(mRenderNode->properties().getZ())) {
|
2016-10-17 16:26:15 -04:00
|
|
|
this->forceDraw(canvas);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 09:44:10 -08:00
|
|
|
class MarkDraw {
|
|
|
|
public:
|
|
|
|
explicit MarkDraw(SkCanvas& canvas, RenderNode& node) : mCanvas(canvas), mNode(node) {
|
|
|
|
if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
|
|
|
|
mNode.markDrawStart(mCanvas);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~MarkDraw() {
|
|
|
|
if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
|
|
|
|
mNode.markDrawEnd(mCanvas);
|
|
|
|
}
|
|
|
|
}
|
2018-12-13 16:40:14 -08:00
|
|
|
|
2018-11-29 09:44:10 -08:00
|
|
|
private:
|
|
|
|
SkCanvas& mCanvas;
|
|
|
|
RenderNode& mNode;
|
|
|
|
};
|
|
|
|
|
2019-11-25 09:34:21 -05:00
|
|
|
void RenderNodeDrawable::forceDraw(SkCanvas* canvas) const {
|
2016-10-17 16:26:15 -04:00
|
|
|
RenderNode* renderNode = mRenderNode.get();
|
2018-11-29 09:44:10 -08:00
|
|
|
MarkDraw _marker{*canvas, *renderNode};
|
2016-10-17 16:26:15 -04:00
|
|
|
|
|
|
|
// We only respect the nothingToDraw check when we are composing a layer. This
|
|
|
|
// ensures that we paint the layer even if it is not currently visible in the
|
|
|
|
// event that the properties change and it becomes visible.
|
2017-11-29 09:53:06 -05:00
|
|
|
if ((mProjectedDisplayList == nullptr && !renderNode->isRenderable()) ||
|
2018-12-13 16:40:14 -08:00
|
|
|
(renderNode->nothingToDraw() && mComposeLayer)) {
|
2016-10-17 16:26:15 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkiaDisplayList* displayList = (SkiaDisplayList*)renderNode->getDisplayList();
|
|
|
|
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
const RenderProperties& properties = this->getNodeProperties();
|
2017-11-03 10:12:19 -07:00
|
|
|
// pass this outline to the children that may clip backward projected nodes
|
|
|
|
displayList->mProjectedOutline =
|
|
|
|
displayList->containsProjectionReceiver() ? &properties.getOutline() : nullptr;
|
2016-11-08 14:18:31 -05:00
|
|
|
if (!properties.getProjectBackwards()) {
|
|
|
|
drawContent(canvas);
|
|
|
|
if (mProjectedDisplayList) {
|
2017-11-03 10:12:19 -07:00
|
|
|
acr.restore(); // draw projected children using parent matrix
|
2016-11-08 14:18:31 -05:00
|
|
|
LOG_ALWAYS_FATAL_IF(!mProjectedDisplayList->mProjectedOutline);
|
|
|
|
const bool shouldClip = mProjectedDisplayList->mProjectedOutline->getPath();
|
|
|
|
SkAutoCanvasRestore acr2(canvas, shouldClip);
|
2018-06-14 18:00:10 -04:00
|
|
|
canvas->setMatrix(mProjectedDisplayList->mParentMatrix);
|
2016-11-08 14:18:31 -05:00
|
|
|
if (shouldClip) {
|
2019-02-22 10:16:43 -05:00
|
|
|
canvas->clipPath(*mProjectedDisplayList->mProjectedOutline->getPath());
|
2016-11-08 14:18:31 -05:00
|
|
|
}
|
|
|
|
drawBackwardsProjectedNodes(canvas, *mProjectedDisplayList);
|
2016-10-17 16:26:15 -04:00
|
|
|
}
|
|
|
|
}
|
2016-11-08 14:18:31 -05:00
|
|
|
displayList->mProjectedOutline = nullptr;
|
2016-10-17 16:26:15 -04:00
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
static bool layerNeedsPaint(const LayerProperties& properties, float alphaMultiplier,
|
|
|
|
SkPaint* paint) {
|
2018-01-22 10:36:33 -05:00
|
|
|
paint->setFilterQuality(kLow_SkFilterQuality);
|
2017-11-03 10:12:19 -07:00
|
|
|
if (alphaMultiplier < 1.0f || properties.alpha() < 255 ||
|
2018-07-12 12:41:28 -04:00
|
|
|
properties.xferMode() != SkBlendMode::kSrcOver || properties.getColorFilter() != nullptr) {
|
2016-10-17 16:26:15 -04:00
|
|
|
paint->setAlpha(properties.alpha() * alphaMultiplier);
|
|
|
|
paint->setBlendMode(properties.xferMode());
|
2018-07-12 12:41:28 -04:00
|
|
|
paint->setColorFilter(sk_ref_sp(properties.getColorFilter()));
|
2016-10-17 16:26:15 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-20 18:05:35 -04:00
|
|
|
class AlphaFilterCanvas : public SkPaintFilterCanvas {
|
|
|
|
public:
|
|
|
|
AlphaFilterCanvas(SkCanvas* canvas, float alpha) : SkPaintFilterCanvas(canvas), mAlpha(alpha) {}
|
2017-11-03 10:12:19 -07:00
|
|
|
|
2017-09-20 18:05:35 -04:00
|
|
|
protected:
|
2019-04-15 11:59:33 -04:00
|
|
|
bool onFilter(SkPaint& paint) const override {
|
|
|
|
paint.setAlpha((uint8_t)paint.getAlpha() * mAlpha);
|
2017-09-20 18:05:35 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override {
|
|
|
|
// We unroll the drawable using "this" canvas, so that draw calls contained inside will
|
2019-04-15 11:59:33 -04:00
|
|
|
// get their alpha applied. The default SkPaintFilterCanvas::onDrawDrawable does not unroll.
|
2017-09-20 18:05:35 -04:00
|
|
|
drawable->draw(this, matrix);
|
|
|
|
}
|
2017-11-03 10:12:19 -07:00
|
|
|
|
2017-09-20 18:05:35 -04:00
|
|
|
private:
|
|
|
|
float mAlpha;
|
|
|
|
};
|
|
|
|
|
2016-10-17 16:26:15 -04:00
|
|
|
void RenderNodeDrawable::drawContent(SkCanvas* canvas) const {
|
|
|
|
RenderNode* renderNode = mRenderNode.get();
|
|
|
|
float alphaMultiplier = 1.0f;
|
|
|
|
const RenderProperties& properties = renderNode->properties();
|
|
|
|
|
|
|
|
// If we are drawing the contents of layer, we don't want to apply any of
|
|
|
|
// the RenderNode's properties during this pass. Those will all be applied
|
|
|
|
// when the layer is composited.
|
|
|
|
if (mComposeLayer) {
|
|
|
|
setViewProperties(properties, canvas, &alphaMultiplier);
|
|
|
|
}
|
2016-11-08 14:18:31 -05:00
|
|
|
SkiaDisplayList* displayList = (SkiaDisplayList*)mRenderNode->getDisplayList();
|
2018-06-14 18:00:10 -04:00
|
|
|
displayList->mParentMatrix = canvas->getTotalMatrix();
|
2016-10-17 16:26:15 -04:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
// TODO should we let the bound of the drawable do this for us?
|
2016-10-17 16:26:15 -04:00
|
|
|
const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
|
|
|
|
bool quickRejected = properties.getClipToBounds() && canvas->quickReject(bounds);
|
|
|
|
if (!quickRejected) {
|
|
|
|
SkiaDisplayList* displayList = (SkiaDisplayList*)renderNode->getDisplayList();
|
|
|
|
const LayerProperties& layerProperties = properties.layerProperties();
|
|
|
|
// composing a hardware layer
|
|
|
|
if (renderNode->getLayerSurface() && mComposeLayer) {
|
|
|
|
SkASSERT(properties.effectiveLayerType() == LayerType::RenderLayer);
|
2018-01-22 10:36:33 -05:00
|
|
|
SkPaint paint;
|
|
|
|
layerNeedsPaint(layerProperties, alphaMultiplier, &paint);
|
2017-12-04 15:07:08 -05:00
|
|
|
|
|
|
|
// surfaces for layers are created on LAYER_SIZE boundaries (which are >= layer size) so
|
|
|
|
// we need to restrict the portion of the surface drawn to the size of the renderNode.
|
|
|
|
SkASSERT(renderNode->getLayerSurface()->width() >= bounds.width());
|
|
|
|
SkASSERT(renderNode->getLayerSurface()->height() >= bounds.height());
|
2019-11-25 09:34:21 -05:00
|
|
|
|
|
|
|
// If SKP recording is active save an annotation that indicates this drawImageRect
|
|
|
|
// could also be rendered with the commands saved at ID associated with this node.
|
|
|
|
if (CC_UNLIKELY(Properties::skpCaptureEnabled)) {
|
|
|
|
canvas->drawAnnotation(bounds, String8::format(
|
|
|
|
"SurfaceID|%" PRId64, renderNode->uniqueId()).c_str(), nullptr);
|
|
|
|
}
|
|
|
|
canvas->drawImageRect(renderNode->getLayerSurface()->makeImageSnapshot(), bounds,
|
2018-12-13 16:40:14 -08:00
|
|
|
bounds, &paint);
|
2016-11-09 16:13:54 -05:00
|
|
|
|
2016-11-14 18:33:38 -05:00
|
|
|
if (!renderNode->getSkiaLayer()->hasRenderedSinceRepaint) {
|
2016-11-09 16:13:54 -05:00
|
|
|
renderNode->getSkiaLayer()->hasRenderedSinceRepaint = true;
|
2016-11-14 18:33:38 -05:00
|
|
|
if (CC_UNLIKELY(Properties::debugLayersUpdates)) {
|
|
|
|
SkPaint layerPaint;
|
|
|
|
layerPaint.setColor(0x7f00ff00);
|
|
|
|
canvas->drawRect(bounds, layerPaint);
|
|
|
|
} else if (CC_UNLIKELY(Properties::debugOverdraw)) {
|
|
|
|
// Render transparent rect to increment overdraw for repaint area.
|
|
|
|
// This can be "else if" because flashing green on layer updates
|
|
|
|
// will also increment the overdraw if it happens to be turned on.
|
|
|
|
SkPaint transparentPaint;
|
|
|
|
transparentPaint.setColor(SK_ColorTRANSPARENT);
|
|
|
|
canvas->drawRect(bounds, transparentPaint);
|
|
|
|
}
|
2016-11-09 16:13:54 -05:00
|
|
|
}
|
2016-10-17 16:26:15 -04:00
|
|
|
} else {
|
2017-09-20 18:05:35 -04:00
|
|
|
if (alphaMultiplier < 1.0f) {
|
|
|
|
// Non-layer draw for a view with getHasOverlappingRendering=false, will apply
|
|
|
|
// the alpha to the paint of each nested draw.
|
|
|
|
AlphaFilterCanvas alphaCanvas(canvas, alphaMultiplier);
|
|
|
|
displayList->draw(&alphaCanvas);
|
|
|
|
} else {
|
|
|
|
displayList->draw(canvas);
|
|
|
|
}
|
2016-10-17 16:26:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderNodeDrawable::setViewProperties(const RenderProperties& properties, SkCanvas* canvas,
|
2017-11-03 10:12:19 -07:00
|
|
|
float* alphaMultiplier) {
|
2016-10-17 16:26:15 -04:00
|
|
|
if (properties.getLeft() != 0 || properties.getTop() != 0) {
|
|
|
|
canvas->translate(properties.getLeft(), properties.getTop());
|
|
|
|
}
|
|
|
|
if (properties.getStaticMatrix()) {
|
|
|
|
canvas->concat(*properties.getStaticMatrix());
|
|
|
|
} else if (properties.getAnimationMatrix()) {
|
|
|
|
canvas->concat(*properties.getAnimationMatrix());
|
|
|
|
}
|
|
|
|
if (properties.hasTransformMatrix()) {
|
|
|
|
if (properties.isTransformTranslateOnly()) {
|
|
|
|
canvas->translate(properties.getTranslationX(), properties.getTranslationY());
|
|
|
|
} else {
|
|
|
|
canvas->concat(*properties.getTransformMatrix());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const bool isLayer = properties.effectiveLayerType() != LayerType::None;
|
|
|
|
int clipFlags = properties.getClippingFlags();
|
|
|
|
if (properties.getAlpha() < 1) {
|
|
|
|
if (isLayer) {
|
2017-11-03 10:12:19 -07:00
|
|
|
clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
|
2016-10-17 16:26:15 -04:00
|
|
|
}
|
|
|
|
if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
|
|
|
|
*alphaMultiplier = properties.getAlpha();
|
|
|
|
} else {
|
|
|
|
// savelayer needed to create an offscreen buffer
|
|
|
|
Rect layerBounds(0, 0, properties.getWidth(), properties.getHeight());
|
|
|
|
if (clipFlags) {
|
|
|
|
properties.getClippingRectForFlags(clipFlags, &layerBounds);
|
2017-11-03 10:12:19 -07:00
|
|
|
clipFlags = 0; // all clipping done by savelayer
|
2016-10-17 16:26:15 -04:00
|
|
|
}
|
2017-11-03 10:12:19 -07:00
|
|
|
SkRect bounds = SkRect::MakeLTRB(layerBounds.left, layerBounds.top, layerBounds.right,
|
|
|
|
layerBounds.bottom);
|
|
|
|
canvas->saveLayerAlpha(&bounds, (int)(properties.getAlpha() * 255));
|
2016-10-17 16:26:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
|
|
|
|
// pretend alpha always causes savelayer to warn about
|
|
|
|
// performance problem affecting old versions
|
|
|
|
ATRACE_FORMAT("alpha caused saveLayer %dx%d", properties.getWidth(),
|
2017-11-03 10:12:19 -07:00
|
|
|
properties.getHeight());
|
2016-10-17 16:26:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const SkRect* pendingClip = nullptr;
|
|
|
|
SkRect clipRect;
|
|
|
|
|
|
|
|
if (clipFlags) {
|
|
|
|
Rect tmpRect;
|
|
|
|
properties.getClippingRectForFlags(clipFlags, &tmpRect);
|
|
|
|
clipRect = tmpRect.toSkRect();
|
|
|
|
pendingClip = &clipRect;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (properties.getRevealClip().willClip()) {
|
2016-12-14 10:29:54 -05:00
|
|
|
canvas->clipPath(*properties.getRevealClip().getPath(), SkClipOp::kIntersect, true);
|
2016-10-17 16:26:15 -04:00
|
|
|
} else if (properties.getOutline().willClip()) {
|
|
|
|
clipOutline(properties.getOutline(), canvas, pendingClip);
|
|
|
|
pendingClip = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pendingClip) {
|
|
|
|
canvas->clipRect(*pendingClip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 15:51:58 -08:00
|
|
|
} // namespace skiapipeline
|
|
|
|
} // namespace uirenderer
|
|
|
|
} // namespace android
|