1 /* 2 * Copyright (C) 2012 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.test.hwuicompare; 18 19 import java.lang.reflect.InvocationTargetException; 20 import java.lang.reflect.Method; 21 22 import com.android.test.hwuicompare.R; 23 24 import android.app.Activity; 25 import android.graphics.Bitmap; 26 import android.graphics.Canvas; 27 import android.graphics.Color; 28 import android.graphics.drawable.ColorDrawable; 29 import android.os.Handler; 30 import android.os.Trace; 31 import android.util.Log; 32 import android.view.View; 33 34 abstract public class CompareActivity extends Activity { 35 private static final String LOG_TAG = "CompareActivity"; 36 37 protected MainView mHardwareView = null; 38 39 protected Bitmap mSoftwareBitmap; 40 protected Bitmap mHardwareBitmap; 41 42 protected ErrorCalculator mErrorCalculator; 43 44 protected Handler mHandler; 45 46 Runnable mDrawCallback = null; 47 protected boolean mRedrewFlag = true; 48 49 protected void onCreateCommon(final Runnable postDrawCallback) { 50 mDrawCallback = new Runnable() { 51 @Override 52 public void run() { 53 mRedrewFlag = true; 54 mHandler.post(postDrawCallback); 55 }; 56 }; 57 getWindow().setBackgroundDrawable(new ColorDrawable(0xffefefef)); 58 ResourceModifiers.init(getResources()); 59 60 mHardwareView = (MainView) findViewById(R.id.hardware_view); 61 mHardwareView.setLayerType(View.LAYER_TYPE_HARDWARE, null); 62 mHardwareView.setBackgroundColor(Color.WHITE); 63 mHardwareView.addDrawCallback(mDrawCallback); 64 65 int width = getResources().getDimensionPixelSize(R.dimen.layer_width); 66 int height = getResources().getDimensionPixelSize(R.dimen.layer_height); 67 mSoftwareBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 68 mHardwareBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 69 70 mErrorCalculator = new ErrorCalculator(getApplicationContext(), getResources()); 71 72 mHandler = new Handler(); 73 } 74 75 protected abstract boolean forceRecreateBitmaps(); 76 77 protected void loadBitmaps() { 78 Trace.traceBegin(Trace.TRACE_TAG_ALWAYS, "loadBitmaps"); 79 if (forceRecreateBitmaps()) { 80 int width = mSoftwareBitmap.getWidth(); 81 int height = mSoftwareBitmap.getHeight(); 82 83 mSoftwareBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 84 mHardwareBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 85 } 86 87 Trace.traceBegin(Trace.TRACE_TAG_ALWAYS, "softwareDraw"); 88 mHardwareView.draw(new Canvas(mSoftwareBitmap)); 89 Trace.traceEnd(Trace.TRACE_TAG_ALWAYS); 90 91 try { 92 Method getHardwareLayer = View.class.getDeclaredMethod("getHardwareLayer"); 93 if (!getHardwareLayer.isAccessible()) 94 getHardwareLayer.setAccessible(true); 95 Object hardwareLayer = getHardwareLayer.invoke(mHardwareView); 96 if (hardwareLayer == null) { 97 Log.d(LOG_TAG, "failure to access hardware layer"); 98 return; 99 } 100 Method copyInto = hardwareLayer.getClass() 101 .getDeclaredMethod("copyInto", Bitmap.class); 102 if (!copyInto.isAccessible()) 103 copyInto.setAccessible(true); 104 105 Trace.traceBegin(Trace.TRACE_TAG_ALWAYS, "copyInto"); 106 boolean success = (Boolean) copyInto.invoke(hardwareLayer, mHardwareBitmap); 107 Trace.traceEnd(Trace.TRACE_TAG_ALWAYS); 108 if (!success) { 109 Log.d(LOG_TAG, "failure to copy hardware layer into bitmap"); 110 } 111 } catch (NoSuchMethodException e) { 112 e.printStackTrace(); 113 } catch (IllegalArgumentException e) { 114 e.printStackTrace(); 115 } catch (IllegalAccessException e) { 116 e.printStackTrace(); 117 } catch (InvocationTargetException e) { 118 e.printStackTrace(); 119 } 120 Trace.traceEnd(Trace.TRACE_TAG_ALWAYS); 121 } 122 } 123