Home | History | Annotate | Download | only in systemui
      1 /*
      2  * Copyright (C) 2015 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.systemui;
     18 
     19 import android.os.Handler;
     20 import android.os.Looper;
     21 import android.os.StrictMode;
     22 import android.view.Choreographer;
     23 
     24 import java.util.ArrayList;
     25 
     26 /**
     27  * Utility class for methods used to dejank the UI.
     28  */
     29 public class DejankUtils {
     30 
     31     private static final Choreographer sChoreographer = Choreographer.getInstance();
     32     private static final Handler sHandler = new Handler();
     33 
     34     private static final ArrayList<Runnable> sPendingRunnables = new ArrayList<>();
     35 
     36     private static final Runnable sAnimationCallbackRunnable = new Runnable() {
     37         @Override
     38         public void run() {
     39             for (int i = 0; i < sPendingRunnables.size(); i++) {
     40                 sHandler.post(sPendingRunnables.get(i));
     41             }
     42             sPendingRunnables.clear();
     43         }
     44     };
     45 
     46     /**
     47      * Executes {@code r} after performTraversals. Use this do to CPU heavy work for which the
     48      * timing is not critical for animation. The work is then scheduled at the same time
     49      * RenderThread is doing its thing, leading to better parallelization.
     50      *
     51      * <p>Needs to be called from the main thread.
     52      */
     53     public static void postAfterTraversal(Runnable r) {
     54         throwIfNotCalledOnMainThread();
     55         sPendingRunnables.add(r);
     56         postAnimationCallback();
     57     }
     58 
     59     /**
     60      * Removes a previously scheduled runnable.
     61      *
     62      * <p>Needs to be called from the main thread.
     63      */
     64     public static void removeCallbacks(Runnable r) {
     65         throwIfNotCalledOnMainThread();
     66         sPendingRunnables.remove(r);
     67         sHandler.removeCallbacks(r);
     68     }
     69 
     70     private static void postAnimationCallback() {
     71         sChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION, sAnimationCallbackRunnable,
     72                 null);
     73     }
     74 
     75     private static void throwIfNotCalledOnMainThread() {
     76         if (!Looper.getMainLooper().isCurrentThread()) {
     77             throw new IllegalStateException("should be called from the main thread.");
     78         }
     79     }
     80 }
     81