Skip to content

Commit a2cf2dc

Browse files
committed
Added "color bars" activity
Just shows some full-intensity RGB color bars. Looks best in landscape. Also, renamed multi-surface test activity to have "activity" in the class name.
1 parent 713ab86 commit a2cf2dc

10 files changed

+276
-79
lines changed

AndroidManifest.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
1919
package="com.android.grafika"
20-
android:versionCode="27"
20+
android:versionCode="28"
2121
android:versionName="1.0" >
2222

2323
<uses-sdk
@@ -110,7 +110,7 @@
110110
android:label="@string/title_activity_play_movie_surface" >
111111
</activity>
112112
<activity
113-
android:name="com.android.grafika.MultiSurfaceTest"
113+
android:name="com.android.grafika.MultiSurfaceActivity"
114114
android:label="@string/title_activity_multi_surface_test" >
115115
</activity>
116116
<activity
@@ -121,6 +121,11 @@
121121
android:name="com.android.grafika.TextureUploadActivity"
122122
android:label="@string/title_activity_texture_upload" >
123123
</activity>
124+
<activity
125+
android:name="com.android.grafika.ColorBarActivity"
126+
android:label="@string/title_color_bar"
127+
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
128+
</activity>
124129
</application>
125130

126131
</manifest>

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ Current features
147147
- Renders as quickly as possible. On most devices it will exceed 60fps and flicker wildly,
148148
but in 4.4 ("KitKat") a bug prevents the system from dropping frames.
149149

150+
[Color bars](src/com/android/grafika/ColorBarActivity.java). Displays RGB color bars.
151+
150152
[OpenGL ES Info](src/com/android/grafika/GlesInfoActivity.java). Dumps version info and extension lists.
151153
- The "Save" button writes a copy of the output to the app's file area.
152154

res/layout/activity_color_bar.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2014 Google Inc. All rights reserved.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
19+
xmlns:tools="http://schemas.android.com/tools"
20+
android:layout_width="match_parent"
21+
android:layout_height="match_parent"
22+
tools:context=".ColorBarActivity" >
23+
24+
<SurfaceView
25+
android:id="@+id/colorBarSurfaceView"
26+
android:layout_width="fill_parent"
27+
android:layout_height="fill_parent" />
28+
29+
</RelativeLayout>

res/layout/activity_multi_surface_test.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
android:paddingLeft="@dimen/activity_horizontal_margin"
2424
android:paddingRight="@dimen/activity_horizontal_margin"
2525
android:paddingTop="@dimen/activity_vertical_margin"
26-
tools:context=".MultiSurfaceTest" >
26+
tools:context=".MultiSurfaceActivity" >
2727

2828
<SurfaceView
2929
android:id="@+id/multiSurfaceView1"

res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,6 @@
118118
<string name="title_activity_multi_surface_test">Multi-Surface Test</string>
119119
<string name="title_activity_codec_open">Codec Open Activity</string>
120120
<string name="title_activity_texture_upload">Texture upload speed test</string>
121+
<string name="title_color_bar">RGB color bars</string>
121122

122123
</resources>
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
* Copyright 2014 Google Inc. All rights reserved.
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.grafika;
18+
19+
import android.os.Bundle;
20+
import android.util.Log;
21+
import android.view.Surface;
22+
import android.view.SurfaceHolder;
23+
import android.view.SurfaceView;
24+
import android.app.Activity;
25+
import android.graphics.Canvas;
26+
import android.graphics.Paint;
27+
import android.graphics.PixelFormat;
28+
import android.graphics.Typeface;
29+
30+
/**
31+
* Show color bars.
32+
*/
33+
public class ColorBarActivity extends Activity implements SurfaceHolder.Callback {
34+
private static final String TAG = MainActivity.TAG;
35+
36+
private SurfaceView mSurfaceView;
37+
38+
private static final String[] COLOR_NAMES = {
39+
"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"
40+
};
41+
42+
@Override
43+
protected void onCreate(Bundle savedInstanceState) {
44+
super.onCreate(savedInstanceState);
45+
setContentView(R.layout.activity_color_bar);
46+
47+
mSurfaceView = (SurfaceView) findViewById(R.id.colorBarSurfaceView);
48+
mSurfaceView.getHolder().addCallback(this);
49+
mSurfaceView.getHolder().setFormat(PixelFormat.RGBA_8888);
50+
}
51+
52+
@Override
53+
public void surfaceCreated(SurfaceHolder holder) {
54+
// ignore
55+
Log.v(TAG, "surfaceCreated holder=" + holder);
56+
}
57+
58+
/**
59+
* SurfaceHolder.Callback method
60+
* <p>
61+
* Draws when the surface changes. Since nothing else is touching the surface, and
62+
* we're not animating, we just draw here and ignore it.
63+
*/
64+
@Override
65+
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
66+
Log.v(TAG, "surfaceChanged fmt=" + format + " size=" + width + "x" + height +
67+
" holder=" + holder);
68+
Surface surface = holder.getSurface();
69+
drawColorBars(surface);
70+
}
71+
72+
@Override
73+
public void surfaceDestroyed(SurfaceHolder holder) {
74+
// ignore
75+
Log.v(TAG, "Surface destroyed holder=" + holder);
76+
}
77+
78+
/**
79+
* Draw color bars with text labels.
80+
*/
81+
private void drawColorBars(Surface surface) {
82+
Canvas canvas = surface.lockCanvas(null);
83+
try {
84+
// TODO: if the device is in portrait, draw the color bars horizontally. Right
85+
// now this only looks good in landscape.
86+
int width = canvas.getWidth();
87+
int height = canvas.getHeight();
88+
int least = Math.min(width, height);
89+
90+
Log.d(TAG, "Drawing color bars at " + width + "x" + height);
91+
92+
Paint textPaint = new Paint();
93+
Typeface typeface = Typeface.defaultFromStyle(Typeface.NORMAL);
94+
textPaint.setTypeface(typeface);
95+
textPaint.setTextSize(least / 20);
96+
textPaint.setAntiAlias(true);
97+
98+
Paint rectPaint = new Paint();
99+
for (int i = 0; i < 8; i++) {
100+
int color = 0xff000000;
101+
if ((i & 0x01) != 0) {
102+
color |= 0x00ff0000;
103+
}
104+
if ((i & 0x02) != 0) {
105+
color |= 0x0000ff00;
106+
}
107+
if ((i & 0x04) != 0) {
108+
color |= 0x000000ff;
109+
}
110+
rectPaint.setColor(color);
111+
112+
float sliceWidth = width / 8;
113+
canvas.drawRect(sliceWidth * i, 0, sliceWidth * (i+1), height, rectPaint);
114+
}
115+
rectPaint.setColor(0x80808080); // ARGB 50/50 grey (non-premul)
116+
float sliceHeight = height / 8;
117+
int posn = 6;
118+
canvas.drawRect(0, sliceHeight * posn, width, sliceHeight * (posn+1), rectPaint);
119+
120+
// Draw the labels last so they're on top of everything.
121+
for (int i = 0; i < 8; i++) {
122+
drawOutlineText(canvas, textPaint, COLOR_NAMES[i],
123+
(width / 8) * i + 4, (height / 8) * ((i & 1) + 1));
124+
}
125+
} finally {
126+
surface.unlockCanvasAndPost(canvas);
127+
}
128+
}
129+
130+
/**
131+
* Draw white text surrounded by a 1-pixel black outline.
132+
*/
133+
private static void drawOutlineText(Canvas canvas, Paint textPaint, String str,
134+
float x, float y) {
135+
// Is there a better way to do this?
136+
textPaint.setColor(0xff000000);
137+
canvas.drawText(str, x-1, y, textPaint);
138+
canvas.drawText(str, x+1, y, textPaint);
139+
canvas.drawText(str, x, y-1, textPaint);
140+
canvas.drawText(str, x, y+1, textPaint);
141+
canvas.drawText(str, x-0.7f, y-0.7f, textPaint);
142+
canvas.drawText(str, x+0.7f, y-0.7f, textPaint);
143+
canvas.drawText(str, x-0.7f, y+0.7f, textPaint);
144+
canvas.drawText(str, x+0.7f, y+0.7f, textPaint);
145+
textPaint.setColor(0xffffffff);
146+
canvas.drawText(str, x, y, textPaint);
147+
}
148+
}

src/com/android/grafika/MainActivity.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ public class MainActivity extends ListActivity {
6565
"Trivially feeds the camera preview to a view",
6666
"LiveCameraActivity" },
6767
{ "Multi-surface test",
68-
"Two overlapping SurfaceViews, one secure",
69-
"MultiSurfaceTest" },
68+
"Three overlapping SurfaceViews, one secure",
69+
"MultiSurfaceActivity" },
7070
{ "Play video (SurfaceView)",
7171
"Plays .mp4 videos created by Grafika",
7272
"PlayMovieSurfaceActivity" },
@@ -88,6 +88,9 @@ public class MainActivity extends ListActivity {
8888
{ "{bench} glTexImage2d speed test",
8989
"Tests glTexImage2d() performance on 512x512 image",
9090
"TextureUploadActivity" },
91+
{ "{util} Color bars",
92+
"Shows RGB color bars",
93+
"ColorBarActivity" },
9194
{ "{util} OpenGL ES info",
9295
"Dumps info about graphics drivers",
9396
"GlesInfoActivity" },

src/com/android/grafika/MultiSurfaceTest.java renamed to src/com/android/grafika/MultiSurfaceActivity.java

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
* <code>systrace.py --app=com.android.grafika gfx view sched dalvik</code>
4747
* (most interesting while bouncing).
4848
*/
49-
public class MultiSurfaceTest extends Activity implements SurfaceHolder.Callback {
49+
public class MultiSurfaceActivity extends Activity implements SurfaceHolder.Callback {
5050
private static final String TAG = MainActivity.TAG;
5151

5252
// Number of steps in each direction. There's actually N+1 positions because we
@@ -292,10 +292,13 @@ private void drawCircleSurface(Surface surface, int x, int y, int radius) {
292292
paint.setShadowLayer(radius / 4 + 1, 0, 0, Color.RED);
293293

294294
Canvas canvas = surface.lockCanvas(null);
295-
Log.v(TAG, "drawCircleSurface: isHwAcc=" + canvas.isHardwareAccelerated());
296-
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
297-
canvas.drawCircle(x, y, radius, paint);
298-
surface.unlockCanvasAndPost(canvas);
295+
try {
296+
Log.v(TAG, "drawCircleSurface: isHwAcc=" + canvas.isHardwareAccelerated());
297+
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
298+
canvas.drawCircle(x, y, radius, paint);
299+
} finally {
300+
surface.unlockCanvasAndPost(canvas);
301+
}
299302
}
300303

301304
/**
@@ -309,31 +312,34 @@ private void drawBouncingCircle(Surface surface, int i) {
309312
paint.setStyle(Paint.Style.FILL);
310313

311314
Canvas canvas = surface.lockCanvas(null);
312-
Trace.beginSection("drawBouncingCircle");
313-
Trace.beginSection("drawColor");
314-
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
315-
Trace.endSection(); // drawColor
316-
317-
int width = canvas.getWidth();
318-
int height = canvas.getHeight();
319-
int radius, x, y;
320-
if (width < height) {
321-
// portrait
322-
radius = width / 4;
323-
x = width / 4 + ((width / 2 * i) / BOUNCE_STEPS);
324-
y = height * 3 / 4;
325-
} else {
326-
// landscape
327-
radius = height / 4;
328-
x = width * 3 / 4;
329-
y = height / 4 + ((height / 2 * i) / BOUNCE_STEPS);
330-
}
315+
try {
316+
Trace.beginSection("drawBouncingCircle");
317+
Trace.beginSection("drawColor");
318+
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
319+
Trace.endSection(); // drawColor
320+
321+
int width = canvas.getWidth();
322+
int height = canvas.getHeight();
323+
int radius, x, y;
324+
if (width < height) {
325+
// portrait
326+
radius = width / 4;
327+
x = width / 4 + ((width / 2 * i) / BOUNCE_STEPS);
328+
y = height * 3 / 4;
329+
} else {
330+
// landscape
331+
radius = height / 4;
332+
x = width * 3 / 4;
333+
y = height / 4 + ((height / 2 * i) / BOUNCE_STEPS);
334+
}
331335

332-
paint.setShadowLayer(radius / 4 + 1, 0, 0, Color.RED);
336+
paint.setShadowLayer(radius / 4 + 1, 0, 0, Color.RED);
333337

334-
canvas.drawCircle(x, y, radius, paint);
335-
Trace.endSection(); // drawBouncingCircle
336-
surface.unlockCanvasAndPost(canvas);
338+
canvas.drawCircle(x, y, radius, paint);
339+
Trace.endSection(); // drawBouncingCircle
340+
} finally {
341+
surface.unlockCanvasAndPost(canvas);
342+
}
337343
}
338344

339345
}

src/com/android/grafika/SoftInputSurfaceActivity.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -257,32 +257,34 @@ private void drainEncoder(boolean endOfStream) {
257257
*/
258258
private void generateFrame(int frameNum) {
259259
Canvas canvas = mInputSurface.lockCanvas(null);
260-
int width = canvas.getWidth();
261-
int height = canvas.getHeight();
262-
263-
Paint paint = new Paint();
264-
for (int i = 0; i < 8; i++) {
265-
int color = 0xff000000;
266-
if ((i & 0x01) != 0) {
267-
color |= 0x00ff0000;
268-
}
269-
if ((i & 0x02) != 0) {
270-
color |= 0x0000ff00;
271-
}
272-
if ((i & 0x04) != 0) {
273-
color |= 0x000000ff;
260+
try {
261+
int width = canvas.getWidth();
262+
int height = canvas.getHeight();
263+
264+
Paint paint = new Paint();
265+
for (int i = 0; i < 8; i++) {
266+
int color = 0xff000000;
267+
if ((i & 0x01) != 0) {
268+
color |= 0x00ff0000;
269+
}
270+
if ((i & 0x02) != 0) {
271+
color |= 0x0000ff00;
272+
}
273+
if ((i & 0x04) != 0) {
274+
color |= 0x000000ff;
275+
}
276+
paint.setColor(color);
277+
278+
float sliceWidth = width / 8;
279+
canvas.drawRect(sliceWidth * i, 0, sliceWidth * (i+1), height, paint);
274280
}
275-
paint.setColor(color);
276281

277-
float sliceWidth = width / 8;
278-
canvas.drawRect(sliceWidth * i, 0, sliceWidth * (i+1), height, paint);
282+
paint.setColor(0x80808080);
283+
float sliceHeight = height / 8;
284+
int frameMod = frameNum % 8;
285+
canvas.drawRect(0, sliceHeight * frameMod, width, sliceHeight * (frameMod+1), paint);
286+
} finally {
287+
mInputSurface.unlockCanvasAndPost(canvas);
279288
}
280-
281-
paint.setColor(0x80808080);
282-
float sliceHeight = height / 8;
283-
int frameMod = frameNum % 8;
284-
canvas.drawRect(0, sliceHeight * frameMod, width, sliceHeight * (frameMod+1), paint);
285-
286-
mInputSurface.unlockCanvasAndPost(canvas);
287289
}
288290
}

0 commit comments

Comments
 (0)