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.os.Bundle;
24 import android.view.Gravity;
25 import android.view.Surface;
26 import android.view.SurfaceHolder;
27 import android.view.SurfaceHolder.Callback;
28 import android.view.SurfaceView;
29 import android.widget.FrameLayout;
30 
31 @SuppressWarnings({"UnusedDeclaration"})
32 public class HardwareCanvasSurfaceViewActivity extends Activity implements Callback {
33     private SurfaceView mSurfaceView;
34     private HardwareCanvasSurfaceViewActivity.RenderingThread mThread;
35 
36     @Override
onCreate(Bundle savedInstanceState)37     protected void onCreate(Bundle savedInstanceState) {
38         super.onCreate(savedInstanceState);
39 
40         FrameLayout content = new FrameLayout(this);
41 
42         mSurfaceView = new SurfaceView(this);
43         mSurfaceView.getHolder().addCallback(this);
44 
45         content.addView(mSurfaceView, new FrameLayout.LayoutParams(
46                 FrameLayout.LayoutParams.MATCH_PARENT,
47                 FrameLayout.LayoutParams.MATCH_PARENT,
48                 Gravity.CENTER));
49         setContentView(content);
50     }
51 
52     @Override
surfaceCreated(SurfaceHolder holder)53     public void surfaceCreated(SurfaceHolder holder) {
54         mThread = new RenderingThread(holder.getSurface());
55         mThread.start();
56     }
57 
58     @Override
surfaceChanged(SurfaceHolder holder, int format, int width, int height)59     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
60         mThread.setSize(width, height);
61     }
62 
63     @Override
surfaceDestroyed(SurfaceHolder holder)64     public void surfaceDestroyed(SurfaceHolder holder) {
65         if (mThread != null) mThread.stopRendering();
66     }
67 
68     private static class RenderingThread extends Thread {
69         private final Surface mSurface;
70         private volatile boolean mRunning = true;
71         private int mWidth, mHeight;
72 
RenderingThread(Surface surface)73         public RenderingThread(Surface surface) {
74             mSurface = surface;
75         }
76 
setSize(int width, int height)77         void setSize(int width, int height) {
78             mWidth = width;
79             mHeight = height;
80         }
81 
82         @Override
run()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 >= mWidth || x + speedX <= 0.0f) {
102                     speedX = -speedX;
103                 }
104                 if (y + 20.0f + speedY >= mHeight || 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 
stopRendering()119         void stopRendering() {
120             interrupt();
121             mRunning = false;
122         }
123     }
124 }
125