blob: 85457cb48218adcfe2cc1e7cefcc25f843c4ed9a [file] [log] [blame]
Jorim Jaggi64be98d2018-04-26 23:23:29 +02001/*
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 Jaggi5bb571d2018-11-06 14:42:04 +010014 * limitations under the License.
Jorim Jaggi64be98d2018-04-26 23:23:29 +020015 */
16
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010017package android.view;
Jorim Jaggi64be98d2018-04-26 23:23:29 +020018
19import android.graphics.Matrix;
20import android.graphics.Rect;
Jorim Jaggicd560732018-05-29 16:29:24 +020021import android.view.SurfaceControl.Transaction;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010022
23import com.android.internal.annotations.VisibleForTesting;
Jorim Jaggi64be98d2018-04-26 23:23:29 +020024
Winson Chung71eda582018-12-11 12:54:36 -080025import java.util.function.Consumer;
26
Jorim Jaggi64be98d2018-04-26 23:23:29 +020027/**
28 * Helper class to apply surface transactions in sync with RenderThread.
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010029 * @hide
Jorim Jaggi64be98d2018-04-26 23:23:29 +020030 */
31public class SyncRtSurfaceTransactionApplier {
32
Jorim Jaggi64be98d2018-04-26 23:23:29 +020033 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 Jaggi64be98d2018-04-26 23:23:29 +020048 * @param params The surface parameters to apply. DO NOT MODIFY the list after passing into
49 * this method to avoid synchronization issues.
50 */
Sunny Goyalbba378e2018-08-22 12:50:26 -070051 public void scheduleApply(final SurfaceParams... params) {
Jorim Jaggi2d39fb92018-05-16 14:37:02 -070052 if (mTargetViewRootImpl == null) {
53 return;
Jorim Jaggi64be98d2018-04-26 23:23:29 +020054 }
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010055 mTargetViewRootImpl.registerRtFrameCallback(frame -> {
56 if (mTargetSurface == null || !mTargetSurface.isValid()) {
57 return;
Sunny Goyalbba378e2018-08-22 12:50:26 -070058 }
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010059 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 Jaggi2d39fb92018-05-16 14:37:02 -070068 });
Jorim Jaggi42b04752018-05-18 18:23:08 +020069
70 // Make sure a frame gets scheduled.
71 mTargetViewRootImpl.getView().invalidate();
Jorim Jaggi64be98d2018-04-26 23:23:29 +020072 }
73
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010074 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 Jaggi67684882019-01-22 17:36:34 +010080 if (params.visible) {
81 t.show(params.surface);
82 } else {
83 t.hide(params.surface);
84 }
Jorim Jaggicd560732018-05-29 16:29:24 +020085 }
86
Winson Chung71eda582018-12-11 12:54:36 -080087 /**
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 Jaggi64be98d2018-04-26 23:23:29 +0200116 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 Jaggi5bb571d2018-11-06 14:42:04 +0100127 public SurfaceParams(SurfaceControl surface, float alpha, Matrix matrix,
Jorim Jaggi67684882019-01-22 17:36:34 +0100128 Rect windowCrop, int layer, float cornerRadius, boolean visible) {
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100129 this.surface = surface;
Jorim Jaggi64be98d2018-04-26 23:23:29 +0200130 this.alpha = alpha;
131 this.matrix = new Matrix(matrix);
132 this.windowCrop = new Rect(windowCrop);
133 this.layer = layer;
Lucas Dupin086c6fc2018-10-16 18:06:43 -0700134 this.cornerRadius = cornerRadius;
Jorim Jaggi67684882019-01-22 17:36:34 +0100135 this.visible = visible;
Jorim Jaggi64be98d2018-04-26 23:23:29 +0200136 }
137
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100138 @VisibleForTesting
139 public final SurfaceControl surface;
140
141 @VisibleForTesting
142 public final float alpha;
143
144 @VisibleForTesting
Lucas Dupin086c6fc2018-10-16 18:06:43 -0700145 final float cornerRadius;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100146
147 @VisibleForTesting
148 public final Matrix matrix;
149
150 @VisibleForTesting
151 public final Rect windowCrop;
152
153 @VisibleForTesting
154 public final int layer;
Jorim Jaggi67684882019-01-22 17:36:34 +0100155
156 public final boolean visible;
Jorim Jaggi64be98d2018-04-26 23:23:29 +0200157 }
158}