1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
/*
* 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 "ReorderBarrierDrawables.h"
#include "RenderNode.h"
#include "SkiaDisplayList.h"
#include "SkiaPipeline.h"
#include <SkBlurMask.h>
#include <SkBlurMaskFilter.h>
#include <SkPathOps.h>
#include <SkRRectsGaussianEdgeMaskFilter.h>
#include <SkShadowUtils.h>
namespace android {
namespace uirenderer {
namespace skiapipeline {
StartReorderBarrierDrawable::StartReorderBarrierDrawable(SkiaDisplayList* data)
: mEndChildIndex(0)
, mBeginChildIndex(data->mChildNodes.size())
, mDisplayList(data) {
}
void StartReorderBarrierDrawable::onDraw(SkCanvas* canvas) {
if (mChildren.empty()) {
//mChildren is allocated and initialized only the first time onDraw is called and cached for
//subsequent calls
mChildren.reserve(mEndChildIndex - mBeginChildIndex + 1);
for (int i = mBeginChildIndex; i <= mEndChildIndex; i++) {
mChildren.push_back(const_cast<RenderNodeDrawable*>(&mDisplayList->mChildNodes[i]));
}
}
std::stable_sort(mChildren.begin(), mChildren.end(),
[](RenderNodeDrawable* a, RenderNodeDrawable* b) {
const float aZValue = a->getNodeProperties().getZ();
const float bZValue = b->getNodeProperties().getZ();
return aZValue < bZValue;
});
size_t drawIndex = 0;
const size_t endIndex = mChildren.size();
while (drawIndex < endIndex) {
RenderNodeDrawable* childNode = mChildren[drawIndex];
SkASSERT(childNode);
const float casterZ = childNode->getNodeProperties().getZ();
if (casterZ >= -NON_ZERO_EPSILON) { //draw only children with negative Z
return;
}
childNode->forceDraw(canvas);
drawIndex++;
}
}
EndReorderBarrierDrawable::EndReorderBarrierDrawable(StartReorderBarrierDrawable* startBarrier)
: mStartBarrier(startBarrier) {
mStartBarrier->mEndChildIndex = mStartBarrier->mDisplayList->mChildNodes.size() - 1;
}
#define SHADOW_DELTA 0.1f
void EndReorderBarrierDrawable::onDraw(SkCanvas* canvas) {
auto& zChildren = mStartBarrier->mChildren;
/**
* Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
* with very similar Z heights to draw together.
*
* This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
* underneath both, and neither's shadow is drawn on top of the other.
*/
size_t drawIndex = 0;
const size_t endIndex = zChildren.size();
while (drawIndex < endIndex //draw only children with positive Z
&& zChildren[drawIndex]->getNodeProperties().getZ() <= NON_ZERO_EPSILON) drawIndex++;
size_t shadowIndex = drawIndex;
float lastCasterZ = 0.0f;
while (shadowIndex < endIndex || drawIndex < endIndex) {
if (shadowIndex < endIndex) {
const float casterZ = zChildren[shadowIndex]->getNodeProperties().getZ();
// attempt to render the shadow if the caster about to be drawn is its caster,
// OR if its caster's Z value is similar to the previous potential caster
if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
this->drawShadow(canvas, zChildren[shadowIndex]);
lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
shadowIndex++;
continue;
}
}
RenderNodeDrawable* childNode = zChildren[drawIndex];
SkASSERT(childNode);
childNode->forceDraw(canvas);
drawIndex++;
}
}
// copied from FrameBuilder::deferShadow
void EndReorderBarrierDrawable::drawShadow(SkCanvas* canvas, RenderNodeDrawable* caster) {
const RenderProperties& casterProperties = caster->getNodeProperties();
if (casterProperties.getAlpha() <= 0.0f
|| casterProperties.getOutline().getAlpha() <= 0.0f
|| !casterProperties.getOutline().getPath()
|| casterProperties.getScaleX() == 0
|| casterProperties.getScaleY() == 0) {
// no shadow to draw
return;
}
const SkScalar casterAlpha = casterProperties.getAlpha()
* casterProperties.getOutline().getAlpha();
if (casterAlpha <= 0.0f) {
return;
}
float ambientAlpha = (SkiaPipeline::getAmbientShadowAlpha()/255.f)*casterAlpha;
float spotAlpha = (SkiaPipeline::getSpotShadowAlpha()/255.f)*casterAlpha;
const RevealClip& revealClip = casterProperties.getRevealClip();
const SkPath* revealClipPath = revealClip.getPath();
if (revealClipPath && revealClipPath->isEmpty()) {
// An empty reveal clip means nothing is drawn
return;
}
bool clippedToBounds = casterProperties.getClippingFlags() & CLIP_TO_CLIP_BOUNDS;
SkRect casterClipRect = SkRect::MakeEmpty();
if (clippedToBounds) {
Rect clipBounds;
casterProperties.getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
casterClipRect = clipBounds.toSkRect();
if (casterClipRect.isEmpty()) {
// An empty clip rect means nothing is drawn
return;
}
}
SkAutoCanvasRestore acr(canvas, true);
SkMatrix shadowMatrix;
mat4 hwuiMatrix;
// TODO we don't pass the optional boolean to treat it as a 4x4 matrix
caster->getRenderNode()->applyViewPropertyTransforms(hwuiMatrix);
hwuiMatrix.copyTo(shadowMatrix);
canvas->concat(shadowMatrix);
const SkPath* casterOutlinePath = casterProperties.getOutline().getPath();
// holds temporary SkPath to store the result of intersections
SkPath tmpPath;
const SkPath* casterPath = casterOutlinePath;
// TODO: In to following course of code that calculates the final shape, is there an optimal
// of doing the Op calculations?
// intersect the shadow-casting path with the reveal, if present
if (revealClipPath) {
Op(*casterPath, *revealClipPath, kIntersect_SkPathOp, &tmpPath);
tmpPath.setIsVolatile(true);
casterPath = &tmpPath;
}
// intersect the shadow-casting path with the clipBounds, if present
if (clippedToBounds) {
SkPath clipBoundsPath;
clipBoundsPath.addRect(casterClipRect);
Op(*casterPath, clipBoundsPath, kIntersect_SkPathOp, &tmpPath);
tmpPath.setIsVolatile(true);
casterPath = &tmpPath;
}
const Vector3 lightPos = SkiaPipeline::getLightCenter();
SkPoint3 skiaLightPos = SkPoint3::Make(lightPos.x, lightPos.y, lightPos.z);
SkPoint3 zParams;
if (shadowMatrix.hasPerspective()) {
// get the matrix with the full 3D transform
mat4 zMatrix;
caster->getRenderNode()->applyViewPropertyTransforms(zMatrix, true);
zParams = SkPoint3::Make(zMatrix[2], zMatrix[6], zMatrix[mat4::kTranslateZ]);
} else {
zParams = SkPoint3::Make(0, 0, casterProperties.getZ());
}
SkShadowUtils::DrawShadow(canvas, *casterPath, zParams, skiaLightPos,
SkiaPipeline::getLightRadius(), ambientAlpha, spotAlpha, SK_ColorBLACK,
casterAlpha < 1.0f ? SkShadowFlags::kTransparentOccluder_ShadowFlag : 0);
}
}; // namespace skiapipeline
}; // namespace uirenderer
}; // namespace android
|