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

Thursday, October 2, 2014

Not Dead Yet - PDB Inspector

Today I had to analyze some PDB debugging symbols in order to verify if the needed debugging information is properly generated.
Archived PDBs are useful if you want to analyze a dump file like the minidump the Windows Phone Store Crash Reports provides.
To my surprise is there no ready-to-use free tool for inspecting a PDB file and its functions. Fortunately wrote Marc Ochsenmeier a great Codeproject article a few years ago and provided sample code for a nice little tool he calls Pdb Inspector. Unfortunately was there no binary which runs on my Win 8.1 x64 machine and the code did not compile, so I had to modify it a bit and got it compiling. You can grab a binary of a fresh Pdb Inspector here.

Thursday, September 25, 2014

Matrix of the Universe - Matrix3DEx now available for Universal Apps

Do you know about the semi-3D features of XAML and the UIElement Projection available in WinRT and Silverlight? I know many aren't aware of this feature which enables actually pretty nice semi-3D experiences just with XAML. To make the handling easier, I created the Matrix3DEx open source project a few years ago which provides helper methods and samples.

It's better described in action than with words, so you should try the live Silverlight sample which is now also available for WinRT.
There were also some cool games developed using Matrix3DEx: Cubemania is a great and highly addictive rigid body physics game and Go Go Racer! is a nice 2.5D retro racing game.


The Matrix3DEx library is now available for WinRT including Universal Apps, Windows Phone Silverlight and goo old Web Silverlight.
You can download the binaries here.
As usual all samples and the source code can be found in the repository.

The Matrix3DEx project description from the CodePlex site:
The Matrix3DEx library is a collection of extension and factory methods for the Matrix3D struct. It's available for Windows WinRT including Universal Apps, Silverlight and Windows Phone Silverlight. The Matrix3D struct represents a 4x4 matrix that is used in combination with the Matrix3DProjection to apply more complex semi-3D scenarios to any UIElement than are possible with the simple PlaneProjection. This makes it possible to apply arbitrary model transformation matrices and perspective matrices to Silverlight elements.
The Matrix3D struct is very minimalistic and has only a few members. The Matrix3DEx library tries to compensate that with extension and factory methods for common transformation matrices that are easy to use like built in methods.

Friday, April 4, 2014

Content for the Modern Camera and Imaging Apps Build 2014 Session

I had a chance to give a presentation at Microsoft's largest developer conference //build 2014 about the great new capturing APIs. The session Modern Camera and Imaging Apps was given together with Jeff Day who's the Windows Camera API lead PM.

The session was recorded and available at Channel 9.

The slides can also be viewed / downloaded here.
The source code of the demos is available here.

Friday, January 31, 2014

No Biggie - How to Query the Available Storage Size in WinRT

Tape drive photo by P. Hollenback
The Windows Runtime (WinRT) provides a nice file IO API called Windows.Storage. Part of it are atomic operations to create files and folders, etc. It also has a KnownFolders class with static references to different library folders like documents, videos, photos, plus RemovableDevices. Additionally does ApplicationData.Current provide references to the app's temporary, local and roaming folder.
Unfortunately there's no built-in way to check for the available free storage size / free disk space at those locations, although there are scenarios where that information can be essential, especially when dealing with removable devices. But no worries, the good old Win32 has it covered with the GetFreeDiskSpaceEx  function and it can be used with Windows 8 and Windows Phone 8. In order to use it from managed code it just has to be called through P/Invoke or wrapped in a WinRT component which I prefer nowadays, therefore this post provides a short WinRT C++/Cx snippet.

I assume you know how to create a custom C++/Cx WinRT component with Visual Studio, if not go back to my previous blog post which shows just that.


How it works

  1. Open the generated precompiled header file pch.h of your WinRT component's Visual Studio project and add an include for windows.h:
    // pch.h - Header for standard system include files.
    #pragma once
    
    #include <windows.h>

  2. Open the header file of your component and add the method declaration of GetAvailableBytes to your WinRT component which will internally use the GetFreeDiskSpaceEx, but only takes a path as string parameter and returns a WinRT uint64 (unsigned long) type with the available free space in bytes.
    namespace MyNativeStuff
    {
        public ref class MyStorageExtensions sealed
        {
        public:
            uint64 GetAvailableBytes(Platform::String^ path);
        };
    }

  3. Add the method implementation in the source file of the component (.cpp).
    The GetDiskFreeSpaceEx function takes a pointer to an ULARGE_INTEGER which is an union from ancient times when compilers didn't support 64 bit types. Our method then returns the filled unsigned long QuadPart of it.
    #include "pch.h"
    #include "MyNativeStuff.h"
    
    using namespace MyNativeStuff;
    using namespace Platform;
    
    uint64 MyStorageExtensions::GetAvailableBytes(Platform::String^ path)
    {
        ULARGE_INTEGER availableBytesToCaller;
        availableBytesToCaller.QuadPart = 0;
    
        GetDiskFreeSpaceEx(path->Data(), &availableBytesToCaller, NULL, NULL);
    
        return availableBytesToCaller.QuadPart;
    }

  4. You are now ready to use the component in your managed C# code.
    var myNativeComponent = new MyNativeStuff.MyStorageExtensions();
    
    // Create a folder in temp folder of the app and check the available free space
    var myTempFolder = await ApplicationData.Current.TemporaryFolder.CreateFolderAsync("test", CreationCollisionOption.OpenIfExists);
    var availableSpaceTempFolder = myNativeComponent.GetAvailableBytes(myTempFolder.Path);
    
    // Create a folder in videos lib and check the available free space
    var myVideoFolder = await KnownFolders.VideosLibrary.CreateFolderAsync("test", 
    CreationCollisionOption.OpenIfExists);
    var availableSpaceVideoLib = myNativeComponent.GetAvailableBytes(myVideoFolder.Path);

Note, the GetFreeDiskSpaceEx works with any directory path the calling code is allowed to access.

Tuesday, January 7, 2014

Windows Phone Natives - How to Leverage Native Code on Windows Phone

With Windows Phone 8 the possibility to use native code for development was introduced to the public SDK. It's a powerful feature and it can help to improve the performance for certain heavy computing tasks, reduce the memory footprint or just to leverage certain APIs which are only available in native code. Media Foundation or Direct3D for example are native only, but it could as well be a proprietary library from another third party written in C/C++.
In order to use native code on Windows Phone one has to write a native Windows Phone Runtime Component (WinPRT). Such a WinPRT library encapsulates the native code internally and only exposes WinRT types, therefore it can be consumed through the WinMD projections also in managed C# projects.

This blog post provides an introduction with an easy sample to get started. The MSDN also provides a very good Windows Phone native code overview, a list of the available Win32 and COM APIs on Windows Phone including an index of all supported Win32 functions. It contains such important things like the native sockets API Winsock, the National Language Support (NLS) functions for all things around region, language and culture and much more native APIs.

How it works

  1. First of all you need to create a Windows Phone App 8 project with Visual Studio.

  2. Then add a new Visual C++ Windows Phone Runtime Component (WinPRT) project to the solution.


  3. Now you need to add the solution's WinPRT project as reference to the App project. Note that Visual Studio will automatically take care of adding the required ActivatableClass definition to your consuming App project. Refer to this if you ever need to add a WinRT component manually to another project.

  4. Open the generated precompiled header file pch.h and add an include for windows.h:
    // pch.h - Header for standard system include files.
    #pragma once
    
    #include <windows.h>

    For this sample we use the Win32 function IsProcessorFeaturePresent to query if certain processor features are supported. This function is defined in windows.h.

  5. Open the header file of your component (ProcessorInfoComponent.h) and add the method declaration of IsNeonSupported to our WinPRT component which will internally use the IsProcessorFeaturePresent, but only expose a WinRT bool type. The MSDN has a nice Quick Reference which provides a table how Standard C++ types and constructs map to C++/Cx WinRT.
    namespace ProcessorInfoComponent
    {
        public ref class ProcessorInfoProvider sealed
        {
        public:
           bool IsNeonSupported();
        };
    }

  6. Open the source file (ProcessorInfoComponent.cpp) and add the method implementation to our WinPRT component which will call IsProcessorFeaturePresent and return the result.
    The IsNeonSupported wrapper method here will provide the information if the processor supports the ARM VFP/Neon: 32 x 64bit register bank. There are many other queryable parameters available beside PF_ARM_VFP_32_REGISTERS_AVAILABLE used here. Those are defined in winnt.h and partly documented at the MSDN.
    #include "ProcessorInfoComponent.h"
    using namespace ProcessorInfoComponent;
    
    bool ProcessorInfoProvider::IsNeonSupported()
    {
       return IsProcessorFeaturePresent(PF_ARM_VFP_32_REGISTERS_AVAILABLE);
    }

  7. You are now ready to use the component in your managed C# code.
    private void Button_Click(object sender, RoutedEventArgs e)
    {
       var infoProvider = new ProcessorInfoComponent.ProcessorInfoProvider();
       var hasNeon = infoProvider.IsNeonSupported();
    
       Info.Text = string.Format("Is ARM Neon supported: {0}", hasNeon);
    }
The complete sample solution can be downloaded here.
If you run the sample in the emulator you will see IsNeonSupported() returns false, since it's running on the x86 CPU, but if you execute the sample on a device IsNeonSupported() should return true. Keep in mind you are now using native code so you have to compile a build for ARM (device) or x86 (emulator). AnyCPU won't work anymore.

The presented is just a very easy sample but it already shows how native code on Windows Phone can help you to access information not available otherwise.