# SurfaceHolder

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

# SurfaceHolder

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/view/SurfaceHolder "View this page in Kotlin")\|Java

`
public


interface
SurfaceHolder
`  
`


`

|----------------------------|
| android.view.SurfaceHolder |

<br />

*** ** * ** ***

Abstract interface to someone holding a display surface. Allows you to control the surface size and format, edit the pixels in the surface, and monitor changes to the surface. This interface is typically available through the[SurfaceView](https://developer.android.com/reference/android/view/SurfaceView)class.

When using this interface from a thread other than the one running its[SurfaceView](https://developer.android.com/reference/android/view/SurfaceView), you will want to carefully read the methods[lockCanvas()](https://developer.android.com/reference/android/view/SurfaceHolder#lockCanvas())and[Callback.surfaceCreated()](https://developer.android.com/reference/android/view/SurfaceHolder.Callback#surfaceCreated(android.view.SurfaceHolder)).

## Summary

|                                                                                                                                                                      ### Nested classes                                                                                                                                                                       ||
|--------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` class`     | [SurfaceHolder.BadSurfaceTypeException](https://developer.android.com/reference/android/view/SurfaceHolder.BadSurfaceTypeException) Exception that is thrown from[SurfaceHolder.lockCanvas()](https://developer.android.com/reference/android/view/SurfaceHolder#lockCanvas())when called on a Surface whose type is SURFACE_TYPE_PUSH_BUFFERS. |
| ` interface` | [SurfaceHolder.Callback](https://developer.android.com/reference/android/view/SurfaceHolder.Callback) A client may implement this interface to receive information about changes to the surface.                                                                                                                                                |
| ` interface` | [SurfaceHolder.Callback2](https://developer.android.com/reference/android/view/SurfaceHolder.Callback2) Additional callbacks that can be received for[Callback](https://developer.android.com/reference/android/view/SurfaceHolder.Callback).                                                                                                   |

|                                                                                                                 ### Constants                                                                                                                  ||
|-------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `int` | [SURFACE_TYPE_GPU](https://developer.android.com/reference/android/view/SurfaceHolder#SURFACE_TYPE_GPU) *This constant was deprecated in API level 15. this is ignored, this value is set automatically when needed.*                   |
| `int` | [SURFACE_TYPE_HARDWARE](https://developer.android.com/reference/android/view/SurfaceHolder#SURFACE_TYPE_HARDWARE) *This constant was deprecated in API level 15. this is ignored, this value is set automatically when needed.*         |
| `int` | [SURFACE_TYPE_NORMAL](https://developer.android.com/reference/android/view/SurfaceHolder#SURFACE_TYPE_NORMAL) *This constant was deprecated in API level 15. this is ignored, this value is set automatically when needed.*             |
| `int` | [SURFACE_TYPE_PUSH_BUFFERS](https://developer.android.com/reference/android/view/SurfaceHolder#SURFACE_TYPE_PUSH_BUFFERS) *This constant was deprecated in API level 15. this is ignored, this value is set automatically when needed.* |

|                                                                                                                                                                                                                  ### Public methods                                                                                                                                                                                                                   ||
|---------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` abstract void`                                                                      | ` `[addCallback](https://developer.android.com/reference/android/view/SurfaceHolder#addCallback(android.view.SurfaceHolder.Callback))`(`[SurfaceHolder.Callback](https://developer.android.com/reference/android/view/SurfaceHolder.Callback)` callback) ` Add a Callback interface for this holder.                                                           |
| ` abstract `[Surface](https://developer.android.com/reference/android/view/Surface)   | ` `[getSurface](https://developer.android.com/reference/android/view/SurfaceHolder#getSurface())`() ` Direct access to the surface object.                                                                                                                                                                                                                     |
| ` abstract `[Rect](https://developer.android.com/reference/android/graphics/Rect)     | ` `[getSurfaceFrame](https://developer.android.com/reference/android/view/SurfaceHolder#getSurfaceFrame())`() ` Retrieve the current size of the surface.                                                                                                                                                                                                      |
| ` abstract boolean`                                                                   | ` `[isCreating](https://developer.android.com/reference/android/view/SurfaceHolder#isCreating())`() ` Use this method to find out if the surface is in the process of being created from Callback methods.                                                                                                                                                     |
| ` abstract `[Canvas](https://developer.android.com/reference/android/graphics/Canvas) | ` `[lockCanvas](https://developer.android.com/reference/android/view/SurfaceHolder#lockCanvas())`() ` Start editing the pixels in the surface.                                                                                                                                                                                                                 |
| ` abstract `[Canvas](https://developer.android.com/reference/android/graphics/Canvas) | ` `[lockCanvas](https://developer.android.com/reference/android/view/SurfaceHolder#lockCanvas(android.graphics.Rect))`(`[Rect](https://developer.android.com/reference/android/graphics/Rect)` dirty) ` Just like[lockCanvas()](https://developer.android.com/reference/android/view/SurfaceHolder#lockCanvas())but allows specification of a dirty rectangle. |
| ` default `[Canvas](https://developer.android.com/reference/android/graphics/Canvas)  | ` `[lockHardwareCanvas](https://developer.android.com/reference/android/view/SurfaceHolder#lockHardwareCanvas())`() ` <br /> Just like[lockCanvas()](https://developer.android.com/reference/android/view/SurfaceHolder#lockCanvas())but the returned canvas is hardware-accelerated.                                                                          |
| ` abstract void`                                                                      | ` `[removeCallback](https://developer.android.com/reference/android/view/SurfaceHolder#removeCallback(android.view.SurfaceHolder.Callback))`(`[SurfaceHolder.Callback](https://developer.android.com/reference/android/view/SurfaceHolder.Callback)` callback) ` Removes a previously added Callback interface from this holder.                               |
| ` abstract void`                                                                      | ` `[setFixedSize](https://developer.android.com/reference/android/view/SurfaceHolder#setFixedSize(int,%20int))`(int width, int height) ` Make the surface a fixed size.                                                                                                                                                                                        |
| ` abstract void`                                                                      | ` `[setFormat](https://developer.android.com/reference/android/view/SurfaceHolder#setFormat(int))`(int format) ` Set the desired PixelFormat of the surface.                                                                                                                                                                                                   |
| ` abstract void`                                                                      | ` `[setKeepScreenOn](https://developer.android.com/reference/android/view/SurfaceHolder#setKeepScreenOn(boolean))`(boolean screenOn) ` Enable or disable option to keep the screen turned on while this surface is displayed.                                                                                                                                  |
| ` abstract void`                                                                      | ` `[setSizeFromLayout](https://developer.android.com/reference/android/view/SurfaceHolder#setSizeFromLayout())`() ` Allow the surface to resized based on layout of its container (this is the default).                                                                                                                                                       |
| ` abstract void`                                                                      | ` `[setType](https://developer.android.com/reference/android/view/SurfaceHolder#setType(int))`(int type) ` *This method was deprecated in API level 15. this is ignored, this value is set automatically when needed.*                                                                                                                                         |
| ` abstract void`                                                                      | ` `[unlockCanvasAndPost](https://developer.android.com/reference/android/view/SurfaceHolder#unlockCanvasAndPost(android.graphics.Canvas))`(`[Canvas](https://developer.android.com/reference/android/graphics/Canvas)` canvas) ` Finish editing pixels in the surface.                                                                                         |

## Constants

### SURFACE_TYPE_GPU

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Deprecated in[API level 15](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static final int SURFACE_TYPE_GPU
```

<br />

**This constant was deprecated in API level 15.**   
this is ignored, this value is set automatically when needed.

<br />

<br />

Constant Value: 2 (0x00000002)  

### SURFACE_TYPE_HARDWARE

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Deprecated in[API level 15](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static final int SURFACE_TYPE_HARDWARE
```

<br />

**This constant was deprecated in API level 15.**   
this is ignored, this value is set automatically when needed.

<br />

<br />

Constant Value: 1 (0x00000001)  

### SURFACE_TYPE_NORMAL

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Deprecated in[API level 15](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static final int SURFACE_TYPE_NORMAL
```

<br />

**This constant was deprecated in API level 15.**   
this is ignored, this value is set automatically when needed.

<br />

<br />

Constant Value: 0 (0x00000000)  

### SURFACE_TYPE_PUSH_BUFFERS

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Deprecated in[API level 15](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static final int SURFACE_TYPE_PUSH_BUFFERS
```

<br />

**This constant was deprecated in API level 15.**   
this is ignored, this value is set automatically when needed.

<br />

<br />

Constant Value: 3 (0x00000003)

## Public methods

### addCallback

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract void addCallback (SurfaceHolder.Callback callback)
```

Add a Callback interface for this holder. There can several Callback interfaces associated with a holder.

<br />

|                                Parameters                                ||
|------------|--------------------------------------------------------------|
| `callback` | `SurfaceHolder.Callback`: The new Callback interface. <br /> |

### getSurface

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract Surface getSurface ()
```

Direct access to the surface object. The Surface may not always be available -- for example when using a[SurfaceView](https://developer.android.com/reference/android/view/SurfaceView)the holder's Surface is not created until the view has been attached to the window manager and performed a layout in order to determine the dimensions and screen position of the Surface. You will thus usually need to implement[Callback.surfaceCreated](https://developer.android.com/reference/android/view/SurfaceHolder.Callback#surfaceCreated(android.view.SurfaceHolder))to find out when the Surface is available for use.

Note that if you directly access the Surface from another thread, it is critical that you correctly implement[Callback.surfaceCreated](https://developer.android.com/reference/android/view/SurfaceHolder.Callback#surfaceCreated(android.view.SurfaceHolder))and[Callback.surfaceDestroyed](https://developer.android.com/reference/android/view/SurfaceHolder.Callback#surfaceDestroyed(android.view.SurfaceHolder))to ensure that thread only accesses the Surface while it is valid, and that the Surface does not get destroyed while the thread is using it.

This method is intended to be used by frameworks which often need direct access to the Surface object (usually to pass it to native code).

<br />

|                                               Returns                                                ||
|-------------------------------------------------------------------------|-----------------------------|
| [Surface](https://developer.android.com/reference/android/view/Surface) | Surface The surface. <br /> |

### getSurfaceFrame

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract Rect getSurfaceFrame ()
```

Retrieve the current size of the surface. Note: do not modify the returned Rect. This is only safe to call from the thread of[SurfaceView](https://developer.android.com/reference/android/view/SurfaceView)'s window, or while inside of[lockCanvas()](https://developer.android.com/reference/android/view/SurfaceHolder#lockCanvas()).

<br />

|                                                                   Returns                                                                   ||
|-----------------------------------------------------------------------|----------------------------------------------------------------------|
| [Rect](https://developer.android.com/reference/android/graphics/Rect) | Rect The surface's dimensions. The left and top are always 0. <br /> |

### isCreating

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract boolean isCreating ()
```

Use this method to find out if the surface is in the process of being created from Callback methods. This is intended to be used with[Callback.surfaceChanged](https://developer.android.com/reference/android/view/SurfaceHolder.Callback#surfaceChanged(android.view.SurfaceHolder,%20int,%20int,%20int)).

<br />

|                                  Returns                                  ||
|-----------|----------------------------------------------------------------|
| `boolean` | true if the surface is in the process of being created. <br /> |

### lockCanvas

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract Canvas lockCanvas ()
```

Start editing the pixels in the surface. The returned Canvas can be used to draw into the surface's bitmap. A null is returned if the surface has not been created or otherwise cannot be edited. You will usually need to implement[Callback.surfaceCreated](https://developer.android.com/reference/android/view/SurfaceHolder.Callback#surfaceCreated(android.view.SurfaceHolder))to find out when the Surface is available for use.

The content of the Surface is never preserved between unlockCanvas() and lockCanvas(), for this reason, every pixel within the Surface area must be written. The only exception to this rule is when a dirty rectangle is specified, in which case, non-dirty pixels will be preserved.

If you call this repeatedly when the Surface is not ready (before[Callback.surfaceCreated](https://developer.android.com/reference/android/view/SurfaceHolder.Callback#surfaceCreated(android.view.SurfaceHolder))or after[Callback.surfaceDestroyed](https://developer.android.com/reference/android/view/SurfaceHolder.Callback#surfaceDestroyed(android.view.SurfaceHolder))), your calls will be throttled to a slow rate in order to avoid consuming CPU.

If null is not returned, this function internally holds a lock until the corresponding[unlockCanvasAndPost(Canvas)](https://developer.android.com/reference/android/view/SurfaceHolder#unlockCanvasAndPost(android.graphics.Canvas))call, preventing[SurfaceView](https://developer.android.com/reference/android/view/SurfaceView)from creating, destroying, or modifying the surface while it is being drawn. This can be more convenient than accessing the Surface directly, as you do not need to do special synchronization with a drawing thread in[Callback.surfaceDestroyed](https://developer.android.com/reference/android/view/SurfaceHolder.Callback#surfaceDestroyed(android.view.SurfaceHolder)).

<br />

|                                                        Returns                                                         ||
|---------------------------------------------------------------------------|---------------------------------------------|
| [Canvas](https://developer.android.com/reference/android/graphics/Canvas) | Canvas Use to draw into the surface. <br /> |

### lockCanvas

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract Canvas lockCanvas (Rect dirty)
```

Just like[lockCanvas()](https://developer.android.com/reference/android/view/SurfaceHolder#lockCanvas())but allows specification of a dirty rectangle. Every pixel within that rectangle must be written; however pixels outside the dirty rectangle will be preserved by the next call to lockCanvas().

<br />

|                             Parameters                             ||
|---------|-----------------------------------------------------------|
| `dirty` | `Rect`: Area of the Surface that will be modified. <br /> |

|                                                        Returns                                                         ||
|---------------------------------------------------------------------------|---------------------------------------------|
| [Canvas](https://developer.android.com/reference/android/graphics/Canvas) | Canvas Use to draw into the surface. <br /> |

**See also:**

- [lockCanvas()](https://developer.android.com/reference/android/view/SurfaceHolder#lockCanvas())  

### lockHardwareCanvas

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public Canvas lockHardwareCanvas ()
```

<br />

Just like[lockCanvas()](https://developer.android.com/reference/android/view/SurfaceHolder#lockCanvas())but the returned canvas is hardware-accelerated.

See the[unsupported drawing operations](https://developer.android.com/guide/topics/graphics/hardware-accel#unsupported)for a list of what is and isn't supported in a hardware-accelerated canvas.

<br />

|                                                        Returns                                                         ||
|---------------------------------------------------------------------------|---------------------------------------------|
| [Canvas](https://developer.android.com/reference/android/graphics/Canvas) | Canvas Use to draw into the surface. <br /> |

|                                                              Throws                                                               ||
|--------------------------------------------------------------------------------------------------|---------------------------------|
| [IllegalStateException](https://developer.android.com/reference/java/lang/IllegalStateException) | If the canvas cannot be locked. |

### removeCallback

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract void removeCallback (SurfaceHolder.Callback callback)
```

Removes a previously added Callback interface from this holder.

<br />

|                                   Parameters                                   ||
|------------|--------------------------------------------------------------------|
| `callback` | `SurfaceHolder.Callback`: The Callback interface to remove. <br /> |

### setFixedSize

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract void setFixedSize (int width, 
                int height)
```

Make the surface a fixed size. It will never change from this size. When working with a[SurfaceView](https://developer.android.com/reference/android/view/SurfaceView), this must be called from the same thread running the SurfaceView's window.

<br />

|                  Parameters                   ||
|----------|-------------------------------------|
| `width`  | `int`: The surface's width. <br />  |
| `height` | `int`: The surface's height. <br /> |

### setFormat

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract void setFormat (int format)
```

Set the desired PixelFormat of the surface. The default is OPAQUE. When working with a[SurfaceView](https://developer.android.com/reference/android/view/SurfaceView), this must be called from the same thread running the SurfaceView's window.

<br />

|                      Parameters                      ||
|----------|--------------------------------------------|
| `format` | `int`: A constant from PixelFormat. <br /> |

**See also:**

- [PixelFormat](https://developer.android.com/reference/android/graphics/PixelFormat)  

### setKeepScreenOn

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract void setKeepScreenOn (boolean screenOn)
```

Enable or disable option to keep the screen turned on while this surface is displayed. The default is false, allowing it to turn off. This is safe to call from any thread.

<br />

|                                                Parameters                                                ||
|------------|----------------------------------------------------------------------------------------------|
| `screenOn` | `boolean`: Set to true to force the screen to stay on, false to allow it to turn off. <br /> |

### setSizeFromLayout

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract void setSizeFromLayout ()
```

Allow the surface to resized based on layout of its container (this is the default). When this is enabled, you should monitor[Callback.surfaceChanged](https://developer.android.com/reference/android/view/SurfaceHolder.Callback#surfaceChanged(android.view.SurfaceHolder,%20int,%20int,%20int))for changes to the size of the surface. When working with a[SurfaceView](https://developer.android.com/reference/android/view/SurfaceView), this must be called from the same thread running the SurfaceView's window.

<br />

### setType

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Deprecated in[API level 15](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract void setType (int type)
```

<br />

**This method was deprecated in API level 15.**   
this is ignored, this value is set automatically when needed.

Sets the surface's type.

<br />

|      Parameters      ||
|--------|--------------|
| `type` | `int` <br /> |

### unlockCanvasAndPost

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract void unlockCanvasAndPost (Canvas canvas)
```

Finish editing pixels in the surface. After this call, the surface's current pixels will be shown on the screen, but its content is lost, in particular there is no guarantee that the content of the Surface will remain unchanged when lockCanvas() is called again.

<br />

|                                 Parameters                                 ||
|----------|------------------------------------------------------------------|
| `canvas` | `Canvas`: The Canvas previously returned by lockCanvas(). <br /> |

**See also:**

- [lockCanvas()](https://developer.android.com/reference/android/view/SurfaceHolder#lockCanvas())