4 * Lxdream GL view. Derived from android
6 * Copyright (c) 2011 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
21 * Copyright (C) 2008,2009 The Android Open Source Project
23 * Licensed under the Apache License, Version 2.0 (the "License");
24 * you may not use this file except in compliance with the License.
25 * You may obtain a copy of the License at
27 * http://www.apache.org/licenses/LICENSE-2.0
29 * Unless required by applicable law or agreed to in writing, software
30 * distributed under the License is distributed on an "AS IS" BASIS,
31 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32 * See the License for the specific language governing permissions and
33 * limitations under the License.
37 import android.content.Context;
38 import android.graphics.PixelFormat;
39 import android.opengl.GLSurfaceView;
40 import android.util.AttributeSet;
41 import android.util.Log;
42 import android.view.KeyEvent;
43 import android.view.MotionEvent;
45 import javax.microedition.khronos.egl.EGL10;
46 import javax.microedition.khronos.egl.EGLConfig;
47 import javax.microedition.khronos.egl.EGLContext;
48 import javax.microedition.khronos.egl.EGLDisplay;
49 import javax.microedition.khronos.opengles.GL10;
52 * A simple GLSurfaceView sub-class that demonstrate how to perform
53 * OpenGL ES 2.0 rendering into a GL Surface. Note the following important
56 * - The class must use a custom context factory to enable 2.0 rendering.
57 * See ContextFactory class definition below.
59 * - The class must use a custom EGLConfigChooser to be able to select
60 * an EGLConfig that supports 2.0. This is done by providing a config
61 * specification to eglChooseConfig() that has the attribute
62 * EGL10.ELG_RENDERABLE_TYPE containing the EGL_OPENGL_ES2_BIT flag
63 * set. See ConfigChooser class definition below.
65 * - The class must select the surface's format, then choose an EGLConfig
66 * that matches it exactly (with regards to red/green/blue/alpha channels
67 * bit depths). Failure to do so would result in an EGL_BAD_MATCH error.
69 class LxdreamView extends GLSurfaceView {
70 private static String TAG = "LxdreamView";
71 private static final boolean DEBUG = false;
73 public LxdreamView(Context context) {
78 public LxdreamView(Context context, boolean translucent, int depth, int stencil) {
80 init(translucent, depth, stencil);
83 private void init(boolean translucent, int depth, int stencil) {
85 /* By default, GLSurfaceView() creates a RGB_565 opaque surface.
86 * If we want a translucent one, we should change the surface's
87 * format here, using PixelFormat.TRANSLUCENT for GL Surfaces
88 * is interpreted as any 32-bit surface with alpha by SurfaceFlinger.
91 this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
94 /* Setup the context factory for 2.0 rendering.
95 * See ContextFactory class definition below
97 setEGLContextFactory(new ContextFactory());
99 /* We need to choose an EGLConfig that matches the format of
100 * our surface exactly. This is going to be done in our
101 * custom config chooser. See ConfigChooser class definition
104 setEGLConfigChooser( translucent ?
105 new ConfigChooser(8, 8, 8, 8, depth, stencil) :
106 new ConfigChooser(5, 6, 5, 0, depth, stencil) );
108 /* Set the renderer responsible for frame rendering */
109 setRenderer(new Renderer());
112 private static class ContextFactory implements GLSurfaceView.EGLContextFactory {
113 private static int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
114 public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig) {
115 Log.w(TAG, "creating OpenGL ES 2.0 context");
116 checkEglError("Before eglCreateContext", egl);
117 int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
118 EGLContext context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list);
119 checkEglError("After eglCreateContext", egl);
123 public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context) {
124 egl.eglDestroyContext(display, context);
128 private static void checkEglError(String prompt, EGL10 egl) {
130 while ((error = egl.eglGetError()) != EGL10.EGL_SUCCESS) {
131 Log.e(TAG, String.format("%s: EGL error: 0x%x", prompt, error));
136 private static class Renderer implements GLSurfaceView.Renderer {
137 public void onDrawFrame(GL10 gl) {
141 public void onSurfaceChanged(GL10 gl, int width, int height) {
143 Dreamcast.setViewSize(width,height);
146 public void onSurfaceCreated(GL10 gl, EGLConfig config) {
.