Example #1
0
        protected override byte GetAlphaAt(int x, int y)
        {
            var rowPitch = size.Width * bytesPerPixel;

            var cursorAlphaOffset = 0
                                    + (Math.Min(Math.Max(x, 0), size.Width - 1) * bytesPerPixel)
                                    + (Math.Min(Math.Max(y, 0), size.Height - 1) * rowPitch)
                                    + 3;

            byte alpha = 255;

            try
            {
                bitmapBuffer.Read(ptr =>
                {
                    alpha = Marshal.ReadByte(ptr, cursorAlphaOffset);
                });
            }
            catch
            {
                Console.Error.WriteLine("Failed to read alpha value from bitmap buffer.");
                alpha = 255;
            }

            return(alpha);
        }
Example #2
0
        public void ReadWrite_TimeoutException()
        {
            bool timedout = false;
            var  name     = Guid.NewGuid().ToString();

            byte[] data     = new byte[1024];
            byte[] readData = new byte[1024];


            using (var buf = new BufferReadWrite(name, 1024))
                using (var buf2 = new BufferReadWrite(name))
                {
                    // Set a small timeout to speed up the test
                    buf2.ReadWriteTimeout = 0;

                    // Introduce possible deadlock by acquiring without releasing the write lock.
                    buf.AcquireWriteLock();

                    // We want the AcquireReadLock to fail
                    if (!buf2.AcquireReadLock(1))
                    {
                        try
                        {
                            // Read should timeout with TimeoutException because buf.ReleaseWriteLock has not been called
                            buf2.Read(readData);
                        }
                        catch (TimeoutException)
                        {
                            timedout = true;
                        }
                    }
                    Assert.AreEqual(true, timedout, "The TimeoutException was not thrown.");

                    // Remove the deadlock situation, by releasing the write lock...
                    buf.ReleaseWriteLock();
                    // ...and ensure that we can now read the data
                    if (buf.AcquireReadLock(1))
                    {
                        buf2.Read(readData);
                    }
                    else
                    {
                        Assert.Fail("Failed to acquire read lock after releasing write lock.");
                    }
                }
        }
Example #3
0
        private void button_BufferReadWrite_Read_Click(object sender, EventArgs e)
        {
            var buffer = new BufferReadWrite("MySharedBuffer");
            int readData;

            buffer.Read(out readData);

            richTextBox1.AppendText(String.Format("SharedMemory.BufferReadWrite(Read): {0}\n", readData));
        }
Example #4
0
        public static T GetObjectBuf <T>()
        {
            _sharedMemory.Read(out int bufSize, BufferSizePos);

            if (bufSize == 0)
            {
                return(default(T));
            }

            var data = new byte[bufSize];

            _sharedMemory.Read(data, BufferPos);
            using (var memoryStream = new MemoryStream(data))
            {
                return((T)BinaryFormatter.Deserialize(memoryStream));
            }
        }
Example #5
0
        private void RenderFrame(BitmapFrame frame)
        {
            // Make sure there's a texture to render to
            // TODO: Can probably afford to remove width/height from frame, and just add the buffer name. this client code can then check that the buffer name matches what it expects, and noop if it doesn't
            if (texture == null)
            {
                BuildTexture(frame.Width, frame.Height);
            }

            // If the details don't match our expected sizes, noop the frame to avoid a CTD.
            // This may "stick" and cause no rendering at all, but can be fixed by jiggling the size a bit, or reloading.
            if (
                texture.Description.Width != frame.Width ||
                texture.Description.Height != frame.Height ||
                bitmapBuffer.BufferSize != frame.Length
                )
            {
                return;
            }

            // Calculate multipliers for the frame
            var depthPitch    = frame.Length;
            var rowPitch      = frame.Length / frame.Height;
            var bytesPerPixel = rowPitch / frame.Width;

            // Build the destination region for the dirty rect we're drawing
            var texDesc            = texture.Description;
            var sourceRegionOffset = (frame.DirtyX * bytesPerPixel) + (frame.DirtyY * rowPitch);
            var destinationRegion  = new D3D11.ResourceRegion()
            {
                Top    = Math.Min(frame.DirtyY, texDesc.Height),
                Bottom = Math.Min(frame.DirtyY + frame.DirtyHeight, texDesc.Height),
                Left   = Math.Min(frame.DirtyX, texDesc.Width),
                Right  = Math.Min(frame.DirtyX + frame.DirtyWidth, texDesc.Width),
                Front  = 0,
                Back   = 1,
            };

            // Write data from the buffer
            var context = DxHandler.Device.ImmediateContext;

            bitmapBuffer.Read(ptr =>
            {
                context.UpdateSubresource(texture, 0, destinationRegion, ptr + sourceRegionOffset, rowPitch, depthPitch);
            });
        }
Example #6
0
        public void ReadWrite_Bytes_DataMatches()
        {
            var    name = Guid.NewGuid().ToString();
            Random r    = new Random();

            byte[] data     = new byte[1024];
            byte[] readData = new byte[1024];
            r.NextBytes(data);

            using (var buf = new BufferReadWrite(name, 1024))
                using (var buf2 = new BufferReadWrite(name))
                {
                    buf.Write(data);
                    buf2.Read(readData);

                    for (var i = 0; i < data.Length; i++)
                    {
                        Assert.AreEqual(data[i], readData[i]);
                    }
                }
        }