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.graphics.Bitmap;
     20 import android.graphics.Bitmap.Config;
     21 import android.graphics.Color;
     22 import android.graphics.Paint;
     23 import android.graphics.RecordingCanvas;
     24 import android.graphics.RenderNode;
     25 import android.perftests.utils.BenchmarkState;
     26 import android.perftests.utils.PerfStatusReporter;
     27 
     28 import androidx.test.filters.LargeTest;
     29 
     30 import org.junit.Rule;
     31 import org.junit.Test;
     32 
     33 @LargeTest
     34 public class CanvasPerfTest {
     35     @Rule
     36     public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
     37 
     38     @Test
     39     public void testBasicViewGroupDraw() {
     40         // This test is a clone of BM_DisplayListCanvas_basicViewGroupDraw
     41 
     42         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
     43         RenderNode node = RenderNode.create("benchmark", null);
     44         RenderNode child = RenderNode.create("child", null);
     45         child.setLeftTopRightBottom(50, 50, 100, 100);
     46 
     47         RecordingCanvas canvas = node.start(100, 100);
     48         node.end(canvas);
     49         canvas = child.start(50, 50);
     50         canvas.drawColor(Color.WHITE);
     51         child.end(canvas);
     52 
     53         while (state.keepRunning()) {
     54             canvas = node.start(200, 200);
     55             int save = canvas.save();
     56             canvas.clipRect(1, 1, 199, 199);
     57             canvas.insertReorderBarrier();
     58             for (int i = 0; i < 5; i++) {
     59                 canvas.drawRenderNode(child);
     60             }
     61             canvas.insertInorderBarrier();
     62             canvas.restoreToCount(save);
     63             node.end(canvas);
     64         }
     65     }
     66 
     67     @Test
     68     public void testRecordSimpleBitmapView() {
     69         // This test is a clone of BM_DisplayListCanvas_record_simpleBitmapView
     70 
     71         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
     72         RenderNode node = RenderNode.create("benchmark", null);
     73 
     74         RecordingCanvas canvas = node.start(100, 100);
     75         node.end(canvas);
     76         Bitmap bitmap = Bitmap.createBitmap(80, 80, Config.ARGB_8888);
     77         Paint paint = new Paint();
     78         paint.setColor(Color.BLACK);
     79 
     80         while (state.keepRunning()) {
     81             canvas = node.start(100, 100);
     82             {
     83                 canvas.save();
     84                 canvas.drawRect(0, 0, 100, 100, paint);
     85                 canvas.restore();
     86             }
     87             {
     88                 canvas.save();
     89                 canvas.translate(10, 10);
     90                 canvas.drawBitmap(bitmap, 0, 0, null);
     91                 canvas.restore();
     92             }
     93             node.end(canvas);
     94         }
     95     }
     96 }
     97