-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderNode.java
1894 lines (1668 loc) · 71.1 KB
/
RenderNode.java
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2010 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.
*/
package android.graphics;
import android.annotation.BytesLong;
import android.annotation.ColorInt;
import android.annotation.FloatRange;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.graphics.animation.RenderNodeAnimator;
import android.view.NativeVectorDrawableAnimator;
import android.view.Surface;
import android.view.View;
import com.android.internal.util.ArrayUtils;
import dalvik.annotation.optimization.CriticalNative;
import libcore.util.NativeAllocationRegistry;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.WeakReference;
/**
* <p>RenderNode is used to build hardware accelerated rendering hierarchies. Each RenderNode
* contains both a display list as well as a set of properties that affect the rendering of the
* display list. RenderNodes are used internally for all Views by default and are not typically
* used directly.</p>
*
* <p>RenderNodes are used to divide up the rendering content of a complex scene into smaller
* pieces that can then be updated individually more cheaply. Updating part of the scene only needs
* to update the display list or properties of a small number of RenderNode instead of redrawing
* everything from scratch. A RenderNode only needs its display list re-recorded when its content
* alone should be changed. RenderNodes can also be transformed without re-recording the display
* list through the transform properties.</p>
*
* <p>A text editor might for instance store each paragraph into its own RenderNode.
* Thus when the user inserts or removes characters, only the display list of the
* affected paragraph needs to be recorded again.</p>
*
* <h3>Hardware acceleration</h3>
* <p>RenderNodes can be drawn using a {@link RecordingCanvas}. They are not
* supported in software. Always make sure that the {@link android.graphics.Canvas}
* you are using to render a display list is hardware accelerated using
* {@link android.graphics.Canvas#isHardwareAccelerated()}.</p>
*
* <h3>Creating a RenderNode</h3>
* <pre class="prettyprint">
* RenderNode renderNode = new RenderNode("myRenderNode");
* renderNode.setPosition(0, 0, 50, 50); // Set the size to 50x50
* RecordingCanvas canvas = renderNode.beginRecording();
* try {
* // Draw with the canvas
* canvas.drawRect(...);
* } finally {
* renderNode.endRecording();
* }</pre>
*
* <h3>Drawing a RenderNode in a View</h3>
* <pre class="prettyprint">
* protected void onDraw(Canvas canvas) {
* if (canvas.isHardwareAccelerated()) {
* // Check that the RenderNode has a display list, re-recording it if it does not.
* if (!myRenderNode.hasDisplayList()) {
* updateDisplayList(myRenderNode);
* }
* // Draw the RenderNode into this canvas.
* canvas.drawRenderNode(myRenderNode);
* }
* }</pre>
*
* <h3>Releasing resources</h3>
* <p>This step is not mandatory but recommended if you want to release resources
* held by a display list as soon as possible. Most significantly any bitmaps it may contain.</p>
* <pre class="prettyprint">
* // Discards the display list content allowing for any held resources to be released.
* // After calling this
* renderNode.discardDisplayList();</pre>
*
*
* <h3>Properties</h3>
* <p>In addition, a RenderNode offers several properties, such as
* {@link #setScaleX(float)} or {@link #setTranslationX(float)}, that can be used to affect all
* the drawing commands recorded within. For instance, these properties can be used
* to move around a large number of images without re-issuing all the individual
* <code>canvas.drawBitmap()</code> calls.</p>
*
* <pre class="prettyprint">
* private void createDisplayList() {
* mRenderNode = new RenderNode("MyRenderNode");
* mRenderNode.setPosition(0, 0, width, height);
* RecordingCanvas canvas = mRenderNode.beginRecording();
* try {
* for (Bitmap b : mBitmaps) {
* canvas.drawBitmap(b, 0.0f, 0.0f, null);
* canvas.translate(0.0f, b.getHeight());
* }
* } finally {
* mRenderNode.endRecording();
* }
* }
*
* protected void onDraw(Canvas canvas) {
* if (canvas.isHardwareAccelerated())
* canvas.drawRenderNode(mRenderNode);
* }
* }
*
* private void moveContentBy(int x) {
* // This will move all the bitmaps recorded inside the display list
* // by x pixels to the right and redraw this view. All the commands
* // recorded in createDisplayList() won't be re-issued, only onDraw()
* // will be invoked and will execute very quickly
* mRenderNode.offsetLeftAndRight(x);
* invalidate();
* }</pre>
*
* <p>A few of the properties may at first appear redundant, such as {@link #setElevation(float)}
* and {@link #setTranslationZ(float)}. The reason for these duplicates are to allow for a
* separation between static & transient usages. For example consider a button that raises from 2dp
* to 8dp when pressed. To achieve that an application may decide to setElevation(2dip), and then
* on press to animate setTranslationZ to 6dip. Combined this achieves the final desired 8dip
* value, but the animation need only concern itself with animating the lift from press without
* needing to know the initial starting value. {@link #setTranslationX(float)} and
* {@link #setTranslationY(float)} are similarly provided for animation uses despite the functional
* overlap with {@link #setPosition(Rect)}.
*
* <p>The RenderNode's transform matrix is computed at render time as follows:
* <pre class="prettyprint">
* Matrix transform = new Matrix();
* transform.setTranslate(renderNode.getTranslationX(), renderNode.getTranslationY());
* transform.preRotate(renderNode.getRotationZ(),
* renderNode.getPivotX(), renderNode.getPivotY());
* transform.preScale(renderNode.getScaleX(), renderNode.getScaleY(),
* renderNode.getPivotX(), renderNode.getPivotY());</pre>
* The current canvas transform matrix, which is translated to the RenderNode's position,
* is then multiplied by the RenderNode's transform matrix. Therefore the ordering of calling
* property setters does not affect the result. That is to say that:
*
* <pre class="prettyprint">
* renderNode.setTranslationX(100);
* renderNode.setScaleX(100);</pre>
*
* is equivalent to:
*
* <pre class="prettyprint">
* renderNode.setScaleX(100);
* renderNode.setTranslationX(100);</pre>
*
* <h3>Threading</h3>
* <p>RenderNode may be created and used on any thread but they are not thread-safe. Only
* a single thread may interact with a RenderNode at any given time. It is critical
* that the RenderNode is only used on the same thread it is drawn with. For example when using
* RenderNode with a custom View, then that RenderNode must only be used from the UI thread.</p>
*
* <h3>When to re-render</h3>
* <p>Many of the RenderNode mutation methods, such as {@link #setTranslationX(float)}, return
* a boolean indicating if the value actually changed or not. This is useful in detecting
* if a new frame should be rendered or not. A typical usage would look like:
* <pre class="prettyprint">
* public void translateTo(int x, int y) {
* boolean needsUpdate = myRenderNode.setTranslationX(x);
* needsUpdate |= myRenderNode.setTranslationY(y);
* if (needsUpdate) {
* myOwningView.invalidate();
* }
* }</pre>
* This is marginally faster than doing a more explicit up-front check if the value changed by
* comparing the desired value against {@link #getTranslationX()} as it minimizes JNI transitions.
* The actual mechanism of requesting a new frame to be rendered will depend on how this
* RenderNode is being drawn. If it's drawn to a containing View, as in the above snippet,
* then simply invalidating that View works. If instead the RenderNode is being drawn to a Canvas
* directly such as with {@link Surface#lockHardwareCanvas()} then a new frame needs to be drawn
* by calling {@link Surface#lockHardwareCanvas()}, re-drawing the root RenderNode or whatever
* top-level content is desired, and finally calling {@link Surface#unlockCanvasAndPost(Canvas)}.
* </p>
*/
public final class RenderNode {
// Use a Holder to allow static initialization in the boot image.
private static class NoImagePreloadHolder {
public static final NativeAllocationRegistry sRegistry =
NativeAllocationRegistry.createMalloced(
RenderNode.class.getClassLoader(), nGetNativeFinalizer());
}
/**
* Not for general use; use only if you are ThreadedRenderer or RecordingCanvas.
*
* @hide
*/
public final long mNativeRenderNode;
private final AnimationHost mAnimationHost;
private RecordingCanvas mCurrentRecordingCanvas;
// Will be null if not currently registered
@Nullable
private CompositePositionUpdateListener mCompositePositionUpdateListener;
/**
* Creates a new RenderNode that can be used to record batches of
* drawing operations, and store / apply render properties when drawn.
*
* @param name The name of the RenderNode, used for debugging purpose. May be null.
*/
public RenderNode(@Nullable String name) {
this(name, null);
}
private RenderNode(String name, AnimationHost animationHost) {
mNativeRenderNode = nCreate(name);
NoImagePreloadHolder.sRegistry.registerNativeAllocation(this, mNativeRenderNode);
mAnimationHost = animationHost;
}
/**
* @see RenderNode#adopt(long)
*/
private RenderNode(long nativePtr) {
mNativeRenderNode = nativePtr;
NoImagePreloadHolder.sRegistry.registerNativeAllocation(this, mNativeRenderNode);
mAnimationHost = null;
}
/** @hide */
public static RenderNode create(String name, @Nullable AnimationHost animationHost) {
return new RenderNode(name, animationHost);
}
/**
* Adopts an existing native render node.
*
* Note: This will *NOT* incRef() on the native object, however it will
* decRef() when it is destroyed. The caller should have already incRef'd it
*
* @hide
*/
public static RenderNode adopt(long nativePtr) {
return new RenderNode(nativePtr);
}
/**
* Listens for RenderNode position updates for synchronous window movement.
*
* This is not suitable for generic position listening, it is only designed & intended
* for use by things which require external position events like SurfaceView, PopupWindow, etc..
*
* @hide
*/
public interface PositionUpdateListener {
/**
* Called by native by a Rendering Worker thread to update window position
*
* @hide
*/
void positionChanged(long frameNumber, int left, int top, int right, int bottom);
/**
* Called by JNI
*
* @hide */
static boolean callPositionChanged(WeakReference<PositionUpdateListener> weakListener,
long frameNumber, int left, int top, int right, int bottom) {
final PositionUpdateListener listener = weakListener.get();
if (listener != null) {
listener.positionChanged(frameNumber, left, top, right, bottom);
return true;
} else {
return false;
}
}
/**
* Call to apply a stretch effect to any child SurfaceControl layers
*
* TODO: Fold this into positionChanged & have HWUI do the ASurfaceControl calls?
* (njawad) update to consume different stretch parameters for horizontal/vertical stretch
* to ensure SkiaGLRenderEngine can also apply the same stretch to a surface
*
* @hide
*/
default void applyStretch(long frameNumber, float width, float height,
float vecX, float vecY,
float maxStretchX, float maxStretchY, float childRelativeLeft,
float childRelativeTop, float childRelativeRight, float childRelativeBottom) { }
/**
* Called by JNI
*
* @hide */
static boolean callApplyStretch(WeakReference<PositionUpdateListener> weakListener,
long frameNumber, float width, float height,
float vecX, float vecY,
float maxStretchX, float maxStretchY, float childRelativeLeft,
float childRelativeTop, float childRelativeRight, float childRelativeBottom) {
final PositionUpdateListener listener = weakListener.get();
if (listener != null) {
listener.applyStretch(frameNumber, width, height, vecX, vecY, maxStretchX,
maxStretchY, childRelativeLeft, childRelativeTop, childRelativeRight,
childRelativeBottom);
return true;
} else {
return false;
}
}
/**
* Called by native on RenderThread to notify that the view is no longer in the
* draw tree. UI thread is blocked at this point.
*
* @hide
*/
void positionLost(long frameNumber);
/**
* Called by JNI
*
* @hide */
static boolean callPositionLost(WeakReference<PositionUpdateListener> weakListener,
long frameNumber) {
final PositionUpdateListener listener = weakListener.get();
if (listener != null) {
listener.positionLost(frameNumber);
return true;
} else {
return false;
}
}
}
private static final class CompositePositionUpdateListener implements PositionUpdateListener {
private final PositionUpdateListener[] mListeners;
private static final PositionUpdateListener[] sEmpty = new PositionUpdateListener[0];
CompositePositionUpdateListener(PositionUpdateListener... listeners) {
mListeners = listeners != null ? listeners : sEmpty;
}
public CompositePositionUpdateListener with(PositionUpdateListener listener) {
return new CompositePositionUpdateListener(
ArrayUtils.appendElement(PositionUpdateListener.class, mListeners, listener));
}
public CompositePositionUpdateListener without(PositionUpdateListener listener) {
return new CompositePositionUpdateListener(
ArrayUtils.removeElement(PositionUpdateListener.class, mListeners, listener));
}
@Override
public void positionChanged(long frameNumber, int left, int top, int right, int bottom) {
for (PositionUpdateListener pul : mListeners) {
pul.positionChanged(frameNumber, left, top, right, bottom);
}
}
@Override
public void positionLost(long frameNumber) {
for (PositionUpdateListener pul : mListeners) {
pul.positionLost(frameNumber);
}
}
@Override
public void applyStretch(long frameNumber, float width, float height,
float vecX, float vecY, float maxStretchX, float maxStretchY, float childRelativeLeft,
float childRelativeTop, float childRelativeRight, float childRelativeBottom) {
for (PositionUpdateListener pul : mListeners) {
pul.applyStretch(frameNumber, width, height, vecX, vecY, maxStretchX,
maxStretchY, childRelativeLeft, childRelativeTop, childRelativeRight,
childRelativeBottom);
}
}
}
/**
* Enable callbacks for position changes. Call only from the UI thread or with
* external synchronization.
*
* @hide
*/
public void addPositionUpdateListener(@NonNull PositionUpdateListener listener) {
CompositePositionUpdateListener comp = mCompositePositionUpdateListener;
if (comp == null) {
comp = new CompositePositionUpdateListener(listener);
} else {
comp = comp.with(listener);
}
mCompositePositionUpdateListener = comp;
nRequestPositionUpdates(mNativeRenderNode, new WeakReference<>(comp));
}
/**
* Disable a callback for position changes. Call only from the UI thread or with
* external synchronization.
*
* @param listener Callback to remove
* @hide
*/
public void removePositionUpdateListener(@NonNull PositionUpdateListener listener) {
CompositePositionUpdateListener comp = mCompositePositionUpdateListener;
if (comp != null) {
comp = comp.without(listener);
mCompositePositionUpdateListener = comp;
nRequestPositionUpdates(mNativeRenderNode, new WeakReference<>(comp));
}
}
/**
* Starts recording a display list for the render node. All
* operations performed on the returned canvas are recorded and
* stored in this display list.
*
* {@link #endRecording()} must be called when the recording is finished in order to apply
* the updated display list. Failing to call {@link #endRecording()} will result in an
* {@link IllegalStateException} if {@link #beginRecording(int, int)} is called again.
*
* @param width The width of the recording viewport. This will not alter the width of the
* RenderNode itself, that must be set with {@link #setPosition(Rect)}.
* @param height The height of the recording viewport. This will not alter the height of the
* RenderNode itself, that must be set with {@link #setPosition(Rect)}.
* @return A canvas to record drawing operations.
* @throws IllegalStateException If a recording is already in progress. That is, the previous
* call to {@link #beginRecording(int, int)} did not call {@link #endRecording()}.
* @see #endRecording()
* @see #hasDisplayList()
*/
public @NonNull RecordingCanvas beginRecording(int width, int height) {
if (mCurrentRecordingCanvas != null) {
throw new IllegalStateException(
"Recording currently in progress - missing #endRecording() call?");
}
mCurrentRecordingCanvas = RecordingCanvas.obtain(this, width, height);
return mCurrentRecordingCanvas;
}
/**
* Same as {@link #beginRecording(int, int)} with the width & height set
* to the RenderNode's own width & height. The RenderNode's width & height may be set
* with {@link #setPosition(int, int, int, int)}.
*
* @return A canvas to record drawing operations.
* @throws IllegalStateException If a recording is already in progress. That is, the previous
* call to {@link #beginRecording(int, int)} did not call {@link #endRecording()}.
* @see #endRecording()
* @see #hasDisplayList()
*/
public @NonNull RecordingCanvas beginRecording() {
return beginRecording(nGetWidth(mNativeRenderNode), nGetHeight(mNativeRenderNode));
}
/**
* `
* Ends the recording for this display list. Calling this method marks
* the display list valid and {@link #hasDisplayList()} will return true.
*
* @see #beginRecording(int, int)
* @see #hasDisplayList()
*/
public void endRecording() {
if (mCurrentRecordingCanvas == null) {
throw new IllegalStateException(
"No recording in progress, forgot to call #beginRecording()?");
}
RecordingCanvas canvas = mCurrentRecordingCanvas;
mCurrentRecordingCanvas = null;
canvas.finishRecording(this);
canvas.recycle();
}
/**
* @hide
* @deprecated use {@link #beginRecording(int, int)} instead
*/
@Deprecated
public RecordingCanvas start(int width, int height) {
return beginRecording(width, height);
}
/**
* @hide
* @deprecated use {@link #endRecording()} instead
*/
@Deprecated
public void end(RecordingCanvas canvas) {
if (canvas != mCurrentRecordingCanvas) {
throw new IllegalArgumentException("Wrong canvas");
}
endRecording();
}
/**
* Reset native resources. This is called when cleaning up the state of display lists
* during destruction of hardware resources, to ensure that we do not hold onto
* obsolete resources after related resources are gone.
*/
public void discardDisplayList() {
nDiscardDisplayList(mNativeRenderNode);
}
/**
* Returns whether the RenderNode has a display list. If this returns false, the RenderNode
* should be re-recorded with {@link #beginRecording()} and {@link #endRecording()}.
*
* A RenderNode without a display list may still be drawn, however it will have no impact
* on the rendering content until its display list is updated.
*
* When a RenderNode is no longer drawn by anything the system may automatically
* invoke {@link #discardDisplayList()}. It is therefore important to ensure that
* {@link #hasDisplayList()} is true on a RenderNode prior to drawing it.
*
* See {@link #discardDisplayList()}
*
* @return boolean true if this RenderNode has a display list, false otherwise.
*/
public boolean hasDisplayList() {
return nIsValid(mNativeRenderNode);
}
///////////////////////////////////////////////////////////////////////////
// Matrix manipulation
///////////////////////////////////////////////////////////////////////////
/**
* Whether or not the RenderNode has an identity transform. This is a faster
* way to do the otherwise equivalent {@link #getMatrix(Matrix)} {@link Matrix#isIdentity()}
* as it doesn't require copying the Matrix first, thus minimizing overhead.
*
* @return true if the RenderNode has an identity transform, false otherwise
*/
public boolean hasIdentityMatrix() {
return nHasIdentityMatrix(mNativeRenderNode);
}
/**
* Gets the current transform matrix
*
* @param outMatrix The matrix to store the transform of the RenderNode
*/
public void getMatrix(@NonNull Matrix outMatrix) {
nGetTransformMatrix(mNativeRenderNode, outMatrix.ni());
}
/**
* Gets the current transform inverted. This is a faster way to do the otherwise
* equivalent {@link #getMatrix(Matrix)} followed by {@link Matrix#invert(Matrix)}
*
* @param outMatrix The matrix to store the inverse transform of the RenderNode
*/
public void getInverseMatrix(@NonNull Matrix outMatrix) {
nGetInverseTransformMatrix(mNativeRenderNode, outMatrix.ni());
}
///////////////////////////////////////////////////////////////////////////
// RenderProperty Setters
///////////////////////////////////////////////////////////////////////////
/**
* @hide
* @deprecated use {@link #setUseCompositingLayer(boolean, Paint)} instead
*/
@Deprecated
public boolean setLayerType(int layerType) {
return nSetLayerType(mNativeRenderNode, layerType);
}
/**
* @hide
* @deprecated use {@link #setUseCompositingLayer(boolean, Paint)} instead
*/
@Deprecated
public boolean setLayerPaint(@Nullable Paint paint) {
return nSetLayerPaint(mNativeRenderNode, paint != null ? paint.getNativeInstance() : 0);
}
/**
* Controls whether or not to force this RenderNode to render to an intermediate buffer.
* Internally RenderNode will already promote itself to a composition layer if it's useful
* for performance or required for the current combination of {@link #setAlpha(float)} and
* {@link #setHasOverlappingRendering(boolean)}.
*
* <p>The usage of this is instead to allow for either overriding of the internal behavior
* if it's measured to be necessary for the particular rendering content in question or, more
* usefully, to add a composition effect to the RenderNode via the optional paint parameter.
*
* <p>Note: When a RenderNode is using a compositing layer it will also result in
* clipToBounds=true behavior.
*
* @param forceToLayer if true this forces the RenderNode to use an intermediate buffer.
* Default & generally recommended value is false.
* @param paint The blend mode, alpha, and ColorFilter to apply to the compositing layer.
* Only applies if forceToLayer is true. The paint's alpha is multiplied
* with {@link #getAlpha()} to resolve the final alpha of the RenderNode.
* If null then no additional composition effects are applied on top of the
* composition layer.
* @return True if the value changed, false if the new value was the same as the previous value.
*/
public boolean setUseCompositingLayer(boolean forceToLayer, @Nullable Paint paint) {
boolean didChange = nSetLayerType(mNativeRenderNode, forceToLayer ? 2 : 0);
didChange |= nSetLayerPaint(mNativeRenderNode,
paint != null ? paint.getNativeInstance() : 0);
return didChange;
}
/**
* Gets whether or not a compositing layer is forced to be used. The default & recommended
* is false, as it is typically faster to avoid using compositing layers.
* See {@link #setUseCompositingLayer(boolean, Paint)}.
*
* @return true if a compositing layer is forced, false otherwise
*/
public boolean getUseCompositingLayer() {
return nGetLayerType(mNativeRenderNode) != 0;
}
/**
* Sets an additional clip on the RenderNode. If null, the extra clip is removed from the
* RenderNode. If non-null, the RenderNode will be clipped to this rect. In addition if
* {@link #setClipToBounds(boolean)} is true, then the RenderNode will be clipped to the
* intersection of this rectangle and the bounds of the render node, which is set with
* {@link #setPosition(Rect)}.
*
* <p>This is equivalent to do a {@link Canvas#clipRect(Rect)} at the start of this
* RenderNode's display list. However, as this is a property of the RenderNode instead
* of part of the display list it can be more easily animated for transient additional
* clipping. An example usage of this would be the {@link android.transition.ChangeBounds}
* transition animation with the resizeClip=true option.
*
* @param rect the bounds to clip to. If null, the additional clip is removed.
* @return True if the value changed, false if the new value was the same as the previous value.
*/
public boolean setClipRect(@Nullable Rect rect) {
if (rect == null) {
return nSetClipBoundsEmpty(mNativeRenderNode);
} else {
return nSetClipBounds(mNativeRenderNode, rect.left, rect.top, rect.right, rect.bottom);
}
}
/**
* Set whether the Render node should clip itself to its bounds. This defaults to true,
* and is useful to the renderer in enable quick-rejection of chunks of the tree as well as
* better partial invalidation support. Clipping can be further restricted or controlled
* through the combination of this property as well as {@link #setClipRect(Rect)}, which
* allows for a different clipping rectangle to be used in addition to or instead of the
* {@link #setPosition(int, int, int, int)} or the RenderNode.
*
* @param clipToBounds true if the display list should clip to its bounds, false otherwise.
* @return True if the value changed, false if the new value was the same as the previous value.
*/
public boolean setClipToBounds(boolean clipToBounds) {
return nSetClipToBounds(mNativeRenderNode, clipToBounds);
}
/**
* Returns whether or not the RenderNode is clipping to its bounds. See
* {@link #setClipToBounds(boolean)} and {@link #setPosition(int, int, int, int)}
*
* @return true if the render node clips to its bounds, false otherwise.
*/
public boolean getClipToBounds() {
return nGetClipToBounds(mNativeRenderNode);
}
/**
* <p>Sets whether the RenderNode should be drawn immediately after the
* closest ancestor RenderNode containing a projection receiver.
*
* <p>The default is false, and the rendering of this node happens in the typical draw order.
*
* <p>If true, then at rendering time this rendernode will not be drawn in order with the
* {@link Canvas#drawRenderNode(RenderNode)} command that drew this RenderNode, but instead
* it will be re-positioned in the RenderNode tree to be drawn on the closet ancestor with a
* child rendernode that has {@link #setProjectionReceiver(boolean)} as true.
*
* <p>The typical usage of this is to allow a child RenderNode to draw on a parent's background,
* such as the platform's usage with {@link android.graphics.drawable.RippleDrawable}. Consider
* the following structure, built out of which RenderNode called drawRenderNode on a different
* RenderNode:
*
* <pre>
* +-------------+
* |RenderNode: P|
* +-+----------++
* | |
* v v
* +-------+-----+ +-+--------------+
* |RenderNode: C| |RenderNode: P'BG|
* +-------+-----+ +----------------+
* |
* |
* +--------+-------+
* |RenderNode: C'BG|
* +----------------+
* </pre>
*
* If P'BG is a projection receiver, and C'BG is set to project backwards then C'BG will
* behave as if it was drawn directly by P'BG instead of by C. This includes inheriting P'BG's
* clip instead of C's clip.
*
* @param shouldProject true if the display list should be projected onto a
* containing volume. Default is false.
* @return True if the value changed, false if the new value was the same as the previous value.
*/
public boolean setProjectBackwards(boolean shouldProject) {
return nSetProjectBackwards(mNativeRenderNode, shouldProject);
}
/**
* Sets whether the RenderNode is a projection receiver. If true then this RenderNode's parent
* should draw any descendant RenderNodes with ProjectBackwards=true directly on top of it.
* Default value is false. See
* {@link #setProjectBackwards(boolean)} for a description of what this entails.
*
* @param shouldRecieve True if this RenderNode is a projection receiver, false otherwise.
* Default is false.
* @return True if the value changed, false if the new value was the same as the previous value.
*/
public boolean setProjectionReceiver(boolean shouldRecieve) {
return nSetProjectionReceiver(mNativeRenderNode, shouldRecieve);
}
/**
* Sets the outline, defining the shape that casts a shadow, and the path to
* be clipped if setClipToOutline is set.
*
* This will make a copy of the provided {@link Outline}, so any future modifications
* to the outline will need to call {@link #setOutline(Outline)} with the modified
* outline for those changes to be applied.
*
* @param outline The outline to use for this RenderNode.
* @return True if the value changed, false if the new value was the same as the previous value.
*/
public boolean setOutline(@Nullable Outline outline) {
if (outline == null) {
return nSetOutlineNone(mNativeRenderNode);
}
switch (outline.mMode) {
case Outline.MODE_EMPTY:
return nSetOutlineEmpty(mNativeRenderNode);
case Outline.MODE_ROUND_RECT:
return nSetOutlineRoundRect(mNativeRenderNode,
outline.mRect.left, outline.mRect.top,
outline.mRect.right, outline.mRect.bottom,
outline.mRadius, outline.mAlpha);
case Outline.MODE_PATH:
return nSetOutlinePath(mNativeRenderNode, outline.mPath.mNativePath,
outline.mAlpha);
}
throw new IllegalArgumentException("Unrecognized outline?");
}
/** @hide */
public boolean clearStretch() {
return nClearStretch(mNativeRenderNode);
}
/** @hide */
public boolean stretch(float vecX, float vecY,
float maxStretchAmountX, float maxStretchAmountY) {
if (Float.isInfinite(vecX) || Float.isNaN(vecX)) {
throw new IllegalArgumentException("vecX must be a finite, non-NaN value " + vecX);
}
if (Float.isInfinite(vecY) || Float.isNaN(vecY)) {
throw new IllegalArgumentException("vecY must be a finite, non-NaN value " + vecY);
}
if (maxStretchAmountX <= 0.0f) {
throw new IllegalArgumentException(
"The max horizontal stretch amount must be >0, got " + maxStretchAmountX);
}
if (maxStretchAmountY <= 0.0f) {
throw new IllegalArgumentException(
"The max vertical stretch amount must be >0, got " + maxStretchAmountY);
}
return nStretch(
mNativeRenderNode,
vecX,
vecY,
maxStretchAmountX,
maxStretchAmountY
);
}
/**
* Checks if the RenderNode has a shadow. That is, if the combination of {@link #getElevation()}
* and {@link #getTranslationZ()} is greater than zero, there is an {@link Outline} set with
* a valid shadow caster path, and the provided outline has a non-zero
* {@link Outline#getAlpha()}.
*
* @return True if this RenderNode has a shadow, false otherwise
*/
public boolean hasShadow() {
return nHasShadow(mNativeRenderNode);
}
/**
* Sets the color of the spot shadow that is drawn when the RenderNode has a positive Z or
* elevation value and is drawn inside of a {@link Canvas#enableZ()} section.
* <p>
* By default the shadow color is black. Generally, this color will be opaque so the intensity
* of the shadow is consistent between different RenderNodes with different colors.
* <p>
* The opacity of the final spot shadow is a function of the shadow caster height, the
* alpha channel of the outlineSpotShadowColor (typically opaque), and the
* {@link android.R.attr#spotShadowAlpha} theme attribute
*
* @param color The color this RenderNode will cast for its elevation spot shadow.
* @return True if the value changed, false if the new value was the same as the previous value.
*/
public boolean setSpotShadowColor(@ColorInt int color) {
return nSetSpotShadowColor(mNativeRenderNode, color);
}
/**
* @return The shadow color set by {@link #setSpotShadowColor(int)}, or black if nothing
* was set
*/
public @ColorInt int getSpotShadowColor() {
return nGetSpotShadowColor(mNativeRenderNode);
}
/**
* Sets the color of the ambient shadow that is drawn when the RenderNode has a positive Z or
* elevation value and is drawn inside of a {@link Canvas#enableZ()} section.
* <p>
* By default the shadow color is black. Generally, this color will be opaque so the intensity
* of the shadow is consistent between different RenderNodes with different colors.
* <p>
* The opacity of the final ambient shadow is a function of the shadow caster height, the
* alpha channel of the outlineAmbientShadowColor (typically opaque), and the
* {@link android.R.attr#ambientShadowAlpha} theme attribute.
*
* @param color The color this RenderNode will cast for its elevation shadow.
* @return True if the value changed, false if the new value was the same as the previous value.
*/
public boolean setAmbientShadowColor(@ColorInt int color) {
return nSetAmbientShadowColor(mNativeRenderNode, color);
}
/**
* @return The shadow color set by {@link #setAmbientShadowColor(int)}, or black if
* nothing was set
*/
public @ColorInt int getAmbientShadowColor() {
return nGetAmbientShadowColor(mNativeRenderNode);
}
/**
* Enables or disables clipping to the outline.
*
* @param clipToOutline true if clipping to the outline.
* @return True if the clipToOutline value changed, false if previous value matched the new
* value.
*/
public boolean setClipToOutline(boolean clipToOutline) {
return nSetClipToOutline(mNativeRenderNode, clipToOutline);
}
/**
* See {@link #setClipToOutline(boolean)}
*
* @return True if this RenderNode clips to its outline, false otherwise
*/
public boolean getClipToOutline() {
return nGetClipToOutline(mNativeRenderNode);
}
/**
* Controls the RenderNode's circular reveal clip.
*
* @hide
*/
public boolean setRevealClip(boolean shouldClip,
float x, float y, float radius) {
return nSetRevealClip(mNativeRenderNode, shouldClip, x, y, radius);
}
/**
* Set the static matrix on the display list. The specified matrix is combined with other
* transforms (such as {@link #setScaleX(float)}, {@link #setRotationZ(float)}, etc.)
*
* @param matrix A transform matrix to apply to this display list
* @hide TODO Do we want this?
*/
public boolean setStaticMatrix(Matrix matrix) {
return nSetStaticMatrix(mNativeRenderNode, matrix.ni());
}
/**
* Set the Animation matrix on the display list. This matrix exists if an Animation is
* currently playing on a View, and is set on the display list during at draw() time. When
* the Animation finishes, the matrix should be cleared by sending <code>null</code>
* for the matrix parameter.
*
* @param matrix The matrix, null indicates that the matrix should be cleared.
* @see #getAnimationMatrix()
*
* @hide TODO Do we want this?
*/
public boolean setAnimationMatrix(@Nullable Matrix matrix) {
return nSetAnimationMatrix(mNativeRenderNode,
(matrix != null) ? matrix.ni() : 0);
}
/**
* Returns the previously set Animation matrix. This matrix exists if an Animation is
* currently playing on a View, and is set on the display list during at draw() time.
* Returns <code>null</code> when there is no transformation provided by
* {@link #setAnimationMatrix(Matrix)}.
*
* @return the current Animation matrix.
* @see #setAnimationMatrix(Matrix)
*
* @hide
*/
@Nullable
public Matrix getAnimationMatrix() {
Matrix output = new Matrix();
if (nGetAnimationMatrix(mNativeRenderNode, output.ni())) {
return output;
} else {
return null;
}
}
/**
* Sets the translucency level for the display list.
*
* @param alpha The translucency of the display list, must be a value between 0.0f and 1.0f
* @see View#setAlpha(float)
* @see #getAlpha()
* @return True if the value changed, false if the new value was the same as the previous value.
*/
public boolean setAlpha(float alpha) {
return nSetAlpha(mNativeRenderNode, alpha);
}
/**
* Configure the {@link android.graphics.RenderEffect} to apply to this RenderNode. This
* will apply a visual effect to the end result of the contents of this RenderNode before
* it is drawn into the destination. For example if
* {@link RenderEffect#createBlurEffect(float, float, RenderEffect, Shader.TileMode)}
* is provided, the contents will be drawn in a separate layer, then this layer will
* be blurred when this RenderNode is drawn into the destination.
* @param renderEffect to be applied to the RenderNode. Passing null clears all previously
* configured RenderEffects
* @return True if the value changed, false if the new value was the same as the previous value.
*/
public boolean setRenderEffect(@Nullable RenderEffect renderEffect) {
return nSetRenderEffect(mNativeRenderNode,
renderEffect != null ? renderEffect.getNativeInstance() : 0);
}
/**
* Returns the translucency level of this display list.
*
* @return A value between 0.0f and 1.0f
* @see #setAlpha(float)
*/
public float getAlpha() {
return nGetAlpha(mNativeRenderNode);
}
/**
* Sets whether the display list renders content which overlaps. Non-overlapping rendering
* can use a fast path for alpha that avoids rendering to an offscreen buffer. By default
* display lists consider they do not have overlapping content.
*
* @param hasOverlappingRendering False if the content is guaranteed to be non-overlapping,
* true otherwise.
* @see android.view.View#hasOverlappingRendering()
* @see #hasOverlappingRendering()
*/
public boolean setHasOverlappingRendering(boolean hasOverlappingRendering) {
return nSetHasOverlappingRendering(mNativeRenderNode, hasOverlappingRendering);
}
/** @hide */
@IntDef({USAGE_BACKGROUND})
@Retention(RetentionPolicy.SOURCE)
public @interface UsageHint {