2

I'm currently learning Python. For that, I'm an programming a stupid tool that draws stuff around the mouse cursor. It has either a particle effect or a relatively static display of the time, the color underneath the cursor, or something like that. One would expect the particle effect to be the only one causing DWM to spike in CPU usage, but it's everything.

Using Process Hacker I can see that this thread of DWM is taking up all the CPU time dwmcore.dll!MilTransport_Release+0xXXXXX. MilTransport_Release is a function inside the dwmcore.dll. No Information about it is available, afaik.
What does that mean?

Here's a minimal example. Press ESC to kill the process.

import pygame
from win32gui import SetWindowLong, SetLayeredWindowAttributes, GetWindowLong, SetWindowPos
from win32con import HWND_TOPMOST, GWL_EXSTYLE, SWP_NOMOVE, SWP_NOSIZE, WS_EX_TRANSPARENT, LWA_COLORKEY, WS_EX_LAYERED
from win32api import RGB


def setWindowAttributes(hwnd):
    SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE)
    SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_TRANSPARENT | WS_EX_LAYERED)
    SetLayeredWindowAttributes(hwnd, RGB(0, 0, 0), 0, LWA_COLORKEY)


def loop():
    looping = True
    while looping:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    return
        display_window.fill("#000000")  # fill with transparent color
        print("running")
        clock.tick(60)
        pygame.display.update()


pygame.init()
display_window = pygame.display.set_mode((0, 0), 0)
setWindowAttributes(pygame.display.get_wm_info()['window'])
clock = pygame.time.Clock()

try:
    loop()
finally:  # to catch all things and quit correctly
    pygame.quit()

(This is not running in the background.) It creates a 60 fps fullscreen transparent layer that is topmost and clickthrough. If you don't click anything after you started it, press ESC to kill it.

On my crappy laptop this minimal program makes DWM use 25% CPU (one full core) and 20 to 25% of the APUs GPU. (It's a AMD A4-6210) On my AMD Ryzen 5 2600 it is similar, maxing out one HT core, but that's not as bad.

Behaviour:
The lower the FPS the lower the CPU usage of DWM, but 60 should be totally normal FPS.
The smaller I make the window that's updated, the fewer ressources needs DWM, but fullscreen shouldn't be the issue.

This can't be correct behaviour, can it?
Do you have any information about this? Anything, like how your fullscreen pygame works together with DWM, might help.

Thank you!

  • I think the ten second crash is because you're not handling events, adding a call to `pygame.event.pump()` in the while loop stops your minimal script from crashing. I see similar DWM CPU & GPU consumption. Also, you should [edit your question](https://meta.stackoverflow.com/a/339451/2280890) to include the [mcve] instead of linking to external sites. – import random Jun 01 '22 at 03:37
  • Should I really include this whole thing here? I thought it would be too big. Edit: Oh it has its own little thing, ok. :) – Distelzombie Jun 01 '22 at 07:53
  • With my experience with pygame, it does tend to use an unreasonable amount of gpu and cpu power – AzlanCoding Jun 02 '22 at 10:35
  • But it isn't pygame that's using this. Pygames processes are prettys low on usage. It's window's DWM that is using just stupid amounts of power. Also, I found that the cpu usage of any process goes up to 25% when I move the mouse over it in circles. Without Pygame even running! The hell is wrong with Windows? I haven't even activated all the Aero or-what's-it-called things. This "wastes" actually huge amounts of power – Distelzombie Jun 02 '22 at 13:55
  • It seems likely that the window attributes you're setting are causing the DWM to consume these resources. Might need to consult some Windows documentation. – import random Jun 06 '22 at 12:18
  • Why do you think that? Idk about documentation. Ich have no idea where to look. I've read the docs that say what the flags do, but they don't say anything about such issues. And this aren't very unusual flags. The only thing not frequently used imo is those that make it transparent. – Distelzombie Jun 07 '22 at 14:07

0 Answers0