Search code examples
androidopengl-esglsurfaceview

android glsurfaceview stretch to fit


I am using glsurfaceview and it's renderer to display GLES to the device full screen. This works as expected.

Now I want to add the option to "zoom" the output size. By this I mean (for example) render the GLES to a viewport a quarter of the screen and then stretch to the full screen when displaying. This will allow slower shaders to be rendered faster with a little blockiness.

I can currently shrink the GLES size altering glViewPort and it renders on the lower left corner of the screen. How can I get this lower quarter to stretch to fill the whole screen?

Many thanks for any tips/answers.

Solution after some more trial and error and googling. This is to render a quarter sized image to full screen.

Just after glsurfaceview is created;

glSurfaceView.getHolder().setFixedSize(glWidth,glHeight);

glHeight and glWidth are the internal GLES render size. So to render an image 1/4 of the screen use glWidth=screenWidth/2 and glHeight=screenHeight/2

And then within the onSurfaceChanged set the glViewPort;

glViewport(0, 0, screenWidth/2, screenHeight/2);

Bingo. Faster rendering (but 2x2 blocky pixels) output.

Hope that helps anyone else with this issue.


Solution

  • You already have the answer in your question, but I'll elaborate with some links.

    The size of the Surface is independent of the size of the View. If they are different, the display hardware scales the Surface as it is on its way to the display. This means you can set an arbitrary size for the Surface with the setFixedSize() call, which can be used to reduce the number of pixels that must be updated by Canvas or GLES.

    If you're drawing in software with Canvas, it can also allow you to avoid having to re-scale Bitmaps, because you can rely on the Surface being the same size on every device.

    There is an official blog post about the feature, and an example of its use in Grafika (video here).