以最有效的方式在跨进程发送图像 [英] Most efficient way to send images across processes

查看:211
本文介绍了以最有效的方式在跨进程发送图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标

将一个进程生成的图像高效地传递到另一个进程。这两个进程在同一台机器上运行,并在同一台桌面上运行。操作系统可能是WinXP,Vista和Win7。

Pass images generated by one process efficiently and at very high speed to another process. The two processes run on the same machine and on the same desktop. The operating system may be WinXP, Vista and Win7.

详细说明

第一个过程仅用于控制与产生图像的设备的通信。这些图像的大小约为500x300像素,每秒可能更新数百次。第二个过程需要这些图像来处理它们。第一个进程使用第三方API将图像从设备绘制到HDC。这个HDC必须由我提供。

The first process is solely for controlling the communication with a device which produces the images. These images are about 500x300px in size and may be updated up to several hundred times per second. The second process needs these images to process them. The first process uses a third party API to paint the images from the device to a HDC. This HDC has to be provided by me.

注意:两个进程之间已经有一个连接打开了。他们通过匿名管道进行通信,并共享内存映射文件视图。

Note: There is already a connection open between the two processes. They are communicating via anonymous pipes and share memory mapped file views.

想法

如何通过尽可能少的工作实现这一目标?我的意思是两台电脑和我的工作(当然是))。我正在使用Delphi,所以也许有一些组件可以做到这一点?我想我可以随时绘制任何图像组件的HDC,将内容保存到内存流中,通过内存映射文件复制内容,并将其打包到另一面,并将其绘制到目标HDC。我还读了一个可以用来编组图像的IPicture接口。我需要尽可能快,所以开销越少越好。我不想通过复制一些图像来强调机器。

How would I achieve this goal with as little work as possible? And I mean both work for the computer and me (of course ;)). I am using Delp so maybe there is some component available for doing this? I think I could always paint to any image component's HDC, save the content to memory stream, copy the contents via the memory mapped file, unpack it on the other side and paint it there to the destination HDC. I also read about a IPicture interface which can be used to marshal images. I need it as quick as possible, so the less overhead the better. I don't want the machine to be stressed just by copying some images.

你的想法是什么?我感谢每一个这样的想法!

What are your ideas? I appreciate every thought on this!

推荐答案

好像内存映射文件和管道是正确的方法。这不是太糟糕,因为这两个进程已经共享了一个MMF和两个管道(用于双向通信)。唯一要解决的问题是如何尽可能少地复制数据。

Ok it seems as if memory mapped files and pipes are the right way to go. That is not too bad because the two processes already share a MMF and two pipes (for bidirectional communication). The only thing left to solve was how to pass the data with as little copy operations as possible.

工作相当好的设计如下(顺序流程):

The design which works quite well looks as follows (sequential flow):

进程1(想要图像)


  • 给过程2(通过管道1)发送信号以将图像存储在共享内存中

  • go睡觉并等待响应(阻止从管道2读取)

过程2(提供图像)


  • 信号(通过管道1)唤醒并告诉硬件设备到HDC 1(这是由共享内存支持,见下文)

  • 给出过程1(通过管道2)的信号

  • 进入睡眠状态并等待新工作(通过管道1)

过程1(想要图像)


  • 信号(通过管道2)唤醒和绘画从共享内存到目标HDC 2

现在通过共享内存传输图像(我的目标是使用不超过一个副本操作):

Now for the image transfer via shared memory (my goal was to use not more than one additional copy operation):

进程2通过 Cr创建一个 HBITMAP eateDIBSection 并提供文件映射的句柄和映射视图的偏移量。因此,图像数据存在于共享存储器中。这将创建一个 HBITMAP ,它被选入HDC 1(也由过程2创建),并从现在开始使用过程2.

Process 2 creates a HBITMAP via CreateDIBSection and provides the handle of the file mapping and the offset of the mapped view. Thus the image data lives in the shared memory. This creates an HBITMAP which is selected into HDC 1 (which is also created by process 2) and which will be used from now on by process 2.

进程1使用 StretchDIBits 指向映射视图的内存(如 here )。这似乎是将内存中的位直接转换为另一个HDC(在本例中为HDC 2)的唯一功能。其他功能会将它们首先复制到一些中间缓冲区,然后才能将它们从那里转移到最终的HDC。

Process 1 uses StretchDIBits with a pointer to the mapped view's memory (as described here). This seems to be the only function for getting bits from memory directly into another HDC (in this case HDC 2). Other functions would copy them first into some intermediary buffer before you could transfer them from there to the final HDC.

所以最终似乎需要传输的位是是一开始的两倍。但是我认为这是一样好,除非在进程之间共享GDI句柄是可能的。

So in the end it seems the bits needed to be transferred are about twice as much as in the beginning. But I think this is as good as it gets unless sharing GDI handles between processes would be possible.

注意:我使用管道而不是信号,因为我需要传输一些额外的数据也是。

Note: I used pipes instead of signals because I need to transfer some additional data, too.

这篇关于以最有效的方式在跨进程发送图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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