Android: How to composite semi-transparent SDLSurface and another SurfaceView


#1

The Android wrapper for SDL2 defines SDLActivity, which automatically sets a subclass of SurfaceView (i.e. SDLSurface) as its contentView. Note by SDLSurface I am referring to the Java class; I don’t mean the SDL-internal SDL_Surface.

What I’m trying to do is render my SDL content alongside a video that is being output onto another (stock standard) SurfaceView by ExoPlayer. The aim is to have the video in the background and the SDL content as an overlay with transparent areas (through which the video is visible).

The Android docs seem to suggest that SurfaceViews are considered transparent, and I am able to layer a View with a semi-transparent background colour (and even the video) on top of my SDL content, but anything I render behind is invisible, because the SDLSurface’s background is opaque black.

My renderer (SDL_gpu) is theoretically clearing the context every frame with a transparent colour (0,0,0,0) and the background colour is an opaque black even if I don’t render anything at all. Changing the background colour to e.g. (50,50,50,50) again produces an opaque colour, not the expected semi-transparent one.

  • Is there are WindowFlag or similar that I’m missing to allow the SDLSurface to have a transparent background?
  • Would it help to try to get a transparent background working in a desktop environment (I saw some other older threads asking about this in that context)?

#2

I figured this out just after posting it. I’m going to leave it here because I’m excited this works at all.

In the constructor of SDLSurface, add the line: getHolder().setFormat(PixelFormat.RGBA_8888); to allow rendering an alpha channel (i.e. transparent background).