Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

High CPU use when showing CustomDialogs #4574

Closed
2 tasks done
dweymouth opened this issue Jan 27, 2024 · 23 comments
Closed
2 tasks done

High CPU use when showing CustomDialogs #4574

dweymouth opened this issue Jan 27, 2024 · 23 comments
Labels
bug Something isn't working

Comments

@dweymouth
Copy link
Contributor

Checklist

  • I have searched the issue tracker for open issues that relate to the same problem, before opening a new one.
  • This issue only relates to a single bug. I will open new issues for any other problems.

Describe the bug

Showing a custom confirm dialog using dialog.ShowCustomConfirm uses high CPU while the dialog is displayed. After dismissing the dialog the CPU settles back down to normal.

How to reproduce

Run the example code, with an Activity Monitor/Task Manager/etc window open, and click the button to show the custom confirm dialog

Screenshots

No response

Example code

package main

import (
	"log"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/widget"
)

func main() {
	myApp := app.New()
	myWindow := myApp.NewWindow("Custom Confirm CPU bug")

	button := widget.NewButton("Show dialog", func() {
		dialog.ShowCustomConfirm("Custom confirm", "Confirm text", "Dismiss text",
			widget.NewLabel("Content"), func(b bool) {
				log.Printf("Confirm dismissed with value %v\n", b)
			}, myWindow)
	})
	myWindow.SetContent(container.NewCenter(button))
	myWindow.Resize(fyne.NewSize(400, 300))
	myWindow.ShowAndRun()
}

Fyne version

2.4.3

Go compiler version

go1.21.1

Operating system and version

macOS Ventura M2

Additional Information

No response

@dweymouth dweymouth added unverified A bug that has been reported but not verified bug Something isn't working and removed unverified A bug that has been reported but not verified labels Jan 27, 2024
@dweymouth dweymouth changed the title High CPU use by dialog.ShowCustomConfirm High CPU use by dialog.ShowCustom[Confirm] Jan 27, 2024
@dweymouth
Copy link
Contributor Author

Reproes with dialog.ShowCustom too

@dweymouth dweymouth changed the title High CPU use by dialog.ShowCustom[Confirm] High CPU use when showing CustomDialogs Jan 27, 2024
@Jacalz
Copy link
Member

Jacalz commented Jan 27, 2024

Is this specific to custom dialogs or dialogs in general? I can’t think of anything specific that the custom ones do but the others don’t

@dweymouth
Copy link
Contributor Author

Specific to custom dialogs. Regular ShowInformation, etc don't show the issue. I think it must be somehow related to the CustomDialog type itself

@Jacalz
Copy link
Member

Jacalz commented Jan 27, 2024

It is just a struct with a pointer to the good old dialog struct. Nothing special really. I'm not saying that you are wrong but I have a hard time seeing that it would be specific to custom only

@dweymouth
Copy link
Contributor Author

Yeah I'm equally confused. Maybe it's a platform-specific thing too. Are you able to replicate it?

@Jacalz
Copy link
Member

Jacalz commented Jan 27, 2024

Just on my phone now, sorry. Can't guarantee when I can have a look

@vinser
Copy link

vinser commented Jan 28, 2024

On my Ubuntu SBC your example code with it myWindow.Resize(fyne.NewSize(400, 300)) doesn't make CPU load
image
But if I stretch main window 1500x600 then it loads CPU high
Screenshot at 2024-01-28 21-05-31

@vinser
Copy link

vinser commented Jan 28, 2024

Loads continuously

@vinser
Copy link

vinser commented Jan 28, 2024

If you have some controls with data binding or OnChanged defined in dialog.NewCustomConfirm then high CPU load is guaranteed :)
It may be so high that bindings skip ticks IMO/ So I have to use OnChangeEnded instead of OnChanged for sliders.
I could of course be wrong about the reason

@vinser
Copy link

vinser commented Jan 29, 2024

It seems that this bug is typical not only for complex CustomConfirm , but also for simpe CustomDialog which only displays some info
image

@vinser
Copy link

vinser commented Jan 29, 2024

I tested my app on Windows PC. Here it is
image
When I open above dialog GPU consumption rises up to 42-43% (plateau under the red arrow).
On my Ubuntu SBC it loads CPU because GPU does not work.

@dweymouth
Copy link
Contributor Author

Hmm sounds like a refresh loop where it's triggering redrawing on every frame

@Jacalz
Copy link
Member

Jacalz commented Jan 29, 2024

Are there any other dialogs that also show this behaviour? Form perhaps?

@vinser
Copy link

vinser commented Jan 30, 2024

All my app dialogs (5 pieces) show this behavior except OpenFolder. Maybe because OpenFolder doesn't contain any other widgets.
Some of them are quite simple like Hot-keys list and the other like photo editor are as mini app
Screenshot at 2024-01-30 08-02-44
Screenshot at 2024-01-30 07-52-29

@vinser
Copy link

vinser commented Jan 30, 2024

You can look at https://github.com/vinser/pixyne/blob/master/m_dialogs.go if you wish

@vinser
Copy link

vinser commented Jan 30, 2024

Dialogs in fyne_demo works great - no high CPU load :)

@vinser
Copy link

vinser commented Jan 30, 2024

All but custom dialogs of fyne_demo work great - no high CPU load. But this one do

func colorPicked(c color.Color, w fyne.Window) {
	log.Println("Color picked:", c)
	rectangle := canvas.NewRectangle(c)
	size := 2 * theme.IconInlineSize()
	rectangle.SetMinSize(fyne.NewSize(size, size))
	dialog.ShowCustom("Color Picked", "Ok", rectangle, w)
}

Screenshot at 2024-01-30 08-47-32
So it's not my app feature :)

@dweymouth
Copy link
Contributor Author

So the one problematic dialog in fyne_demo is the single one that is created using dialog.NewCustom. More evidence that most dialogs are OK, it's just the CustomDialog type is somehow messed up

@andydotxyz
Copy link
Member

Good find, I can confirm that the colour chosen dialog in fyne_demo causes high CPU here too!

It would appear that on every walk it is generating new image textures.
I think this could relate to dialogs having an image, and custom dialogs setting them to nil - perhaps it is a duplicate of #4345...

@andydotxyz
Copy link
Member

I wonder if the following is a good solution? It doesn't seem to take the CPU usage to 0 though...

--- a/internal/painter/gl/draw.go
+++ b/internal/painter/gl/draw.go
@@ -36,6 +36,9 @@ func (p *painter) drawGradient(o fyne.CanvasObject, texCreator func(fyne.CanvasO
 }
 
 func (p *painter) drawImage(img *canvas.Image, pos fyne.Position, frame fyne.Size) {
+       if img.Image == nil && img.File == "" && img.Resource == nil || img.Translucency == 1.0 {
+               return
+       }
        p.drawTextureWithDetails(img, p.newGlImageTexture, pos, img.Size(), frame, img.FillMode, float32(img.Alpha()), 0)
 }

@dweymouth
Copy link
Contributor Author

Good sleuthing @andydotxyz - if the image is really the root cause, it could be solved for dialogs without even solving #4345 by setting the dialog's image to Hidden whenever it is empty

@andydotxyz
Copy link
Member

Good sleuthing @andydotxyz - if the image is really the root cause, it could be solved for dialogs without even solving #4345 by setting the dialog's image to Hidden whenever it is empty

Aha that is a much better plan :)

andydotxyz added a commit to andydotxyz/fyne that referenced this issue Feb 6, 2024
@andydotxyz
Copy link
Member

Fixed on develop, we will pick it into v2.4.4 as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants