D2D mix Graphics and Image

There has been a ton of good work over the last year getting Direct2D working well, thanks!

I have one issue that keeps coming up though, and I wonder if someone can make some suggestion.

Specifically, if you mix Image and Graphics methods in a paint routine, the UI will not render correctly.

So …. this

void Paint(Graphics& g) {

  myImage.multiplyAllAlphas(.9); // do all IMAGE methods

  Graphics myGraphic (myImage); // THEN create a graphic for the image

  myGraphic.drawLine(...)

  g.drawImage(myImage)

}

is fine …. but this ….

void Paint(Graphics& g) {

  Graphics myGraphic (myImage); // CREATE the graphic

  myImage.multiplyAllAlphas(.9); // THEN do something to the image

  myGraphic.drawLine(...)

  g.drawImage(myImage)

}

is broken ….

Presumably, this is something to do with the copying of the Image to the GPU. Once you copy the Image to Graphic, subsequent calls to alter the Image will go to the wrong copy of the image maybe?

Not totally my area, but I suspect this will make quick sense to someone.

I am of course just making sure to put the Graphics calls toward the end of any paint routine … but I wonder if there is some way you could add a jassert, or some other catch, when this happens.

I mostly program on mac, so when I make this mistake it is not immediately apparent to me (and then I get emails).

IIRC, in D2D, the image won’t be drawn until the graphics is destroyed. You may try something like this (I am not sure whether it will work):

void Paint(Graphics& g) {
  {
    Graphics myGraphic (myImage); // CREATE the graphic

    myImage.multiplyAllAlphas(.9); // THEN do something to the image

    myGraphic.drawLine(...)
  }
  g.drawImage(myImage)
}
2 Likes