1 /* 2 * Copyright (C) 2018 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.amm.test; 18 19 import android.content.Context; 20 import android.graphics.Canvas; 21 import android.view.TextureView; 22 import android.view.ViewGroup; 23 24 class TextureViewUse { 25 26 private TextureView mTextureView; 27 28 /** 29 * Constructs a TextureView object with given dimensions. 30 * The texture view is added to the given ViewGroup object, which should be 31 * included in the main display. 32 */ 33 public TextureViewUse(Context context, ViewGroup vg, int width, int height) { 34 mTextureView = new TextureView(context); 35 vg.addView(mTextureView, width, height); 36 mTextureView.post(new CycleRunnable()); 37 } 38 39 // To force as many graphics buffers as will ever be used to actually be 40 // used, we cycle the color of the texture view a handful of times right 41 // when things start up. 42 private class CycleRunnable implements Runnable { 43 private int mCycles = 0; 44 private int mRed = 255; 45 private int mGreen = 255; 46 private int mBlue = 0; 47 48 public void run() { 49 if (mCycles < 10) { 50 mCycles++; 51 updateTextureView(); 52 mTextureView.post(this); 53 } 54 } 55 56 private void updateTextureView() { 57 Canvas canvas = mTextureView.lockCanvas(); 58 if (canvas != null) { 59 canvas.drawRGB(mRed, mGreen, mBlue); 60 int tmp = mRed; 61 mTextureView.unlockCanvasAndPost(canvas); 62 mRed = mGreen; 63 mGreen = mBlue; 64 mBlue = tmp; 65 } 66 } 67 } 68 } 69 70