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 androidx.appcompat.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 UploadView(Context context, AttributeSet attrs)38 public UploadView(Context context, AttributeSet attrs) { 39 super(context, attrs); 40 } 41 42 @SuppressWarnings("unused") setColorValue(int colorValue)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 onAttachedToWindow()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 onDraw(Canvas canvas)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 private ObjectAnimator mColorValueAnimator; 78 private ObjectAnimator mYAnimator; 79 80 @Override onCreate(Bundle savedInstanceState)81 protected void onCreate(Bundle savedInstanceState) { 82 super.onCreate(savedInstanceState); 83 setContentView(R.layout.activity_bitmap_upload); 84 85 // animate color to force bitmap uploads 86 UploadView uploadView = findViewById(R.id.upload_view); 87 mColorValueAnimator = ObjectAnimator.ofInt(uploadView, "colorValue", 0, 255); 88 mColorValueAnimator.setRepeatMode(ValueAnimator.REVERSE); 89 mColorValueAnimator.setRepeatCount(ValueAnimator.INFINITE); 90 mColorValueAnimator.start(); 91 92 // animate scene root to guarantee there's a minimum amount of GPU rendering work 93 View uploadRoot = findViewById(R.id.upload_root); 94 mYAnimator = ObjectAnimator.ofFloat(uploadRoot, "translationY", 0, 100); 95 mYAnimator.setRepeatMode(ValueAnimator.REVERSE); 96 mYAnimator.setRepeatCount(ValueAnimator.INFINITE); 97 mYAnimator.start(); 98 } 99 100 @Override onPause()101 protected void onPause() { 102 super.onPause(); 103 if (mColorValueAnimator != null) { 104 mColorValueAnimator.cancel(); 105 } 106 107 if (mYAnimator != null) { 108 mYAnimator.cancel(); 109 } 110 } 111 } 112