1

I currently have two different surfaces (one from SurfaceView and another surface created from MediaCodec).

What are the different ways available to copy from one surface to another?

dev.bmax
  • 8,998
  • 3
  • 30
  • 41
Ayyappa
  • 1,876
  • 1
  • 21
  • 41
  • Can you tell more about your use case? Are you trying to display and encode video at the same time? – dev.bmax Sep 13 '21 at 14:46
  • I'm planning to copy the data from another surface and pass it to persistent input surface of MediaCodec – Ayyappa Sep 13 '21 at 19:02
  • A Surface is a consumer of media buffers, but who is the producer in your case? – dev.bmax Sep 13 '21 at 20:38
  • Source => Surface View , Destination => Surface created from MediaCodec.CreatePersistentInputSurface(); – Ayyappa Sep 13 '21 at 20:50
  • Something is missing in the flow. SurfaceView doesn't produce frames, it provides a Surface for someone else to draw onto. – dev.bmax Sep 13 '21 at 21:00
  • Yes, Consider someone is drawing to a SurfaceView and I need to copy the contents of it to another surface. Does it makes sense? – Ayyappa Sep 14 '21 at 04:56

1 Answers1

0

In the Android graphics architecture a Surface plays the role of a consumer of buffers containing the graphical data (e.g. video frames).

A typical consumer does not provide access to the buffers that it holds. An exception is the special type ImageReader that allows direct application access to image data rendered into its Surface.

There is a less efficient way to copy the contents of a SurfaceView into a Bitmap using PixelCopy. While TextureView allows you to get the Bitmap directly.

You can then draw the Bitmap image onto another Surface using its Canvas.

Links:

https://source.android.com/devices/graphics/arch-sh

https://developer.android.com/reference/android/media/ImageReader

https://developer.android.com/reference/android/view/PixelCopy

https://developer.android.com/reference/android/view/Surface#lockCanvas(android.graphics.Rect)

https://developer.android.com/reference/android/graphics/Canvas#drawBitmap(android.graphics.Bitmap,%20android.graphics.Rect,%20android.graphics.Rect,%20android.graphics.Paint)

dev.bmax
  • 8,998
  • 3
  • 30
  • 41
  • If I have a surface, is it possible to attach to a SurfaceView or any drawing handler? – Ayyappa Sep 15 '21 at 06:48
  • Behind the scenes a Surface works with a queue of buffers. It has to release buffers back to the queue after consuming them. If a Surface would be connected to two different objects, how would it know that both objects are done handling the current buffer? – dev.bmax Sep 16 '21 at 17:22
  • Ok, can you please share some inputs on possibilities to draw on to the second surface if I have some content drawn on to a different surfaceview? I understand what you are saying but I didn't find a way to "copy/transfer" the contents drawn in one surfaceview to a surface (as i have surface alone but not any handler attached to it). – Ayyappa Sep 16 '21 at 20:21
  • A Surface is like an interface that allows you to draw on it, but not to read from it. If your Surface was created from a SurfaceView for example, then you can copy the Bitmap from the source SurfaceView using PixelCopy. Then you lock the canvas of the destination Surface and draw the Bitmap on it. – dev.bmax Sep 17 '21 at 07:38
  • Ok, we are getting closer, which is a great sign. Any other ways to copy other than with bitmap as I see it could be a costly operation to do it every frame and draw. – Ayyappa Sep 17 '21 at 07:50
  • I saw here (https://github.com/SkyDragonGPlay/GPlayUnity/blob/e10b95187e4e0a7caf14de97f43b8dec08b79966/framework/UnityRuntime/src/main/java/com/unity3d/player/UnityPlayer.java#L248) that they are able to create a surfaceview from surface. So wondering if its possible to do the same. – Ayyappa Sep 17 '21 at 08:54
  • If I'm not wrong, the above example they create a regular SurfaceView, take its Surface and draw on it with OpenGL. – dev.bmax Sep 17 '21 at 09:44
  • Can you please share any pointers on drawing to surface with OpenGL? – Ayyappa Sep 17 '21 at 13:08
  • Docs are pretty useful: https://developer.android.com/guide/topics/graphics/opengl – dev.bmax Sep 17 '21 at 14:29
  • This project has a lot if example code: https://github.com/google/grafika – dev.bmax Sep 17 '21 at 14:30
  • @dev.bmax Assuming I had an `ImageReader`, how would I be able to write those `Image`s to another Surface (e.g. one from MediaRecorder)? https://stackoverflow.com/questions/76914334/ – mrousavy Aug 16 '23 at 14:04