Home | History | Annotate | Download | only in hwui
      1 /*
      2  * Copyright (C) 2014 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.test.hwui;
     18 
     19 import android.app.Activity;
     20 import android.graphics.Canvas;
     21 import android.graphics.Paint;
     22 import android.graphics.PorterDuff;
     23 import android.graphics.SurfaceTexture;
     24 import android.os.Bundle;
     25 import android.view.Gravity;
     26 import android.view.Surface;
     27 import android.view.TextureView;
     28 import android.widget.FrameLayout;
     29 
     30 @SuppressWarnings({"UnusedDeclaration"})
     31 public class HardwareCanvasTextureViewActivity extends Activity
     32         implements TextureView.SurfaceTextureListener {
     33     private TextureView mTextureView;
     34     private HardwareCanvasTextureViewActivity.RenderingThread mThread;
     35 
     36     @Override
     37     protected void onCreate(Bundle savedInstanceState) {
     38         super.onCreate(savedInstanceState);
     39 
     40         FrameLayout content = new FrameLayout(this);
     41 
     42         mTextureView = new TextureView(this);
     43         mTextureView.setSurfaceTextureListener(this);
     44         mTextureView.setOpaque(false);
     45 
     46         content.addView(mTextureView, new FrameLayout.LayoutParams(500, 500, Gravity.CENTER));
     47         setContentView(content);
     48     }
     49 
     50     @Override
     51     public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
     52         mThread = new RenderingThread(mTextureView);
     53         mThread.start();
     54     }
     55 
     56     @Override
     57     public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
     58         // Ignored
     59     }
     60 
     61     @Override
     62     public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
     63         if (mThread != null) mThread.stopRendering();
     64         return true;
     65     }
     66 
     67     @Override
     68     public void onSurfaceTextureUpdated(SurfaceTexture surface) {
     69         // Ignored
     70     }
     71 
     72     private static class RenderingThread extends Thread {
     73         private final TextureView mView;
     74         private final Surface mSurface;
     75         private volatile boolean mRunning = true;
     76 
     77         public RenderingThread(TextureView view) {
     78             mView = view;
     79             mSurface = new Surface(mView.getSurfaceTexture());
     80         }
     81 
     82         @Override
     83         public void run() {
     84             float x = 0.0f;
     85             float y = 0.0f;
     86             float speedX = 5.0f;
     87             float speedY = 3.0f;
     88 
     89             Paint paint = new Paint();
     90             paint.setColor(0xff00ff00);
     91 
     92             while (mRunning && !Thread.interrupted()) {
     93                 final Canvas canvas = mSurface.lockHardwareCanvas();
     94                 try {
     95                     canvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
     96                     canvas.drawRect(x, y, x + 20.0f, y + 20.0f, paint);
     97                 } finally {
     98                     mSurface.unlockCanvasAndPost(canvas);
     99                 }
    100 
    101                 if (x + 20.0f + speedX >= mView.getWidth() || x + speedX <= 0.0f) {
    102                     speedX = -speedX;
    103                 }
    104                 if (y + 20.0f + speedY >= mView.getHeight() || y + speedY <= 0.0f) {
    105                     speedY = -speedY;
    106                 }
    107 
    108                 x += speedX;
    109                 y += speedY;
    110 
    111                 try {
    112                     Thread.sleep(15);
    113                 } catch (InterruptedException e) {
    114                     // Interrupted
    115                 }
    116             }
    117         }
    118 
    119         void stopRendering() {
    120             interrupt();
    121             mRunning = false;
    122         }
    123     }
    124 }
    125