1 /*
2  * Copyright (C) 2011 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.acceleration;
18 
19 import android.content.Context;
20 import android.graphics.Canvas;
21 import android.util.AttributeSet;
22 import android.view.View;
23 
24 import java.util.concurrent.CountDownLatch;
25 import java.util.concurrent.TimeUnit;
26 
27 public class AcceleratedView extends View {
28 
29     private final CountDownLatch mDrawLatch = new CountDownLatch(1);
30 
31     private boolean mIsHardwareAccelerated;
32 
AcceleratedView(Context context)33     public AcceleratedView(Context context) {
34         super(context);
35     }
36 
AcceleratedView(Context context, AttributeSet attrs)37     public AcceleratedView(Context context, AttributeSet attrs) {
38         super(context, attrs);
39     }
40 
AcceleratedView(Context context, AttributeSet attrs, int defStyle)41     public AcceleratedView(Context context, AttributeSet attrs, int defStyle) {
42         super(context, attrs, defStyle);
43     }
44 
45     @Override
onDraw(Canvas canvas)46     protected void onDraw(Canvas canvas) {
47         super.onDraw(canvas);
48         synchronized (this) {
49             mIsHardwareAccelerated = canvas.isHardwareAccelerated();
50         }
51         mDrawLatch.countDown();
52     }
53 
isCanvasHardwareAccelerated()54     public boolean isCanvasHardwareAccelerated() {
55         try {
56             if (mDrawLatch.await(1, TimeUnit.SECONDS)) {
57                 synchronized (this) {
58                     return mIsHardwareAccelerated;
59                 }
60             } else {
61                 throw new IllegalStateException("View was not drawn...");
62             }
63         } catch (InterruptedException e) {
64             throw new RuntimeException(e);
65         }
66     }
67 }
68