WPF Perf: RenderCapability.Tier & DesiredFrameRate
来源:互联网 发布:二次元网址导航源码 编辑:程序博客网 时间:2024/06/06 02:43
http://blogs.msdn.com/b/henryh/archive/2006/08/23/719568.aspx
In this post, I’m going to talk about two key API’s for performance in WPF. These are RenderCapability.Tier and Storyboard.DesiredFrameRate. In this post, I’m going to show:
- How to leverage RenderCapability.Tier to scale your app up or down.
- How to use RenderCapability.Tier in markup.
- How to apply DesiredFrameRate to reduce CPU consumption.
RenderCapability.Tier
For the machine on which it’s run, RenderCapability.Tier signals the machine’s hardware capabilities. Tier=0 means software rendering; Tier=2 is hardware rendering (for those features that can be rendered in hardware); Tier=1 is a middle ground (some things in hardware and some in software.) You can probably see how this might be useful to scale up or down the richness of an application depending on the hardware.
(Note: RenderCapability.Tier is an integer value using the high word to indicate the tier. You’ll need to shift by 16 bits to get the corresponding tier values. Adam Smith posts about why this was chosen.)
Storyboard.DesiredFrameRate
The second API I mentioned was Storyboard.DesiredFrameRate (DFR.) DFR allows you to specify, manually, what the frame rate “should” be (the animation system attempts to get as close as possible to the DFR value but there are no guarantees.) By default, WPF attempts 60 fps. The up side to this is that animations look better; the down side is that you may not need 60 fps but you may be spending the extra CPU cycles.
Putting Them Together
You can use RenderCapability.Tier in markup with a small helper class. The trick is to wrap up the property in a DependencyProperty. From there, the property can be used to set DFR. Below, you’ll find both the code for the RenderCapability.Tier, how to use it in markup and how to set DesiredFrameRate. I picked different DFR values for the different Tiers. The resulting UI isn't that exciting (a spinning Button) but it demonstrates the concept.
using System;using System.Windows;using System.Windows.Media; namespace PerformanceUtilities{ public static class RenderCapabilityWrapper { public static readonly DependencyProperty TierProperty = DependencyProperty.RegisterAttached( "Tier", typeof(int), typeof(PerformanceUtilities.RenderCapabilityWrapper), new PropertyMetadata(RenderCapability.Tier >> 16)); public static int GetTier(DependencyObject depObj) { return (int)TierProperty.DefaultMetadata.DefaultValue; } }}
<Border xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Perf="clr-namespace:PerformanceUtilities;assembly=PerfTier" Background="silver"> <Border.Resources> <Style x:Key="{x:Type Button}" TargetType="{x:Type Button}"> <Style.Triggers> <Trigger Property="Perf:RenderCapabilityWrapper.Tier" Value="2"> <Setter Property="Tag" Value="60"/> </Trigger> <Trigger Property="Perf:RenderCapabilityWrapper.Tier" Value="1"> <Setter Property="Tag" Value="30"/> </Trigger> <Trigger Property="Perf:RenderCapabilityWrapper.Tier" Value="0"> <Setter Property="Tag" Value="15"/> </Trigger> </Style.Triggers> </Style> </Border.Resources> <Button Width="80" Height="80" Name="MyButton" Content="{Binding RelativeSource={RelativeSource Self}, Path=Tag}"> <Button.Triggers> <EventTrigger RoutedEvent="FrameworkElement.Loaded"> <BeginStoryboard> <Storyboard RepeatBehavior="Forever" Storyboard.DesiredFrameRate="{Binding ElementName=MyButton, Path=Tag}"> <DoubleAnimation Storyboard.TargetName="MyAnimatedTransform" Storyboard.TargetProperty="(RotateTransform.Angle)" From="0.0" To="360" Duration="0:0:10" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Button.Triggers> <Button.RenderTransform> <RotateTransform CenterX="40" CenterY="40" x:Name="MyAnimatedTransform"/> </Button.RenderTransform> </Button> </Border>
- WPF Perf: RenderCapability.Tier & DesiredFrameRate
- perf
- perf
- perf
- perf
- perf
- perf
- perf
- perf
- Perf
- Perf
- 最优化WPF 3D性能(基于“Tier-2”硬件)
- 最优化WPF 3D性能(基于“Tier-2”硬件)
- 最优化WPF 3D性能(基于“Tier-2”硬件)
- 最优化WPF 3D性能(基于“Tier-2”硬件)
- 最优化WPF 3D性能(基于“Tier-2”硬件)
- Perf 简介
- linux perf
- 产品经理-需求分析的六原则
- Hibernate之对象三种状态
- 作为投资人和创业者最重要的经验
- 历史上十大著名思想实验
- html的总结
- WPF Perf: RenderCapability.Tier & DesiredFrameRate
- Linuxsed命令实例详解
- socket
- 将源代码中的全角符号转换成半角符号并对代码风格进行美化
- ListCtrl 锁定列宽度的简单方法
- Git学习的最佳教程
- android 4.0的一种拷贝文件方式
- cocos2d-x游戏实例(25)-简易动作游戏(3)
- VC++中的Win32 Application和Win32 Console Application区别