Win API中的内存DC有多便宜/昂贵? [英] How cheap/expensive is memory DC in Win API?

查看:168
本文介绍了Win API中的内存DC有多便宜/昂贵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个程序,它将有很多DIB位图(由 CreateDIBSection 创建),并且必须使用Win API在其上绘制大量文本。 p>

为了绘制位图,Windows需要设备上下文,由 CreateCompatibleDC 创建。



现在有两种方法:


  1. DC每次一个位图,使用它绘制和删除位图时删除它。


  2. 或者我可以创建DC,只有当我需要绘制到位图,调用绘制函数和删除DC。

更好的方法是什么?我喜欢第一个,因为调用次数更少 - 这将使我的代码更小,也更快一点。



但是不是太贵,持有一个长生活DC为每个位图?



Edit1:应用程序实际上是一个GUI工具包库,可以在将来以不同的和不可预测的方式使用,所以我需要一个平衡决定最大可能的性能和最小的系统资源使用。

解决方案

GDI对象都受到限制,无论是每个进程还是每个会话。您正在与在同一会话中运行的所有其他进程竞争资源。考虑到这一点,你应该只在需要时使用GDI资源(你的问题中的选项2)。



Mark Russinovich的博客条目推送Windows的限制:USER和GDI对象 - 第2部分有相当数量的详情。下面是窗口管理器对GDI资源的限制列表:




  • 每个进程有10.000个GDI对象值,可通过注册表项配置HKEY_LOCAL_MACHINE \SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\GDIProcessHandleQuota )。

  • 65.535 GDI对象每个用户会话。

  • GDI对象内存限制是分页池限制(请参阅推送Windows的限制:分页和非分页池)。


I am working on a program that will have many DIB bitmaps (created by CreateDIBSection) and will have to draw a lot of text on them using Win API.

In order to draw on the bitmap, Windows needs device context, created by CreateCompatibleDC.

And now here are two approaches:

  1. I can create the DC once per bitmap, using it for drawing and delete it when freeing the bitmap.

  2. Or I can create DC only when I need to draw to the bitmap, call the draw functions and delete the DC.

What is the better approach? I prefer the first, because of less calls - this will make my code much smaller and also a little bit faster.

But isn't it too expensive to hold a long living DC for every bitmap?

Edit1: The application is actually a GUI toolkit library that can be used in different and unpredictable way in the future, so I need a well balanced decision with maximal possible performance and minimal system resource usage.

解决方案

GDI objects are limited, both per process as well as per session. You are competing for resources with all other processes running in the same session. With that in mind, you should consume GDI resources only when needed (option 2 in your question).

Mark Russinovich's blog entry Pushing the Limits of Windows: USER and GDI Objects – Part 2 goes into a fair amount of detail. To sum up the gist, here is a list of limits that the window manager places on GDI resources:

  • 10.000 GDI objects per process (default value, configurable through the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\GDIProcessHandleQuota).
  • 65.535 GDI objects per user session.
  • GDI object memory limit is the paged pool limit (see Pushing the Limits of Windows: Paged and Nonpaged Pool).

这篇关于Win API中的内存DC有多便宜/昂贵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆