Does SurfaceTexture keep a pause / resume cycle?

Below is a small experiment in which I attach a SurfaceTextureListener to a TextureView in the onResume () part of the action.

  • on new start: surface texture becomes available ("onSurfaceTextureAvailable")

  • when changing the orientation: as expected, the surface texture is destroyed ("onSurfaceTextureDestroyed"), and then the surface texture is obtained ("onSurfaceTextureAvailable").

Now here is what surprises me:

  • on the task switch and then resume: the surface texture is not destroyed (no "onSurfaceTextureDestroyed"), and the surface texture is not available (no "onSurfaceTextureAvailable")

Under these conditions, am I sure that the old SurfaceTexture is valid? How can I make sure that I really know when my SurfaceTexture is available? Why is changing orientation different from task switching and how should I know?


public class MainActivity extends AppCompatActivity { private final String TAG = "MainActivity"; TextureView mTextureView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextureView = (TextureView) findViewById(R.id.textureView); } @Override public void onResume() { super.onResume(); mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { Log.d(TAG, "onSurfaceTextureAvailable"); } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { Log.d(TAG, "onSurfaceTextureSizeChanged"); } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { Log.d(TAG, "onSurfaceTextureDestroyed"); return false; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surface) { } }); } } 
+5
source share
1 answer

I would say that this is not a problem, and that this is not β€œnormal” or β€œexpected” behavior. This is due to the hardware of your device and how the system handles overloading in resources or changes in music.

Your device will have a very flexible graphics accelerator, then your surfaces can survive a change in orientation. Look at the name of the device, the video mode changes completely and remembers those times when programming for desktop devices, when all DirectX surfaces (surfaces are video memory), must be recreated after changing the video mode from the desktop screen to full screen or semaphore.

The same thing happens when, maybe, your application is a difficult game to use resources, which uses the entire amount of video memory, both for surfaces for textures or back buffers, or so ... If you switch to another application, and this new one is required surface space, some or all of your video memory must be killed for this new screen to work.

And so ... See? ... It may happen that you don’t use all the significant part of the video memory or you just have a device with a poor supply with the latest software that can process as light or soft changes that do not destroy your resources.

The moral of my otherwise useless speech is that you must handle destruction if it is destroyed, even if it never happens on your device, or you will have many exceptions trying to block the canvas or render to zero.

0
source

Source: https://habr.com/ru/post/1240279/


All Articles