3

I have similar code to many examples floating around the net:

mSurfaceHolder = mVideoSurface.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setFormat(PixelFormat.TRANSPARENT);
mSurfaceHolder.setFixedSize(20, 10);

Then in the callbacks I have:

@Override public void surfaceCreated(SurfaceHolder holder)
{
    Log.d(TAG, "SurfaceCreated");
    mSurfaceHolder.setFixedSize(20, 10);
}

@Override public void surfaceChanged(
    SurfaceHolder holder, int format, int width, int height
)
{
    Log.d(TAG, "SurfaceChanged to " +
        format + " width " + width + " height " + height);
}

From this code I would expect the video surface to be set to the tiny size of 20x10 pixels, then scaled back up to whatever layout size I'm using it, showing a pixelated/blurred version. However, the video being played back looks right in its full native resolution, it doesn't scale down to 20x10. But I get logs like these:

SurfaceChanged to -2 width 20 height 10

So if the video surface is set to this tiny size, but graphically the video still looks high definition, what is the use of setting the surface size?

Full source code available at https://github.com/gradha/Stackoverflow38118219.

Grzegorz Adam Hankiewicz
  • 7,349
  • 1
  • 36
  • 78

1 Answers1

8

The size matters for things like OpenGL ES, where you draw on the Surface at whatever size it happens to be. When you're sending buffers of data from the camera or a video decoder, the buffers arrive at whatever size they were given by the camera or video. It's scaled to match the size of the SurfaceView's View, but it's not scaled to match the size of the SurfaceView's Surface.

The one place the two concepts cross is with the Camera2 preview API, which will apparently resize its capture to match the Surface in some cases.

You can read more about a primary use case in this blog post (demo here), and more about the graphics architecture as a whole in this doc.

fadden
  • 51,356
  • 5
  • 116
  • 166
  • I checked out the demo of Grafika. I set holder's size as OP inside `onSurfaceCreateded` to 100,100 . `onSurfaceChanged` width and height are both 100 but `Rect rect = holder.getSurfaceFrame();` returns the size of the Framelayout i used for preview. – Thracian Aug 25 '18 at 15:50
  • Is using has `mSurfaceHolder.setFixedSize(20, 10);` has no effect for camera preview? So changing it does not do anything for the camera preview right? Also what does happen when you setFixedSize greater than screen size? Do you get 4k drawings, i multiplied it on Grafika scaler test but i couldn't notice any difference. – Thracian Aug 25 '18 at 15:53
  • @fadden do you have any more detail _re_ the `Camera2 API` crossover? I'm using a SurfaceView that does NOT take up the fullscreen and the preview images a stretched to tall. – Mudlabs May 06 '19 at 06:38
  • @Mudlabs I'm currently having the same issue! How did you solve this? – mrousavy Jan 05 '21 at 11:17