-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathRenderNode.cpp
More file actions
347 lines (297 loc) · 10.7 KB
/
RenderNode.cpp
File metadata and controls
347 lines (297 loc) · 10.7 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include <SkNWayCanvas.h>
#include <SkPathBuilder.h>
#include <SkPicture.h>
#include <SkShadowUtils.h>
#include "node/RenderNode.h"
#include "node/RenderNodeContext.h"
namespace skiko {
namespace node {
// The goal with selecting the size of the rectangle here is to avoid limiting the
// drawable area as much as possible.
// Due to https://issuetracker.google.com/issues/324465764 we have to
// leave room for scale between the values we specify here and Float.MAX_VALUE.
// The maximum possible scale that can be applied to the canvas will be
// Float.MAX_VALUE divided by the largest value below.
// 2^30 was chosen because it's big enough, leaves quite a lot of room between it
// and Float.MAX_VALUE, and also lets the width and height fit into int32 (just in
// case).
static const float PICTURE_MIN_VALUE = static_cast<float>(-(1L << 30));
static const float PICTURE_MAX_VALUE = static_cast<float>((1L << 30) - 1);
static SkRect PICTURE_BOUNDS {
PICTURE_MIN_VALUE,
PICTURE_MIN_VALUE,
PICTURE_MAX_VALUE,
PICTURE_MAX_VALUE
};
static const float DEFAULT_CAMERA_DISTANCE = 8.0f;
static const float NON_ZERO_EPSILON = 0.001f;
/**
* Check for floats that are close enough to zero.
*/
inline static bool isZero(float value) {
// Using fabsf is more performant as ARM computes
// fabsf in a single instruction.
return fabsf(value) <= NON_ZERO_EPSILON;
}
static SkColor multiplyAlpha(SkColor color, float alpha) {
return SkColorSetA(color, alpha * SkColorGetA(color));
}
class UnrollDrawableCanvas : public SkNWayCanvas {
public:
UnrollDrawableCanvas(SkCanvas* canvas)
: SkNWayCanvas(canvas->imageInfo().width(), canvas->imageInfo().height()) {
this->addCanvas(canvas);
}
protected:
void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override {
drawable->draw(this, matrix);
}
};
RenderNode::RenderNode(const sk_sp<RenderNodeContext>& context)
: context(context),
bbhFactory(context->shouldMeasureDrawBounds() ? new SkRTreeFactory() : nullptr),
recorder(),
contentCache(),
layerPaint(),
bounds { 0.0f, 0.0f, 0.0f, 0.0f },
pivot { SK_FloatNaN, SK_FloatNaN },
alpha(1.0f),
scaleX(1.0f),
scaleY(1.0f),
translationX(0.0f),
translationY(0.0f),
shadowElevation(0.0f),
ambientShadowColor(SK_ColorBLACK),
spotShadowColor(SK_ColorBLACK),
rotationX(0.0f),
rotationY(0.0f),
rotationZ(0.0f),
clipRect(),
clipRRect(),
clipPath(),
clipOp(SkClipOp::kIntersect),
clipAntiAlias(false),
clip(false),
transformMatrix(),
transformCamera(),
matrixIdentity(true),
matrixDirty(false) {
this->setCameraLocation(0.0f, 0.0f, DEFAULT_CAMERA_DISTANCE);
}
RenderNode::~RenderNode() {
if (this->bbhFactory) {
delete this->bbhFactory;
this->bbhFactory = nullptr;
}
}
void RenderNode::setLayerPaint(const std::optional<SkPaint>& layerPaint) {
this->layerPaint = layerPaint;
}
void RenderNode::setBounds(const SkRect& bounds) {
this->bounds = bounds;
this->matrixDirty = true;
}
void RenderNode::setPivot(const SkPoint& pivot) {
this->pivot = pivot;
this->matrixDirty = true;
}
void RenderNode::setAlpha(float alpha) {
this->alpha = alpha;
}
void RenderNode::setScaleX(float scaleX) {
this->scaleX = scaleX;
this->matrixDirty = true;
}
void RenderNode::setScaleY(float scaleY) {
this->scaleY = scaleY;
this->matrixDirty = true;
}
void RenderNode::setTranslationX(float translationX) {
this->translationX = translationX;
this->matrixDirty = true;
}
void RenderNode::setTranslationY(float translationY) {
this->translationY = translationY;
this->matrixDirty = true;
}
void RenderNode::setShadowElevation(float shadowElevation) {
this->shadowElevation = shadowElevation;
}
void RenderNode::setAmbientShadowColor(SkColor ambientShadowColor) {
this->ambientShadowColor = ambientShadowColor;
}
void RenderNode::setSpotShadowColor(SkColor spotShadowColor) {
this->spotShadowColor = spotShadowColor;
}
void RenderNode::setRotationX(float rotationX) {
this->rotationX = rotationX;
this->matrixDirty = true;
}
void RenderNode::setRotationY(float rotationY) {
this->rotationY = rotationY;
this->matrixDirty = true;
}
void RenderNode::setRotationZ(float rotationZ) {
this->rotationZ = rotationZ;
this->matrixDirty = true;
}
float RenderNode::getCameraDistance() const {
return this->transformCamera.fLocation.z / 72.0f;
}
void RenderNode::setCameraDistance(float cameraDistance) {
this->setCameraLocation(0.0f, 0.0f, cameraDistance);
this->matrixDirty = true;
}
void RenderNode::setClipRect(const std::optional<SkRect>& clipRect, SkClipOp op, bool doAntiAlias) {
this->clipRect = clipRect;
this->clipRRect.reset();
this->clipPath.reset();
this->clipOp = op;
this->clipAntiAlias = doAntiAlias;
}
void RenderNode::setClipRRect(const std::optional<SkRRect>& clipRRect, SkClipOp op, bool doAntiAlias) {
this->clipRect.reset();
this->clipRRect = clipRRect;
this->clipPath.reset();
this->clipOp = op;
this->clipAntiAlias = doAntiAlias;
}
void RenderNode::setClipPath(const std::optional<SkPath>& clipPath, SkClipOp op, bool doAntiAlias) {
this->clipRect.reset();
this->clipRRect.reset();
this->clipPath = clipPath;
this->clipOp = op;
this->clipAntiAlias = doAntiAlias;
}
void RenderNode::setClip(bool clip) {
this->clip = clip;
}
const SkMatrix& RenderNode::getMatrix() {
this->updateMatrix();
return this->transformMatrix;
}
SkCanvas *RenderNode::beginRecording() {
bool measureDrawBounds = !clip || shadowElevation > 0.0f;
const SkRect& bounds = measureDrawBounds ? PICTURE_BOUNDS : SkRect::MakeWH(this->bounds.width(), this->bounds.height());
SkBBHFactory* bbhFactory = measureDrawBounds ? this->bbhFactory : nullptr;
return this->recorder.beginRecording(bounds, bbhFactory);
}
void RenderNode::endRecording() {
this->contentCache = this->recorder.finishRecordingAsDrawable();
}
void RenderNode::drawInto(SkCanvas* canvas) {
canvas->drawDrawable(this);
}
void RenderNode::onDraw(SkCanvas* canvas) {
this->updateMatrix();
canvas->translate(this->bounds.left(), this->bounds.top());
if (!this->matrixIdentity) {
canvas->concat(this->transformMatrix);
}
if (this->shadowElevation > 0) {
auto lightGeometry = this->context->getLightGeometry();
auto lightInfo = this->context->getLightInfo();
this->drawShadow(canvas, lightGeometry, lightInfo);
}
if (this->clip) {
canvas->save();
if (this->clipRect) {
canvas->clipRect(*this->clipRect, this->clipOp, this->clipAntiAlias);
} else if (this->clipRRect) {
canvas->clipRRect(*this->clipRRect, this->clipOp, this->clipAntiAlias);
} else if (this->clipPath) {
canvas->clipPath(*this->clipPath, this->clipOp, this->clipAntiAlias);
} else {
auto rect = SkRect::MakeWH(this->bounds.width(), this->bounds.height());
canvas->clipRect(rect, this->clipOp, this->clipAntiAlias);
}
}
if (this->layerPaint) {
auto rect = SkRect::MakeWH(this->bounds.width(), this->bounds.height());
canvas->saveLayer(rect, &*this->layerPaint);
} else {
canvas->save();
}
canvas->drawDrawable(this->contentCache.get());
}
SkRect RenderNode::onGetBounds() {
return this->bounds;
}
sk_sp<SkPicture> RenderNode::onMakePictureSnapshot() {
SkCanvas* canvas = this->beginRecording();
UnrollDrawableCanvas unrollCanvas(canvas);
this->draw(&unrollCanvas);
return this->recorder.finishRecordingAsPicture();
}
// Adoption from frameworks/base/libs/hwui/RenderProperties.cpp
void RenderNode::updateMatrix() {
if (!this->matrixDirty) {
return;
}
float pivotX, pivotY;
if (this->pivot.isFinite()) {
pivotX = this->pivot.fX;
pivotY = this->pivot.fY;
} else {
pivotX = this->bounds.width() / 2.0f;
pivotY = this->bounds.height() / 2.0f;
}
this->transformMatrix.reset();
if (isZero(this->rotationX) && isZero(this->rotationY)) {
this->transformMatrix.setTranslate(this->translationX, this->translationY);
this->transformMatrix.preRotate(this->rotationZ, pivotX, pivotY);
this->transformMatrix.preScale(this->scaleX, this->scaleY, pivotX, pivotY);
} else {
this->transformMatrix.preScale(this->scaleX, this->scaleY, pivotX, pivotY);
SkM44 transform3D;
transform3D.preConcat(SkM44::Rotate({1, 0, 0}, -this->rotationX * SK_ScalarPI / 180));
transform3D.preConcat(SkM44::Rotate({0,-1, 0}, -this->rotationY * SK_ScalarPI / 180));
transform3D.preConcat(SkM44::Rotate({0, 0, 1}, -this->rotationZ * SK_ScalarPI / 180));
SkPatch3D patch3D;
patch3D.transform(transform3D);
SkMatrix transform;
this->transformCamera.patchToMatrix(patch3D, &transform);
transform.preTranslate(-pivotX, -pivotY);
transform.postTranslate(pivotX + this->translationX, pivotY + this->translationY);
this->transformMatrix.postConcat(transform);
}
this->matrixDirty = false;
this->matrixIdentity = this->transformMatrix.isIdentity();
}
void RenderNode::drawShadow(SkCanvas *canvas, const LightGeometry& lightGeometry, const LightInfo& lightInfo) {
SkPath tmpPath, *path = &tmpPath;
if (this->clipRect) {
tmpPath = SkPathBuilder().addRect(*this->clipRect).detach();
} else if (this->clipRRect) {
tmpPath = SkPathBuilder().addRRect(*this->clipRRect).detach();
} else if (this->clipPath) {
path = &*this->clipPath;
} else {
return;
}
SkPoint3 zParams{0.0f, 0.0f, this->shadowElevation};
float ambientAlpha = lightInfo.ambientShadowAlpha * this->alpha;
float spotAlpha = lightInfo.spotShadowAlpha * this->alpha;
SkColor ambientColor = multiplyAlpha(this->ambientShadowColor, ambientAlpha);
SkColor spotColor = multiplyAlpha(this->spotShadowColor, spotAlpha);
SkShadowUtils::DrawShadow(
canvas,
*path,
zParams,
lightGeometry.center,
lightGeometry.radius,
ambientColor,
spotColor,
this->alpha < 1.0f ? SkShadowFlags::kTransparentOccluder_ShadowFlag : SkShadowFlags::kNone_ShadowFlag
);
}
// Adoption from frameworks/base/libs/hwui/RenderProperties.cpp
void RenderNode::setCameraLocation(float x, float y, float z) {
// the camera location is passed in inches, set in pt
SkScalar lz = z * 72.0f;
this->transformCamera.fLocation = {x * 72.0f, y * 72.0f, lz};
this->transformCamera.fObserver = {0, 0, lz};
this->transformCamera.update();
}
} // namespace node
} // namespace skiko