125cfa134835e3791bdb6572f5e25cf4599015678Robert Carr/*
225cfa134835e3791bdb6572f5e25cf4599015678Robert Carr * Copyright (C) 2016 The Android Open Source Project
325cfa134835e3791bdb6572f5e25cf4599015678Robert Carr *
425cfa134835e3791bdb6572f5e25cf4599015678Robert Carr * Licensed under the Apache License, Version 2.0 (the "License");
525cfa134835e3791bdb6572f5e25cf4599015678Robert Carr * you may not use this file except in compliance with the License.
625cfa134835e3791bdb6572f5e25cf4599015678Robert Carr * You may obtain a copy of the License at
725cfa134835e3791bdb6572f5e25cf4599015678Robert Carr *
825cfa134835e3791bdb6572f5e25cf4599015678Robert Carr *      http://www.apache.org/licenses/LICENSE-2.0
925cfa134835e3791bdb6572f5e25cf4599015678Robert Carr *
1025cfa134835e3791bdb6572f5e25cf4599015678Robert Carr * Unless required by applicable law or agreed to in writing, software
1125cfa134835e3791bdb6572f5e25cf4599015678Robert Carr * distributed under the License is distributed on an "AS IS" BASIS,
1225cfa134835e3791bdb6572f5e25cf4599015678Robert Carr * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1325cfa134835e3791bdb6572f5e25cf4599015678Robert Carr * See the License for the specific language governing permissions and
1425cfa134835e3791bdb6572f5e25cf4599015678Robert Carr * limitations under the License.
1525cfa134835e3791bdb6572f5e25cf4599015678Robert Carr */
1625cfa134835e3791bdb6572f5e25cf4599015678Robert Carr
1725cfa134835e3791bdb6572f5e25cf4599015678Robert Carrpackage com.android.internal.view;
1825cfa134835e3791bdb6572f5e25cf4599015678Robert Carr
1925cfa134835e3791bdb6572f5e25cf4599015678Robert Carrimport android.os.RemoteException;
2025cfa134835e3791bdb6572f5e25cf4599015678Robert Carrimport android.view.Surface;
2125cfa134835e3791bdb6572f5e25cf4599015678Robert Carrimport android.view.SurfaceHolder;
2225cfa134835e3791bdb6572f5e25cf4599015678Robert Carr
2325cfa134835e3791bdb6572f5e25cf4599015678Robert Carrpublic class SurfaceCallbackHelper {
24d5c7dd6da810a6b89151b337bea79fd817e6b72aRobert Carr    Runnable mRunnable;
2525cfa134835e3791bdb6572f5e25cf4599015678Robert Carr
2625cfa134835e3791bdb6572f5e25cf4599015678Robert Carr    int mFinishDrawingCollected = 0;
2725cfa134835e3791bdb6572f5e25cf4599015678Robert Carr    int mFinishDrawingExpected = 0;
2825cfa134835e3791bdb6572f5e25cf4599015678Robert Carr
2925cfa134835e3791bdb6572f5e25cf4599015678Robert Carr    private Runnable mFinishDrawingRunnable = new Runnable() {
3025cfa134835e3791bdb6572f5e25cf4599015678Robert Carr            @Override
3125cfa134835e3791bdb6572f5e25cf4599015678Robert Carr            public void run() {
3225cfa134835e3791bdb6572f5e25cf4599015678Robert Carr                synchronized (SurfaceCallbackHelper.this) {
3325cfa134835e3791bdb6572f5e25cf4599015678Robert Carr                    mFinishDrawingCollected++;
3425cfa134835e3791bdb6572f5e25cf4599015678Robert Carr                    if (mFinishDrawingCollected < mFinishDrawingExpected) {
3525cfa134835e3791bdb6572f5e25cf4599015678Robert Carr                        return;
3625cfa134835e3791bdb6572f5e25cf4599015678Robert Carr                    }
37d5c7dd6da810a6b89151b337bea79fd817e6b72aRobert Carr                    mRunnable.run();
3825cfa134835e3791bdb6572f5e25cf4599015678Robert Carr                }
3925cfa134835e3791bdb6572f5e25cf4599015678Robert Carr            }
4025cfa134835e3791bdb6572f5e25cf4599015678Robert Carr    };
4125cfa134835e3791bdb6572f5e25cf4599015678Robert Carr
42d5c7dd6da810a6b89151b337bea79fd817e6b72aRobert Carr    public SurfaceCallbackHelper(Runnable callbacksCollected) {
43d5c7dd6da810a6b89151b337bea79fd817e6b72aRobert Carr        mRunnable = callbacksCollected;
4425cfa134835e3791bdb6572f5e25cf4599015678Robert Carr    }
4525cfa134835e3791bdb6572f5e25cf4599015678Robert Carr
4625cfa134835e3791bdb6572f5e25cf4599015678Robert Carr    public void dispatchSurfaceRedrawNeededAsync(SurfaceHolder holder, SurfaceHolder.Callback callbacks[]) {
4725cfa134835e3791bdb6572f5e25cf4599015678Robert Carr        if (callbacks == null || callbacks.length == 0) {
48d5c7dd6da810a6b89151b337bea79fd817e6b72aRobert Carr            mRunnable.run();
4925cfa134835e3791bdb6572f5e25cf4599015678Robert Carr            return;
5025cfa134835e3791bdb6572f5e25cf4599015678Robert Carr        }
5125cfa134835e3791bdb6572f5e25cf4599015678Robert Carr
5225cfa134835e3791bdb6572f5e25cf4599015678Robert Carr        synchronized (this) {
5325cfa134835e3791bdb6572f5e25cf4599015678Robert Carr            mFinishDrawingExpected = callbacks.length;
5425cfa134835e3791bdb6572f5e25cf4599015678Robert Carr            mFinishDrawingCollected = 0;
5525cfa134835e3791bdb6572f5e25cf4599015678Robert Carr        }
5625cfa134835e3791bdb6572f5e25cf4599015678Robert Carr
5725cfa134835e3791bdb6572f5e25cf4599015678Robert Carr        for (SurfaceHolder.Callback c : callbacks) {
5825cfa134835e3791bdb6572f5e25cf4599015678Robert Carr            if (c instanceof SurfaceHolder.Callback2) {
5925cfa134835e3791bdb6572f5e25cf4599015678Robert Carr                ((SurfaceHolder.Callback2) c).surfaceRedrawNeededAsync(
6025cfa134835e3791bdb6572f5e25cf4599015678Robert Carr                        holder, mFinishDrawingRunnable);
6125cfa134835e3791bdb6572f5e25cf4599015678Robert Carr            } else {
6225cfa134835e3791bdb6572f5e25cf4599015678Robert Carr                mFinishDrawingRunnable.run();
6325cfa134835e3791bdb6572f5e25cf4599015678Robert Carr            }
6425cfa134835e3791bdb6572f5e25cf4599015678Robert Carr        }
6525cfa134835e3791bdb6572f5e25cf4599015678Robert Carr    }
6625cfa134835e3791bdb6572f5e25cf4599015678Robert Carr}