How to add a OpenGl object into a surfaceView camera? - android

I'm not certain how to implement the object made from OpenGl into the surfaceView of the Camera. Is it possible to set the OpenGl objectinto a custom android camera that uses surfaceView. ?

No.
The SurfaceView's Surface has a producer-consumer relationship. In this case, the producer is the Camera, and the consumer is the system compositor (surfaceflinger). To draw on the Surface with OpenGL ES, you would need to attach OpenGL as the producer, and a Surface cannot have two producers at the same time.
What you can do instead is create a second SurfaceView, and call setZOrderMediaOverlay() to put it above the Camera-driven Surface but below the UI, then render on that with GLES. See Grafika's "multi-surface test" for a demonstration of overlapping SurfaceViews.

Related

Android : Single SurfaceView vs multiple SurfaceView

I am trying to draw 3D object onto camera preview frames (Android). Should I use two surface views, one for camera preview and another GLSurfaceView for drawing. The views should be synchronized and frame rate of display should be good enough to provide a good user experience. So most of the tutorials talk about using multiple views. The alternate idea is to get texture from camera preview and merge it with the 3D object to be drawn so as to get the appropriate 2D raster image.
Which method would be better for performance gains?
P.S : I will be using the Java APIs for openGL es 2.0
Since two surface views increase the number of API calls per frame and require transparency, they will be slower.
You don't need two surface views for your purpose.
Disable depth writes.
Render the camera preview on a 2D quad filling the screen.
Enable depth writes.
Render 3D object.
This will make sure your 3D objects are rendered over the camera preview.
You can also achieve this with two surface views and transparency, but it will be slower.

Need to draw to surface used by MediaRecorder

I need to record a video that just contains one single frame: an image specified by the user (it can be of any length, but it will only have the same static image). So, I figured I could use the new MediaRecorder.VideoSource.SURFACE and just draw to the Surface being used by the recorder. I initialize the recorder properly, and I can even call MediaRecorder.getSurface() without an exception (something that is apparently tricky).
My problem is somewhat embarrassing: I don't know what to do with the surface returned. I need to draw to it somehow, but all examples I can find involve drawing to a SurfaceView. Is this surface the same surface used by MediaRecorder.setPreviewDisplay()? How do I draw something to it?
In theory you can use Surface#lockCanvas() to get a Canvas to draw on if you want to render in software. There used to be problems with this on some platforms; not sure if that has been fixed.
The other option is to create an EGLSurface from the Surface and render onto it with OpenGL ES. You can find examples of this, with some code to manage all of the EGL setup, in Grafika.
The GLES recording examples uses MediaCodec rather than MediaRecorder, but the idea is the same, and it should be much simpler with MediaRecorder.

3D transformation for SurfaceView in Android?

I tried to apply some 3D transformation (such as setRotationX) on a surfaceview which is used for camera previewing, but only the frame changes and the content don't.
A SurfaceView has two parts, the Surface and the View. The Surface is a separate layer that is rendered and composited independently. The View part is, by default, a transparent rectangle that creates a "hole" in the View layer, so that you can see through the Views to the Surface behind it.
The transformation you mention (setRotationX()) is a View method, but the camera preview is sent to the Surface. That's why the frame changed but the preview itself didn't.
You can send your preview to a TextureView, which can take an arbitrary transformation matrix (setTransform()), by using the Camera.setPreviewTexture() method. Or you can send it through a SurfaceTexture to an OpenGL ES texture, which can be rendered on the SurfaceView's Surface, using whatever GLES transformations you want. For an example of the latter, see Grafika's "texture from Camera" Activity.

Android: Dual Pass Render To SurfaceTexture Using OpenGL

In order to perform a Gaussian blur on a SurfaceTexture, I am performing a dual pass render, meaning that I am passing the texture through one shader (horizontal blur) and then through another shader (vertical blur).
I understand the theory behind this: render the first texture to an FBO and the second one onto the SurfaceTexture itself.
There are some examples of this, but none of them seem applicable since a SurfaceTexture uses GL_TEXTURE_EXTERNAL_OES as its target in glBindTexture rather than GL_TEXTURE_2D. Therefore, in the call to glFramebufferTexture2D, GL_TEXTURE_2D cannot be used as the textarget, and I don't think GL_TEXTURE_EXTERNAL_OES can be used in this call.
Can anyone suggest a way to render a texture twice, with the final rendering going to a SurfaceTexture?
Important update: I am using a SurfaceTexture since this is a dynamic blur of a video that plays onto a surface.
Edit: This question was asked with some misunderstanding on my part. A SurfaceTexture is not a display element. It instead receives data from a surface, and is attached to a GL_TEXTURE_EXTERNAL_OES.
Thank you.
Rendering to a SurfaceTexture seems like an odd thing to do here. The point of SurfaceTexture is to take whatever is sent to the Surface and convert it into a GLES "external" texture. Since you're rendering with GLES, you can just use an FBO to render into a GL_TEXTURE_2D for your second pass.
SurfaceTexture is used when receiving frames from Camera or a video decoder because the source is usually YUV. The "external" texture format allows for a wider range of pixel formats, but constrains the uses of the texture. There's no value in rendering to a SurfaceTexture with GLES if your goal is to create a GLES texture.

How does GLSurfaceView connect with surfaceflinger?

Recently I am confused with GLSurfaceView. In java level, it use glsurfaceview and how it connect with surfaceflinger?
thanks
james.
Using GLSurfaceView , you will draw into a EGLSurface which is created from A Surface. The surface ,inside of surfaceFlinger, is called SurfaceTexture with a BufferQueue . Therefore, SurfacFlinger still own that piece of memory the GLSurfaceView will be drawing to and thus could composite it.
Have a look here. In brief:
The surface flinger is the compositor and handles drawing Surface instances.
A GLSurfaceView contains a Surface that can be drawn to using OpenGL ES.

Categories

Resources