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 android.surfaceflinger;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.os.Bundle;
22 import android.view.SurfaceControl;
23 import android.view.SurfaceHolder;
24 import android.view.SurfaceView;
25 import android.view.Window;
26 import android.view.WindowManager;
27 
28 import java.util.concurrent.CountDownLatch;
29 
30 /**
31  * A simple activity used for testing, e.g. performance of activity switching, or as a base
32  * container of testing view.
33  */
34 public class SurfaceFlingerTestActivity extends Activity {
35     public TestSurfaceView mTestSurfaceView;
36     SurfaceControl mSurfaceControl;
37     CountDownLatch mIsReady = new CountDownLatch(1);
38 
39     @Override
onCreate(Bundle savedInstanceState)40     protected void onCreate(Bundle savedInstanceState) {
41         super.onCreate(savedInstanceState);
42         requestWindowFeature(Window.FEATURE_NO_TITLE);
43         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
44                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
45         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
46         mTestSurfaceView = new TestSurfaceView(this);
47         setContentView(mTestSurfaceView);
48     }
49 
createChildSurfaceControl()50     public SurfaceControl createChildSurfaceControl() {
51         return mTestSurfaceView.getChildSurfaceControlHelper();
52     }
53 
54     public class TestSurfaceView extends SurfaceView {
TestSurfaceView(Context context)55         public TestSurfaceView(Context context) {
56             super(context);
57             SurfaceHolder holder = getHolder();
58             holder.addCallback(new SurfaceHolder.Callback() {
59                 @Override
60                 public void surfaceCreated(SurfaceHolder holder) {
61                     mIsReady.countDown();
62                 }
63                 @Override
64                 public void surfaceChanged(SurfaceHolder holder, int format, int width,
65                         int height) {}
66                 @Override
67                 public void surfaceDestroyed(SurfaceHolder holder) {
68                 }
69             });
70         }
71 
getChildSurfaceControlHelper()72         public SurfaceControl getChildSurfaceControlHelper() {
73             try {
74                 mIsReady.await();
75             } catch (InterruptedException ignore) {
76             }
77             SurfaceHolder holder = getHolder();
78 
79             // check to see if surface is valid
80             if (holder.getSurface().isValid()) {
81                 mSurfaceControl = getSurfaceControl();
82             }
83             return new SurfaceControl.Builder()
84                     .setName("ChildSurfaceControl")
85                     .setParent(mSurfaceControl)
86                     .build();
87         }
88     }
89 }
90