3 #if SILICONSTUDIO_PLATFORM_ANDROID
6 using Javax.Microedition.Khronos.Egl;
10 using OpenTK.Platform.Android;
13 namespace SiliconStudio.
Paradox.Graphics
18 internal class AndroidAsyncGraphicsContext : IGraphicsContext, IGraphicsContextInternal
20 private readonly IEGL10 egl;
22 public EGLContext EGLContext {
get;
private set; }
23 public EGLDisplay EGLDisplay {
get;
private set; }
24 public EGLSurface EGLSurface {
get;
private set; }
26 public EGLConfig EGLConfig {
get;
private set; }
29 private bool isDisposed;
31 internal AndroidAsyncGraphicsContext(AndroidGraphicsContext graphicsContext, AndroidWindow androidWindow)
33 egl = EGLContext.EGL.JavaCast<IEGL10>();
35 var pbufferAttribList =
new[]
42 EGLDisplay = androidWindow.Display;
43 var androidGraphicsContext = graphicsContext;
44 var config = androidGraphicsContext.EGLConfig;
46 var attribList =
new[]
48 EGL10.EglSurfaceType, EGL10.EglPbufferBit,
49 EGL10.EglRenderableType, 4,
51 EGL10.EglRedSize, graphicsContext.GraphicsMode.ColorFormat.Red,
52 EGL10.EglGreenSize, graphicsContext.GraphicsMode.ColorFormat.Green,
53 EGL10.EglBlueSize, graphicsContext.GraphicsMode.ColorFormat.Blue,
54 EGL10.EglAlphaSize, graphicsContext.GraphicsMode.ColorFormat.Alpha,
56 EGL10.EglDepthSize, graphicsContext.GraphicsMode.Depth,
57 EGL10.EglStencilSize, graphicsContext.GraphicsMode.Stencil,
66 var numConfig =
new int[1];
67 if (!egl.EglChooseConfig(EGLDisplay, attribList, null, 0, numConfig))
69 throw new InvalidOperationException(
string.Format(
"EglChooseConfig {0:x}", egl.EglGetError()));
73 var configs =
new EGLConfig[numConfig[0]];
74 if (!egl.EglChooseConfig(EGLDisplay, attribList, configs, configs.Length, numConfig))
76 throw new InvalidOperationException(
string.Format(
"EglChooseConfig {0:x}", egl.EglGetError()));
80 EGLConfig = ChooseConfigEGL(configs);
83 EGLSurface = egl.EglCreatePbufferSurface(EGLDisplay, EGLConfig, pbufferAttribList);
84 if (EGLSurface == EGL10.EglNoSurface)
86 throw new InvalidOperationException(
string.Format(
"EglCreatePBufferSurface {0:x}", egl.EglGetError()));
89 var attribList3 =
new[] { 0x3098, 2, EGL10.EglNone };
90 EGLContext = egl.EglCreateContext(EGLDisplay, config, androidGraphicsContext.EGLContext, attribList3);
91 if (EGLContext == EGL10.EglNoContext)
93 throw new InvalidOperationException(
string.Format(
"EglCreateContext {0:x}", egl.EglGetError()));
97 private struct UserReadableEglConfig
99 public int SurfaceType;
100 public int RenderableType;
102 public int GreenSize;
104 public int AlphaSize;
105 public int DepthSize;
106 public int StencilSize;
110 private UserReadableEglConfig EglConfigToUserReadableEglConfig(EGLConfig eglConfig)
112 var surfaceType =
new int[1];
113 var renderableType =
new int[1];
114 var redSize =
new int[1];
115 var greenSize =
new int[1];
116 var blueSize =
new int[1];
117 var alphaSize =
new int[1];
118 var depthSize =
new int[1];
119 var stencilSize =
new int[1];
120 var samples =
new int[1];
122 if (!egl.EglGetConfigAttrib(EGLDisplay, eglConfig, EGL10.EglSurfaceType, surfaceType))
123 throw new InvalidOperationException(
string.Format(
"EglGetConfigAttrib {0:x}", egl.EglGetError()));
124 if (!egl.EglGetConfigAttrib(EGLDisplay, eglConfig, EGL10.EglRenderableType, renderableType))
125 throw new InvalidOperationException(
string.Format(
"EglGetConfigAttrib {0:x}", egl.EglGetError()));
126 if (!egl.EglGetConfigAttrib(EGLDisplay, eglConfig, EGL10.EglRedSize, redSize))
127 throw new InvalidOperationException(
string.Format(
"EglGetConfigAttrib {0:x}", egl.EglGetError()));
128 if (!egl.EglGetConfigAttrib(EGLDisplay, eglConfig, EGL10.EglGreenSize, greenSize))
129 throw new InvalidOperationException(
string.Format(
"EglGetConfigAttrib {0:x}", egl.EglGetError()));
130 if (!egl.EglGetConfigAttrib(EGLDisplay, eglConfig, EGL10.EglBlueSize, blueSize))
131 throw new InvalidOperationException(
string.Format(
"EglGetConfigAttrib {0:x}", egl.EglGetError()));
132 if (!egl.EglGetConfigAttrib(EGLDisplay, eglConfig, EGL10.EglAlphaSize, alphaSize))
133 throw new InvalidOperationException(
string.Format(
"EglGetConfigAttrib {0:x}", egl.EglGetError()));
134 if (!egl.EglGetConfigAttrib(EGLDisplay, eglConfig, EGL10.EglDepthSize, depthSize))
135 throw new InvalidOperationException(
string.Format(
"EglGetConfigAttrib {0:x}", egl.EglGetError()));
136 if (!egl.EglGetConfigAttrib(EGLDisplay, eglConfig, EGL10.EglStencilSize, stencilSize))
137 throw new InvalidOperationException(
string.Format(
"EglGetConfigAttrib {0:x}", egl.EglGetError()));
138 if (!egl.EglGetConfigAttrib(EGLDisplay, eglConfig, EGL10.EglSamples, samples))
139 throw new InvalidOperationException(
string.Format(
"EglGetConfigAttrib {0:x}", egl.EglGetError()));
141 return new UserReadableEglConfig
143 SurfaceType = surfaceType[0],
144 RenderableType = renderableType[0],
145 RedSize = redSize[0],
146 GreenSize = greenSize[0],
147 BlueSize = blueSize[0],
148 AlphaSize = alphaSize[0],
149 DepthSize = depthSize[0],
150 StencilSize = stencilSize[0],
155 private EGLConfig ChooseConfigEGL(EGLConfig[] configs)
157 if(configs.Length == 0)
158 throw new NotSupportedException(
"The graphic device configuration demanded is not supported.");
160 var readableConfigs =
new UserReadableEglConfig[configs.Length];
163 for (
int i = 0; i < configs.Length; i++)
164 readableConfigs[i] = EglConfigToUserReadableEglConfig(configs[i]);
169 public void Dispose()
171 EGLContext.Dispose();
172 EGLSurface.Dispose();
175 public void SwapBuffers()
177 if (!egl.EglSwapBuffers(EGLDisplay, EGLSurface))
179 var error = egl.EglGetError();
181 throw new InvalidOperationException(
string.Format(
"EglSwapBuffers {0:x}", error));
185 public void MakeCurrent(IWindowInfo window)
189 if (!egl.EglMakeCurrent(EGLDisplay, EGLSurface, EGLSurface, EGLContext))
190 throw new InvalidOperationException();
194 if (!egl.EglMakeCurrent(EGLDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext))
195 throw new InvalidOperationException();
199 public void Update(IWindowInfo window)
205 public void LoadAll()
209 public bool IsCurrent
211 get {
return (egl.EglGetCurrentContext() == EGLContext);
215 public bool IsDisposed
217 get {
return isDisposed; }
222 get {
return false; }
226 public int SwapInterval {
get; set; }
228 public GraphicsMode GraphicsMode
230 get {
throw new NotImplementedException(); }
233 public bool ErrorChecking
235 get {
return false; }
239 IntPtr IGraphicsContextInternal.GetAddress(
string function)
244 public IGraphicsContext Implementation
249 public ContextHandle Context
251 get {
return new ContextHandle(EGLContext.Handle); }