Exemplo n.º 1
0
        public void DevicePixelFormat_Copy()
        {
            DevicePixelFormat pf1 = new DevicePixelFormat(24);
            DevicePixelFormat pf2 = pf1.Copy();

            Assert.AreNotSame(pf1, pf2);
        }
Exemplo n.º 2
0
        private void CreateDeviceContext()
        {
            this.DeviceContext = DeviceContextFactory.Create(this.Handle);
            this.DeviceContext.IncRef();

            // Set pixel format
            var pixelFormats     = this.DeviceContext.PixelsFormats;
            var controlReqFormat = new DevicePixelFormat
            {
                ColorBits       = (int)this.ColorBits,
                DepthBits       = (int)this.DepthBits,
                StencilBits     = (int)this.StencilBits,
                MultisampleBits = (int)this.MultisampleBits,
                DoubleBuffer    = this.DoubleBuffer
            };

            var matchingPixelFormats = pixelFormats.Choose(controlReqFormat);

            if (matchingPixelFormats.Count == 0)
            {
                throw new InvalidOperationException("unable to find a suitable pixel format");
            }

            this.DeviceContext.SetPixelFormat(matchingPixelFormats[0]);
        }
Exemplo n.º 3
0
        public void DevicePixelFormat_Constructor2()
        {
            // ReSharper disable once ObjectCreationAsStatement
            Assert.DoesNotThrow(() => new DevicePixelFormat(24));

            DevicePixelFormat pf = new DevicePixelFormat(32);

            Assert.AreEqual(32, pf.ColorBits);
            Assert.IsTrue(pf.RgbaUnsigned);
            Assert.IsTrue(pf.RenderWindow);
        }
Exemplo n.º 4
0
 public Device(DevicePixelFormat pixelFormat)
 {
     if (DeviceContext.IsPBufferSupported)
     {
         _NativePBuffer = DeviceContext.CreatePBuffer(pixelFormat, 64, 64);
         Context        = DeviceContext.Create(_NativePBuffer);
     }
     else
     {
         Assert.Inconclusive("no UI backend available");
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a new graphics context.
        /// </summary>
        private void _createGraphicsContext(DevicePixelFormat pixelFormat)
        {
            _graphicsContext = GraphicsManager.CreateGraphicsContext(_getWindowHandle());

            #region Set Pixel Format

            DevicePixelFormatCollection pixelFormats = _graphicsContext.PixelsFormats;
            System.Collections.Generic.List <DevicePixelFormat> matchingPixelFormats = pixelFormats.Choose(pixelFormat);

            if ((matchingPixelFormats.Count == 0) && pixelFormat.MultisampleBits > 0)
            {
                // Try to select the maximum multisample configuration
                int multisampleBits = 0;

                pixelFormats.ForEach(delegate(DevicePixelFormat item) { multisampleBits = Math.Max(multisampleBits, item.MultisampleBits); });

                pixelFormat.MultisampleBits = multisampleBits;

                matchingPixelFormats = pixelFormats.Choose(pixelFormat);
            }

            if ((matchingPixelFormats.Count == 0) && pixelFormat.DoubleBuffer)
            {
                // Try single buffered pixel formats
                pixelFormat.DoubleBuffer = false;

                matchingPixelFormats = pixelFormats.Choose(pixelFormat);
                if (matchingPixelFormats.Count == 0)
                {
                    throw new InvalidOperationException(String.Format("unable to find a suitable pixel format: {0}", pixelFormats.GuessChooseError(pixelFormat)));
                }
            }
            else if (matchingPixelFormats.Count == 0)
            {
                throw new InvalidOperationException(String.Format("unable to find a suitable pixel format: {0}", pixelFormats.GuessChooseError(pixelFormat)));
            }

            _graphicsContext.SetPixelFormat(matchingPixelFormats[0]);

            #endregion

            // TODO: Platform specific extension checker
            if (WGL.IsExtensionSupported(_graphicsContext.DeviceHandle.Handle, WGL.EXT.SwapControl))
            {
                // TODO: Handle tear
                WGL.SwapIntervalEXT(SwapInterval);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Create the device context and set the pixel format.
        /// </summary>
        private void CreateDeviceContext(DevicePixelFormat controlReqFormat)
        {
            #region Create device context

            _DeviceContext = DeviceContext.Create(GetDisplay(), GetWindowHandle());
            _DeviceContext.IncRef();

            #endregion

            #region Set pixel format

            DevicePixelFormatCollection pixelFormats         = _DeviceContext.PixelsFormats;
            List <DevicePixelFormat>    matchingPixelFormats = pixelFormats.Choose(controlReqFormat);

            if ((matchingPixelFormats.Count == 0) && controlReqFormat.MultisampleBits > 0)
            {
                // Try to select the maximum multisample configuration
                int multisampleBits = 0;

                pixelFormats.ForEach(delegate(DevicePixelFormat item) { multisampleBits = Math.Max(multisampleBits, item.MultisampleBits); });

                controlReqFormat.MultisampleBits = multisampleBits;

                matchingPixelFormats = pixelFormats.Choose(controlReqFormat);
            }

            if ((matchingPixelFormats.Count == 0) && controlReqFormat.DoubleBuffer)
            {
                // Try single buffered pixel formats
                controlReqFormat.DoubleBuffer = false;

                matchingPixelFormats = pixelFormats.Choose(controlReqFormat);
                if (matchingPixelFormats.Count == 0)
                {
                    throw new InvalidOperationException(String.Format("unable to find a suitable pixel format: {0}", pixelFormats.GuessChooseError(controlReqFormat)));
                }
            }
            else if (matchingPixelFormats.Count == 0)
            {
                throw new InvalidOperationException(String.Format("unable to find a suitable pixel format: {0}", pixelFormats.GuessChooseError(controlReqFormat)));
            }

            _DeviceContext.SetPixelFormat(matchingPixelFormats[0]);

            #endregion

            #region Set V-Sync

            if (Gl.PlatformExtensions.SwapControl)
            {
                int swapInterval = SwapInterval;

                // Mask value in case it is not supported
                if (!Gl.PlatformExtensions.SwapControlTear && swapInterval == -1)
                {
                    swapInterval = 1;
                }

                _DeviceContext.SwapInterval(swapInterval);
            }

            #endregion
        }
Exemplo n.º 7
0
        /// <summary>
        /// Create the device context and set the pixel format.
        /// </summary>
        private void CreateDeviceContext(IntPtr displayHandle, IntPtr windowHandle, DevicePixelFormat controlReqFormat)
        {
            #region Support ES/SC API

            switch (_ProfileType)
            {
            case ProfileType.Embedded:
                DeviceContext.DefaultAPI = KhronosVersion.ApiGles2;
                break;

            case ProfileType.SecurityCritical2:
                DeviceContext.DefaultAPI = KhronosVersion.ApiGlsc2;
                break;
            }

            #endregion

            #region Create device context

            DeviceContext = DeviceContext.Create(displayHandle, windowHandle);
            DeviceContext.IncRef();

            #endregion

            #region Set pixel format

            DevicePixelFormatCollection pixelFormats         = DeviceContext.PixelsFormats;
            List <DevicePixelFormat>    matchingPixelFormats = pixelFormats.Choose(controlReqFormat);

            if (matchingPixelFormats.Count == 0 && controlReqFormat.MultisampleBits > 0)
            {
                // Try to select the maximum multisample configuration
                int multisampleBits = 0;

                pixelFormats.ForEach(delegate(DevicePixelFormat item) { multisampleBits = Math.Max(multisampleBits, item.MultisampleBits); });

                controlReqFormat.MultisampleBits = multisampleBits;

                matchingPixelFormats = pixelFormats.Choose(controlReqFormat);
            }

            if (matchingPixelFormats.Count == 0 && controlReqFormat.DoubleBuffer)
            {
                // Try single buffered pixel formats
                controlReqFormat.DoubleBuffer = false;

                matchingPixelFormats = pixelFormats.Choose(controlReqFormat);
                if (matchingPixelFormats.Count == 0)
                {
                    throw new InvalidOperationException(
                              $"unable to find a suitable pixel format: {pixelFormats.GuessChooseError(controlReqFormat)}");
                }
            }
            else if (matchingPixelFormats.Count == 0)
            {
                throw new InvalidOperationException(
                          $"unable to find a suitable pixel format: {pixelFormats.GuessChooseError(controlReqFormat)}");
            }

            DeviceContext.SetPixelFormat(matchingPixelFormats[0]);

            #endregion

            #region Set V-Sync

            if (Gl.PlatformExtensions.SwapControl)
            {
                int swapInterval = SwapInterval;

                // Mask value in case it is not supported
                if (!Gl.PlatformExtensions.SwapControlTear && swapInterval == -1)
                {
                    swapInterval = 1;
                }

                DeviceContext.SwapInterval(swapInterval);
            }

            #endregion
        }
        /// <summary>
        /// This is called immediately after the surface is first created.
        /// </summary>
        /// <param name="holder">
        /// The SurfaceHolder whose surface is being created.
        /// </param>
        public void SurfaceCreated(ISurfaceHolder holder)
        {
            Debug.WriteLine("Sparrow: SurfaceCreated");
            // Get actual native window handle
            _NativeWindowHandle = ANativeWindow_fromSurface(JNIEnv.Handle, holder.Surface.Handle);

            // Create device context
            _DeviceContext = DeviceContext.Create(IntPtr.Zero, _NativeWindowHandle);
            _DeviceContext.IncRef();

            // Set pixel format
            DevicePixelFormatCollection pixelFormats     = _DeviceContext.PixelsFormats;
            DevicePixelFormat           controlReqFormat = new DevicePixelFormat();

            controlReqFormat.RgbaUnsigned = true;
            controlReqFormat.RenderWindow = true;
            controlReqFormat.ColorBits    = 24;
            //controlReqFormat.DepthBits = (int)DepthBits;
            //controlReqFormat.StencilBits = (int)StencilBits;
            //controlReqFormat.MultisampleBits = (int)MultisampleBits;
            //controlReqFormat.DoubleBuffer = true;

            List <DevicePixelFormat> matchingPixelFormats = pixelFormats.Choose(controlReqFormat);

            if (matchingPixelFormats.Count == 0)
            {
                throw new InvalidOperationException("unable to find a suitable pixel format");
            }
            _DeviceContext.SetPixelFormat(matchingPixelFormats[0]);

            // Create OpenGL context using compatibility profile
            if ((_RenderContext = _DeviceContext.CreateContext(IntPtr.Zero)) == IntPtr.Zero)
            {
                throw new InvalidOperationException("unable to create render context");
            }
            // Make context current
            if (_DeviceContext.MakeCurrent(_RenderContext) == false)
            {
                throw new InvalidOperationException("unable to make context current");
            }

            Invalidate();

            if (_RenderTimer != null)
            {
                throw new InvalidOperationException("rendering already active");
            }

            if (SparrowSharp.Root == null)
            {
                SparrowSharp.NativeWindow = this;
                // TODO get viewport dimensions?
                SparrowSharp.Start((uint)Width, (uint)Height, (uint)Width, (uint)Height, _rootClass);
            }
            else
            {
                SparrowSharp.OnContextCreated();
            }

            _RenderTimerDueTime = (int)Math.Ceiling(1000.0f / 60.0f);
            _RenderTimer        = new Timer(RenderTimerCallback, null, _RenderTimerDueTime, Timeout.Infinite);
        }