| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#pragma once |
| 18 |
|
| 19 |
#include <SkCamera.h> |
| 20 |
#include <SkMatrix.h> |
| 21 |
|
| 22 |
#include <utils/LinearAllocator.h> |
| 23 |
#include <utils/RefBase.h> |
| 24 |
#include <utils/String8.h> |
| 25 |
|
| 26 |
#include <cutils/compiler.h> |
| 27 |
|
| 28 |
#include <androidfw/ResourceTypes.h> |
| 29 |
|
| 30 |
#include <ui/FatVector.h> |
| 31 |
|
| 32 |
#include "AnimatorManager.h" |
| 33 |
#include "CanvasTransform.h" |
| 34 |
#include "Debug.h" |
| 35 |
#include "DisplayList.h" |
| 36 |
#include "Matrix.h" |
| 37 |
#include "RenderProperties.h" |
| 38 |
#include "pipeline/skia/HolePunch.h" |
| 39 |
#include "pipeline/skia/SkiaDisplayList.h" |
| 40 |
#include "pipeline/skia/SkiaLayer.h" |
| 41 |
|
| 42 |
#include <vector> |
| 43 |
#include <pipeline/skia/StretchMask.h> |
| 44 |
|
| 45 |
class SkBitmap; |
| 46 |
class SkPaint; |
| 47 |
class SkPath; |
| 48 |
class SkRegion; |
| 49 |
class SkSurface; |
| 50 |
|
| 51 |
namespace android { |
| 52 |
namespace uirenderer { |
| 53 |
|
| 54 |
class CanvasState; |
| 55 |
class Rect; |
| 56 |
class SkiaShader; |
| 57 |
struct RenderNodeOp; |
| 58 |
|
| 59 |
class TreeInfo; |
| 60 |
class TreeObserver; |
| 61 |
|
| 62 |
namespace proto { |
| 63 |
class RenderNode; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
class RenderNode : public VirtualLightRefBase { |
| 79 |
friend class TestUtils; |
| 80 |
|
| 81 |
public: |
| 82 |
enum DirtyPropertyMask { |
| 83 |
GENERIC = 1 << 1, |
| 84 |
TRANSLATION_X = 1 << 2, |
| 85 |
TRANSLATION_Y = 1 << 3, |
| 86 |
TRANSLATION_Z = 1 << 4, |
| 87 |
SCALE_X = 1 << 5, |
| 88 |
SCALE_Y = 1 << 6, |
| 89 |
ROTATION = 1 << 7, |
| 90 |
ROTATION_X = 1 << 8, |
| 91 |
ROTATION_Y = 1 << 9, |
| 92 |
X = 1 << 10, |
| 93 |
Y = 1 << 11, |
| 94 |
Z = 1 << 12, |
| 95 |
ALPHA = 1 << 13, |
| 96 |
DISPLAY_LIST = 1 << 14, |
| 97 |
}; |
| 98 |
|
| 99 |
RenderNode(); |
| 100 |
virtual ~RenderNode(); |
| 101 |
|
| 102 |
|
| 103 |
enum ReplayFlag { kReplayFlag_ClipChildren = 0x1 }; |
| 104 |
|
| 105 |
void setStagingDisplayList(DisplayList&& newData); |
| 106 |
void discardStagingDisplayList(); |
| 107 |
|
| 108 |
void output(); |
| 109 |
int getUsageSize(); |
| 110 |
int getAllocatedSize(); |
| 111 |
|
| 112 |
bool isRenderable() const { return mDisplayList.hasContent(); } |
| 113 |
|
| 114 |
bool hasProjectionReceiver() const { |
| 115 |
return mDisplayList.containsProjectionReceiver(); |
| 116 |
} |
| 117 |
|
| 118 |
const char* getName() const { return mName.string(); } |
| 119 |
|
| 120 |
void setName(const char* name) { |
| 121 |
if (name) { |
| 122 |
const char* lastPeriod = strrchr(name, '.'); |
| 123 |
if (lastPeriod) { |
| 124 |
mName.setTo(lastPeriod + 1); |
| 125 |
} else { |
| 126 |
mName.setTo(name); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
StretchMask& getStretchMask() { return mStretchMask; } |
| 132 |
|
| 133 |
VirtualLightRefBase* getUserContext() const { return mUserContext.get(); } |
| 134 |
|
| 135 |
void setUserContext(VirtualLightRefBase* context) { mUserContext = context; } |
| 136 |
|
| 137 |
bool isPropertyFieldDirty(DirtyPropertyMask field) const { |
| 138 |
return mDirtyPropertyFields & field; |
| 139 |
} |
| 140 |
|
| 141 |
void setPropertyFieldsDirty(uint32_t fields) { mDirtyPropertyFields |= fields; } |
| 142 |
|
| 143 |
const RenderProperties& properties() const { return mProperties; } |
| 144 |
|
| 145 |
RenderProperties& animatorProperties() { return mProperties; } |
| 146 |
|
| 147 |
const RenderProperties& stagingProperties() { return mStagingProperties; } |
| 148 |
|
| 149 |
RenderProperties& mutateStagingProperties() { return mStagingProperties; } |
| 150 |
|
| 151 |
bool isValid() { return mValid; } |
| 152 |
|
| 153 |
int getWidth() const { return properties().getWidth(); } |
| 154 |
|
| 155 |
int getHeight() const { return properties().getHeight(); } |
| 156 |
|
| 157 |
virtual void prepareTree(TreeInfo& info); |
| 158 |
void destroyHardwareResources(TreeInfo* info = nullptr); |
| 159 |
void destroyLayers(); |
| 160 |
|
| 161 |
|
| 162 |
void addAnimator(const sp<BaseRenderNodeAnimator>& animator); |
| 163 |
void removeAnimator(const sp<BaseRenderNodeAnimator>& animator); |
| 164 |
|
| 165 |
|
| 166 |
void onAnimatorTargetChanged(BaseRenderNodeAnimator* animator) { |
| 167 |
mAnimatorManager.onAnimatorTargetChanged(animator); |
| 168 |
} |
| 169 |
|
| 170 |
AnimatorManager& animators() { return mAnimatorManager; } |
| 171 |
|
| 172 |
void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false) const; |
| 173 |
|
| 174 |
bool nothingToDraw() const { |
| 175 |
const Outline& outline = properties().getOutline(); |
| 176 |
return !mDisplayList.isValid() || properties().getAlpha() <= 0 || |
| 177 |
(outline.getShouldClip() && outline.isEmpty()) || properties().getScaleX() == 0 || |
| 178 |
properties().getScaleY() == 0; |
| 179 |
} |
| 180 |
|
| 181 |
const DisplayList& getDisplayList() const { return mDisplayList; } |
| 182 |
|
| 183 |
DisplayList& getDisplayList() { return mDisplayList; } |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
class PositionListener : public VirtualLightRefBase { |
| 190 |
public: |
| 191 |
virtual ~PositionListener() {} |
| 192 |
|
| 193 |
virtual void onPositionUpdated(RenderNode& node, const TreeInfo& info) = 0; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
virtual void onPositionLost(RenderNode& node, const TreeInfo* info) = 0; |
| 198 |
}; |
| 199 |
|
| 200 |
void setPositionListener(PositionListener* listener) { |
| 201 |
mStagingPositionListener = listener; |
| 202 |
mPositionListenerDirty = true; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
bool hasParents() { return mParentCount; } |
| 208 |
|
| 209 |
void onRemovedFromTree(TreeInfo* info); |
| 210 |
|
| 211 |
|
| 212 |
void makeRoot() { incParentRefCount(); } |
| 213 |
|
| 214 |
|
| 215 |
void clearRoot(); |
| 216 |
|
| 217 |
void output(std::ostream& output, uint32_t level); |
| 218 |
|
| 219 |
void setUsageHint(UsageHint usageHint) { mUsageHint = usageHint; } |
| 220 |
|
| 221 |
UsageHint usageHint() const { return mUsageHint; } |
| 222 |
|
| 223 |
int64_t uniqueId() const { return mUniqueId; } |
| 224 |
|
| 225 |
void markDrawStart(SkCanvas& canvas); |
| 226 |
void markDrawEnd(SkCanvas& canvas); |
| 227 |
|
| 228 |
private: |
| 229 |
void computeOrderingImpl(RenderNodeOp* opState, |
| 230 |
std::vector<RenderNodeOp*>* compositedChildrenOfProjectionSurface, |
| 231 |
const mat4* transformFromProjectionSurface); |
| 232 |
|
| 233 |
void syncProperties(); |
| 234 |
void syncDisplayList(TreeObserver& observer, TreeInfo* info); |
| 235 |
void handleForceDark(TreeInfo* info); |
| 236 |
|
| 237 |
void prepareTreeImpl(TreeObserver& observer, TreeInfo& info, bool functorsNeedLayer); |
| 238 |
void pushStagingPropertiesChanges(TreeInfo& info); |
| 239 |
void pushStagingDisplayListChanges(TreeObserver& observer, TreeInfo& info); |
| 240 |
void prepareLayer(TreeInfo& info, uint32_t dirtyMask); |
| 241 |
void pushLayerUpdate(TreeInfo& info); |
| 242 |
void deleteDisplayList(TreeObserver& observer, TreeInfo* info = nullptr); |
| 243 |
void damageSelf(TreeInfo& info); |
| 244 |
|
| 245 |
void incParentRefCount() { mParentCount++; } |
| 246 |
void decParentRefCount(TreeObserver& observer, TreeInfo* info = nullptr); |
| 247 |
|
| 248 |
const int64_t mUniqueId; |
| 249 |
String8 mName; |
| 250 |
sp<VirtualLightRefBase> mUserContext; |
| 251 |
|
| 252 |
uint32_t mDirtyPropertyFields; |
| 253 |
RenderProperties mProperties; |
| 254 |
RenderProperties mStagingProperties; |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
bool mValid = false; |
| 259 |
|
| 260 |
bool mNeedsDisplayListSync; |
| 261 |
|
| 262 |
DisplayList mDisplayList; |
| 263 |
DisplayList mStagingDisplayList; |
| 264 |
|
| 265 |
int64_t mDamageGenerationId; |
| 266 |
|
| 267 |
friend class AnimatorManager; |
| 268 |
AnimatorManager mAnimatorManager; |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
std::vector<RenderNodeOp*> mProjectedNodes; |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
uint32_t mParentCount; |
| 284 |
|
| 285 |
bool mPositionListenerDirty = false; |
| 286 |
sp<PositionListener> mStagingPositionListener; |
| 287 |
sp<PositionListener> mPositionListener; |
| 288 |
|
| 289 |
UsageHint mUsageHint = UsageHint::Unknown; |
| 290 |
|
| 291 |
bool mHasHolePunches; |
| 292 |
StretchMask mStretchMask; |
| 293 |
|
| 294 |
|
| 295 |
public: |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
std::unique_ptr<skiapipeline::SkiaDisplayList> detachAvailableList() { |
| 301 |
return std::move(mAvailableDisplayList); |
| 302 |
} |
| 303 |
|
| 304 |
bool hasHolePunches() { return mHasHolePunches; } |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
void attachAvailableList(skiapipeline::SkiaDisplayList* skiaDisplayList) { |
| 310 |
mAvailableDisplayList.reset(skiaDisplayList); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
bool hasLayer() const { return mSkiaLayer.get(); } |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
void setLayerSurface(sk_sp<SkSurface> layer) { |
| 324 |
if (layer.get()) { |
| 325 |
if (!mSkiaLayer.get()) { |
| 326 |
mSkiaLayer = std::make_unique<skiapipeline::SkiaLayer>(); |
| 327 |
} |
| 328 |
mSkiaLayer->layerSurface = std::move(layer); |
| 329 |
mSkiaLayer->inverseTransformInWindow.loadIdentity(); |
| 330 |
} else { |
| 331 |
mSkiaLayer.reset(); |
| 332 |
} |
| 333 |
|
| 334 |
mProperties.mutateLayerProperties().mutableStretchEffect().clear(); |
| 335 |
mStretchMask.clear(); |
| 336 |
|
| 337 |
|
| 338 |
mSnapshotResult.snapshot = nullptr; |
| 339 |
mTargetImageFilter = nullptr; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
SkSurface* getLayerSurface() const { |
| 352 |
return mSkiaLayer.get() ? mSkiaLayer->layerSurface.get() : nullptr; |
| 353 |
} |
| 354 |
|
| 355 |
struct SnapshotResult { |
| 356 |
sk_sp<SkImage> snapshot; |
| 357 |
SkIRect outSubset; |
| 358 |
SkIPoint outOffset; |
| 359 |
}; |
| 360 |
|
| 361 |
std::optional<SnapshotResult> updateSnapshotIfRequired(GrRecordingContext* context, |
| 362 |
const SkImageFilter* imageFilter, |
| 363 |
const SkIRect& clipBounds); |
| 364 |
|
| 365 |
skiapipeline::SkiaLayer* getSkiaLayer() const { return mSkiaLayer.get(); } |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
const SkPath* getClippedOutline(const SkRect& clipRect) const; |
| 378 |
|
| 379 |
private: |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
std::unique_ptr<skiapipeline::SkiaDisplayList> mAvailableDisplayList; |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
std::unique_ptr<skiapipeline::SkiaLayer> mSkiaLayer; |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
sk_sp<SkImageFilter> mTargetImageFilter; |
| 399 |
uint32_t mTargetImageFilterLayerSurfaceGenerationId = 0; |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
SkIRect mImageFilterClipBounds; |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
SnapshotResult mSnapshotResult; |
| 411 |
|
| 412 |
struct ClippedOutlineCache { |
| 413 |
|
| 414 |
uint32_t outlineID = 0; |
| 415 |
SkRect clipRect; |
| 416 |
|
| 417 |
|
| 418 |
SkPath clippedOutline; |
| 419 |
}; |
| 420 |
mutable ClippedOutlineCache mClippedOutlineCache; |
| 421 |
}; |
| 422 |
|
| 423 |
class MarkAndSweepRemoved : public TreeObserver { |
| 424 |
PREVENT_COPY_AND_ASSIGN(MarkAndSweepRemoved); |
| 425 |
|
| 426 |
public: |
| 427 |
explicit MarkAndSweepRemoved(TreeInfo* info) : mTreeInfo(info) {} |
| 428 |
|
| 429 |
void onMaybeRemovedFromTree(RenderNode* node) override { mMarked.emplace_back(node); } |
| 430 |
|
| 431 |
~MarkAndSweepRemoved() { |
| 432 |
for (auto& node : mMarked) { |
| 433 |
if (!node->hasParents()) { |
| 434 |
node->onRemovedFromTree(mTreeInfo); |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
private: |
| 440 |
FatVector<sp<RenderNode>, 10> mMarked; |
| 441 |
TreeInfo* mTreeInfo; |
| 442 |
}; |
| 443 |
|
| 444 |
} |
| 445 |
} |