Debian Codegraph @

tests/SurfaceViewSyncTest/src/com/android/test/SurfaceViewSyncActivity.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2022 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
14 * limitations under the License.
15 */
16
17 package com.android.test;
18
19 import android.annotation.NonNull;
20 import android.app.Activity;
21 import android.graphics.Canvas;
22 import android.graphics.Color;
23 import android.graphics.Paint;
24 import android.graphics.Point;
25 import android.graphics.Rect;
26 import android.os.Bundle;
27 import android.os.Handler;
28 import android.os.HandlerThread;
29 import android.view.SurfaceHolder;
30 import android.view.SurfaceView;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.view.WindowManager;
34 import android.view.WindowMetrics;
35 import android.widget.Button;
36 import android.widget.LinearLayout;
37 import android.widget.Switch;
38 import android.window.SurfaceSyncer;
39
40 /**
41 * Test app that allows the user to resize the SurfaceView and have the new buffer sync with the
42 * main window. This tests that {@link SurfaceSyncer} is working correctly.
43 */
44 public class SurfaceViewSyncActivity extends Activity implements SurfaceHolder.Callback {
45 private static final String TAG = "SurfaceViewSyncActivity";
46
47 private SurfaceView mSurfaceView;
48 private boolean mLastExpanded = true;
49
50 private RenderingThread mRenderingThread;
51
52 private final SurfaceSyncer mSurfaceSyncer = new SurfaceSyncer();
53
54 private Button mExpandButton;
55 private Switch mEnableSyncSwitch;
56
57 private int mLastSyncId = -1;
58
59 @Override
60 protected void onCreate(Bundle savedInstanceState) {
61 super.onCreate(savedInstanceState);
62 setContentView(R.layout.activity_surfaceview_sync);
63 mSurfaceView = findViewById(R.id.surface_view);
64 mSurfaceView.getHolder().addCallback(this);
65
66 WindowManager windowManager = getWindowManager();
67 WindowMetrics metrics = windowManager.getCurrentWindowMetrics();
68 Rect bounds = metrics.getBounds();
69
70 LinearLayout container = findViewById(R.id.container);
71 mExpandButton = findViewById(R.id.expand_sv);
72 mEnableSyncSwitch = findViewById(R.id.enable_sync_switch);
73 mExpandButton.setOnClickListener(view -> updateSurfaceViewSize(bounds, container));
74
75 mRenderingThread = new RenderingThread(mSurfaceView.getHolder());
76 }
77
78 private void updateSurfaceViewSize(Rect bounds, View container) {
79 if (mLastSyncId >= 0) {
80 return;
81 }
82
83 final float height;
84 if (mLastExpanded) {
85 height = bounds.height() / 2f;
86 mExpandButton.setText("EXPAND SV");
87 } else {
88 height = bounds.height() / 1.5f;
89 mExpandButton.setText("COLLAPSE SV");
90 }
91 mLastExpanded = !mLastExpanded;
92
93 if (mEnableSyncSwitch.isChecked()) {
94 mLastSyncId = mSurfaceSyncer.setupSync(() -> { });
95 mSurfaceSyncer.addToSync(mLastSyncId, container);
96 }
97
98 ViewGroup.LayoutParams svParams = mSurfaceView.getLayoutParams();
99 svParams.height = (int) height;
100 mSurfaceView.setLayoutParams(svParams);
101 }
102
103 @Override
104 public void surfaceCreated(@NonNull SurfaceHolder holder) {
105 final Canvas canvas = holder.lockCanvas();
106 canvas.drawARGB(255, 255, 0, 0);
107 holder.unlockCanvasAndPost(canvas);
108 mRenderingThread.startRendering();
109 mRenderingThread.renderFrame(null, mSurfaceView.getWidth(), mSurfaceView.getHeight());
110 }
111
112 @Override
113 public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
114 if (mEnableSyncSwitch.isChecked()) {
115 if (mLastSyncId < 0) {
116 mRenderingThread.renderFrame(null, width, height);
117 return;
118 }
119 mSurfaceSyncer.addToSync(mLastSyncId, mSurfaceView, frameCallback ->
120 mRenderingThread.renderFrame(frameCallback, width, height));
121 mSurfaceSyncer.markSyncReady(mLastSyncId);
122 mLastSyncId = -1;
123 } else {
124 mRenderingThread.renderFrame(null, width, height);
125 }
126 }
127
128 @Override
129 public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
130 mRenderingThread.stopRendering();
131 }
132
133 private static class RenderingThread extends HandlerThread {
134 private final SurfaceHolder mSurfaceHolder;
135 private Handler mHandler;
136 private SurfaceSyncer.SurfaceViewFrameCallback mFrameCallback;
137 private final Point mSurfaceSize = new Point();
138
139 int mColorValue = 0;
140 int mColorDelta = 10;
141 private final Paint mPaint = new Paint();
142
143 RenderingThread(SurfaceHolder holder) {
144 super("RenderingThread");
145 mSurfaceHolder = holder;
146 mPaint.setColor(Color.BLACK);
147 mPaint.setTextSize(100);
148 }
149
150 public void renderFrame(SurfaceSyncer.SurfaceViewFrameCallback frameCallback, int width,
151 int height) {
152 if (mHandler != null) {
153 mHandler.post(() -> {
154 mFrameCallback = frameCallback;
155 mSurfaceSize.set(width, height);
156 mRunnable.run();
157 });
158 }
159 }
160
161 private final Runnable mRunnable = new Runnable() {
162 @Override
163 public void run() {
164 if (mFrameCallback != null) {
165 mFrameCallback.onFrameStarted();
166 }
167
168 try {
169 // Long delay from start to finish to mimic slow draw
170 Thread.sleep(1000);
171 } catch (InterruptedException e) {
172 }
173
174 mColorValue += mColorDelta;
175 if (mColorValue > 245 || mColorValue < 10) {
176 mColorDelta *= -1;
177 }
178
179 Canvas c = mSurfaceHolder.lockCanvas();
180 c.drawRGB(255, 0, 0);
181 c.drawText("RENDERED CONTENT", 0, mSurfaceSize.y / 2, mPaint);
182 mSurfaceHolder.unlockCanvasAndPost(c);
183 mFrameCallback = null;
184 }
185 };
186
187 public void startRendering() {
188 start();
189 mHandler = new Handler(getLooper());
190 }
191
192 public void stopRendering() {
193 if (mHandler != null) {
194 mHandler.removeCallbacks(mRunnable);
195 }
196 }
197 }
198 }