图形上下文到底是什么?

发布于 2024-11-25 19:58:14 字数 773 浏览 1 评论 0原文

图形上下文(或 Windows 中的设备上下文)到底封装了什么?

网络上的各种定义都同意上下文封装了各种图形操作的参数。请参阅:X11Mac 操作系统< /a>,Windows

尚不清楚上下文是否也封装执行图形操作的内存缓冲区。

在 X11 条目中提到了单独的 Drawable 对象、Window 和 Pixmap,它们代表绘图表面。更进一步,进入 OpenGL GLX 文档,< em>渲染上下文和绘图表面。有趣的是,还有人说“应用程序可以使用不同的上下文渲染到同一个表面”,并且“也可以使用单个上下文渲染到多个表面” >。

What exactly is encapsulated by a Graphic Context (or Device Context in Windows)?

The various definitions across the net agree that a context encapsulates the parameters for the various graphics operations. See: X11, Mac OS, Windows

What is not clear is whether a context also encapsulates the memory buffer upon which the graphics operations are performed.

In the X11 entry there is a mention about separate Drawable objects, Window and Pixmap, which represent the drawing surfaces. Going a bit further, into the OpenGL GLX documentation, there is a clear separation between Rendering Contexts and Drawing Surfaces. Interestingly it is also said that "applications can render into the same surface, using different contexts" and that "it is also possible to use a single context to render into multiple surfaces".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感情旳空白 2024-12-02 19:58:14

X11 GC 不包含内存缓冲区,Drawable 和 GC 都会传递到所有绘图操作,并且 GC 可以与所有“类似”Drawable(同一屏幕上具有相同位深度的可绘制对象)一起使用。

然而,如今 X11 中的绘图通常是使用 Cairo 等库在客户端完成的,因此“真正的”答案取决于该库。在开罗的情况下,开罗上下文确实包含当前目标表面,但您可以更改目标表面。

OpenGL 还有一个“当前目标”概念,尽管您可以再次更改当前目标。

如果你想概括的话,我会说,从概念上讲,上下文与缓冲区是分开的,尽管为了节省打字,人们可能有一个可以在上下文上设置的当前缓冲区。

An X11 GC does not contain the memory buffer, both the Drawable and the GC are passed in to all the drawing operations, and the GC can be use with all "like" Drawables (drawables on the same screen with same bit depth).

However drawing in X11 these days is normally done client-side using a library such as Cairo, so the "real" answer depends on that library. In Cairo's case the Cairo context does include a current target surface, though you can change the target surface.

OpenGL also has a "current target" kind of concept, though again you can change the current target.

If you want to generalize I'd say that conceptually a context is separate from the buffer, though to save typing people may have a current buffer you can set on the context.

高跟鞋的旋律 2024-12-02 19:58:14

具体查看 Windows 设备上下文,您提出的主要问题的答案似乎是“是和否”。

设备上下文基本上创建了一种完成绘图的模式——即,在任何给定时间,它将具有诸如以下内容的当前设置:

  1. 背景颜色
  2. 前景色
  3. 线条宽度
  4. 线条图案
  5. 字体

(等等)事物)。

现在,就绘图表面而言:是的,我相信每个设备上下文总是有一个附加的绘图表面。在窗口的设备上下文的常见情况下,绘图表面将是显示窗口的屏幕缓冲区的一部分。在“兼容”设备上下文的情况下(例如,来自 CreateCompatibleDC 的结果),它将是一个非常无用的绘图表面——具体来说,它是一个单一的单色像素。它将被设置为黑色或白色,具体取决于您绘制到 DC 的任何内容的整体亮度级别是否超过某个阈值(不,我不记得确切的阈值)。

不过,这确实有一个(某种程度上)有用的目的:特别是,它意味着 DC 总是“可用”——永远不会出现因为没有 DC 而导致绘制 DC 失败的情况。附有绘图表面。为了帮助维护这一点,没有 DeselectObject 函数 - 您可以使用 SelectObject 在设备上下文中选择不同的位图(这将也取消选择原始位图),但是如果不选择另一个位图,就无法从设备上下文中取消选择一个位图 - 因此它总是附加一个绘图表面。

同时,兼容设备上下文的默认绘图表面几乎毫无用处,几乎可以视为根本没有附加绘图表面。

编辑:我还应该补充一点,选择到兼容设备上下文中的默认绘图表面是很多问题的根源。特别是,当您创建兼容的 DC 来执行双缓冲绘图时,您必须执行以下操作:

DC memDC = CreateCompatibleDC(windowDC);
BITMAP bmp = CreateCompatibleBitmap(WindowDC, sizeX, sizeY);
SelectObject(memDC, bmp);

但是,如果您稍微搞砸了,请执行以下操作:

DC memDC = CreateCompatibleDC(windowDC);
BITMAP bmp = CreateCompatibleBitmap(memDC, sizeX, sizeY);
SelectObject(memDC, bmp);

...一切都会成功,并且在某种程度上,它甚至都可以工作——除了你通过兼容的 DC 绘制的所有内容最终都会变成单色。这又回到了单像素单色位图。由于默认情况下兼容 DC 会选择单色位图,因此当您请求兼容位图时,您将获得指定大小的单色位图。在第一个版本中,您要求与原始 DC 中选择的位图兼容的位图(通常是屏幕),因此您创建的内容将具有屏幕设置的全色。

Looking specifically at a Windows Device Context, the answer to the primary question you're asking seems to be "yes and no".

A device context basically creates a mode in which drawing will be done -- i.e., at any given time, it'll have current settings for things like:

  1. Background color
  2. foreground color
  3. line width
  4. line pattern
  5. font

(and so on for quite a few more things).

Now, as far as there being a drawing surface: yes, I believe every device context always has a drawing surface attached to it. In the common case of a device context for a window, that drawing surface will be the part of the screen buffer where the window is displayed. In the case of a "compatible" device context (e.g., result from CreateCompatibleDC) it'll be a pretty useless drawing surface -- specifically, it's a single, monochrome pixel. It'll be set to either black or white, depending on whether the overall brightness level of whatever you draw to the DC exceeds a certain threshold or not (and no, offhand I don't remember the exact threshold).

This does have a (sort of) useful purpose though: in particular, it means that a DC is always "usable" -- there's never a situation in which drawing to a DC will fail just because there's no drawing surface attached. As an aid in maintaining this, there's no DeselectObject function -- you can use SelectObject to select a different bitmap into the device context (which will also deslect the original one) but there's no way to deselect one bitmap from the device context without selecting another into it -- so it always has a drawing surface attached.

At the same time, the default drawing surface for a compatible device context is so close to useless that it almost counts as not having a drawing surface attached at all.

Edit: I should also add that the default drawing surface that's selected into a compatible device context is a source of quite a few problems. In particular, when you're creating a compatible DC to do double-buffered drawing, you have to do something like this:

DC memDC = CreateCompatibleDC(windowDC);
BITMAP bmp = CreateCompatibleBitmap(WindowDC, sizeX, sizeY);
SelectObject(memDC, bmp);

If, however, you screw up ever so slightly and do this instead:

DC memDC = CreateCompatibleDC(windowDC);
BITMAP bmp = CreateCompatibleBitmap(memDC, sizeX, sizeY);
SelectObject(memDC, bmp);

...everything will succeed, and to some extent it'll all even work -- except that everything you draw via the compatible DC will end up in monochrome. This goes back to that one-pixel monochrome bitmap. Since the compatible DC has a monochrome bitmap selected into it by default, when you ask for a compatible bitmap, you'll get a monochrome bitmap of the specified size. In the first version you're asking for a bitmap compatible with the bitmap selected into the original DC, which will normally be the screen, so what you create will have the full color your screen is set for.

爱你不解释 2024-12-02 19:58:14

不确定其他平台,但在 Windows 上,设备上下文(或 DC)只是一个不透明的指针。内部结构由 GDI 维护,包含有关绘制到屏幕上的内容的信息。

要操纵正在绘制的对象,您可以将此不透明指针(或句柄)传递给GDI函数。这与管理窗口属性的 HWND 句柄在概念上相同,但 HDC 管理图形。

Not sure about other platforms, but on Windows a Device Context (or DC) is simply an opaque pointer. The internal structure is maintained by GDI and contains information about stuff being drawn to the screen.

To manipulate the objects being drawn you pass this opaque pointer (or Handle) to GDI functions. This is the same in concept as HWND handles that manage a window's attributes, except an HDC manages graphics.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文