Home | History | Annotate | Download | only in perftests
      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 android.graphics.perftests;
     18 
     19 import android.app.Activity;
     20 import android.graphics.Bitmap;
     21 import android.graphics.Canvas;
     22 import android.graphics.Color;
     23 import android.graphics.drawable.VectorDrawable;
     24 import android.perftests.utils.BenchmarkState;
     25 import android.perftests.utils.BitmapUtils;
     26 import android.perftests.utils.PerfStatusReporter;
     27 import android.perftests.utils.StubActivity;
     28 import android.support.test.InstrumentationRegistry;
     29 import android.support.test.rule.ActivityTestRule;
     30 import android.support.test.runner.AndroidJUnit4;
     31 import android.test.suitebuilder.annotation.LargeTest;
     32 
     33 import com.android.perftests.core.R;
     34 
     35 import org.junit.Rule;
     36 import org.junit.Test;
     37 import org.junit.runner.RunWith;
     38 
     39 import java.io.IOException;
     40 
     41 import static junit.framework.Assert.assertTrue;
     42 
     43 @RunWith(AndroidJUnit4.class)
     44 @LargeTest
     45 public class VectorDrawablePerfTest {
     46 
     47     private static final boolean DUMP_BITMAP = false;
     48 
     49     private int[] mTestWidths = {1024, 512};
     50     private int[] mTestHeights = {512, 1024};
     51 
     52     @Rule
     53     public ActivityTestRule<StubActivity> mActivityRule =
     54             new ActivityTestRule(StubActivity.class);
     55 
     56     @Rule
     57     public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
     58 
     59     @Test
     60     public void testBitmapDrawPerf() {
     61         int resId = R.drawable.vector_drawable01;
     62         Activity activity = mActivityRule.getActivity();
     63         VectorDrawable vd = (VectorDrawable) activity.getDrawable(resId);
     64 
     65         int w = 1024, h = 1024;
     66         Bitmap.Config conf = Bitmap.Config.ARGB_8888;
     67         Bitmap bmp = Bitmap.createBitmap(w, h, conf);
     68         Canvas canvas = new Canvas(bmp);
     69 
     70         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
     71         int i = 0;
     72         while (state.keepRunning()) {
     73             // Use different width / height each to force the vectorDrawable abandon the cache.
     74             vd.setBounds(0, 0, mTestWidths[i % 2], mTestHeights[i % 2]);
     75             i++;
     76             vd.draw(canvas);
     77         }
     78 
     79         // Double check the bitmap pixels to make sure we draw correct content.
     80         int backgroundColor = bmp.getPixel(w / 4, h / 2);
     81         int objColor = bmp.getPixel(w / 8, h / 2 + 1);
     82         int emptyColor = bmp.getPixel(w * 3 / 4, h * 3 / 4);
     83         assertTrue("The background should be white", backgroundColor == Color.WHITE);
     84         assertTrue("The object should be black", objColor == Color.BLACK);
     85         assertTrue("The right bottom part should be empty", emptyColor == Color.TRANSPARENT);
     86 
     87         if (DUMP_BITMAP) {
     88             BitmapUtils.saveBitmapIntoPNG(activity, bmp, resId);
     89         }
     90     }
     91 }
     92