20

Can anyone tell me what the basic difference is between SurfaceView and GLSurfaceView? When should I use SurfaceView, and when should I use GLSurfaceView?

I read some already answered questions on Stack Overflow, but they did not satisfy my queries.

Any help would be appreciated.

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
AndroDev
  • 3,236
  • 8
  • 35
  • 49

3 Answers3

37

A GLSurfaceView is a SurfaceView that you can render into with OpenGL. Choosing between them is simple:

  • If you're familiar with OpenGL and need what it provides, use a GLSurfaceView.
  • Otherwise, use a SurfaceView.

OpenGL is low-level. If you're not already familiar with it, it's an undertaking to learn. If you only need 2D drawing, SurfaceView uses the high-level, reasonably high-performance Canvas. It's very easy to work with.

Unless you have a strong reason to use a GLSurfaceView, you should use a regular SurfaceView. I would suggest that if you don't already know that you need GL, then you probably don't.

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
  • Thank you for answer. I have one question...for Camera, which one would be better? – AndroDev Jun 14 '12 at 10:20
  • 2
    @AndroDev You're going to have to be a lot more specific. Although taking a stab at what you mean: if you want to show a preview of live images from the camera, you only need a `SurfaceView`: http://developer.android.com/guide/topics/media/camera.html#custom-camera – Darshan Rivka Whittle Jun 14 '12 at 10:29
  • 3
    Note that it's possible to use OpenGL with a `SurfaceView`. It's just less complicated to do it with a `GLSurfaceView`. See the [Graphics architecture document](http://source.android.com/devices/graphics/architecture.html#glsurfaceview). – Lii May 29 '14 at 10:47
  • This is a pretty bad answer. check @pierrotlefou's instead – frankelot Jul 23 '20 at 20:45
14

GLSurfaceView is the primary building block for 3D applications as View is for 2D applications. It is widely used not only in 3D games but also multimedia applications such as camera to create special preview affect.

GLSurfaceView extends SurfaceView and additionally owns a render thread and a render object set by the client. The render thread keeps running , continuously or on-demand, and delegates to the render object to draw frame using OpenGL API. For both SurfaceView and GLSurfaceView, rendering is performing in a separate thread other than main thread. The difference is with SurfaceView the rendering thread is created by client while with GLSurfaceView it is created by the system. What's more, GLSurfaceView will internally handle the synchronization between main thread and rendering thread.

For more, check out this and this

pierrotlefou
  • 39,805
  • 37
  • 135
  • 175
5

SurfaceView

AFAIK Canvas is Simple to implement and effective in 2D drawing but 3D drawing are not supported on it

GLSurfaceView

If you want to design some 3D Game then you shold go with GLSurfaceView and OGLES

Whats my experience is if you just want to do 2D processing then select Canvas because its easier to implement and effective compare to GLSurfaceView.

Vipul
  • 27,808
  • 7
  • 60
  • 75
  • In case of of 2D processing, why Canvas is effective? – AndroDev Jun 14 '12 at 09:32
  • 1
    Note that it's possible to use OpenGL with a `SurfaceView`. It's just less complicated to do it with a `GLSurfaceView`. See the [Graphics architecture document](http://source.android.com/devices/graphics/architecture.html#glsurfaceview). – Lii May 29 '14 at 10:47