|
| 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.opengl.GLES20; |
| 20 | +import android.os.Bundle; |
| 21 | +import android.util.Log; |
| 22 | +import android.view.Surface; |
| 23 | +import android.view.SurfaceHolder; |
| 24 | +import android.view.SurfaceView; |
| 25 | +import android.app.Activity; |
| 26 | +import android.graphics.Canvas; |
| 27 | +import android.graphics.Color; |
| 28 | +import android.graphics.Paint; |
| 29 | +import android.graphics.PixelFormat; |
| 30 | + |
| 31 | +import com.android.grafika.gles.EglCore; |
| 32 | +import com.android.grafika.gles.WindowSurface; |
| 33 | + |
| 34 | +/** |
| 35 | + * Exercises some less-commonly-used aspects of SurfaceView. In particular: |
| 36 | + * <ul> |
| 37 | + * <li> We have three overlapping SurfaceViews. |
| 38 | + * <li> One is at the default depth, one is at "media overlay" depth, and one is on top of the UI. |
| 39 | + * <li> One is marked "secure". |
| 40 | + * </ul> |
| 41 | + */ |
| 42 | +public class MultiSurfaceTest extends Activity implements SurfaceHolder.Callback { |
| 43 | + private static final String TAG = MainActivity.TAG; |
| 44 | + |
| 45 | + private SurfaceView mSurfaceView1; |
| 46 | + private SurfaceView mSurfaceView2; |
| 47 | + private SurfaceView mSurfaceView3; |
| 48 | + |
| 49 | + @Override |
| 50 | + protected void onCreate(Bundle savedInstanceState) { |
| 51 | + super.onCreate(savedInstanceState); |
| 52 | + setContentView(R.layout.activity_multi_surface_test); |
| 53 | + |
| 54 | + // #1 is at the bottom; mark it as secure just for fun |
| 55 | + mSurfaceView1 = (SurfaceView) findViewById(R.id.multiSurfaceView1); |
| 56 | + mSurfaceView1.getHolder().addCallback(this); |
| 57 | + mSurfaceView1.setSecure(true); |
| 58 | + |
| 59 | + // #2 is above it, in the "media overlay"; must be translucent or we will |
| 60 | + // totally obscure #1 and it will be ignored by the compositor |
| 61 | + mSurfaceView2 = (SurfaceView) findViewById(R.id.multiSurfaceView2); |
| 62 | + mSurfaceView2.getHolder().addCallback(this); |
| 63 | + mSurfaceView2.getHolder().setFormat(PixelFormat.TRANSLUCENT); |
| 64 | + mSurfaceView2.setZOrderMediaOverlay(true); |
| 65 | + |
| 66 | + // #3 is above everything, including the UI |
| 67 | + mSurfaceView3 = (SurfaceView) findViewById(R.id.multiSurfaceView3); |
| 68 | + mSurfaceView3.getHolder().addCallback(this); |
| 69 | + mSurfaceView3.getHolder().setFormat(PixelFormat.TRANSLUCENT); |
| 70 | + mSurfaceView3.setZOrderOnTop(true); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Returns an ordinal value for the SurfaceHolder, or -1 for an invalid surface. |
| 75 | + */ |
| 76 | + private int getSurfaceId(SurfaceHolder holder) { |
| 77 | + if (holder.equals(mSurfaceView1.getHolder())) { |
| 78 | + return 1; |
| 79 | + } else if (holder.equals(mSurfaceView2.getHolder())) { |
| 80 | + return 2; |
| 81 | + } else if (holder.equals(mSurfaceView3.getHolder())) { |
| 82 | + return 3; |
| 83 | + } else { |
| 84 | + return -1; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void surfaceCreated(SurfaceHolder holder) { |
| 90 | + int id = getSurfaceId(holder); |
| 91 | + if (id < 0) { |
| 92 | + Log.w(TAG, "surfaceCreated UNKNOWN holder=" + holder); |
| 93 | + } else { |
| 94 | + Log.d(TAG, "surfaceCreated #" + id + " holder=" + holder); |
| 95 | + |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * SurfaceHolder.Callback method |
| 101 | + * <p> |
| 102 | + * Draws when the surface changes. Since nothing else is touching the surface, and |
| 103 | + * we're not animating, we just draw here and ignore it. |
| 104 | + */ |
| 105 | + @Override |
| 106 | + public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { |
| 107 | + Log.d(TAG, "surfaceChanged fmt=" + format + " size=" + width + "x" + height + |
| 108 | + " holder=" + holder); |
| 109 | + |
| 110 | + int id = getSurfaceId(holder); |
| 111 | + boolean portrait = height > width; |
| 112 | + Surface surface = holder.getSurface(); |
| 113 | + |
| 114 | + switch (id) { |
| 115 | + case 1: |
| 116 | + // default layer: circle on left / top |
| 117 | + if (portrait) { |
| 118 | + drawCircleSurface(surface, width / 2, height / 4, width / 4); |
| 119 | + } else { |
| 120 | + drawCircleSurface(surface, width / 4, height / 2, height / 4); |
| 121 | + } |
| 122 | + break; |
| 123 | + case 2: |
| 124 | + // media overlay layer: circle on right / bottom |
| 125 | + if (portrait) { |
| 126 | + drawCircleSurface(surface, width / 2, height * 3 / 4, width / 4); |
| 127 | + } else { |
| 128 | + drawCircleSurface(surface, width * 3 / 4, height / 2, height / 4); |
| 129 | + } |
| 130 | + break; |
| 131 | + case 3: |
| 132 | + // top layer: faint blue line |
| 133 | + if (portrait) { |
| 134 | + int halfLine = width / 16 + 1; |
| 135 | + drawRectSurface(surface, width/2 - halfLine, 0, width/2 + halfLine, height); |
| 136 | + } else { |
| 137 | + int halfLine = height / 16 + 1; |
| 138 | + drawRectSurface(surface, 0, height/2 - halfLine, width, height/2 + halfLine); |
| 139 | + } |
| 140 | + break; |
| 141 | + default: |
| 142 | + throw new RuntimeException("wha?"); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public void surfaceDestroyed(SurfaceHolder holder) { |
| 148 | + // ignore |
| 149 | + Log.d(TAG, "Surface destroyed holder=" + holder); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Clears the surface, then draws a blue alpha-blended rectangle with GL. |
| 154 | + * <p> |
| 155 | + * Creates a temporary EGL context just for the duration of the call. |
| 156 | + */ |
| 157 | + private void drawRectSurface(Surface surface, int left, int top, int right, int bottom) { |
| 158 | + EglCore eglCore = new EglCore(); |
| 159 | + WindowSurface win = new WindowSurface(eglCore, surface, false); |
| 160 | + win.makeCurrent(); |
| 161 | + GLES20.glClearColor(0, 0, 0, 0); |
| 162 | + GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); |
| 163 | + |
| 164 | + GLES20.glEnable(GLES20.GL_SCISSOR_TEST); |
| 165 | + GLES20.glScissor(left, top, right - left, bottom - top); |
| 166 | + GLES20.glClearColor(0.0f, 0.0f, 0.5f, 0.25f); |
| 167 | + GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); |
| 168 | + GLES20.glDisable(GLES20.GL_SCISSOR_TEST); |
| 169 | + |
| 170 | + win.swapBuffers(); |
| 171 | + win.release(); |
| 172 | + eglCore.release(); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Clears the surface, then draws a filled circle with a shadow. |
| 177 | + * <p> |
| 178 | + * The Canvas drawing we're doing may not be fully implemented for hardware-accelerated |
| 179 | + * renderers (shadow layers only supported for text). However, Surface#lockCanvas() |
| 180 | + * currently only returns an unaccelerated Canvas, so it all comes out looking fine. |
| 181 | + */ |
| 182 | + private void drawCircleSurface(Surface surface, int x, int y, int radius) { |
| 183 | + Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 184 | + paint.setColor(Color.WHITE); |
| 185 | + paint.setStyle(Paint.Style.FILL); |
| 186 | + paint.setShadowLayer(radius / 4 + 1, 0, 0, Color.RED); |
| 187 | + |
| 188 | + Canvas canvas = surface.lockCanvas(null); |
| 189 | + Log.d(TAG, "drawCircleSurface: isHwAcc=" + canvas.isHardwareAccelerated()); |
| 190 | + canvas.drawARGB(0, 0, 0, 0); |
| 191 | + canvas.drawCircle(x, y, radius, paint); |
| 192 | + surface.unlockCanvasAndPost(canvas); |
| 193 | + } |
| 194 | +} |
0 commit comments