Ejemplo n.º 1
0
        /// <summary>
        /// Creates a context, specifying attributes.
        /// </summary>
        /// <param name="sharedContext">
        /// A <see cref="IntPtr"/> that specify a context that will share objects with the returned one. If
        /// it is IntPtr.Zero, no sharing is performed.
        /// </param>
        /// <param name="attribsList">
        /// A <see cref="T:Int32[]"/> that specifies the attributes list.
        /// </param>
        /// <param name="api">
        /// A <see cref="KhronosVersion"/> that specifies the API to be implemented by the returned context. It can be null indicating the
        /// default API for this DeviceContext implementation. If it is possible, try to determine the API version also.
        /// </param>
        /// <returns>
        /// A <see cref="IntPtr"/> that represents the handle of the created context. If the context cannot be
        /// created, it returns IntPtr.Zero.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// Exception thrown if <paramref name="attribsList"/> is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Exception thrown if <paramref name="attribsList"/> length is zero or if the last item of <paramref name="attribsList"/>
        /// is not zero.
        /// </exception>
        public override IntPtr CreateContextAttrib(IntPtr sharedContext, int[] attribsList, KhronosVersion api)
        {
            if (attribsList == null)
            {
                throw new ArgumentNullException(nameof(attribsList));
            }
            if (attribsList.Length == 0)
            {
                throw new ArgumentException("zero length array", nameof(attribsList));
            }
            if (attribsList[attribsList.Length - 1] != Egl.NONE)
            {
                throw new ArgumentException("not EGL_NONE-terminated array", nameof(attribsList));
            }

            IntPtr context;

            // Select surface pixel format automatically
            if (_NativeSurface != null && _NativeSurface.Handle != IntPtr.Zero)
            {
                int[] configId = new int[1];

                if (Egl.QuerySurface(Display, EglSurface, Egl.CONFIG_ID, configId) == false)
                {
                    throw new InvalidOperationException("unable to query EGL surface config ID");
                }

                _Config = ChoosePixelFormat(Display, configId[0]);
            }

            // Bind API
            if (Version >= Egl.Version_120)
            {
                uint apiEnum;

                switch (api.Api)
                {
                case KhronosVersion.ApiGles2:
                case KhronosVersion.ApiGles1:
                case null:
                    // Default
                    apiEnum = Egl.OPENGL_ES_API;
                    break;

                case KhronosVersion.ApiGl:
                    apiEnum = Egl.OPENGL_API;
                    break;

                case KhronosVersion.ApiVg:
                    apiEnum = Egl.OPENVG_API;
                    break;

                default:
                    throw new InvalidOperationException($"'{api}' API not available");
                }
                if (Egl.BindAPI(apiEnum) == false)
                {
                    throw new InvalidOperationException("no ES API");
                }
            }
            else if (api != null && api.Api != KhronosVersion.ApiGles2 && api.Api != KhronosVersion.ApiGles1)
            {
                throw new InvalidOperationException($"'{api}' API not available");
            }

            // Create context
            if ((context = Egl.CreateContext(Display, _Config, sharedContext, attribsList)) == IntPtr.Zero)
            {
                throw new EglException(Egl.GetError());
            }

            // Create native surface (pixel format pending)
            // @todo Back-buffer?
            if (_NativeSurface != null && _NativeSurface.Handle == IntPtr.Zero)
            {
                _NativeSurface.CreateHandle(_Config, new[] { Egl.NONE });
            }

            return(context);
        }