1 /*
2  * Copyright 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 androidx.graphics.opengl
18 
19 import android.app.Activity
20 import android.content.Context
21 import android.graphics.Color
22 import android.graphics.drawable.ColorDrawable
23 import android.os.Bundle
24 import android.view.SurfaceHolder
25 import android.view.SurfaceView
26 import android.view.ViewGroup
27 
28 class SurfaceViewTestActivity : Activity() {
29 
30     private lateinit var mSurfaceView: TestSurfaceView
31 
onCreatenull32     override fun onCreate(savedInstanceState: Bundle?) {
33         super.onCreate(savedInstanceState)
34         window.setBackgroundDrawable(ColorDrawable(Color.WHITE))
35         val surfaceView = TestSurfaceView(this).also { mSurfaceView = it }
36         setContentView(surfaceView, ViewGroup.LayoutParams(WIDTH, HEIGHT))
37     }
38 
39     private var mOnDestroyCallback: (() -> Unit)? = null
40 
getSurfaceViewnull41     fun getSurfaceView(): TestSurfaceView = mSurfaceView
42 
43     companion object {
44         const val WIDTH = 100
45         const val HEIGHT = 100
46     }
47 
48     class TestSurfaceView(context: Context) : SurfaceView(context) {
49 
50         private var mHolderWrapper: HolderWrapper? = null
51 
getHoldernull52         override fun getHolder(): SurfaceHolder {
53             var wrapper = mHolderWrapper
54             if (wrapper == null) {
55                 wrapper = HolderWrapper(super.getHolder()).also { mHolderWrapper = it }
56             }
57             return wrapper
58         }
59 
getCallbackCountnull60         fun getCallbackCount(): Int = mHolderWrapper?.mCallbacks?.size ?: 0
61 
62         class HolderWrapper(val wrapped: SurfaceHolder) : SurfaceHolder by wrapped {
63 
64             val mCallbacks = ArrayList<SurfaceHolder.Callback>()
65 
66             override fun addCallback(callback: SurfaceHolder.Callback) {
67                 if (!mCallbacks.contains(callback)) {
68                     mCallbacks.add(callback)
69                     wrapped.addCallback(callback)
70                 }
71             }
72 
73             override fun removeCallback(callback: SurfaceHolder.Callback) {
74                 if (mCallbacks.contains(callback)) {
75                     mCallbacks.remove(callback)
76                     wrapped.removeCallback(callback)
77                 }
78             }
79         }
80     }
81 
setOnDestroyCallbacknull82     fun setOnDestroyCallback(callback: (() -> Unit)?) {
83         mOnDestroyCallback = callback
84     }
85 
onDestroynull86     override fun onDestroy() {
87         super.onDestroy()
88         mOnDestroyCallback?.invoke()
89     }
90 }
91