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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
/*
* Copyright (C) 2014 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.
*/
#pragma once
#include <SkCamera.h>
#include <SkMatrix.h>
#include <utils/LinearAllocator.h>
#include <utils/RefBase.h>
#include <utils/String8.h>
#include <cutils/compiler.h>
#include <androidfw/ResourceTypes.h>
#include "AnimatorManager.h"
#include "CanvasTransform.h"
#include "Debug.h"
#include "DisplayList.h"
#include "Matrix.h"
#include "RenderProperties.h"
#include "pipeline/skia/SkiaDisplayList.h"
#include "pipeline/skia/SkiaLayer.h"
#include "utils/FatVector.h"
#include <vector>
class SkBitmap;
class SkPaint;
class SkPath;
class SkRegion;
class SkSurface;
namespace android {
namespace uirenderer {
class CanvasState;
class Rect;
class SkiaShader;
struct RenderNodeOp;
class TreeInfo;
class TreeObserver;
namespace proto {
class RenderNode;
}
/**
* Primary class for storing recorded canvas commands, as well as per-View/ViewGroup display
* properties.
*
* Recording of canvas commands is somewhat similar to SkPicture, except the canvas-recording
* functionality is split between RecordingCanvas (which manages the recording), DisplayList
* (which holds the actual data), and RenderNode (which holds properties used for render playback).
*
* Note that DisplayList is swapped out from beneath an individual RenderNode when a view's
* recorded stream of canvas operations is refreshed. The RenderNode (and its properties) stay
* attached.
*/
class RenderNode : public VirtualLightRefBase {
friend class TestUtils; // allow TestUtils to access syncDisplayList / syncProperties
public:
enum DirtyPropertyMask {
GENERIC = 1 << 1,
TRANSLATION_X = 1 << 2,
TRANSLATION_Y = 1 << 3,
TRANSLATION_Z = 1 << 4,
SCALE_X = 1 << 5,
SCALE_Y = 1 << 6,
ROTATION = 1 << 7,
ROTATION_X = 1 << 8,
ROTATION_Y = 1 << 9,
X = 1 << 10,
Y = 1 << 11,
Z = 1 << 12,
ALPHA = 1 << 13,
DISPLAY_LIST = 1 << 14,
};
ANDROID_API RenderNode();
ANDROID_API virtual ~RenderNode();
// See flags defined in DisplayList.java
enum ReplayFlag { kReplayFlag_ClipChildren = 0x1 };
ANDROID_API void setStagingDisplayList(DisplayList* newData);
ANDROID_API void output();
ANDROID_API int getDebugSize();
bool isRenderable() const { return mDisplayList && !mDisplayList->isEmpty(); }
bool hasProjectionReceiver() const {
return mDisplayList && mDisplayList->containsProjectionReceiver();
}
const char* getName() const { return mName.string(); }
void setName(const char* name) {
if (name) {
const char* lastPeriod = strrchr(name, '.');
if (lastPeriod) {
mName.setTo(lastPeriod + 1);
} else {
mName.setTo(name);
}
}
}
VirtualLightRefBase* getUserContext() const { return mUserContext.get(); }
void setUserContext(VirtualLightRefBase* context) { mUserContext = context; }
bool isPropertyFieldDirty(DirtyPropertyMask field) const {
return mDirtyPropertyFields & field;
}
void setPropertyFieldsDirty(uint32_t fields) { mDirtyPropertyFields |= fields; }
const RenderProperties& properties() const { return mProperties; }
RenderProperties& animatorProperties() { return mProperties; }
const RenderProperties& stagingProperties() { return mStagingProperties; }
RenderProperties& mutateStagingProperties() { return mStagingProperties; }
bool isValid() { return mValid; }
int getWidth() const { return properties().getWidth(); }
int getHeight() const { return properties().getHeight(); }
ANDROID_API virtual void prepareTree(TreeInfo& info);
void destroyHardwareResources(TreeInfo* info = nullptr);
void destroyLayers();
// UI thread only!
ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator);
void removeAnimator(const sp<BaseRenderNodeAnimator>& animator);
// This can only happen during pushStaging()
void onAnimatorTargetChanged(BaseRenderNodeAnimator* animator) {
mAnimatorManager.onAnimatorTargetChanged(animator);
}
AnimatorManager& animators() { return mAnimatorManager; }
void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false) const;
bool nothingToDraw() const {
const Outline& outline = properties().getOutline();
return mDisplayList == nullptr || properties().getAlpha() <= 0 ||
(outline.getShouldClip() && outline.isEmpty()) || properties().getScaleX() == 0 ||
properties().getScaleY() == 0;
}
const DisplayList* getDisplayList() const { return mDisplayList; }
// Note: The position callbacks are relying on the listener using
// the frameNumber to appropriately batch/synchronize these transactions.
// There is no other filtering/batching to ensure that only the "final"
// state called once per frame.
class ANDROID_API PositionListener : public VirtualLightRefBase {
public:
virtual ~PositionListener() {}
// Called when the RenderNode's position changes
virtual void onPositionUpdated(RenderNode& node, const TreeInfo& info) = 0;
// Called when the RenderNode no longer has a position. As in, it's
// no longer being drawn.
// Note, tree info might be null
virtual void onPositionLost(RenderNode& node, const TreeInfo* info) = 0;
};
ANDROID_API void setPositionListener(PositionListener* listener) {
mStagingPositionListener = listener;
mPositionListenerDirty = true;
}
// This is only modified in MODE_FULL, so it can be safely accessed
// on the UI thread.
ANDROID_API bool hasParents() { return mParentCount; }
void onRemovedFromTree(TreeInfo* info);
// Called by CanvasContext to promote a RenderNode to be a root node
void makeRoot() { incParentRefCount(); }
// Called by CanvasContext when it drops a RenderNode from being a root node
void clearRoot();
void output(std::ostream& output, uint32_t level);
void setUsageHint(UsageHint usageHint) { mUsageHint = usageHint; }
UsageHint usageHint() const { return mUsageHint; }
int64_t uniqueId() const { return mUniqueId; }
void markDrawStart(SkCanvas& canvas);
void markDrawEnd(SkCanvas& canvas);
private:
void computeOrderingImpl(RenderNodeOp* opState,
std::vector<RenderNodeOp*>* compositedChildrenOfProjectionSurface,
const mat4* transformFromProjectionSurface);
void syncProperties();
void syncDisplayList(TreeObserver& observer, TreeInfo* info);
void handleForceDark(TreeInfo* info);
void prepareTreeImpl(TreeObserver& observer, TreeInfo& info, bool functorsNeedLayer);
void pushStagingPropertiesChanges(TreeInfo& info);
void pushStagingDisplayListChanges(TreeObserver& observer, TreeInfo& info);
void prepareLayer(TreeInfo& info, uint32_t dirtyMask);
void pushLayerUpdate(TreeInfo& info);
void deleteDisplayList(TreeObserver& observer, TreeInfo* info = nullptr);
void damageSelf(TreeInfo& info);
void incParentRefCount() { mParentCount++; }
void decParentRefCount(TreeObserver& observer, TreeInfo* info = nullptr);
const int64_t mUniqueId;
String8 mName;
sp<VirtualLightRefBase> mUserContext;
uint32_t mDirtyPropertyFields;
RenderProperties mProperties;
RenderProperties mStagingProperties;
// Owned by UI. Set when DL is set, cleared when DL cleared or when node detached
// (likely by parent re-record/removal)
bool mValid = false;
bool mNeedsDisplayListSync;
// WARNING: Do not delete this directly, you must go through deleteDisplayList()!
DisplayList* mDisplayList;
DisplayList* mStagingDisplayList;
int64_t mDamageGenerationId;
friend class AnimatorManager;
AnimatorManager mAnimatorManager;
/**
* Draw time state - these properties are only set and used during rendering
*/
// for projection surfaces, contains a list of all children items
std::vector<RenderNodeOp*> mProjectedNodes;
// How many references our parent(s) have to us. Typically this should alternate
// between 2 and 1 (when a staging push happens we inc first then dec)
// When this hits 0 we are no longer in the tree, so any hardware resources
// (specifically Layers) should be released.
// This is *NOT* thread-safe, and should therefore only be tracking
// mDisplayList, not mStagingDisplayList.
uint32_t mParentCount;
bool mPositionListenerDirty = false;
sp<PositionListener> mStagingPositionListener;
sp<PositionListener> mPositionListener;
UsageHint mUsageHint = UsageHint::Unknown;
// METHODS & FIELDS ONLY USED BY THE SKIA RENDERER
public:
/**
* Detach and transfer ownership of an already allocated displayList for use
* in recording updated content for this renderNode
*/
std::unique_ptr<skiapipeline::SkiaDisplayList> detachAvailableList() {
return std::move(mAvailableDisplayList);
}
/**
* Attach unused displayList to this node for potential future reuse.
*/
void attachAvailableList(skiapipeline::SkiaDisplayList* skiaDisplayList) {
mAvailableDisplayList.reset(skiaDisplayList);
}
/**
* Returns true if an offscreen layer from any renderPipeline is attached
* to this node.
*/
bool hasLayer() const { return mSkiaLayer.get(); }
/**
* Used by the RenderPipeline to attach an offscreen surface to the RenderNode.
* The surface is then will be used to store the contents of a layer.
*/
void setLayerSurface(sk_sp<SkSurface> layer) {
if (layer.get()) {
if (!mSkiaLayer.get()) {
mSkiaLayer = std::make_unique<skiapipeline::SkiaLayer>();
}
mSkiaLayer->layerSurface = std::move(layer);
mSkiaLayer->inverseTransformInWindow.loadIdentity();
} else {
mSkiaLayer.reset();
}
}
/**
* If the RenderNode is of type LayerType::RenderLayer then this method will
* return the an offscreen rendering surface that is used to both render into
* the layer and composite the layer into its parent. If the type is not
* LayerType::RenderLayer then it will return a nullptr.
*
* NOTE: this function is only guaranteed to return accurate results after
* prepareTree has been run for this RenderNode
*/
SkSurface* getLayerSurface() const {
return mSkiaLayer.get() ? mSkiaLayer->layerSurface.get() : nullptr;
}
skiapipeline::SkiaLayer* getSkiaLayer() const { return mSkiaLayer.get(); }
/**
* Returns the path that represents the outline of RenderNode intersected with
* the provided rect. This call will internally cache the resulting path in
* order to potentially return that path for subsequent calls to this method.
* By reusing the same path we get better performance on the GPU backends since
* those resources are cached in the hardware based on the path's genID.
*
* The returned path is only guaranteed to be valid until this function is called
* again or the RenderNode's outline is mutated.
*/
const SkPath* getClippedOutline(const SkRect& clipRect) const;
private:
/**
* If this RenderNode has been used in a previous frame then the SkiaDisplayList
* from that frame is cached here until one of the following conditions is met:
* 1) The RenderNode is deleted (causing this to be deleted)
* 2) It is replaced with the displayList from the next completed frame
* 3) It is detached and used to to record a new displayList for a later frame
*/
std::unique_ptr<skiapipeline::SkiaDisplayList> mAvailableDisplayList;
/**
* An offscreen rendering target used to contain the contents this RenderNode
* when it has been set to draw as a LayerType::RenderLayer.
*/
std::unique_ptr<skiapipeline::SkiaLayer> mSkiaLayer;
struct ClippedOutlineCache {
// keys
uint32_t outlineID = 0;
SkRect clipRect;
// value
SkPath clippedOutline;
};
mutable ClippedOutlineCache mClippedOutlineCache;
}; // class RenderNode
class MarkAndSweepRemoved : public TreeObserver {
PREVENT_COPY_AND_ASSIGN(MarkAndSweepRemoved);
public:
explicit MarkAndSweepRemoved(TreeInfo* info) : mTreeInfo(info) {}
void onMaybeRemovedFromTree(RenderNode* node) override { mMarked.emplace_back(node); }
~MarkAndSweepRemoved() {
for (auto& node : mMarked) {
if (!node->hasParents()) {
node->onRemovedFromTree(mTreeInfo);
}
}
}
private:
FatVector<sp<RenderNode>, 10> mMarked;
TreeInfo* mTreeInfo;
};
} /* namespace uirenderer */
} /* namespace android */
|