Tuesday, March 31, 2015

Staying Alive! - WriteableBitmapEx 1.5 is out

After a couple of minor updates on top of version 1.0 which lead to 1.0.14, I'm happy to announce that WriteableBitmapEx 1.5 is now available.
Many contributions were integrated and lots of bugs fixed. Among those are some nice new color modifications and also long awaited DrawLine with variable thickness, pen support, better anti-aliased lines, Cohen-Sutherland line clipping, even-odd polygon filling and alpha-blended shape filling... Read the details at the end of this post or the release notes.

WriteableBitmapEx supports a variety of Windows platforms and versions: WPF, Silverlight, Windows 10 Universal App Platform (UAP), Windows 8/8.1, Windows Phone WinRT and Silverlight 7/8/8.1.


You can download the binaries here or via the NuGet package. The packages contain the WriteableBitmapEx binaries. All samples and the source code can be found in the repository.

A big thank you to all the contributors, bug reporters and users of the library who helped to shape this. You rock!

Changes
  • Added lots of contributions including DrawLine with variable thickness, penning, improved anti-aliasing and Wu's anti-aliasing algorithm
  • Added usage of CohenSutherland line clipping for DrawLineAa and DrawLine, etc.
  • Added support for alpha blended filled shapes and adapted the FillSample for WPF
  • Added FillPolygonsEvenOdd() which uses the even-odd algorithm to fill complex polygons with more than one closed outline like for the letter O
  • Added AdjustBrightness(), AdjustContrast() and AdjustGamma() methods
  • Added Gray() method which returns the gray scaled version the bitmap
  • Fixed regression issue with alpha blending for Blit for non-WinRT
  • Fixed bug in Blit Alpha code for WPF when source format is not pre-multiplied alpha
  • Fixed bug #21778 where FromStream for WPF needs to be called inside Init scope
  • Fixed issue with IndexOutOfRangeEx in DrawLine method
  • Fixed Invalidate for Silverlight BitmapContext.Dispose
  • Fixed many more reported issues
  • ...

Friday, December 19, 2014

The Walking HeadlessHost - WP 8.1 Feedback Feature and Zombies

Photo by Juanedc

I was recently working on a rather large Windows Phone app project and came across an issue which took many head scratching hours to figure out. Therefore here's a short heads up if you work on Windows Phone apps with background agents and have those running on Windows Phone 8.1 devices: The WP 8.1's Feedback setting can influence how your background process behaves.

We are talking about a WP 8.0 app with an UI (TaskHost.exe) and a VoIP background agent (HeadlessHost.exe) communicating with each other through an out-of-process RPC server. This approach follows the WP 8.0 VoIP app architecture. When this 8.0 app runs on 8.1 devices I've seen sporadic issues where an instance of the HeadlessHost background process was kept alive when the app was deactivated, for example with fast app switching. When the app get's activated again another instance of the HeadlessHost is started while the previous zombie HeadlessHost is still not dead, so you will end up with two HeadlessHost processes which leads to issues like endless "Resuming..." screens and app crashes when navigating back. This can be seen even without a debugger attached.


If you disable the WP 8.1 device's Feedback setting that issue is gone and the ForegroundLifetimeAgent's HeadlessHost is quickly exited as expected when the UI gets deactivated.
I'm not sure why the Feedback feature is related, my best guess right now: The WP 8.1 Feedback feature gathers telemetry data and therefore keeps the HeadlessHost undead. The weird part is that the HeadlessHost is even undead after a minute and no crash of the background agent happened which means no Watson data should actually have been collected at all or at least not that long.

I'm not sure yet if Silverlight 8.1 apps on 8.1 devices are affected as well or if it's only an issue with 8.0 apps on 8.1 developer unlocked devices. Nevertheless I still wanted to share my experience since it's a rather unknown case and not much information can be found anywhere else so far.

Beyond Kinect for Windows v2 - KinectEx

Kinect for Windows (K4W) and the Kinect v2 sensor are amazing for NUI development and I'm glad that we at IdentityMine have advanced projects where we can leverage this technology.
The K4W SDK already provides lots of nice APIs and a ton of samples to get started. Like usual, there's always more and an SDK can not cover everything but if the official APIs are well built, the open source community is always happy to provide functionality beyond the standard SDK.
One very good open source K4W project is KinectEx which provides some really cool features one often needs when working with K4W v2. KinectEx brings Kinect data recording and playback APIs, so it can be integrated in your own apps, multiple skeleton data smoothing algorithms and much more.
I've contributed a few changes to the project and the KinectEx author is really open for pull requests. Funnily does KinectEx also use one of my open source projects WriteableBitmapEx.

I just wanted to share this very nice open source project and spread the word.
You should definitely look into KinectEx if you are doing any K4W v2 development.

Friday, November 14, 2014

Native Android Application Development with C++ and Visual Studio 2015

Visual Studio 2015 ships the new C++ cross platform support which currently provides native C++ library compilation for Android and Windows platforms. iOS compiler support is supposed to be added as well.
That's not all. VS 2015 even provides a new project type called "Native-Activity Application" for Android app development. This pure C/C++ Android application model is mainly used for game development using full screen OpenGL rendering.
The Native-Activity Application project type allows developers to write native Android apps with C++ and even to run and debug those in the Android emulator only by using Visual Studio 2015.
No Xamarin, no Apache Cordova needed, just VS 2015 and pure C/C++.



How it works

It's pretty straightforward to get started.
Just install Visual Studio 2015 including its Secondary installer which adds the required Android development tools.
Start VS 2015 and select the Visual C++ -> Cross Platform -> Native-Activity Application (Android) project type:


The generated template code provides the base native Android app code including simple code for cycling the back buffer clear color inside the engine_draw_frame function.

For this sample here I decided to make the Hello World of 3D computer graphics: a rainbow colored rotating cube. Here's how you can achieve this as well:

Add the cube definitions to the beginning of the main.cpp:

static GLint vertices[][3] =
{
 { -0x10000, -0x10000, -0x10000 },
 { 0x10000, -0x10000, -0x10000 },
 { 0x10000,  0x10000, -0x10000 },
 { -0x10000,  0x10000, -0x10000 },
 { -0x10000, -0x10000,  0x10000 },
 { 0x10000, -0x10000,  0x10000 },
 { 0x10000,  0x10000,  0x10000 },
 { -0x10000,  0x10000,  0x10000 }
};

static GLint colors[][4] =
{
 { 0x00000, 0x00000, 0x00000, 0x10000 },
 { 0x10000, 0x00000, 0x00000, 0x10000 },
 { 0x10000, 0x10000, 0x00000, 0x10000 },
 { 0x00000, 0x10000, 0x00000, 0x10000 },
 { 0x00000, 0x00000, 0x10000, 0x10000 },
 { 0x10000, 0x00000, 0x10000, 0x10000 },
 { 0x10000, 0x10000, 0x10000, 0x10000 },
 { 0x00000, 0x10000, 0x10000, 0x10000 }
};

GLubyte indices[] = {
 0, 4, 5,    0, 5, 1,
 1, 5, 6,    1, 6, 2,
 2, 6, 7,    2, 7, 3,
 3, 7, 4,    3, 4, 0,
 4, 7, 6,    4, 6, 5,
 3, 0, 1,    3, 1, 2
};


And replace the // Initialize GL State statements at the end of the engine_init_display function with this:

 // Initialize GL state.
 glDisable(GL_DITHER);
 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
 glClearColor(1.0f, 0.41f, 0.71f, 1.0f); // Hot pink! :D
 glEnable(GL_CULL_FACE);
 glShadeModel(GL_SMOOTH);
 glEnable(GL_DEPTH_TEST);
 glViewport(0, 0, w, h);
 GLfloat ratio = (GLfloat)w / h;
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 glFrustumf(-ratio, ratio, -1, 1, 1, 10);


Finally replace the engine_draw_frame function:

static void engine_draw_frame(struct engine* engine) {
 if (engine->display == NULL) {
  // No display.
  return;
 }

 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 glTranslatef(0, 0, -3.0f);
 glRotatef(engine->state.angle * 0.25f, 1, 0, 0);  // X
 glRotatef(engine->state.angle, 0, 1, 0);          // Y

 glEnableClientState(GL_VERTEX_ARRAY);
 glEnableClientState(GL_COLOR_ARRAY);

 glFrontFace(GL_CW);
 glVertexPointer(3, GL_FIXED, 0, vertices);
 glColorPointer(4, GL_FIXED, 0, colors);
 glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
 
 eglSwapBuffers(engine->display, engine->surface);
}

You also might want to modify the angle increment so the cube rotates a bit faster, like engine.state.angle += 1.f;

Make sure the project is set to compile as x86 in order to use VS' own Android emulator.
Then F5 and enjoy the magic of the rotating cube.


Now try to add a breakpoint in your engine_draw_frame function. Yes, there's native debugging support for Android apps in Visual Studio 2015. Times are definitely changing at Microsoft.


Sample code

You can download the complete VS 2015 sample solution here.


More resources

This blog post is a nice overview of the new Visual C++ cross platform features.
This short Ch9 video gives a quick intro to the new Visual Studio 2015 C++ cross platform support.
NeHe is a quite old resource for OpenGL tutorials but mostly still useful.

Thursday, November 13, 2014

Cross Platform Mobile Development with C++ and Visual Studio 2015

Screenshot from Herb's video
Beside the Xamarin partnership and their great solutions for C# cross platform development, Microsoft is also heavily investing in C++ cross platform development with Visual Studio 2015.
With VS 2015 it's possible to build shared cross platform C++ libraries or even full native Android apps only with VS 2015. VS 2015 even ships its own Android emulator. iOS compiler support is supposed to be added as well.

Don't get me wrong, Xamarin and C# are without a doubt great for sharing code in native greenfield apps. However, existing code bases are often already built in C++. In fact I was involved in at least two large apps over the last 1.5 year which used already existing shared C++ layers. Most of the backend connectivity and core data logic was implemented in C++ and shared between WP, iOS, Android and BB10. Many large projects follow this shared C++ layers approach including the MS Office and Dropbox mobile apps.
Of course Xamarin C# and shared cross platform C++ libraries can be combined to get the best of all. For example by leveraging an existing code base for the shared C++ core libraries and Xamarin for the UI layer.

Is it the renaissance of C++? I'm not sure. It was always here driving many projects, even on the smallest devices and never gone, but I’m sure the new shared C++ library project support in VS 2015 is a strong commitment to the future of Visual C++.

Here's a good overview of the new C++ cross platform features in VS 2015:
Cross-Platform Mobile Development with Visual C++

And some very nice short videos from the Connect() event. Highly recommended to watch:
Herb's Ch9 video "C++:Conformance And Cross-Platform Mobile Development"
Ch9 video: "Visual C++ for Cross-Platform Mobile Development using Visual Studio 2015"

There's much more to discover in VS 2015 for C++ developers:
Visual Studio 2015 Preview is Now Available