├── Microsoft.UI.Composition.Toolkit ├── packages.config ├── pch.cpp ├── CompositionImageOptions.h ├── Microsoft.UI.Composition.Toolkit.vcxproj.filters ├── CompositionImageFactory.h ├── CompositionImageOptions.cpp ├── pch.h ├── WICBitmapSource.h ├── CompositionGraphicsDevice.h ├── Microsoft.UI.Composition.Toolkit.sln ├── CompositionImageFactory.cpp ├── CompositionImage.h ├── WICBitmapSource.cpp ├── CompositionGraphicsDevice.cpp ├── Microsoft.UI.Composition.Toolkit.vcxproj └── CompositionImage.cpp ├── XamlBrewer.Uwp.Composition.ClockSample ├── Assets │ ├── Silly_Face.png │ ├── Silly_Hour.png │ ├── StoreLogo.png │ ├── hour_gold.png │ ├── hour_hand.png │ ├── roman_face.jpg │ ├── Classic_Face.jpg │ ├── Silly_Front.png │ ├── Silly_Minute.png │ ├── designer_face.jpg │ ├── minute_gold.png │ ├── minute_hand.png │ ├── modern_face.png │ ├── SplashScreen.scale-200.png │ ├── LockScreenLogo.scale-200.png │ ├── Square44x44Logo.scale-200.png │ ├── Wide310x150Logo.scale-200.png │ ├── Square150x150Logo.scale-200.png │ └── Square44x44Logo.targetsize-24_altform-unplated.png ├── App.xaml ├── project.json ├── ViewModels │ ├── ViewModelBase.cs │ ├── ShellViewModel.cs │ └── MenuItem.cs ├── Controls │ ├── UIElementExtensions.cs │ ├── ImageClock.xaml │ ├── SillyWalkerClock.xaml │ ├── Clock.xaml │ ├── SillyWalkerClock.xaml.cs │ ├── ImageClock.xaml.cs │ └── Clock.xaml.cs ├── Views │ ├── SillyPage.xaml │ ├── OtherPage.xaml.cs │ ├── SillyPage.xaml.cs │ ├── MainPage.xaml.cs │ ├── Shell.xaml.cs │ ├── MainPage.xaml │ ├── OtherPage.xaml │ └── Shell.xaml ├── Mvvm │ ├── BooleanToVisibilityConverter.cs │ ├── BindableBase.cs │ └── DelegateCommand.cs ├── Properties │ ├── AssemblyInfo.cs │ └── Default.rd.xml ├── Services │ └── Theme.cs ├── Package.appxmanifest ├── App.xaml.cs └── XamlBrewer.Uwp.Composition.ClockSample.csproj ├── README.md ├── LICENSE ├── .gitattributes ├── XamlBrewer.Uwp.Composition.ClockSample.sln └── .gitignore /Microsoft.UI.Composition.Toolkit/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Assets/Silly_Face.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XamlBrewer/UWP-Composition-API-Clock/HEAD/XamlBrewer.Uwp.Composition.ClockSample/Assets/Silly_Face.png -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Assets/Silly_Hour.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XamlBrewer/UWP-Composition-API-Clock/HEAD/XamlBrewer.Uwp.Composition.ClockSample/Assets/Silly_Hour.png -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Assets/StoreLogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XamlBrewer/UWP-Composition-API-Clock/HEAD/XamlBrewer.Uwp.Composition.ClockSample/Assets/StoreLogo.png -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Assets/hour_gold.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XamlBrewer/UWP-Composition-API-Clock/HEAD/XamlBrewer.Uwp.Composition.ClockSample/Assets/hour_gold.png -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Assets/hour_hand.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XamlBrewer/UWP-Composition-API-Clock/HEAD/XamlBrewer.Uwp.Composition.ClockSample/Assets/hour_hand.png -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Assets/roman_face.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XamlBrewer/UWP-Composition-API-Clock/HEAD/XamlBrewer.Uwp.Composition.ClockSample/Assets/roman_face.jpg -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Assets/Classic_Face.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XamlBrewer/UWP-Composition-API-Clock/HEAD/XamlBrewer.Uwp.Composition.ClockSample/Assets/Classic_Face.jpg -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Assets/Silly_Front.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XamlBrewer/UWP-Composition-API-Clock/HEAD/XamlBrewer.Uwp.Composition.ClockSample/Assets/Silly_Front.png -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Assets/Silly_Minute.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XamlBrewer/UWP-Composition-API-Clock/HEAD/XamlBrewer.Uwp.Composition.ClockSample/Assets/Silly_Minute.png -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Assets/designer_face.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XamlBrewer/UWP-Composition-API-Clock/HEAD/XamlBrewer.Uwp.Composition.ClockSample/Assets/designer_face.jpg -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Assets/minute_gold.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XamlBrewer/UWP-Composition-API-Clock/HEAD/XamlBrewer.Uwp.Composition.ClockSample/Assets/minute_gold.png -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Assets/minute_hand.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XamlBrewer/UWP-Composition-API-Clock/HEAD/XamlBrewer.Uwp.Composition.ClockSample/Assets/minute_hand.png -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Assets/modern_face.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XamlBrewer/UWP-Composition-API-Clock/HEAD/XamlBrewer.Uwp.Composition.ClockSample/Assets/modern_face.png -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Assets/SplashScreen.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XamlBrewer/UWP-Composition-API-Clock/HEAD/XamlBrewer.Uwp.Composition.ClockSample/Assets/SplashScreen.scale-200.png -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Assets/LockScreenLogo.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XamlBrewer/UWP-Composition-API-Clock/HEAD/XamlBrewer.Uwp.Composition.ClockSample/Assets/LockScreenLogo.scale-200.png -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Assets/Square44x44Logo.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XamlBrewer/UWP-Composition-API-Clock/HEAD/XamlBrewer.Uwp.Composition.ClockSample/Assets/Square44x44Logo.scale-200.png -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Assets/Wide310x150Logo.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XamlBrewer/UWP-Composition-API-Clock/HEAD/XamlBrewer.Uwp.Composition.ClockSample/Assets/Wide310x150Logo.scale-200.png -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Assets/Square150x150Logo.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XamlBrewer/UWP-Composition-API-Clock/HEAD/XamlBrewer.Uwp.Composition.ClockSample/Assets/Square150x150Logo.scale-200.png -------------------------------------------------------------------------------- /Microsoft.UI.Composition.Toolkit/pch.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // 3 | // Licensed under the MIT License. See LICENSE.txt in the project root for license information. 4 | 5 | #include "pch.h" 6 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Assets/Square44x44Logo.targetsize-24_altform-unplated.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XamlBrewer/UWP-Composition-API-Clock/HEAD/XamlBrewer.Uwp.Composition.ClockSample/Assets/Square44x44Logo.targetsize-24_altform-unplated.png -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/App.xaml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/project.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0" 4 | }, 5 | "frameworks": { 6 | "uap10.0": {} 7 | }, 8 | "runtimes": { 9 | "win10-arm": {}, 10 | "win10-arm-aot": {}, 11 | "win10-x86": {}, 12 | "win10-x86-aot": {}, 13 | "win10-x64": {}, 14 | "win10-x64-aot": {} 15 | } 16 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UWP-Composition-API-Clock 2 | Demonstrates using the Composition API in a Universal Windows Platform XAML control. 3 | 4 | Requires Visual Studio 2015 update 1, and Windows 10 10586 SDK. 5 | 6 | Contains a copy of Microsoft.UI.Composition.Toolkit that is maintained here: https://github.com/Microsoft/composition. 7 | 8 | A blog post explaining the code is here: https://xamlbrewer.wordpress.com/2016/01/04/using-the-composition-api-in-uwp-apps/. 9 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/ViewModels/ViewModelBase.cs: -------------------------------------------------------------------------------- 1 | using Mvvm; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Collections.ObjectModel; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace Mvvm 10 | { 11 | class ViewModelBase : BindableBase 12 | { 13 | private static ObservableCollection menu = new ObservableCollection(); 14 | 15 | public ViewModelBase() 16 | {} 17 | 18 | public ObservableCollection Menu { 19 | get { return menu; } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Controls/UIElementExtensions.cs: -------------------------------------------------------------------------------- 1 | using Windows.UI.Composition; 2 | using Windows.UI.Xaml; 3 | using Windows.UI.Xaml.Hosting; 4 | 5 | namespace XamlBrewer.Uwp.Controls 6 | { 7 | public static class UIElementExtensions 8 | { 9 | public static ContainerVisual GetVisual(this UIElement element) 10 | { 11 | var hostVisual = ElementCompositionPreview.GetElementVisual(element); 12 | var root = hostVisual.Compositor.CreateContainerVisual(); 13 | ElementCompositionPreview.SetElementChildVisual(element, root); 14 | return root; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Views/SillyPage.xaml: -------------------------------------------------------------------------------- 1 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/ViewModels/ShellViewModel.cs: -------------------------------------------------------------------------------- 1 | using Windows.UI.Xaml.Controls; 2 | using XamlBrewer.Uwp.Composition.ClockSample; 3 | 4 | namespace Mvvm 5 | { 6 | class ShellViewModel : ViewModelBase 7 | { 8 | public ShellViewModel() 9 | { 10 | // Build the menu 11 | Menu.Add(new MenuItem() { Glyph = Symbol.Clock, Text = "Modern", NavigationDestination = typeof(MainPage) }); 12 | Menu.Add(new MenuItem() { Glyph = Symbol.OutlineStar, Text = "Classic", NavigationDestination = typeof(OtherPage) }); 13 | Menu.Add(new MenuItem() { Glyph = Symbol.Emoji, Text = "Silly", NavigationDestination = typeof(SillyPage) }); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Controls/ImageClock.xaml: -------------------------------------------------------------------------------- 1 | 10 | 11 | 12 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Controls/SillyWalkerClock.xaml: -------------------------------------------------------------------------------- 1 | 10 | 11 | 12 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Views/OtherPage.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Runtime.InteropServices.WindowsRuntime; 6 | using Windows.Foundation; 7 | using Windows.Foundation.Collections; 8 | using Windows.UI.Xaml; 9 | using Windows.UI.Xaml.Controls; 10 | using Windows.UI.Xaml.Controls.Primitives; 11 | using Windows.UI.Xaml.Data; 12 | using Windows.UI.Xaml.Input; 13 | using Windows.UI.Xaml.Media; 14 | using Windows.UI.Xaml.Navigation; 15 | 16 | namespace XamlBrewer.Uwp.Composition.ClockSample 17 | { 18 | /// 19 | /// An empty page that can be used on its own or navigated to within a Frame. 20 | /// 21 | public sealed partial class OtherPage : Page 22 | { 23 | public OtherPage() 24 | { 25 | this.InitializeComponent(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Views/SillyPage.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Runtime.InteropServices.WindowsRuntime; 6 | using Windows.Foundation; 7 | using Windows.Foundation.Collections; 8 | using Windows.UI.Xaml; 9 | using Windows.UI.Xaml.Controls; 10 | using Windows.UI.Xaml.Controls.Primitives; 11 | using Windows.UI.Xaml.Data; 12 | using Windows.UI.Xaml.Input; 13 | using Windows.UI.Xaml.Media; 14 | using Windows.UI.Xaml.Navigation; 15 | 16 | namespace XamlBrewer.Uwp.Composition.ClockSample 17 | { 18 | /// 19 | /// An empty page that can be used on its own or navigated to within a Frame. 20 | /// 21 | public sealed partial class SillyPage : Page 22 | { 23 | public SillyPage() 24 | { 25 | this.InitializeComponent(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Mvvm/BooleanToVisibilityConverter.cs: -------------------------------------------------------------------------------- 1 | namespace Mvvm 2 | { 3 | using System; 4 | using Windows.UI.Xaml; 5 | using Windows.UI.Xaml.Data; 6 | 7 | internal sealed class BooleanToVisibilityConverter : IValueConverter 8 | { 9 | public bool IsReversed { get; set; } 10 | 11 | public object Convert(object value, Type targetType, object parameter, string language) 12 | { 13 | var val = System.Convert.ToBoolean(value); 14 | if (this.IsReversed) 15 | { 16 | val = !val; 17 | } 18 | 19 | if (val) 20 | { 21 | return Visibility.Visible; 22 | } 23 | 24 | return Visibility.Collapsed; 25 | } 26 | 27 | public object ConvertBack(object value, Type targetType, object parameter, string language) 28 | { 29 | throw new NotImplementedException(); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Views/MainPage.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Runtime.InteropServices.WindowsRuntime; 6 | using Windows.Foundation; 7 | using Windows.Foundation.Collections; 8 | using Windows.UI.Xaml; 9 | using Windows.UI.Xaml.Controls; 10 | using Windows.UI.Xaml.Controls.Primitives; 11 | using Windows.UI.Xaml.Data; 12 | using Windows.UI.Xaml.Input; 13 | using Windows.UI.Xaml.Media; 14 | using Windows.UI.Xaml.Navigation; 15 | 16 | // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 17 | 18 | namespace XamlBrewer.Uwp.Composition.ClockSample 19 | { 20 | /// 21 | /// An empty page that can be used on its own or navigated to within a Frame. 22 | /// 23 | public sealed partial class MainPage : Page 24 | { 25 | public MainPage() 26 | { 27 | this.InitializeComponent(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Controls/Clock.xaml: -------------------------------------------------------------------------------- 1 | 10 | 11 | 12 | 16 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Microsoft.UI.Composition.Toolkit/CompositionImageOptions.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // 3 | // Licensed under the MIT License. See LICENSE.txt in the project root for license information. 4 | 5 | #pragma once 6 | 7 | namespace Microsoft { 8 | namespace UI { 9 | namespace Composition { 10 | namespace Toolkit { 11 | 12 | // Wrapper class that can be provided to the CompositionImage creation functions 13 | // to specify the desired size of the decoded bitmap. 14 | [Windows::Foundation::Metadata::WebHostHidden] 15 | public ref class CompositionImageOptions sealed 16 | { 17 | public: 18 | CompositionImageOptions(); 19 | 20 | property int DecodeWidth 21 | { 22 | int get(); 23 | void set(int); 24 | }; 25 | 26 | property int DecodeHeight 27 | { 28 | int get(); 29 | void set(int); 30 | }; 31 | 32 | private: 33 | int _decodeWidth; 34 | int _decodeHeight; 35 | }; 36 | 37 | } // Toolkit 38 | } // Composition 39 | } // UI 40 | } // Microsoft 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Diederik Krols 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/ViewModels/MenuItem.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Windows.Input; 3 | using Windows.UI.Xaml.Controls; 4 | 5 | namespace Mvvm 6 | { 7 | class MenuItem : BindableBase 8 | { 9 | private Symbol glyph; 10 | private string text; 11 | private DelegateCommand command; 12 | private Type navigationDestination; 13 | 14 | public Symbol Glyph 15 | { 16 | get { return glyph; } 17 | set { SetProperty(ref glyph, value); } 18 | } 19 | 20 | public string Text 21 | { 22 | get { return text; } 23 | set { SetProperty(ref text, value); } 24 | } 25 | 26 | public ICommand Command 27 | { 28 | get { return command; } 29 | set { SetProperty(ref command, (DelegateCommand)value); } 30 | } 31 | 32 | public Type NavigationDestination 33 | { 34 | get { return navigationDestination; } 35 | set { SetProperty(ref navigationDestination, value); } 36 | } 37 | 38 | public bool IsNavigation 39 | { 40 | get { return navigationDestination != null; } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("XamlBrewer.Uwp.Composition.ClockSample")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("XamlBrewer.Uwp.Composition.ClockSample")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Version information for an assembly consists of the following four values: 18 | // 19 | // Major Version 20 | // Minor Version 21 | // Build Number 22 | // Revision 23 | // 24 | // You can specify all the values or you can default the Build and Revision Numbers 25 | // by using the '*' as shown below: 26 | // [assembly: AssemblyVersion("1.0.*")] 27 | [assembly: AssemblyVersion("1.0.0.0")] 28 | [assembly: AssemblyFileVersion("1.0.0.0")] 29 | [assembly: ComVisible(false)] -------------------------------------------------------------------------------- /Microsoft.UI.Composition.Toolkit/Microsoft.UI.Composition.Toolkit.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 09d27466-d553-4479-8728-1054288356d9 6 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tga;tiff;tif;png;wav;mfcribbon-ms 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Microsoft.UI.Composition.Toolkit/CompositionImageFactory.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // 3 | // Licensed under the MIT License. See LICENSE.txt in the project root for license information. 4 | 5 | #pragma once 6 | 7 | namespace Microsoft { 8 | namespace UI { 9 | namespace Composition { 10 | namespace Toolkit { 11 | 12 | [Windows::Foundation::Metadata::WebHostHidden] 13 | public ref class CompositionImageFactory sealed 14 | { 15 | public: 16 | static CompositionImageFactory^ CreateCompositionImageFactory( 17 | Compositor^ compositor); 18 | 19 | CompositionImage^ CreateImageFromUri(Uri^ uri); 20 | 21 | CompositionImage^ CreateImageFromUri( 22 | Uri^ uri, 23 | CompositionImageOptions^ options); 24 | 25 | CompositionImage^ CreateImageFromFile(StorageFile^ file); 26 | 27 | CompositionImage^ CreateImageFromFile( 28 | StorageFile^ file, 29 | CompositionImageOptions^ options); 30 | 31 | private: 32 | CompositionImageFactory( 33 | Compositor^ compositor, 34 | CompositionGraphicsDevice^ graphicsDevice); 35 | 36 | private: 37 | CompositionGraphicsDevice^ _graphicsDevice; 38 | Compositor^ _compositor; 39 | }; 40 | 41 | } // Toolkit 42 | } // Composition 43 | } // UI 44 | } // Microsoft 45 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Properties/Default.rd.xml: -------------------------------------------------------------------------------- 1 | 17 | 18 | 19 | 20 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Microsoft.UI.Composition.Toolkit/CompositionImageOptions.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // 3 | // Licensed under the MIT License. See LICENSE.txt in the project root for license information. 4 | 5 | #include "pch.h" 6 | #include "CompositionImageOptions.h" 7 | 8 | namespace Microsoft { 9 | namespace UI { 10 | namespace Composition { 11 | namespace Toolkit { 12 | 13 | CompositionImageOptions::CompositionImageOptions() 14 | { 15 | } 16 | 17 | int CompositionImageOptions::DecodeWidth::get() 18 | { 19 | return _decodeWidth; 20 | } 21 | 22 | void CompositionImageOptions::DecodeWidth::set(int value) 23 | { 24 | if (value < 0) 25 | { 26 | ERR(E_INVALIDARG, L"The DecodeWidth property of CompositionImageOptions cannot be less than 0."); 27 | __abi_ThrowIfFailed(E_INVALIDARG); 28 | } 29 | 30 | _decodeWidth = value; 31 | } 32 | 33 | int CompositionImageOptions::DecodeHeight::get() 34 | { 35 | return _decodeHeight; 36 | } 37 | 38 | void CompositionImageOptions::DecodeHeight::set(int value) 39 | { 40 | if (value < 0) 41 | { 42 | ERR(E_INVALIDARG, L"The DecodeHeight property of CompositionImageOptions cannot be less than 0."); 43 | __abi_ThrowIfFailed(E_INVALIDARG); 44 | } 45 | 46 | _decodeHeight = value; 47 | } 48 | 49 | } // namespace Toolkit 50 | } // namespace Composition 51 | } // namespace UI 52 | } // namespace Microsoft -------------------------------------------------------------------------------- /Microsoft.UI.Composition.Toolkit/pch.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // 3 | // Licensed under the MIT License. See LICENSE.txt in the project root for license information. 4 | 5 | #pragma once 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | using namespace concurrency; 21 | using namespace Microsoft::WRL; 22 | using namespace Platform; 23 | using namespace Windows::Storage; 24 | using namespace Windows::Storage::Streams; 25 | using namespace Windows::Graphics::DirectX; 26 | using namespace Windows::UI::Composition; 27 | using namespace Windows::Foundation; 28 | 29 | // Helper to goto a Cleanup label if the provided HRESULT value fails. 30 | #define IFC(expr) {hr = expr; if (FAILED(hr)) goto Cleanup;} 31 | 32 | // Logs an error message and HRESULT using RoOriginateError. 33 | inline void ERR(HRESULT hr, LPCWSTR msg) 34 | { 35 | RoOriginateError(hr, StringReference(msg).GetHSTRING()); 36 | } 37 | 38 | // Raises a fail fast exception if the provided HRESULT value fails. 39 | inline void FAILHARD(HRESULT hr) 40 | { 41 | if (FAILED(hr)) 42 | { 43 | __debugbreak(); 44 | ::RaiseFailFastException(nullptr, nullptr, 0); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Services/Theme.cs: -------------------------------------------------------------------------------- 1 | using Windows.Foundation.Metadata; 2 | using Windows.UI; 3 | using Windows.UI.ViewManagement; 4 | 5 | namespace Mvvm.Services 6 | { 7 | public class Theme 8 | { 9 | // Call this in App OnLaunched. 10 | // Requires references to Windows Desktop and Windows Mobile Extensions for the UWP. 11 | public static void ApplyToContainer() 12 | { 13 | //PC customization 14 | if (ApiInformation.IsTypePresent("Windows.UI.ViewManagement.ApplicationView")) 15 | { 16 | var titleBar = ApplicationView.GetForCurrentView().TitleBar; 17 | if (titleBar != null) 18 | { 19 | titleBar.ButtonBackgroundColor = Colors.Red; 20 | titleBar.ButtonForegroundColor = Colors.White; 21 | titleBar.BackgroundColor = Colors.Red; 22 | titleBar.ForegroundColor = Colors.White; 23 | } 24 | } 25 | 26 | //Mobile customization 27 | if (ApiInformation.IsTypePresent("Windows.UI.ViewManagement.StatusBar")) 28 | { 29 | var statusBar = StatusBar.GetForCurrentView(); 30 | if (statusBar != null) 31 | { 32 | statusBar.BackgroundOpacity = 1; 33 | statusBar.BackgroundColor = Colors.Blue; 34 | statusBar.ForegroundColor = Colors.White; 35 | } 36 | } 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Package.appxmanifest: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | XAML Brewer UWP Composition Sample 7 | Diederik 8 | Assets\StoreLogo.png 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Microsoft.UI.Composition.Toolkit/WICBitmapSource.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // 3 | // Licensed under the MIT License. See LICENSE.txt in the project root for license information. 4 | 5 | #pragma once 6 | 7 | namespace Microsoft { 8 | namespace UI { 9 | namespace Composition { 10 | namespace Toolkit { 11 | 12 | class WICBitmapSourceWrapper sealed : 13 | public IWICBitmapSource 14 | { 15 | public: 16 | STDMETHOD_(ULONG, AddRef)(void); 17 | STDMETHOD_(ULONG, Release)(void); 18 | STDMETHOD(QueryInterface)(REFIID riid, void** object); 19 | 20 | private: 21 | WICBitmapSourceWrapper( 22 | __in IWICBitmapSource* bitmapSource, 23 | __in IWICBitmapSourceTransform* bitmapSourceTransform, 24 | UINT width, 25 | UINT height, 26 | REFWICPixelFormatGUID pixelFormat); 27 | 28 | public: 29 | 30 | // IWICBitmapSource 31 | HRESULT STDMETHODCALLTYPE CopyPalette(__in IWICPalette* pPalette); 32 | HRESULT STDMETHODCALLTYPE CopyPixels(__in_opt const WICRect* prc, UINT stride, UINT bufferSize, __out_ecount(bufferSize) BYTE* buffer); 33 | HRESULT STDMETHODCALLTYPE GetPixelFormat(__out WICPixelFormatGUID* pixelFormat); 34 | HRESULT STDMETHODCALLTYPE GetResolution(__out double* dpiX, __out double* dpiY); 35 | HRESULT STDMETHODCALLTYPE GetSize(__out UINT* width, __out UINT* height); 36 | 37 | static bool CreateSourceTransform( 38 | __in IWICBitmapSource* bitmapSource, 39 | UINT desiredWidth, 40 | UINT desiredHeight, 41 | REFWICPixelFormatGUID desiredFormat, 42 | __out IWICBitmapSource** newBitmapSource); 43 | 44 | private: 45 | UINT _width; 46 | UINT _height; 47 | WICPixelFormatGUID _pixelFormat; 48 | 49 | WRL::ComPtr _bitmapSource; 50 | WRL::ComPtr _bitmapSourceTransform; 51 | 52 | volatile LONG _cRef; 53 | }; 54 | } 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Views/Shell.xaml.cs: -------------------------------------------------------------------------------- 1 | using Mvvm; 2 | using System.Linq; 3 | using Windows.UI.Xaml; 4 | using Windows.UI.Xaml.Controls; 5 | using Windows.UI.Xaml.Input; 6 | 7 | namespace XamlBrewer.Uwp.Composition.ClockSample 8 | { 9 | public sealed partial class Shell : Page 10 | { 11 | public Shell() 12 | { 13 | this.InitializeComponent(); 14 | 15 | // Navigate to the first page. 16 | // var type = (DataContext as ShellViewModel).Menu.First().NavigationDestination; 17 | SplitViewFrame.Navigate(typeof(MainPage)); 18 | } 19 | 20 | // Navigate to another page. 21 | private void Menu_SelectionChanged(object sender, SelectionChangedEventArgs e) 22 | { 23 | if (e.AddedItems.Count > 0) 24 | { 25 | var menuItem = e.AddedItems.First() as MenuItem; 26 | if (menuItem.IsNavigation) 27 | { 28 | SplitViewFrame.Navigate(menuItem.NavigationDestination); 29 | } 30 | else 31 | { 32 | menuItem.Command.Execute(null); 33 | } 34 | } 35 | } 36 | 37 | // Swipe to open the splitview panel. 38 | private void SplitViewOpener_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e) 39 | { 40 | if (e.Cumulative.Translation.X > 50) 41 | { 42 | MySplitView.IsPaneOpen = true; 43 | } 44 | } 45 | 46 | // Swipe to close the splitview panel. 47 | private void SplitViewPane_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e) 48 | { 49 | if (e.Cumulative.Translation.X < -50) 50 | { 51 | MySplitView.IsPaneOpen = false; 52 | } 53 | } 54 | 55 | // Open or close the splitview panel through Hamburger button. 56 | private void HamburgerButton_Click(object sender, RoutedEventArgs e) 57 | { 58 | MySplitView.IsPaneOpen = !MySplitView.IsPaneOpen; 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Microsoft.UI.Composition.Toolkit/CompositionGraphicsDevice.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // 3 | // Licensed under the MIT License. See LICENSE.txt in the project root for license information. 4 | 5 | #pragma once 6 | 7 | namespace Microsoft { 8 | namespace UI { 9 | namespace Composition { 10 | namespace Toolkit { 11 | 12 | // Forward declare for CompositionGraphicsDeviceLostEventHandler delegate. 13 | ref class CompositionGraphicsDevice; 14 | 15 | public delegate void CompositionGraphicsDeviceLostEventHandler(CompositionGraphicsDevice^ sender); 16 | 17 | [Windows::Foundation::Metadata::Internal] 18 | [Windows::Foundation::Metadata::WebHostHidden] 19 | public ref class CompositionGraphicsDevice sealed 20 | { 21 | public: 22 | static CompositionGraphicsDevice^ CreateCompositionGraphicsDevice(Compositor^ compositor); 23 | 24 | // Called anytime the underlying D3D11Device is lost allowing the event handler to redraw 25 | // any device specific resources. 26 | event CompositionGraphicsDeviceLostEventHandler^ DeviceLost; 27 | 28 | ICompositionSurface^ CreateDrawingSurface( 29 | Size sizePixels, 30 | DirectXPixelFormat pixelFormat, 31 | DirectXAlphaMode alphaMode); 32 | 33 | void AcquireDrawingLock(); 34 | 35 | void ReleaseDrawingLock(); 36 | 37 | virtual ~CompositionGraphicsDevice(); 38 | 39 | private: 40 | CompositionGraphicsDevice(Compositor^ compositor); 41 | 42 | HRESULT InitializeGraphicsDevice(); 43 | 44 | HRESULT InitializeDX(); 45 | 46 | void UninitializeDX(); 47 | 48 | HRESULT AttachDeviceLostHandler(); 49 | 50 | static void CALLBACK OnDeviceLostCallback(PTP_CALLBACK_INSTANCE, PVOID context, PTP_WAIT, TP_WAIT_RESULT); 51 | 52 | private: 53 | Compositor^ _compositor; 54 | 55 | // Any changes to member variables should be made while holding _stateLock. 56 | ComPtr _graphicsFactoryBackingDXDevice; 57 | ComPtr _igraphicsDevice; 58 | ComPtr _d3dDevice4; 59 | HANDLE _deviceLostEvent; 60 | DWORD _deviceLostRegistrationCookie; 61 | PTP_WAIT _threadPoolWait; 62 | 63 | std::mutex _stateLock; 64 | std::mutex _drawingLock; 65 | }; 66 | 67 | } // Toolkit 68 | } // Composition 69 | } // UI 70 | } // Microsoft 71 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Mvvm/BindableBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel; 3 | using System.Runtime.CompilerServices; 4 | using Windows.UI.Xaml.Data; 5 | 6 | namespace Mvvm 7 | { 8 | /// 9 | /// Implementation of to simplify models. 10 | /// 11 | public abstract class BindableBase : INotifyPropertyChanged 12 | { 13 | /// 14 | /// Multicast event for property change notifications. 15 | /// 16 | public event PropertyChangedEventHandler PropertyChanged; 17 | 18 | /// 19 | /// Checks if a property already matches a desired value. Sets the property and 20 | /// notifies listeners only when necessary. 21 | /// 22 | /// Type of the property. 23 | /// Reference to a property with both getter and setter. 24 | /// Desired value for the property. 25 | /// Name of the property used to notify listeners. This 26 | /// value is optional and can be provided automatically when invoked from compilers that 27 | /// support CallerMemberName. 28 | /// True if the value was changed, false if the existing value matched the 29 | /// desired value. 30 | protected bool SetProperty(ref T storage, T value, [CallerMemberName] String propertyName = null) 31 | { 32 | if (object.Equals(storage, value)) return false; 33 | 34 | storage = value; 35 | this.OnPropertyChanged(propertyName); 36 | return true; 37 | } 38 | 39 | /// 40 | /// Notifies listeners that a property value has changed. 41 | /// 42 | /// Name of the property used to notify listeners. This 43 | /// value is optional and can be provided automatically when invoked from compilers 44 | /// that support . 45 | protected void OnPropertyChanged([CallerMemberName] string propertyName = null) 46 | { 47 | var eventHandler = this.PropertyChanged; 48 | if (eventHandler != null) 49 | { 50 | eventHandler(this, new PropertyChangedEventArgs(propertyName)); 51 | } 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Views/MainPage.xaml: -------------------------------------------------------------------------------- 1 | 9 | 10 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 20 | 21 | 22 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 40 | 42 | 44 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Views/OtherPage.xaml: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 19 | 20 | 21 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 39 | 41 | 43 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Mvvm/DelegateCommand.cs: -------------------------------------------------------------------------------- 1 | namespace Mvvm 2 | { 3 | using System; 4 | using System.Diagnostics; 5 | using System.Diagnostics.CodeAnalysis; 6 | using System.Windows.Input; 7 | using Windows.UI.Xaml.Input; 8 | 9 | internal class DelegateCommand : ICommand 10 | { 11 | private readonly Action _execute; 12 | 13 | private readonly Func _canExecute; 14 | 15 | /// 16 | /// Initializes a new instance of the RelayCommand class that 17 | /// can always execute. 18 | /// 19 | /// The execution logic. 20 | /// If the execute argument is null. 21 | public DelegateCommand(Action execute) 22 | : this(execute, null) 23 | { 24 | } 25 | 26 | /// 27 | /// Initializes a new instance of the RelayCommand class. 28 | /// 29 | /// The execution logic. 30 | /// The execution status logic. 31 | /// If the execute argument is null. 32 | public DelegateCommand(Action execute, Func canExecute) 33 | { 34 | if (execute == null) 35 | { 36 | throw new ArgumentNullException("execute"); 37 | } 38 | 39 | _execute = execute; 40 | _canExecute = canExecute; 41 | } 42 | 43 | /// 44 | /// Occurs when changes occur that affect whether the command should execute. 45 | /// 46 | public event EventHandler CanExecuteChanged; 47 | 48 | /// 49 | /// Raises the event. 50 | /// 51 | [SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate", 52 | Justification = "This cannot be an event")] 53 | public void RaiseCanExecuteChanged() 54 | { 55 | var handler = CanExecuteChanged; 56 | if (handler != null) 57 | { 58 | handler(this, EventArgs.Empty); 59 | } 60 | } 61 | 62 | /// 63 | /// Defines the method that determines whether the command can execute in its current state. 64 | /// 65 | /// This parameter will always be ignored. 66 | /// true if this command can be executed; otherwise, false. 67 | [DebuggerStepThrough] 68 | public bool CanExecute(object parameter) 69 | { 70 | return _canExecute == null ? true : _canExecute(); 71 | } 72 | 73 | /// 74 | /// Defines the method to be called when the command is invoked. 75 | /// 76 | /// This parameter will always be ignored. 77 | public void Execute(object parameter) 78 | { 79 | if (CanExecute(parameter)) 80 | { 81 | _execute(); 82 | } 83 | } 84 | } 85 | } 86 | 87 | -------------------------------------------------------------------------------- /Microsoft.UI.Composition.Toolkit/Microsoft.UI.Composition.Toolkit.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.23107.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Microsoft.UI.Composition.Toolkit", "Microsoft.UI.Composition.Toolkit.vcxproj", "{08A7D060-593A-4978-8DCC-B2029ACA7C95}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompositionImageSample", "..\CompositionImageSample\CompositionImageSample.csproj", "{B4BCF99C-78C3-4BB6-8DB1-1B49101892F7}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|ARM = Debug|ARM 13 | Debug|x64 = Debug|x64 14 | Debug|x86 = Debug|x86 15 | Release|ARM = Release|ARM 16 | Release|x64 = Release|x64 17 | Release|x86 = Release|x86 18 | EndGlobalSection 19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 20 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Debug|ARM.ActiveCfg = Debug|ARM 21 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Debug|ARM.Build.0 = Debug|ARM 22 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Debug|x64.ActiveCfg = Debug|x64 23 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Debug|x64.Build.0 = Debug|x64 24 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Debug|x86.ActiveCfg = Debug|Win32 25 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Debug|x86.Build.0 = Debug|Win32 26 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Release|ARM.ActiveCfg = Release|ARM 27 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Release|ARM.Build.0 = Release|ARM 28 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Release|x64.ActiveCfg = Release|x64 29 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Release|x64.Build.0 = Release|x64 30 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Release|x86.ActiveCfg = Release|Win32 31 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Release|x86.Build.0 = Release|Win32 32 | {B4BCF99C-78C3-4BB6-8DB1-1B49101892F7}.Debug|ARM.ActiveCfg = Debug|ARM 33 | {B4BCF99C-78C3-4BB6-8DB1-1B49101892F7}.Debug|ARM.Build.0 = Debug|ARM 34 | {B4BCF99C-78C3-4BB6-8DB1-1B49101892F7}.Debug|ARM.Deploy.0 = Debug|ARM 35 | {B4BCF99C-78C3-4BB6-8DB1-1B49101892F7}.Debug|x64.ActiveCfg = Debug|x64 36 | {B4BCF99C-78C3-4BB6-8DB1-1B49101892F7}.Debug|x64.Build.0 = Debug|x64 37 | {B4BCF99C-78C3-4BB6-8DB1-1B49101892F7}.Debug|x64.Deploy.0 = Debug|x64 38 | {B4BCF99C-78C3-4BB6-8DB1-1B49101892F7}.Debug|x86.ActiveCfg = Debug|x86 39 | {B4BCF99C-78C3-4BB6-8DB1-1B49101892F7}.Debug|x86.Build.0 = Debug|x86 40 | {B4BCF99C-78C3-4BB6-8DB1-1B49101892F7}.Debug|x86.Deploy.0 = Debug|x86 41 | {B4BCF99C-78C3-4BB6-8DB1-1B49101892F7}.Release|ARM.ActiveCfg = Release|ARM 42 | {B4BCF99C-78C3-4BB6-8DB1-1B49101892F7}.Release|ARM.Build.0 = Release|ARM 43 | {B4BCF99C-78C3-4BB6-8DB1-1B49101892F7}.Release|ARM.Deploy.0 = Release|ARM 44 | {B4BCF99C-78C3-4BB6-8DB1-1B49101892F7}.Release|x64.ActiveCfg = Release|x64 45 | {B4BCF99C-78C3-4BB6-8DB1-1B49101892F7}.Release|x64.Build.0 = Release|x64 46 | {B4BCF99C-78C3-4BB6-8DB1-1B49101892F7}.Release|x64.Deploy.0 = Release|x64 47 | {B4BCF99C-78C3-4BB6-8DB1-1B49101892F7}.Release|x86.ActiveCfg = Release|x86 48 | {B4BCF99C-78C3-4BB6-8DB1-1B49101892F7}.Release|x86.Build.0 = Release|x86 49 | {B4BCF99C-78C3-4BB6-8DB1-1B49101892F7}.Release|x86.Deploy.0 = Release|x86 50 | EndGlobalSection 51 | GlobalSection(SolutionProperties) = preSolution 52 | HideSolutionNode = FALSE 53 | EndGlobalSection 54 | EndGlobal 55 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.24720.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XamlBrewer.Uwp.Composition.ClockSample", "XamlBrewer.Uwp.Composition.ClockSample\XamlBrewer.Uwp.Composition.ClockSample.csproj", "{382B3EF2-2A20-420D-BFA5-9E3B34515242}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Microsoft.UI.Composition.Toolkit", "Microsoft.UI.Composition.Toolkit\Microsoft.UI.Composition.Toolkit.vcxproj", "{08A7D060-593A-4978-8DCC-B2029ACA7C95}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|ARM = Debug|ARM 13 | Debug|x64 = Debug|x64 14 | Debug|x86 = Debug|x86 15 | Release|ARM = Release|ARM 16 | Release|x64 = Release|x64 17 | Release|x86 = Release|x86 18 | EndGlobalSection 19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 20 | {382B3EF2-2A20-420D-BFA5-9E3B34515242}.Debug|ARM.ActiveCfg = Debug|ARM 21 | {382B3EF2-2A20-420D-BFA5-9E3B34515242}.Debug|ARM.Build.0 = Debug|ARM 22 | {382B3EF2-2A20-420D-BFA5-9E3B34515242}.Debug|ARM.Deploy.0 = Debug|ARM 23 | {382B3EF2-2A20-420D-BFA5-9E3B34515242}.Debug|x64.ActiveCfg = Debug|x64 24 | {382B3EF2-2A20-420D-BFA5-9E3B34515242}.Debug|x64.Build.0 = Debug|x64 25 | {382B3EF2-2A20-420D-BFA5-9E3B34515242}.Debug|x64.Deploy.0 = Debug|x64 26 | {382B3EF2-2A20-420D-BFA5-9E3B34515242}.Debug|x86.ActiveCfg = Debug|x86 27 | {382B3EF2-2A20-420D-BFA5-9E3B34515242}.Debug|x86.Build.0 = Debug|x86 28 | {382B3EF2-2A20-420D-BFA5-9E3B34515242}.Debug|x86.Deploy.0 = Debug|x86 29 | {382B3EF2-2A20-420D-BFA5-9E3B34515242}.Release|ARM.ActiveCfg = Release|ARM 30 | {382B3EF2-2A20-420D-BFA5-9E3B34515242}.Release|ARM.Build.0 = Release|ARM 31 | {382B3EF2-2A20-420D-BFA5-9E3B34515242}.Release|ARM.Deploy.0 = Release|ARM 32 | {382B3EF2-2A20-420D-BFA5-9E3B34515242}.Release|x64.ActiveCfg = Release|x64 33 | {382B3EF2-2A20-420D-BFA5-9E3B34515242}.Release|x64.Build.0 = Release|x64 34 | {382B3EF2-2A20-420D-BFA5-9E3B34515242}.Release|x64.Deploy.0 = Release|x64 35 | {382B3EF2-2A20-420D-BFA5-9E3B34515242}.Release|x86.ActiveCfg = Release|x86 36 | {382B3EF2-2A20-420D-BFA5-9E3B34515242}.Release|x86.Build.0 = Release|x86 37 | {382B3EF2-2A20-420D-BFA5-9E3B34515242}.Release|x86.Deploy.0 = Release|x86 38 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Debug|ARM.ActiveCfg = Debug|ARM 39 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Debug|ARM.Build.0 = Debug|ARM 40 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Debug|x64.ActiveCfg = Debug|x64 41 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Debug|x64.Build.0 = Debug|x64 42 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Debug|x86.ActiveCfg = Debug|Win32 43 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Debug|x86.Build.0 = Debug|Win32 44 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Release|ARM.ActiveCfg = Release|ARM 45 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Release|ARM.Build.0 = Release|ARM 46 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Release|x64.ActiveCfg = Release|x64 47 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Release|x64.Build.0 = Release|x64 48 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Release|x86.ActiveCfg = Release|Win32 49 | {08A7D060-593A-4978-8DCC-B2029ACA7C95}.Release|x86.Build.0 = Release|Win32 50 | EndGlobalSection 51 | GlobalSection(SolutionProperties) = preSolution 52 | HideSolutionNode = FALSE 53 | EndGlobalSection 54 | EndGlobal 55 | -------------------------------------------------------------------------------- /Microsoft.UI.Composition.Toolkit/CompositionImageFactory.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // 3 | // Licensed under the MIT License. See LICENSE.txt in the project root for license information. 4 | 5 | #include "pch.h" 6 | #include "CompositionGraphicsDevice.h" 7 | #include "CompositionImageOptions.h" 8 | #include "CompositionImage.h" 9 | #include "CompositionImageFactory.h" 10 | 11 | namespace Microsoft { 12 | namespace UI { 13 | namespace Composition { 14 | namespace Toolkit { 15 | 16 | // Returns a CompositionImageFactory that can be used to create CompositionImages 17 | // associated with a particular Compositor instance. A reference should be kept to 18 | // the returned factory for the lifetime of the composition images created by the 19 | // factory. 20 | CompositionImageFactory^ CompositionImageFactory::CreateCompositionImageFactory(Compositor^ compositor) 21 | { 22 | if (compositor == nullptr) 23 | { 24 | __abi_ThrowIfFailed(E_INVALIDARG); 25 | } 26 | 27 | CompositionGraphicsDevice^ device = CompositionGraphicsDevice::CreateCompositionGraphicsDevice(compositor); 28 | return ref new CompositionImageFactory(compositor, device); 29 | } 30 | 31 | CompositionImageFactory::CompositionImageFactory(Compositor^ compositor, CompositionGraphicsDevice^ graphicsDevice) : 32 | _compositor(compositor), 33 | _graphicsDevice(graphicsDevice) 34 | { 35 | } 36 | 37 | // Creates a CompositionImage given a Uri that represents a packaged resource (ms-appx:///) 38 | // a application data resource (ms-appdata:///) or an http(s) resource (http:// or https://) 39 | // Does not support creating a CompositionImage from a Uri with the file scheme, instead 40 | // a StorageFile object should be passed as an argument to CreateImageFromFile. 41 | CompositionImage^ CompositionImageFactory::CreateImageFromUri(Windows::Foundation::Uri^ uri) 42 | { 43 | CompositionImageOptions^ options = ref new CompositionImageOptions(); 44 | options->DecodeHeight = 0; 45 | options->DecodeWidth = 0; 46 | 47 | return CreateImageFromUri(uri, options); 48 | } 49 | 50 | // Functions similarly to CreateImageFromUri with the exception that any options provided 51 | // by the CompositionImageOptions are used to initialize the CompositionImage. 52 | CompositionImage^ CompositionImageFactory::CreateImageFromUri(Windows::Foundation::Uri^ uri, CompositionImageOptions^ options) 53 | { 54 | SIZE szDecode = { 0, 0 }; 55 | if (options) 56 | { 57 | szDecode.cx = (ULONG)options->DecodeWidth; 58 | szDecode.cy = (ULONG)options->DecodeHeight; 59 | } 60 | 61 | return CompositionImage::CreateCompositionImage( 62 | _compositor, 63 | _graphicsDevice, 64 | uri, 65 | nullptr, 66 | options); 67 | } 68 | 69 | // Creates a CompositionImage given a StorageFile. 70 | CompositionImage^ CompositionImageFactory::CreateImageFromFile(StorageFile^ file) 71 | { 72 | CompositionImageOptions^ options = ref new CompositionImageOptions(); 73 | options->DecodeHeight = 0; 74 | options->DecodeWidth = 0; 75 | 76 | return CreateImageFromFile(file, options); 77 | } 78 | 79 | // Functions similarly to CreateImageFromFile with the exception that any options provided 80 | // by the CompositionImageOptions are used to initialize the CompositionImage. 81 | CompositionImage^ CompositionImageFactory::CreateImageFromFile(StorageFile^ file, CompositionImageOptions^ options) 82 | { 83 | SIZE szDecode = { 0, 0 }; 84 | if (options) 85 | { 86 | szDecode.cx = (ULONG)options->DecodeWidth; 87 | szDecode.cy = (ULONG)options->DecodeHeight; 88 | } 89 | 90 | return CompositionImage::CreateCompositionImage( 91 | _compositor, 92 | _graphicsDevice, 93 | nullptr, 94 | file, 95 | options); 96 | } 97 | 98 | } // namespace Toolkit 99 | } // namespace Composition 100 | } // namespace UI 101 | } // namespace Microsoft -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Controls/SillyWalkerClock.xaml.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.UI.Composition.Toolkit; 2 | using System; 3 | using System.Numerics; 4 | using Windows.UI.Composition; 5 | using Windows.UI.Xaml; 6 | using Windows.UI.Xaml.Controls; 7 | using Windows.UI.Xaml.Hosting; 8 | 9 | namespace XamlBrewer.Uwp.Controls 10 | { 11 | public sealed partial class SillyWalkerClock : UserControl 12 | { 13 | private Compositor _compositor; 14 | private ContainerVisual _root; 15 | 16 | private SpriteVisual _background; 17 | private SpriteVisual _hourhand; 18 | private SpriteVisual _minutehand; 19 | private SpriteVisual _foreground; 20 | 21 | private DispatcherTimer _timer = new DispatcherTimer(); 22 | 23 | public SillyWalkerClock() 24 | { 25 | this.InitializeComponent(); 26 | 27 | this.Loaded += Clock_Loaded; 28 | 29 | _timer.Interval = TimeSpan.FromSeconds(1); 30 | _timer.Tick += Timer_Tick; 31 | } 32 | 33 | public Uri FaceImage { get; set; } = new Uri("ms-appx:///Assets/Silly_Face.png"); 34 | public Uri HourHandImage { get; set; } = new Uri("ms-appx:///Assets/Silly_Hour.png"); 35 | public Uri MinuteHandImage { get; set; } = new Uri("ms-appx:///Assets/Silly_Minute.png"); 36 | public Uri FrontImage { get; set; } = new Uri("ms-appx:///Assets/Silly_Front.png"); 37 | 38 | private void Clock_Loaded(object sender, RoutedEventArgs e) 39 | { 40 | _root = Container.GetVisual(); 41 | _compositor = _root.Compositor; 42 | 43 | // Background 44 | _background = _compositor.CreateSpriteVisual(); 45 | _background.Size = new Vector2(800.0f, 800.0f); 46 | var _imageFactory = CompositionImageFactory.CreateCompositionImageFactory(_compositor); 47 | var options = new CompositionImageOptions() 48 | { 49 | DecodeWidth = 740, 50 | DecodeHeight = 740 51 | }; 52 | var _image = _imageFactory.CreateImageFromUri(FaceImage, options); 53 | _background.Brush = _compositor.CreateSurfaceBrush(_image.Surface); 54 | _root.Children.InsertAtTop(_background); 55 | 56 | // Hour Hand 57 | _hourhand = _compositor.CreateSpriteVisual(); 58 | _hourhand.Size = new Vector2(800.0f, 800.0f); 59 | _image = _imageFactory.CreateImageFromUri(HourHandImage, options); 60 | _hourhand.Brush = _compositor.CreateSurfaceBrush(_image.Surface); 61 | _hourhand.Offset = new Vector3(0.0f, 0.0f, 0); 62 | _hourhand.CenterPoint = new Vector3(400.0f, 400.0f, 0); 63 | _root.Children.InsertAtTop(_hourhand); 64 | 65 | // Minute Hand 66 | _minutehand = _compositor.CreateSpriteVisual(); 67 | _minutehand.Size = new Vector2(800.0f, 800.0f); 68 | _image = _imageFactory.CreateImageFromUri(MinuteHandImage, options); 69 | _minutehand.Brush = _compositor.CreateSurfaceBrush(_image.Surface); 70 | _minutehand.Offset = new Vector3(0.0f, 0.0f, 0); 71 | _minutehand.CenterPoint = new Vector3(400.0f, 400.0f, 0); 72 | _root.Children.InsertAtTop(_minutehand); 73 | 74 | // Foreground 75 | _foreground = _compositor.CreateSpriteVisual(); 76 | _foreground.Size = new Vector2(800.0f, 800.0f); 77 | _image = _imageFactory.CreateImageFromUri(FrontImage, options); 78 | _foreground.Brush = _compositor.CreateSurfaceBrush(_image.Surface); 79 | _root.Children.InsertAtTop(_foreground); 80 | 81 | SetHoursAndMinutes(); 82 | 83 | _timer.Start(); 84 | } 85 | 86 | private void Timer_Tick(object sender, object e) 87 | { 88 | SetHoursAndMinutes(); 89 | } 90 | 91 | private void SetHoursAndMinutes() 92 | { 93 | var now = DateTime.Now; 94 | _hourhand.RotationAngleInDegrees = (float)now.TimeOfDay.TotalHours * 30; 95 | _minutehand.RotationAngleInDegrees = now.Minute * 6; 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | build/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | 28 | # MSTest test Results 29 | [Tt]est[Rr]esult*/ 30 | [Bb]uild[Ll]og.* 31 | 32 | # NUNIT 33 | *.VisualState.xml 34 | TestResult.xml 35 | 36 | # Build Results of an ATL Project 37 | [Dd]ebugPS/ 38 | [Rr]eleasePS/ 39 | dlldata.c 40 | 41 | # DNX 42 | project.lock.json 43 | artifacts/ 44 | 45 | *_i.c 46 | *_p.c 47 | *_i.h 48 | *.ilk 49 | *.meta 50 | *.obj 51 | *.pch 52 | *.pdb 53 | *.pgc 54 | *.pgd 55 | *.rsp 56 | *.sbr 57 | *.tlb 58 | *.tli 59 | *.tlh 60 | *.tmp 61 | *.tmp_proj 62 | *.log 63 | *.vspscc 64 | *.vssscc 65 | .builds 66 | *.pidb 67 | *.svclog 68 | *.scc 69 | 70 | # Chutzpah Test files 71 | _Chutzpah* 72 | 73 | # Visual C++ cache files 74 | ipch/ 75 | *.aps 76 | *.ncb 77 | *.opensdf 78 | *.sdf 79 | *.cachefile 80 | 81 | # Visual Studio profiler 82 | *.psess 83 | *.vsp 84 | *.vspx 85 | 86 | # TFS 2012 Local Workspace 87 | $tf/ 88 | 89 | # Guidance Automation Toolkit 90 | *.gpState 91 | 92 | # ReSharper is a .NET coding add-in 93 | _ReSharper*/ 94 | *.[Rr]e[Ss]harper 95 | *.DotSettings.user 96 | 97 | # JustCode is a .NET coding add-in 98 | .JustCode 99 | 100 | # TeamCity is a build add-in 101 | _TeamCity* 102 | 103 | # DotCover is a Code Coverage Tool 104 | *.dotCover 105 | 106 | # NCrunch 107 | _NCrunch_* 108 | .*crunch*.local.xml 109 | 110 | # MightyMoose 111 | *.mm.* 112 | AutoTest.Net/ 113 | 114 | # Web workbench (sass) 115 | .sass-cache/ 116 | 117 | # Installshield output folder 118 | [Ee]xpress/ 119 | 120 | # DocProject is a documentation generator add-in 121 | DocProject/buildhelp/ 122 | DocProject/Help/*.HxT 123 | DocProject/Help/*.HxC 124 | DocProject/Help/*.hhc 125 | DocProject/Help/*.hhk 126 | DocProject/Help/*.hhp 127 | DocProject/Help/Html2 128 | DocProject/Help/html 129 | 130 | # Click-Once directory 131 | publish/ 132 | 133 | # Publish Web Output 134 | *.[Pp]ublish.xml 135 | *.azurePubxml 136 | ## TODO: Comment the next line if you want to checkin your 137 | ## web deploy settings but do note that will include unencrypted 138 | ## passwords 139 | #*.pubxml 140 | 141 | *.publishproj 142 | 143 | # NuGet Packages 144 | *.nupkg 145 | # The packages folder can be ignored because of Package Restore 146 | **/packages/* 147 | # except build/, which is used as an MSBuild target. 148 | !**/packages/build/ 149 | # Uncomment if necessary however generally it will be regenerated when needed 150 | #!**/packages/repositories.config 151 | 152 | # Windows Azure Build Output 153 | csx/ 154 | *.build.csdef 155 | 156 | # Windows Store app package directory 157 | AppPackages/ 158 | 159 | # Visual Studio cache files 160 | # files ending in .cache can be ignored 161 | *.[Cc]ache 162 | # but keep track of directories ending in .cache 163 | !*.[Cc]ache/ 164 | 165 | # Others 166 | ClientBin/ 167 | [Ss]tyle[Cc]op.* 168 | ~$* 169 | *~ 170 | *.dbmdl 171 | *.dbproj.schemaview 172 | *.pfx 173 | *.publishsettings 174 | node_modules/ 175 | orleans.codegen.cs 176 | 177 | # RIA/Silverlight projects 178 | Generated_Code/ 179 | 180 | # Backup & report files from converting an old project file 181 | # to a newer Visual Studio version. Backup files are not needed, 182 | # because we have git ;-) 183 | _UpgradeReport_Files/ 184 | Backup*/ 185 | UpgradeLog*.XML 186 | UpgradeLog*.htm 187 | 188 | # SQL Server files 189 | *.mdf 190 | *.ldf 191 | 192 | # Business Intelligence projects 193 | *.rdl.data 194 | *.bim.layout 195 | *.bim_*.settings 196 | 197 | # Microsoft Fakes 198 | FakesAssemblies/ 199 | 200 | # Node.js Tools for Visual Studio 201 | .ntvs_analysis.dat 202 | 203 | # Visual Studio 6 build log 204 | *.plg 205 | 206 | # Visual Studio 6 workspace options file 207 | *.opt 208 | 209 | # LightSwitch generated files 210 | GeneratedArtifacts/ 211 | _Pvt_Extensions/ 212 | ModelManifest.xml 213 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using Mvvm.Services; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.IO; 5 | using System.Linq; 6 | using System.Runtime.InteropServices.WindowsRuntime; 7 | using Windows.ApplicationModel; 8 | using Windows.ApplicationModel.Activation; 9 | using Windows.Foundation; 10 | using Windows.Foundation.Collections; 11 | using Windows.UI.Xaml; 12 | using Windows.UI.Xaml.Controls; 13 | using Windows.UI.Xaml.Controls.Primitives; 14 | using Windows.UI.Xaml.Data; 15 | using Windows.UI.Xaml.Input; 16 | using Windows.UI.Xaml.Media; 17 | using Windows.UI.Xaml.Navigation; 18 | 19 | namespace XamlBrewer.Uwp.Composition.ClockSample 20 | { 21 | /// 22 | /// Provides application-specific behavior to supplement the default Application class. 23 | /// 24 | sealed partial class App : Application 25 | { 26 | /// 27 | /// Initializes the singleton application object. This is the first line of authored code 28 | /// executed, and as such is the logical equivalent of main() or WinMain(). 29 | /// 30 | public App() 31 | { 32 | this.InitializeComponent(); 33 | this.Suspending += OnSuspending; 34 | } 35 | 36 | /// 37 | /// Invoked when the application is launched normally by the end user. Other entry points 38 | /// will be used such as when the application is launched to open a specific file. 39 | /// 40 | /// Details about the launch request and process. 41 | protected override void OnLaunched(LaunchActivatedEventArgs e) 42 | { 43 | 44 | #if DEBUG 45 | //if (System.Diagnostics.Debugger.IsAttached) 46 | //{ 47 | // this.DebugSettings.EnableFrameRateCounter = true; 48 | //} 49 | #endif 50 | 51 | Frame rootFrame = Window.Current.Content as Frame; 52 | 53 | // Do not repeat app initialization when the Window already has content, 54 | // just ensure that the window is active 55 | if (rootFrame == null) 56 | { 57 | // Create a Frame to act as the navigation context and navigate to the first page 58 | rootFrame = new Frame(); 59 | 60 | rootFrame.NavigationFailed += OnNavigationFailed; 61 | 62 | if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) 63 | { 64 | //TODO: Load state from previously suspended application 65 | } 66 | 67 | // Place the frame in the current Window 68 | Window.Current.Content = rootFrame; 69 | } 70 | 71 | if (rootFrame.Content == null) 72 | { 73 | // When the navigation stack isn't restored navigate to the first page, 74 | // configuring the new page by passing required information as a navigation 75 | // parameter 76 | rootFrame.Navigate(typeof(Shell), e.Arguments); 77 | } 78 | 79 | Theme.ApplyToContainer(); 80 | 81 | // Ensure the current window is active 82 | Window.Current.Activate(); 83 | } 84 | 85 | /// 86 | /// Invoked when Navigation to a certain page fails 87 | /// 88 | /// The Frame which failed navigation 89 | /// Details about the navigation failure 90 | void OnNavigationFailed(object sender, NavigationFailedEventArgs e) 91 | { 92 | throw new Exception("Failed to load Page " + e.SourcePageType.FullName); 93 | } 94 | 95 | /// 96 | /// Invoked when application execution is being suspended. Application state is saved 97 | /// without knowing whether the application will be terminated or resumed with the contents 98 | /// of memory still intact. 99 | /// 100 | /// The source of the suspend request. 101 | /// Details about the suspend request. 102 | private void OnSuspending(object sender, SuspendingEventArgs e) 103 | { 104 | var deferral = e.SuspendingOperation.GetDeferral(); 105 | //TODO: Save application state and stop any background activity 106 | deferral.Complete(); 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /Microsoft.UI.Composition.Toolkit/CompositionImage.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // 3 | // Licensed under the MIT License. See LICENSE.txt in the project root for license information. 4 | 5 | #pragma once 6 | 7 | namespace Microsoft { 8 | namespace UI { 9 | namespace Composition { 10 | namespace Toolkit { 11 | 12 | [Windows::Foundation::Metadata::WebHostHidden] 13 | public enum class CompositionImageLoadStatus 14 | { 15 | Success, 16 | FileAccessError, 17 | DecodeError, 18 | NotEnoughResources, 19 | Other 20 | }; 21 | 22 | // Forward declare for CompositionImageLoadedEventHandler delegate. 23 | ref class CompositionImage; 24 | 25 | public delegate void CompositionImageLoadedEventHandler( 26 | CompositionImage^ sender, 27 | CompositionImageLoadStatus status); 28 | 29 | [Windows::Foundation::Metadata::WebHostHidden] 30 | public ref class CompositionImage sealed 31 | { 32 | // Friend CompositionImageFactory so that CreateCompositionImage does not have to 33 | // be exposed. 34 | friend ref class CompositionImageFactory; 35 | 36 | public: 37 | // Called anytime the bitmap associated with this CompositionImage's Surface is drawn, 38 | // or an error occurs during loading the image. Check the CompositionImageLoadStatus 39 | // for details regarding the success or failure to load and draw the image. 40 | // 41 | // Note that this event makes no guarantees as to what thread the associated CompositionImageLoadedEventHandler 42 | // delegate will be invoked on. If there is a specific thread that the contents of the delegate should be 43 | // executed on, the delegate should implementation should handle that. 44 | event CompositionImageLoadedEventHandler^ ImageLoaded; 45 | 46 | // Gets the actual size of the decoded bitmap. 47 | property Windows::Foundation::Size Size 48 | { 49 | Windows::Foundation::Size get() 50 | { 51 | return Windows::Foundation::Size( 52 | static_cast(_sizeBitmapSource.cx), 53 | static_cast(_sizeBitmapSource.cy)); 54 | } 55 | }; 56 | 57 | // Gets the ICompositionSurface that backs this CompositionImage. 58 | // When the CompositionImage is first creates, this will be a 0 x 0 Surface 59 | // (or the size specified by any CompositionImageOptions provided at the time of creation) 60 | // and the Surface will be resized and drawn to once the bitmap has been loaded 61 | // and drawn. To be notified when the image has been completely loaded and drawn, 62 | // call CompleteLoadAsync which will return the CompositionImageLoadStatus indicating 63 | // whether the image was successfully loaded or not. 64 | property ICompositionSurface^ Surface 65 | { 66 | ICompositionSurface^ get() { 67 | return reinterpret_cast(_compositionSurface.Get()); 68 | } 69 | } 70 | 71 | private: 72 | static CompositionImage^ CreateCompositionImage( 73 | Compositor^ compositor, 74 | CompositionGraphicsDevice^ graphicsDevice, 75 | Uri^ uri, 76 | StorageFile^ file, 77 | CompositionImageOptions^ options); 78 | 79 | CompositionImage( 80 | Compositor^ compositor, 81 | CompositionGraphicsDevice^ graphicsDevice, 82 | Uri^ uri, 83 | StorageFile^ file, 84 | CompositionImageOptions^ options); 85 | 86 | IAsyncAction^ LoadImageAsync(); 87 | 88 | static IBuffer^ LoadImageFromUri(Uri^ uri); 89 | 90 | static IBuffer^ LoadImageFromFile(StorageFile^ file); 91 | 92 | static ComPtr DecodeBufferIntoBitmap( 93 | IBuffer^ imageRawBuffer, 94 | SIZE sizeDecode); 95 | 96 | HRESULT CompositionImage::DrawBitmapOnSurface(ComPtr& bitmapSource); 97 | 98 | void HandleGraphicsDeviceLost(CompositionGraphicsDevice^ sender); 99 | 100 | void ReportImageLoaded(CompositionImageLoadStatus status); 101 | 102 | private: 103 | Compositor^ _compositor; 104 | CompositionImageOptions^ _imageOptions; 105 | CompositionGraphicsDevice^ _graphicsDevice; 106 | Uri^ _uri; 107 | StorageFile^ _file; 108 | 109 | ComPtr _compositionSurface; 110 | ComPtr _compositionSurfaceInterop; 111 | 112 | IAsyncAction^ _loadAsyncAction; 113 | 114 | SIZE _sizeBitmapSource{}; 115 | SIZE _sizeCompositionSurface{}; 116 | SIZE _sizeDecode{}; 117 | }; 118 | 119 | } // Toolkit 120 | } // Composition 121 | } // UI 122 | } // Microsoft 123 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Controls/ImageClock.xaml.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.UI.Composition.Toolkit; 2 | using System; 3 | using System.Numerics; 4 | using Windows.UI; 5 | using Windows.UI.Composition; 6 | using Windows.UI.Xaml; 7 | using Windows.UI.Xaml.Controls; 8 | using Windows.UI.Xaml.Hosting; 9 | 10 | namespace XamlBrewer.Uwp.Controls 11 | { 12 | public sealed partial class ImageClock : UserControl 13 | { 14 | private Compositor _compositor; 15 | private ContainerVisual _root; 16 | 17 | private SpriteVisual _background; 18 | private SpriteVisual _hourhand; 19 | private SpriteVisual _minutehand; 20 | private SpriteVisual _secondhand; 21 | 22 | private CompositionScopedBatch _batch; 23 | 24 | private DispatcherTimer _timer = new DispatcherTimer(); 25 | 26 | public ImageClock() 27 | { 28 | this.InitializeComponent(); 29 | 30 | this.Loaded += Clock_Loaded; 31 | 32 | _timer.Interval = TimeSpan.FromSeconds(1); 33 | _timer.Tick += Timer_Tick; 34 | } 35 | 36 | public Uri FaceImage { get; set; } = new Uri("ms-appx:///Assets/roman_face.jpg"); 37 | public Uri HourHandImage { get; set; } = new Uri("ms-appx:///Assets/hour_hand.png"); 38 | public Uri MinuteHandImage { get; set; } = new Uri("ms-appx:///Assets/minute_hand.png"); 39 | 40 | private void Clock_Loaded(object sender, RoutedEventArgs e) 41 | { 42 | _root = Container.GetVisual(); 43 | _compositor = _root.Compositor; 44 | 45 | // Background 46 | _background = _compositor.CreateSpriteVisual(); 47 | _background.Size = new Vector2(200.0f, 200.0f); 48 | var _imageFactory = CompositionImageFactory.CreateCompositionImageFactory(_compositor); 49 | CompositionImageOptions options = new CompositionImageOptions() 50 | { 51 | DecodeWidth = 400, 52 | DecodeHeight = 400, 53 | }; 54 | var _image = _imageFactory.CreateImageFromUri(FaceImage, options); 55 | _background.Brush = _compositor.CreateSurfaceBrush(_image.Surface); 56 | _root.Children.InsertAtTop(_background); 57 | 58 | // Hour Hand 59 | options = new CompositionImageOptions() 60 | { 61 | DecodeWidth = 72, 62 | DecodeHeight = 240, 63 | }; 64 | 65 | _hourhand = _compositor.CreateSpriteVisual(); 66 | _hourhand.Size = new Vector2(24.0f, 80.0f); 67 | _image = _imageFactory.CreateImageFromUri(HourHandImage, options); 68 | _hourhand.Brush = _compositor.CreateSurfaceBrush(_image.Surface); 69 | _hourhand.CenterPoint = new Vector3(12.0f, 70.0f, 0); 70 | _hourhand.Offset = new Vector3(88.0f, 30.0f, 0); 71 | _root.Children.InsertAtTop(_hourhand); 72 | 73 | // Minute Hand 74 | options = new CompositionImageOptions() 75 | { 76 | DecodeWidth = 48, 77 | DecodeHeight = 270, 78 | }; 79 | _image = _imageFactory.CreateImageFromUri(MinuteHandImage, options); 80 | _minutehand = _compositor.CreateSpriteVisual(); 81 | _minutehand.Size = new Vector2(16.0f, 90.0f); 82 | _minutehand.Brush = _compositor.CreateSurfaceBrush(_image.Surface); 83 | _minutehand.CenterPoint = new Vector3(8.0f, 85.0f, 0); 84 | _minutehand.Offset = new Vector3(92.0f, 15.0f, 0); 85 | _root.Children.InsertAtTop(_minutehand); 86 | 87 | SetHoursAndMinutes(); 88 | 89 | // Second Hand 90 | _secondhand = _compositor.CreateSpriteVisual(); 91 | _secondhand.Size = new Vector2(1.0f, 90.0f); 92 | _secondhand.Brush = _compositor.CreateColorBrush(Colors.Red); 93 | _secondhand.CenterPoint = new Vector3(0.5f, 90.0f, 0); 94 | _secondhand.Offset = new Vector3(99.5f, 10.0f, 0); 95 | _root.Children.InsertAtTop(_secondhand); 96 | _secondhand.RotationAngleInDegrees = (float)(int)DateTime.Now.TimeOfDay.TotalSeconds * 6; 97 | 98 | _timer.Start(); 99 | } 100 | 101 | private void Timer_Tick(object sender, object e) 102 | { 103 | var now = DateTime.Now; 104 | 105 | _batch = _compositor.CreateScopedBatch(CompositionBatchTypes.Animation); 106 | var animation = _compositor.CreateScalarKeyFrameAnimation(); 107 | var seconds = (float)(int)now.TimeOfDay.TotalSeconds; 108 | animation.InsertKeyFrame(0.00f, seconds * 6); 109 | animation.InsertKeyFrame(1.00f, (seconds + 1) * 6); 110 | animation.Duration = TimeSpan.FromMilliseconds(900); 111 | _secondhand.StartAnimation(nameof(_secondhand.RotationAngleInDegrees), animation); 112 | _batch.End(); 113 | _batch.Completed += Batch_Completed; 114 | } 115 | 116 | private void Batch_Completed(object sender, CompositionBatchCompletedEventArgs args) 117 | { 118 | _batch.Completed -= Batch_Completed; 119 | 120 | SetHoursAndMinutes(); 121 | } 122 | 123 | private void SetHoursAndMinutes() 124 | { 125 | var now = DateTime.Now; 126 | _hourhand.RotationAngleInDegrees = (float)now.TimeOfDay.TotalHours * 30; 127 | _minutehand.RotationAngleInDegrees = now.Minute * 6; 128 | } 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Controls/Clock.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Numerics; 3 | using Windows.UI; 4 | using Windows.UI.Composition; 5 | using Windows.UI.Xaml; 6 | using Windows.UI.Xaml.Controls; 7 | using Windows.UI.Xaml.Hosting; 8 | using Windows.UI.Xaml.Media; 9 | using Windows.UI.Xaml.Media.Imaging; 10 | 11 | namespace XamlBrewer.Uwp.Controls 12 | { 13 | public sealed partial class Clock : UserControl 14 | { 15 | private Compositor _compositor; 16 | private ContainerVisual _root; 17 | 18 | private SpriteVisual _hourhand; 19 | private SpriteVisual _minutehand; 20 | private SpriteVisual _secondhand; 21 | private CompositionScopedBatch _batch; 22 | 23 | private DispatcherTimer _timer = new DispatcherTimer(); 24 | 25 | public Clock() 26 | { 27 | this.InitializeComponent(); 28 | 29 | this.Loaded += Clock_Loaded; 30 | 31 | _timer.Interval = TimeSpan.FromSeconds(1); 32 | _timer.Tick += Timer_Tick; 33 | } 34 | 35 | 36 | public bool ShowTicks { get; set; } = true; 37 | 38 | public Brush FaceColor { get; set; } = new SolidColorBrush(Colors.Transparent); 39 | public ImageSource BackgroundImage { get; set; } 40 | 41 | private void Clock_Loaded(object sender, RoutedEventArgs e) 42 | { 43 | Face.Fill = FaceColor; 44 | 45 | _root = Container.GetVisual(); 46 | _compositor = _root.Compositor; 47 | 48 | // Hour Ticks 49 | if (ShowTicks) 50 | { 51 | SpriteVisual tick; 52 | for (int i = 0; i < 12; i++) 53 | { 54 | tick = _compositor.CreateSpriteVisual(); 55 | tick.Size = new Vector2(4.0f, 20.0f); 56 | tick.Brush = _compositor.CreateColorBrush(Colors.Silver); 57 | tick.Offset = new Vector3(98.0f, 0.0f, 0); 58 | tick.CenterPoint = new Vector3(2.0f, 100.0f, 0); 59 | tick.RotationAngleInDegrees = i * 30; 60 | _root.Children.InsertAtTop(tick); 61 | } 62 | } 63 | 64 | // Second Hand 65 | _secondhand = _compositor.CreateSpriteVisual(); 66 | _secondhand.Size = new Vector2(2.0f, 120.0f); 67 | _secondhand.Brush = _compositor.CreateColorBrush(Colors.Red); 68 | _secondhand.CenterPoint = new Vector3(1.0f, 100.0f, 0); 69 | _secondhand.Offset = new Vector3(99.0f, 0.0f, 0); 70 | _root.Children.InsertAtTop(_secondhand); 71 | _secondhand.RotationAngleInDegrees = (float)(int)DateTime.Now.TimeOfDay.TotalSeconds * 6; 72 | 73 | // Hour Hand 74 | _hourhand = _compositor.CreateSpriteVisual(); 75 | _hourhand.Size = new Vector2(4.0f, 100.0f); 76 | _hourhand.Brush = _compositor.CreateColorBrush(Colors.Black); 77 | _hourhand.CenterPoint = new Vector3(2.0f, 80.0f, 0); 78 | _hourhand.Offset = new Vector3(98.0f, 20.0f, 0); 79 | _root.Children.InsertAtTop(_hourhand); 80 | 81 | // Minute Hand 82 | _minutehand = _compositor.CreateSpriteVisual(); 83 | _minutehand.Size = new Vector2(4.0f, 120.0f); 84 | _minutehand.Brush = _compositor.CreateColorBrush(Colors.Black); 85 | _minutehand.CenterPoint = new Vector3(2.0f, 100.0f, 0); 86 | _minutehand.Offset = new Vector3(98.0f, 0.0f, 0); 87 | _root.Children.InsertAtTop(_minutehand); 88 | 89 | SetHoursAndMinutes(); 90 | 91 | // Add XAML element. 92 | if (BackgroundImage != null) 93 | { 94 | var xaml = new Image(); 95 | xaml.Source = BackgroundImage; 96 | xaml.Height = 200; 97 | xaml.Width = 200; 98 | Container.Children.Add(xaml); 99 | } 100 | 101 | _timer.Start(); 102 | } 103 | 104 | private void Timer_Tick(object sender, object e) 105 | { 106 | var now = DateTime.Now; 107 | 108 | _batch = _compositor.CreateScopedBatch(CompositionBatchTypes.Animation); 109 | var animation = _compositor.CreateScalarKeyFrameAnimation(); 110 | var seconds = (float)(int)now.TimeOfDay.TotalSeconds; 111 | 112 | // This works: 113 | //animation.InsertKeyFrame(0.00f, seconds * 6); 114 | //animation.InsertKeyFrame(1.00f, (seconds + 1) * 6); 115 | 116 | // Just an example of using expressions: 117 | animation.SetScalarParameter("start", seconds * 6); 118 | animation.InsertExpressionKeyFrame(0.00f, "start"); 119 | animation.SetScalarParameter("delta", 6.0f); 120 | animation.InsertExpressionKeyFrame(1.00f, "start + delta"); 121 | 122 | animation.Duration = TimeSpan.FromMilliseconds(900); 123 | _secondhand.StartAnimation(nameof(_secondhand.RotationAngleInDegrees), animation); 124 | _batch.End(); 125 | _batch.Completed += Batch_Completed; 126 | } 127 | 128 | /// 129 | /// Fired at the end of the secondhand animation. 130 | /// 131 | private void Batch_Completed(object sender, CompositionBatchCompletedEventArgs args) 132 | { 133 | _batch.Completed -= Batch_Completed; 134 | 135 | SetHoursAndMinutes(); 136 | } 137 | 138 | private void SetHoursAndMinutes() 139 | { 140 | var now = DateTime.Now; 141 | _hourhand.RotationAngleInDegrees = (float)now.TimeOfDay.TotalHours * 30; 142 | _minutehand.RotationAngleInDegrees = now.Minute * 6; 143 | } 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /Microsoft.UI.Composition.Toolkit/WICBitmapSource.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // 3 | // Licensed under the MIT License. See LICENSE.txt in the project root for license information. 4 | 5 | #include "pch.h" 6 | 7 | #include 8 | #include "WICBitmapSource.h" 9 | 10 | namespace Microsoft { 11 | namespace UI { 12 | namespace Composition { 13 | namespace Toolkit { 14 | 15 | //------------------------------------------------------------------------------ 16 | // 17 | // WICBitmapSourceWrapper::WICBitmapSourceWrapper 18 | // 19 | //------------------------------------------------------------------------------ 20 | 21 | _Use_decl_annotations_ 22 | WICBitmapSourceWrapper::WICBitmapSourceWrapper( 23 | __in IWICBitmapSource* bitmapSource, 24 | __in IWICBitmapSourceTransform* bitmapSourceTransform, 25 | UINT width, 26 | UINT height, 27 | REFWICPixelFormatGUID pixelFormat) 28 | { 29 | _bitmapSourceTransform = bitmapSourceTransform; 30 | _bitmapSource = bitmapSource; 31 | _width = width; 32 | _height = height; 33 | _pixelFormat = pixelFormat; 34 | } 35 | 36 | 37 | //------------------------------------------------------------------------------ 38 | // 39 | // WICBitmapSourceWrapper::CopyPalette 40 | // 41 | //------------------------------------------------------------------------------ 42 | 43 | _Use_decl_annotations_ 44 | HRESULT 45 | WICBitmapSourceWrapper::CopyPalette(__in IWICPalette* palette) 46 | { 47 | WICPixelFormatGUID sourceFormat; 48 | 49 | if (SUCCEEDED(_bitmapSource->GetPixelFormat(&sourceFormat)) && 50 | (sourceFormat == _pixelFormat)) 51 | { 52 | return _bitmapSource->CopyPalette(palette); 53 | } 54 | 55 | return WINCODEC_ERR_PALETTEUNAVAILABLE; 56 | } 57 | 58 | 59 | //------------------------------------------------------------------------------ 60 | // 61 | // WICBitmapSourceWrapper::CopyPixels 62 | // 63 | //------------------------------------------------------------------------------ 64 | 65 | _Use_decl_annotations_ 66 | HRESULT 67 | WICBitmapSourceWrapper::CopyPixels( 68 | __in_opt const WICRect* prc, 69 | UINT stride, 70 | UINT bufferSize, 71 | __out_ecount(bufferSize) BYTE* buffer) 72 | { 73 | return _bitmapSourceTransform->CopyPixels( 74 | prc, 75 | _width, 76 | _height, 77 | &_pixelFormat, 78 | WICBitmapTransformRotate0, 79 | stride, 80 | bufferSize, 81 | buffer 82 | ); 83 | } 84 | 85 | 86 | //------------------------------------------------------------------------------ 87 | // 88 | // WICBitmapSourceWrapper::GetPixelFormat 89 | // 90 | //------------------------------------------------------------------------------ 91 | 92 | _Use_decl_annotations_ 93 | HRESULT 94 | WICBitmapSourceWrapper::GetPixelFormat(__out WICPixelFormatGUID* pixelFormat) 95 | { 96 | *pixelFormat = _pixelFormat; 97 | return S_OK; 98 | } 99 | 100 | 101 | //------------------------------------------------------------------------------ 102 | // 103 | // WICBitmapSourceWrapper::GetResolution 104 | // 105 | //------------------------------------------------------------------------------ 106 | 107 | _Use_decl_annotations_ 108 | HRESULT 109 | WICBitmapSourceWrapper::GetResolution(__out double* dpiX, __out double* dpiY) 110 | { 111 | return _bitmapSource->GetResolution(dpiX, dpiY); 112 | } 113 | 114 | 115 | //------------------------------------------------------------------------------ 116 | // 117 | // WICBitmapSourceWrapper::GetSize 118 | // 119 | //------------------------------------------------------------------------------ 120 | 121 | _Use_decl_annotations_ 122 | HRESULT 123 | WICBitmapSourceWrapper::GetSize(__out UINT* width, __out UINT* height) 124 | { 125 | *width = _width; 126 | *height = _height; 127 | return S_OK; 128 | } 129 | 130 | 131 | //------------------------------------------------------------------------------ 132 | // 133 | // WICBitmapSourceWrapper::CreateSourceTransform 134 | // 135 | //------------------------------------------------------------------------------ 136 | 137 | _Use_decl_annotations_ 138 | bool 139 | WICBitmapSourceWrapper::CreateSourceTransform( 140 | __in IWICBitmapSource* bitmapSource, 141 | UINT desiredWidth, 142 | UINT desiredHeight, 143 | REFWICPixelFormatGUID desiredFormat, 144 | __out IWICBitmapSource** newBitmapSource) 145 | { 146 | HRESULT hr; 147 | 148 | WRL::ComPtr sourceTransform; 149 | bool transformingScale = false; 150 | bool transformingFormat = false; 151 | UINT cxClosest = desiredWidth; 152 | UINT cyClosest = desiredHeight; 153 | WICPixelFormatGUID closestPixelFormat = desiredFormat; 154 | WICPixelFormatGUID currentPixelFormat; 155 | UINT cxImage = 0; 156 | UINT cyImage = 0; 157 | 158 | 159 | *newBitmapSource = NULL; 160 | 161 | hr = bitmapSource->QueryInterface(__uuidof(sourceTransform), (void**)&sourceTransform); 162 | 163 | if (SUCCEEDED(hr)) 164 | { 165 | hr = bitmapSource->GetPixelFormat(¤tPixelFormat); 166 | } 167 | 168 | if (SUCCEEDED(hr)) 169 | { 170 | hr = bitmapSource->GetSize(&cxImage, &cyImage); 171 | } 172 | 173 | if (SUCCEEDED(hr)) 174 | { 175 | hr = sourceTransform->GetClosestPixelFormat(&closestPixelFormat); 176 | } 177 | 178 | if (SUCCEEDED(hr)) 179 | { 180 | hr = sourceTransform->GetClosestSize(&cxClosest, &cyClosest); 181 | } 182 | 183 | if (SUCCEEDED(hr)) 184 | { 185 | if ((closestPixelFormat == desiredFormat) && (closestPixelFormat != currentPixelFormat)) 186 | { 187 | // 188 | // The codec will convert the pixel format for us. 189 | // 190 | 191 | transformingFormat = true; 192 | } 193 | 194 | if (((cxClosest < cxImage) || (cyClosest < cyImage)) && 195 | ((cxImage >= desiredWidth) && (cyClosest >= desiredHeight))) 196 | { 197 | // 198 | // The codec is partially or fully scaling the image for us. 199 | // 200 | 201 | transformingScale = true; 202 | } 203 | } 204 | 205 | if (transformingFormat || transformingScale) 206 | { 207 | // 208 | // The codec is transforming pixel format and/or source size for us 209 | // Create a wrapper around its IWICBitmapTransform to provide a new 210 | // IWICBitmapSource for the next stage. 211 | // 212 | 213 | *newBitmapSource = new WICBitmapSourceWrapper( 214 | bitmapSource, 215 | sourceTransform.Get(), 216 | transformingScale ? cxClosest : cxImage, 217 | transformingScale ? cyClosest : cyImage, 218 | transformingFormat ? closestPixelFormat : currentPixelFormat); 219 | 220 | if (!*newBitmapSource) 221 | { 222 | ::RaiseFailFastException(nullptr, nullptr, 0); 223 | } 224 | } 225 | 226 | return *newBitmapSource != NULL; 227 | } 228 | 229 | STDMETHODIMP_(ULONG) WICBitmapSourceWrapper::AddRef() 230 | { 231 | return InterlockedIncrement(&_cRef); 232 | } 233 | 234 | STDMETHODIMP_(ULONG) WICBitmapSourceWrapper::Release() 235 | { 236 | ULONG cRef = InterlockedDecrement(&_cRef); 237 | 238 | if (0 == cRef) 239 | { 240 | delete this; 241 | } 242 | 243 | return cRef; 244 | } 245 | 246 | STDMETHODIMP WICBitmapSourceWrapper::QueryInterface(REFIID riid, void** object) 247 | { 248 | if (object == nullptr) 249 | { 250 | return E_POINTER; 251 | } 252 | 253 | if (riid == __uuidof(IUnknown)) 254 | { 255 | *object = static_cast(this); 256 | } 257 | else if (riid == IID_IWICBitmapSource) 258 | { 259 | *object = static_cast(this); 260 | } 261 | else 262 | { 263 | return E_NOINTERFACE; 264 | } 265 | 266 | AddRef(); 267 | return S_OK; 268 | } 269 | } 270 | } 271 | } 272 | } -------------------------------------------------------------------------------- /Microsoft.UI.Composition.Toolkit/CompositionGraphicsDevice.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // 3 | // Licensed under the MIT License. See LICENSE.txt in the project root for license information. 4 | 5 | #include "pch.h" 6 | #include "CompositionGraphicsDevice.h" 7 | 8 | namespace Microsoft { 9 | namespace UI { 10 | namespace Composition { 11 | namespace Toolkit { 12 | 13 | CompositionGraphicsDevice^ CompositionGraphicsDevice::CreateCompositionGraphicsDevice(Compositor^ compositor) 14 | { 15 | CompositionGraphicsDevice^ device = ref new CompositionGraphicsDevice(compositor); 16 | 17 | __abi_ThrowIfFailed(device->InitializeGraphicsDevice()); 18 | 19 | return device; 20 | } 21 | 22 | // Creates an ICompositionSurface that can be provided used to create a CompositionSurfaceBrush 23 | // to be associated with a CompositionSpriteVisual. 24 | ICompositionSurface^ CompositionGraphicsDevice::CreateDrawingSurface(Windows::Foundation::Size sizePixels, DirectXPixelFormat pixelFormat, DirectXAlphaMode alphaMode) 25 | { 26 | ComPtr drawingSurface; 27 | ABI::Windows::Foundation::Size size; 28 | size.Height = sizePixels.Height; 29 | size.Width = sizePixels.Width; 30 | 31 | // Make sure to call CreateDrawingSurface under this CompositionGraphicsDevice's state lock 32 | // to avoid any races with D3D initialization and (re)creation of the underlying device. 33 | std::lock_guard lock(_stateLock); 34 | 35 | __abi_ThrowIfFailed(_igraphicsDevice->CreateDrawingSurface( 36 | size, 37 | (ABI::Windows::Graphics::DirectX::DirectXPixelFormat)pixelFormat, 38 | (ABI::Windows::Graphics::DirectX::DirectXAlphaMode)alphaMode, 39 | &drawingSurface)); 40 | 41 | return reinterpret_cast(drawingSurface.Get()); 42 | } 43 | 44 | // Acquires exclusive access to the underlying D3D11Device for the purpose of drawing to surfaces 45 | // created by this CompositionGraphicsDevice. Once finished, the caller should call ReleaseDrawingLock. 46 | void CompositionGraphicsDevice::AcquireDrawingLock() 47 | { 48 | _drawingLock.lock(); 49 | } 50 | 51 | // Releases ownership of the drawing lock associated with the drawing surfaces created 52 | // by this CompositionGraphicsDevice. It is not safe to call this if AcquireDrawingLock 53 | // was not called previously. 54 | void CompositionGraphicsDevice::ReleaseDrawingLock() 55 | { 56 | _drawingLock.unlock(); 57 | } 58 | 59 | CompositionGraphicsDevice::CompositionGraphicsDevice(Compositor^ compositor) : 60 | _compositor(compositor), 61 | _threadPoolWait(NULL) 62 | { 63 | } 64 | 65 | CompositionGraphicsDevice::~CompositionGraphicsDevice() 66 | { 67 | UninitializeDX(); 68 | } 69 | 70 | // Calls InitializeDX and then creates an ICompositionGraphicsDevice which is 71 | // used to create drawing surfaces to be associated with composition visuals. 72 | HRESULT CompositionGraphicsDevice::InitializeGraphicsDevice() 73 | { 74 | HRESULT hr = S_OK; 75 | ComPtr compositorInterop; 76 | ComPtr graphicsDevice; 77 | ComPtr iCompositor; 78 | 79 | std::lock_guard lock(_stateLock); 80 | 81 | IFC(InitializeDX()); 82 | 83 | iCompositor = reinterpret_cast(_compositor); 84 | IFC(iCompositor.As(&compositorInterop)); 85 | IFC(compositorInterop->CreateGraphicsDevice(_graphicsFactoryBackingDXDevice.Get(), &graphicsDevice)); 86 | 87 | _igraphicsDevice = graphicsDevice; 88 | 89 | Cleanup: 90 | return hr; 91 | } 92 | 93 | // Initializes DirectX and registers for device lost notifications. Should be called with _stateLock held. 94 | HRESULT CompositionGraphicsDevice::InitializeDX() 95 | { 96 | HRESULT hr = S_OK; 97 | ComPtr d3dDevice; 98 | ComPtr d3dContext; 99 | ComPtr dxgiDevice; 100 | ComPtr d2dFactory; 101 | ComPtr d2d1Device; 102 | ComPtr d3dDevice1; 103 | ComPtr d3dDevice4; 104 | ComPtr dxgiFactory; 105 | 106 | UninitializeDX(); 107 | 108 | // D3D11_CREATE_DEVICE_BGRA_SUPPORT is required for Direct2D interoperability with Direct3D resources. 109 | UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; 110 | D3D_FEATURE_LEVEL featureLevels[] = 111 | { 112 | D3D_FEATURE_LEVEL_11_1, 113 | D3D_FEATURE_LEVEL_11_0, 114 | D3D_FEATURE_LEVEL_10_1, 115 | D3D_FEATURE_LEVEL_10_0, 116 | D3D_FEATURE_LEVEL_9_3, 117 | D3D_FEATURE_LEVEL_9_2, 118 | D3D_FEATURE_LEVEL_9_1 119 | }; 120 | D3D_FEATURE_LEVEL usedFeatureLevel; 121 | 122 | IFC(D3D11CreateDevice( 123 | nullptr, 124 | D3D_DRIVER_TYPE_HARDWARE, 125 | nullptr, 126 | creationFlags, 127 | featureLevels, 128 | ARRAYSIZE(featureLevels), 129 | D3D11_SDK_VERSION, 130 | &d3dDevice, 131 | &usedFeatureLevel, 132 | &d3dContext)); 133 | 134 | if (FAILED(hr)) 135 | { 136 | FAILHARD(hr); 137 | } 138 | 139 | IFC(d3dDevice.As(&d3dDevice1)); 140 | 141 | // We're using our own synchronization so we can create the D2D factory in single threaded mode. 142 | IFC(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, __uuidof(ID2D1Factory1), &d2dFactory)); 143 | IFC(d3dDevice.As(&dxgiDevice)); 144 | IFC(d2dFactory->CreateDevice(dxgiDevice.Get(), &d2d1Device)); 145 | 146 | _graphicsFactoryBackingDXDevice = d2d1Device; 147 | 148 | IFC(d3dDevice.As(&_d3dDevice4)); 149 | IFC(AttachDeviceLostHandler()); 150 | 151 | Cleanup: 152 | return hr; 153 | } 154 | 155 | // Registers a device removed event with D3D so that we can be called back 156 | // anytime our underlying D3D11Device is lost or reset. Should be called with _stateLock held. 157 | HRESULT CompositionGraphicsDevice::AttachDeviceLostHandler() 158 | { 159 | HRESULT hr = S_OK; 160 | 161 | _deviceLostEvent = CreateEvent( 162 | NULL, // Attributes 163 | TRUE, // Manual reset 164 | FALSE, // Initial state 165 | NULL); // Name 166 | if (_deviceLostEvent == NULL) 167 | { 168 | FAILHARD(HRESULT_FROM_WIN32(GetLastError())); 169 | } 170 | 171 | // It's okay for us to use a weak reference to ourself here since we unregister on destruction. 172 | _threadPoolWait = CreateThreadpoolWait(CompositionGraphicsDevice::OnDeviceLostCallback, (PVOID)this, NULL); 173 | if (_threadPoolWait == NULL) 174 | { 175 | FAILHARD(HRESULT_FROM_WIN32(GetLastError())); 176 | } 177 | 178 | SetThreadpoolWait(_threadPoolWait, _deviceLostEvent, NULL); 179 | hr = _d3dDevice4->RegisterDeviceRemovedEvent(_deviceLostEvent, &_deviceLostRegistrationCookie); 180 | if (FAILED(hr)) 181 | { 182 | FAILHARD(hr); 183 | } 184 | 185 | return hr; 186 | } 187 | 188 | // Called in response to the underlying D3D11Device being lost, triggers a DeviceLost 189 | // event to any subscribers. 190 | void CALLBACK CompositionGraphicsDevice::OnDeviceLostCallback(PTP_CALLBACK_INSTANCE, PVOID context, PTP_WAIT, TP_WAIT_RESULT) 191 | { 192 | CompositionGraphicsDevice^ pThis = reinterpret_cast(context); 193 | ComPtr graphicsDeviceInterop; 194 | 195 | if (pThis == nullptr) 196 | { 197 | FAILHARD(E_POINTER); 198 | } 199 | 200 | HRESULT hr = pThis->InitializeDX(); 201 | if (FAILED(hr)) 202 | { 203 | FAILHARD(hr); 204 | } 205 | 206 | // Set the new DX context as the rendering device on the CompositionGraphicsDevice 207 | hr = pThis->_igraphicsDevice.As(&graphicsDeviceInterop); 208 | if (FAILED(hr)) 209 | { 210 | FAILHARD(hr); 211 | } 212 | 213 | ComPtr unknownDXDevice; 214 | hr = pThis->_graphicsFactoryBackingDXDevice.As(&unknownDXDevice); 215 | if (FAILED(hr)) 216 | { 217 | FAILHARD(hr); 218 | } 219 | 220 | graphicsDeviceInterop->SetRenderingDevice(unknownDXDevice.Get()); 221 | 222 | // fire the event that this graphics device was lost and reinitialized 223 | pThis->DeviceLost(pThis); 224 | } 225 | 226 | // Should be called with _stateLock held. 227 | void CompositionGraphicsDevice::UninitializeDX() 228 | { 229 | if ((_deviceLostRegistrationCookie != 0) && (_d3dDevice4 != nullptr)) 230 | { 231 | _d3dDevice4->UnregisterDeviceRemoved(_deviceLostRegistrationCookie); 232 | _deviceLostRegistrationCookie = 0; 233 | } 234 | 235 | if (_deviceLostEvent != NULL) 236 | { 237 | CloseHandle(_deviceLostEvent); 238 | _deviceLostEvent = NULL; 239 | } 240 | 241 | if (_threadPoolWait != NULL) 242 | { 243 | CloseThreadpoolWait(_threadPoolWait); 244 | _threadPoolWait = NULL; 245 | } 246 | 247 | _d3dDevice4 = nullptr; 248 | } 249 | 250 | } // namespace Toolkit 251 | } // namespace Composition 252 | } // namespace UI 253 | } // namespace Microsoft -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/XamlBrewer.Uwp.Composition.ClockSample.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | x86 7 | {382B3EF2-2A20-420D-BFA5-9E3B34515242} 8 | AppContainerExe 9 | Properties 10 | XamlBrewer.Uwp.Composition.ClockSample 11 | XamlBrewer.Uwp.Composition.ClockSample 12 | en-US 13 | UAP 14 | 10.0.10586.0 15 | 10.0.10240.0 16 | 14 17 | true 18 | 512 19 | {A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 20 | XamlBrewer.Uwp.Composition.ClockSample_TemporaryKey.pfx 21 | 22 | 23 | true 24 | bin\ARM\Debug\ 25 | DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP 26 | ;2008 27 | full 28 | ARM 29 | false 30 | prompt 31 | true 32 | 33 | 34 | bin\ARM\Release\ 35 | TRACE;NETFX_CORE;WINDOWS_UWP 36 | true 37 | ;2008 38 | pdbonly 39 | ARM 40 | false 41 | prompt 42 | true 43 | true 44 | 45 | 46 | true 47 | bin\x64\Debug\ 48 | DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP 49 | ;2008 50 | full 51 | x64 52 | false 53 | prompt 54 | true 55 | 56 | 57 | bin\x64\Release\ 58 | TRACE;NETFX_CORE;WINDOWS_UWP 59 | true 60 | ;2008 61 | pdbonly 62 | x64 63 | false 64 | prompt 65 | true 66 | true 67 | 68 | 69 | true 70 | bin\x86\Debug\ 71 | DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP 72 | ;2008 73 | full 74 | x86 75 | false 76 | prompt 77 | true 78 | 79 | 80 | bin\x86\Release\ 81 | TRACE;NETFX_CORE;WINDOWS_UWP 82 | true 83 | ;2008 84 | pdbonly 85 | x86 86 | false 87 | prompt 88 | true 89 | true 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | App.xaml 98 | 99 | 100 | SillyWalkerClock.xaml 101 | 102 | 103 | ImageClock.xaml 104 | 105 | 106 | Clock.xaml 107 | 108 | 109 | 110 | MainPage.xaml 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | OtherPage.xaml 122 | 123 | 124 | 125 | SillyPage.xaml 126 | 127 | 128 | 129 | 130 | Designer 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | MSBuild:Compile 159 | Designer 160 | 161 | 162 | MSBuild:Compile 163 | Designer 164 | 165 | 166 | MSBuild:Compile 167 | Designer 168 | 169 | 170 | Designer 171 | MSBuild:Compile 172 | 173 | 174 | MSBuild:Compile 175 | Designer 176 | 177 | 178 | Designer 179 | MSBuild:Compile 180 | 181 | 182 | MSBuild:Compile 183 | Designer 184 | 185 | 186 | Designer 187 | MSBuild:Compile 188 | 189 | 190 | 191 | 192 | {08a7d060-593a-4978-8dcc-b2029aca7c95} 193 | Microsoft.UI.Composition.Toolkit 194 | 195 | 196 | 197 | 198 | Windows Desktop Extensions for the UWP 199 | 200 | 201 | Windows Mobile Extensions for the UWP 202 | 203 | 204 | 205 | 14.0 206 | 207 | 208 | 215 | -------------------------------------------------------------------------------- /XamlBrewer.Uwp.Composition.ClockSample/Views/Shell.xaml: -------------------------------------------------------------------------------- 1 | 9 | 10 | 12 | 14 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 30 | 31 | 37 | 41 | 42 | 43 | 44 | 50 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 65 | 68 | 69 | 72 | 75 | 81 | 82 | 83 | 84 | 86 | 92 | 93 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 107 | 108 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 130 | 131 | 132 | 133 | 136 | 139 | 140 | 141 | 142 | 143 | 144 | 145 |