• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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 android.view.surfacecontrol.cts;
17 
18 import android.app.Activity;
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.Canvas;
22 import android.graphics.Color;
23 import android.graphics.Paint;
24 import android.os.Bundle;
25 import android.view.Surface;
26 import android.view.SurfaceHolder;
27 import android.view.SurfaceView;
28 
29 import java.util.concurrent.CountDownLatch;
30 import java.util.concurrent.TimeUnit;
31 
32 public class SurfaceViewCtsActivity extends Activity {
33     private MockSurfaceView mSurfaceView;
34 
35     @Override
onCreate(Bundle savedInstanceState)36     protected void onCreate(Bundle savedInstanceState) {
37         super.onCreate(savedInstanceState);
38 
39         // New a MockSurfaceView
40         mSurfaceView = new MockSurfaceView(this);
41         setContentView(mSurfaceView);
42     }
43 
getSurfaceView()44     public MockSurfaceView getSurfaceView() {
45         return mSurfaceView;
46     }
47 
48     public class MockSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
49         private static final int FIX_WIDTH = 240;
50         private static final int FIX_HEIGHT = 240;
51         private static final int BITMAP_WIDTH = 100;
52         private static final int BITMAP_HEIGHT = 100;
53         private static final int RECT_LEFT = 20;
54         private static final int RECT_TOP = 100;
55         private static final int RECT_RIGHT = 200;
56         private static final int RECT_BOTTOM = 200;
57 
58         private Canvas mCanvas;
59 
60         private SurfaceHolder mHolder;
61 
62         private boolean mIsDraw;
63         private boolean mIsAttachedToWindow;
64         private boolean mIsDetachedFromWindow;
65         private boolean mIsOnMeasure;
66         private boolean mIsOnScrollChanged;
67         private boolean mIsOnSizeChanged;
68         private boolean mIsOnWindowVisibilityChanged;
69         private boolean mIsDispatchDraw;
70         private boolean mIsSurfaceChanged;
71         CountDownLatch mSurfaceCreatedLatch = new CountDownLatch(1);
72 
73         private int mWidthInOnMeasure;
74         private int mHeightInOnMeasure;
75         private int mOldLOnScrollChanged;
76         private int mOldTOnScrollChanged;
77         private int mOldWOnSizeChanged;
78         private int mOldHOnSizeChanged;
79         private int mVisibilityOnWindowVisibilityChanged;
80 
81         Surface mSurface;
82 
MockSurfaceView(Context context)83         public MockSurfaceView(Context context) {
84             super(context);
85             mHolder = getHolder();
86             mHolder.addCallback(this);
87             mHolder.setFixedSize(FIX_WIDTH, FIX_HEIGHT);
88         }
89 
90         @Override
onWindowVisibilityChanged(int visibility)91         public void onWindowVisibilityChanged(int visibility) {
92             super.onWindowVisibilityChanged(visibility);
93             mVisibilityOnWindowVisibilityChanged = visibility;
94             mIsOnWindowVisibilityChanged = true;
95         }
96 
getVInOnWindowVisibilityChanged()97         public int getVInOnWindowVisibilityChanged() {
98             return mVisibilityOnWindowVisibilityChanged;
99         }
100 
101         @Override
draw(Canvas canvas)102         public void draw(Canvas canvas) {
103             super.draw(canvas);
104             mIsDraw = true;
105         }
106 
107         @Override
onAttachedToWindow()108         public void onAttachedToWindow() {
109             super.onAttachedToWindow();
110             mIsAttachedToWindow = true;
111         }
112 
113         @Override
onDetachedFromWindow()114         public void onDetachedFromWindow() {
115             super.onDetachedFromWindow();
116             mIsDetachedFromWindow = true;
117         }
118 
119         @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)120         public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
121             super.onMeasure(widthMeasureSpec, heightMeasureSpec);
122             mWidthInOnMeasure = getDefaultSize(FIX_WIDTH, widthMeasureSpec);
123             mHeightInOnMeasure = getDefaultSize(FIX_HEIGHT, heightMeasureSpec);
124             mIsOnMeasure = true;
125         }
126 
getWidthInOnMeasure()127         public int getWidthInOnMeasure() {
128             return mWidthInOnMeasure;
129         }
130 
getHeightInOnMeasure()131         public int getHeightInOnMeasure() {
132             return mHeightInOnMeasure;
133         }
134 
135         @Override
onScrollChanged(int l, int t, int oldl, int oldt)136         public void onScrollChanged(int l, int t, int oldl, int oldt) {
137             super.onScrollChanged(l, t, oldl, oldt);
138 
139             mOldLOnScrollChanged = oldl;
140             mOldTOnScrollChanged = oldt;
141             mIsOnScrollChanged = true;
142         }
143 
getOldHorizontal()144         public int getOldHorizontal() {
145             return mOldLOnScrollChanged;
146         }
147 
getOldVertical()148         public int getOldVertical() {
149             return mOldTOnScrollChanged;
150         }
151 
152         @Override
onSizeChanged(int w, int h, int oldw, int oldh)153         public void onSizeChanged(int w, int h, int oldw, int oldh) {
154             super.onSizeChanged(w, h, oldw, oldh);
155 
156             mOldWOnSizeChanged = oldw;
157             mOldHOnSizeChanged = oldh;
158             mIsOnSizeChanged = true;
159         }
160 
getOldWidth()161         public int getOldWidth() {
162             return mOldWOnSizeChanged;
163         }
164 
getOldHeight()165         public int getOldHeight() {
166             return mOldHOnSizeChanged;
167         }
168 
169         @Override
dispatchDraw(Canvas canvas)170         protected void dispatchDraw(Canvas canvas) {
171             super.dispatchDraw(canvas);
172             mIsDispatchDraw = true;
173         }
174 
setFormat(int format)175         public void setFormat(int format) {
176             getHolder().setFormat(format);
177         }
178 
surfaceCreated(SurfaceHolder holder)179         public void surfaceCreated(SurfaceHolder holder) {
180             mSurfaceCreatedLatch.countDown();
181 
182             mSurface = holder.getSurface();
183 
184             // Use mock canvas listening to the drawColor() calling.
185             mCanvas = new Canvas(Bitmap.createBitmap( BITMAP_WIDTH,
186                             BITMAP_HEIGHT,
187                             Bitmap.Config.ARGB_8888));
188             draw(mCanvas);
189 
190             // Lock the surface, this returns a Canvas that can be used to render into.
191             Canvas canvas = mHolder.lockCanvas();
192             Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
193             paint.setColor(Color.BLUE);
194             canvas.drawRect(RECT_LEFT, RECT_TOP, RECT_RIGHT, RECT_BOTTOM, paint);
195 
196             // And finally unlock and post the surface.
197             mHolder.unlockCanvasAndPost(canvas);
198         }
199 
isSurfaceCreatedCalled()200         boolean isSurfaceCreatedCalled() {
201             return mSurfaceCreatedLatch.getCount() == 0;
202         }
203 
awaitSurfaceCreated(long timeout, TimeUnit unit)204         boolean awaitSurfaceCreated(long timeout, TimeUnit unit) {
205             try {
206                 return mSurfaceCreatedLatch.await(timeout, unit);
207             } catch (InterruptedException e) {
208                 return false;
209             }
210         }
211 
surfaceDestroyed(SurfaceHolder holder)212         public void surfaceDestroyed(SurfaceHolder holder) {
213         }
214 
surfaceChanged(SurfaceHolder holder, int format, int w, int h)215         public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
216             mIsSurfaceChanged = true;
217         }
218 
isDraw()219         public boolean isDraw() {
220             return mIsDraw;
221         }
222 
isOnAttachedToWindow()223         public boolean isOnAttachedToWindow() {
224             return mIsAttachedToWindow;
225         }
226 
isDetachedFromWindow()227         public boolean isDetachedFromWindow() {
228             return mIsDetachedFromWindow;
229         }
230 
isOnMeasureCalled()231         public boolean isOnMeasureCalled() {
232             return mIsOnMeasure;
233         }
234 
isOnScrollChanged()235         public boolean isOnScrollChanged() {
236             return mIsOnScrollChanged;
237         }
238 
isOnSizeChangedCalled()239         public boolean isOnSizeChangedCalled() {
240             return mIsOnSizeChanged;
241         }
242 
resetOnSizeChangedFlag(boolean b)243         public void resetOnSizeChangedFlag(boolean b) {
244             mIsOnSizeChanged = b;
245         }
246 
isOnWindowVisibilityChanged()247         public boolean isOnWindowVisibilityChanged() {
248             return mIsOnWindowVisibilityChanged;
249         }
250 
isDispatchDraw()251         public boolean isDispatchDraw() {
252             return mIsDispatchDraw;
253         }
254 
isSurfaceChanged()255         public boolean isSurfaceChanged() {
256             return mIsSurfaceChanged;
257         }
258     }
259 }
260