Jorim Jaggi | 64be98d | 2018-04-26 23:23:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 14 | * limitations under the License. |
Jorim Jaggi | 64be98d | 2018-04-26 23:23:29 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 17 | package android.view; |
Jorim Jaggi | 64be98d | 2018-04-26 23:23:29 +0200 | [diff] [blame] | 18 | |
| 19 | import android.graphics.Matrix; |
| 20 | import android.graphics.Rect; |
Jorim Jaggi | cd56073 | 2018-05-29 16:29:24 +0200 | [diff] [blame] | 21 | import android.view.SurfaceControl.Transaction; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 22 | |
| 23 | import com.android.internal.annotations.VisibleForTesting; |
Jorim Jaggi | 64be98d | 2018-04-26 23:23:29 +0200 | [diff] [blame] | 24 | |
Winson Chung | 71eda58 | 2018-12-11 12:54:36 -0800 | [diff] [blame] | 25 | import java.util.function.Consumer; |
| 26 | |
Jorim Jaggi | 64be98d | 2018-04-26 23:23:29 +0200 | [diff] [blame] | 27 | /** |
| 28 | * Helper class to apply surface transactions in sync with RenderThread. |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 29 | * @hide |
Jorim Jaggi | 64be98d | 2018-04-26 23:23:29 +0200 | [diff] [blame] | 30 | */ |
| 31 | public class SyncRtSurfaceTransactionApplier { |
| 32 | |
Jorim Jaggi | 64be98d | 2018-04-26 23:23:29 +0200 | [diff] [blame] | 33 | private final Surface mTargetSurface; |
| 34 | private final ViewRootImpl mTargetViewRootImpl; |
| 35 | private final float[] mTmpFloat9 = new float[9]; |
| 36 | |
| 37 | /** |
| 38 | * @param targetView The view in the surface that acts as synchronization anchor. |
| 39 | */ |
| 40 | public SyncRtSurfaceTransactionApplier(View targetView) { |
| 41 | mTargetViewRootImpl = targetView != null ? targetView.getViewRootImpl() : null; |
| 42 | mTargetSurface = mTargetViewRootImpl != null ? mTargetViewRootImpl.mSurface : null; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Schedules applying surface parameters on the next frame. |
| 47 | * |
Jorim Jaggi | 64be98d | 2018-04-26 23:23:29 +0200 | [diff] [blame] | 48 | * @param params The surface parameters to apply. DO NOT MODIFY the list after passing into |
| 49 | * this method to avoid synchronization issues. |
| 50 | */ |
Sunny Goyal | bba378e | 2018-08-22 12:50:26 -0700 | [diff] [blame] | 51 | public void scheduleApply(final SurfaceParams... params) { |
Jorim Jaggi | 2d39fb9 | 2018-05-16 14:37:02 -0700 | [diff] [blame] | 52 | if (mTargetViewRootImpl == null) { |
| 53 | return; |
Jorim Jaggi | 64be98d | 2018-04-26 23:23:29 +0200 | [diff] [blame] | 54 | } |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 55 | mTargetViewRootImpl.registerRtFrameCallback(frame -> { |
| 56 | if (mTargetSurface == null || !mTargetSurface.isValid()) { |
| 57 | return; |
Sunny Goyal | bba378e | 2018-08-22 12:50:26 -0700 | [diff] [blame] | 58 | } |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 59 | Transaction t = new Transaction(); |
| 60 | for (int i = params.length - 1; i >= 0; i--) { |
| 61 | SurfaceParams surfaceParams = params[i]; |
| 62 | SurfaceControl surface = surfaceParams.surface; |
| 63 | t.deferTransactionUntilSurface(surface, mTargetSurface, frame); |
| 64 | applyParams(t, surfaceParams, mTmpFloat9); |
| 65 | } |
| 66 | t.setEarlyWakeup(); |
| 67 | t.apply(); |
Jorim Jaggi | 2d39fb9 | 2018-05-16 14:37:02 -0700 | [diff] [blame] | 68 | }); |
Jorim Jaggi | 42b0475 | 2018-05-18 18:23:08 +0200 | [diff] [blame] | 69 | |
| 70 | // Make sure a frame gets scheduled. |
| 71 | mTargetViewRootImpl.getView().invalidate(); |
Jorim Jaggi | 64be98d | 2018-04-26 23:23:29 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 74 | public static void applyParams(Transaction t, SurfaceParams params, float[] tmpFloat9) { |
| 75 | t.setMatrix(params.surface, params.matrix, tmpFloat9); |
| 76 | t.setWindowCrop(params.surface, params.windowCrop); |
| 77 | t.setAlpha(params.surface, params.alpha); |
| 78 | t.setLayer(params.surface, params.layer); |
| 79 | t.setCornerRadius(params.surface, params.cornerRadius); |
Jorim Jaggi | 6768488 | 2019-01-22 17:36:34 +0100 | [diff] [blame] | 80 | if (params.visible) { |
| 81 | t.show(params.surface); |
| 82 | } else { |
| 83 | t.hide(params.surface); |
| 84 | } |
Jorim Jaggi | cd56073 | 2018-05-29 16:29:24 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Winson Chung | 71eda58 | 2018-12-11 12:54:36 -0800 | [diff] [blame] | 87 | /** |
| 88 | * Creates an instance of SyncRtSurfaceTransactionApplier, deferring until the target view is |
| 89 | * attached if necessary. |
| 90 | */ |
| 91 | public static void create(final View targetView, |
| 92 | final Consumer<SyncRtSurfaceTransactionApplier> callback) { |
| 93 | if (targetView == null) { |
| 94 | // No target view, no applier |
| 95 | callback.accept(null); |
| 96 | } else if (targetView.getViewRootImpl() != null) { |
| 97 | // Already attached, we're good to go |
| 98 | callback.accept(new SyncRtSurfaceTransactionApplier(targetView)); |
| 99 | } else { |
| 100 | // Haven't been attached before we can get the view root |
| 101 | targetView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() { |
| 102 | @Override |
| 103 | public void onViewAttachedToWindow(View v) { |
| 104 | targetView.removeOnAttachStateChangeListener(this); |
| 105 | callback.accept(new SyncRtSurfaceTransactionApplier(targetView)); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void onViewDetachedFromWindow(View v) { |
| 110 | // Do nothing |
| 111 | } |
| 112 | }); |
| 113 | } |
| 114 | } |
| 115 | |
Jorim Jaggi | 64be98d | 2018-04-26 23:23:29 +0200 | [diff] [blame] | 116 | public static class SurfaceParams { |
| 117 | |
| 118 | /** |
| 119 | * Constructs surface parameters to be applied when the current view state gets pushed to |
| 120 | * RenderThread. |
| 121 | * |
| 122 | * @param surface The surface to modify. |
| 123 | * @param alpha Alpha to apply. |
| 124 | * @param matrix Matrix to apply. |
| 125 | * @param windowCrop Crop to apply. |
| 126 | */ |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 127 | public SurfaceParams(SurfaceControl surface, float alpha, Matrix matrix, |
Jorim Jaggi | 6768488 | 2019-01-22 17:36:34 +0100 | [diff] [blame] | 128 | Rect windowCrop, int layer, float cornerRadius, boolean visible) { |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 129 | this.surface = surface; |
Jorim Jaggi | 64be98d | 2018-04-26 23:23:29 +0200 | [diff] [blame] | 130 | this.alpha = alpha; |
| 131 | this.matrix = new Matrix(matrix); |
| 132 | this.windowCrop = new Rect(windowCrop); |
| 133 | this.layer = layer; |
Lucas Dupin | 086c6fc | 2018-10-16 18:06:43 -0700 | [diff] [blame] | 134 | this.cornerRadius = cornerRadius; |
Jorim Jaggi | 6768488 | 2019-01-22 17:36:34 +0100 | [diff] [blame] | 135 | this.visible = visible; |
Jorim Jaggi | 64be98d | 2018-04-26 23:23:29 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 138 | @VisibleForTesting |
| 139 | public final SurfaceControl surface; |
| 140 | |
| 141 | @VisibleForTesting |
| 142 | public final float alpha; |
| 143 | |
| 144 | @VisibleForTesting |
Lucas Dupin | 086c6fc | 2018-10-16 18:06:43 -0700 | [diff] [blame] | 145 | final float cornerRadius; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 146 | |
| 147 | @VisibleForTesting |
| 148 | public final Matrix matrix; |
| 149 | |
| 150 | @VisibleForTesting |
| 151 | public final Rect windowCrop; |
| 152 | |
| 153 | @VisibleForTesting |
| 154 | public final int layer; |
Jorim Jaggi | 6768488 | 2019-01-22 17:36:34 +0100 | [diff] [blame] | 155 | |
| 156 | public final boolean visible; |
Jorim Jaggi | 64be98d | 2018-04-26 23:23:29 +0200 | [diff] [blame] | 157 | } |
| 158 | } |