Home | History | Annotate | Download | only in uibench
      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 package com.android.test.uibench;
     17 
     18 import android.animation.ObjectAnimator;
     19 import android.animation.ValueAnimator;
     20 import android.content.Context;
     21 import android.graphics.Bitmap;
     22 import android.graphics.Canvas;
     23 import android.graphics.Color;
     24 import android.graphics.Rect;
     25 import android.os.Bundle;
     26 import android.support.v7.app.AppCompatActivity;
     27 import android.util.AttributeSet;
     28 import android.util.DisplayMetrics;
     29 import android.view.View;
     30 
     31 public class BitmapUploadActivity extends AppCompatActivity {
     32     public static class UploadView extends View {
     33         private int mColorValue;
     34         private Bitmap mBitmap;
     35         private final DisplayMetrics mMetrics = new DisplayMetrics();
     36         private final Rect mRect = new Rect();
     37 
     38         public UploadView(Context context, AttributeSet attrs) {
     39             super(context, attrs);
     40         }
     41 
     42         @SuppressWarnings("unused")
     43         public void setColorValue(int colorValue) {
     44             if (colorValue == mColorValue) return;
     45 
     46             mColorValue = colorValue;
     47 
     48             // modify the bitmap's color to ensure it's uploaded to the GPU
     49             mBitmap.eraseColor(Color.rgb(mColorValue, 255 - mColorValue, 255));
     50 
     51             invalidate();
     52         }
     53 
     54         @Override
     55         protected void onAttachedToWindow() {
     56             super.onAttachedToWindow();
     57 
     58             getDisplay().getMetrics(mMetrics);
     59             int minDisplayDimen = Math.min(mMetrics.widthPixels, mMetrics.heightPixels);
     60             int bitmapSize = Math.min((int) (minDisplayDimen * 0.75), 720);
     61             if (mBitmap == null
     62                     || mBitmap.getWidth() != bitmapSize
     63                     || mBitmap.getHeight() != bitmapSize) {
     64                 mBitmap = Bitmap.createBitmap(bitmapSize, bitmapSize, Bitmap.Config.ARGB_8888);
     65             }
     66         }
     67 
     68         @Override
     69         protected void onDraw(Canvas canvas) {
     70             if (mBitmap != null) {
     71                 mRect.set(0, 0, getWidth(), getHeight());
     72                 canvas.drawBitmap(mBitmap, null, mRect, null);
     73             }
     74         }
     75     }
     76 
     77     @Override
     78     protected void onCreate(Bundle savedInstanceState) {
     79         super.onCreate(savedInstanceState);
     80         setContentView(R.layout.activity_bitmap_upload);
     81 
     82         // animate color to force bitmap uploads
     83         UploadView uploadView = (UploadView) findViewById(R.id.upload_view);
     84         ObjectAnimator colorValueAnimator = ObjectAnimator.ofInt(uploadView, "colorValue", 0, 255);
     85         colorValueAnimator.setRepeatMode(ValueAnimator.REVERSE);
     86         colorValueAnimator.setRepeatCount(ValueAnimator.INFINITE);
     87         colorValueAnimator.start();
     88 
     89         // animate scene root to guarantee there's a minimum amount of GPU rendering work
     90         View uploadRoot = findViewById(R.id.upload_root);
     91         ObjectAnimator yAnimator = ObjectAnimator.ofFloat(uploadRoot, "translationY", 0, 100);
     92         yAnimator.setRepeatMode(ValueAnimator.REVERSE);
     93         yAnimator.setRepeatCount(ValueAnimator.INFINITE);
     94         yAnimator.start();
     95     }
     96 }
     97