• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.internal.view;
18 
19 import android.util.EventLog;
20 import android.view.SurfaceHolder;
21 
22 public class SurfaceCallbackHelper {
23     private static final int LOGTAG_SURFACEVIEW_CALLBACK = 60006;
24     private final String mTag;
25     private boolean mSurfaceRedrawImplemented;
26     Runnable mRunnable;
27 
28     int mFinishDrawingCollected = 0;
29     int mFinishDrawingExpected = 0;
30 
31     private Runnable mFinishDrawingRunnable = new Runnable() {
32             @Override
33             public void run() {
34                 synchronized (SurfaceCallbackHelper.this) {
35                     mFinishDrawingCollected++;
36                     if (mFinishDrawingCollected < mFinishDrawingExpected) {
37                         return;
38                     }
39                     mRunnable.run();
40                     if (mSurfaceRedrawImplemented && mTag != null) {
41                         EventLog.writeEvent(LOGTAG_SURFACEVIEW_CALLBACK, mTag,
42                                 "surfaceRedrawNeeded implemented");
43                     }
44                 }
45             }
46     };
47 
SurfaceCallbackHelper(Runnable callbacksCollected)48     public SurfaceCallbackHelper(Runnable callbacksCollected) {
49         // skip logging surfaceRedrawNeeded calls
50         this(callbacksCollected, null);
51     }
52 
SurfaceCallbackHelper(Runnable callbacksCollected, String tag)53     public SurfaceCallbackHelper(Runnable callbacksCollected, String tag) {
54         mRunnable = callbacksCollected;
55         mTag = tag;
56         mSurfaceRedrawImplemented = false;
57     }
58 
dispatchSurfaceRedrawNeededAsync(SurfaceHolder holder, SurfaceHolder.Callback callbacks[])59     public void dispatchSurfaceRedrawNeededAsync(SurfaceHolder holder, SurfaceHolder.Callback callbacks[]) {
60         if (callbacks == null || callbacks.length == 0) {
61             mRunnable.run();
62             return;
63         }
64 
65         synchronized (this) {
66             mFinishDrawingExpected = callbacks.length;
67             mFinishDrawingCollected = 0;
68         }
69 
70         for (SurfaceHolder.Callback c : callbacks) {
71             if (c instanceof SurfaceHolder.Callback2) {
72                 ((SurfaceHolder.Callback2) c).surfaceRedrawNeededAsync(
73                         holder, mFinishDrawingRunnable);
74                 mSurfaceRedrawImplemented = true;
75             } else {
76                 mFinishDrawingRunnable.run();
77             }
78         }
79     }
80 }