---
title: CAMetalLayer
framework: quartzcore
role: symbol
role_heading: Class
path: quartzcore/cametallayer
---

# CAMetalLayer

A Core Animation layer that Metal can render into, typically displayed onscreen.

## Declaration

```swift
class CAMetalLayer
```

## Overview

Overview Use a CAMetalLayer when you want to use Metal to render a layer’s contents; for example, to render into a view. Consider using MTKView instead, because this class automatically wraps a CAMetalLayer object and provides a higher-level abstraction. If you’re using UIKit, to create a view that uses a CAMetalLayer, create a subclass of UIView and override its layerClass class method to return a CAMetalLayer: + (Class) layerClass {     return [CAMetalLayer class]; } If you’re using AppKit, configure an NSView object to use a backing layer and assign a CAMetalLayer object to the view: myView.wantsLayer = YES; myView.layer = [CAMetalLayer layer]; Adjust the layer’s properties to configure its underlying pixel format and other display behaviors. Rendering the Layer’s Contents A CAMetalLayer creates a pool of Metal drawable objects (CAMetalDrawable). At any given time, one of these drawable objects contains the contents of the layer. To change the layer’s contents, ask the layer for a drawable object, render into it, and then update the layer’s contents to point to the new drawable. Call the layer’s nextDrawable() method to obtain a drawable object. Get the drawable object’s texture and create a render pass that renders to that texture, as shown in the code below: CAMetalLayer *metalLayer = (CAMetalLayer*)self.layer; id<CAMetalDrawable> *drawable = [metalLayer nextDrawable];

MTLRenderPassDescriptor *renderPassDescriptor                                = [MTLRenderPassDescriptor renderPassDescriptor];

renderPassDescriptor.colorAttachments[0].texture = drawable.texture; renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear; renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0.0,0.0,0.0,1.0); ... To change the layer’s contents to the new drawable, call the present(_:) method (or one of its variants) on the command buffer containing the encoded render pass, passing in the drawable object to present. [commandBuffer presentDrawable:drawable]; Keeping References to Drawables The layer reuses a drawable only if it isn’t onscreen and there are no strong references to it. Further, if a drawable isn’t available when you call nextDrawable(), the system waits for one to become available. To avoid stalls in your app, request a new drawable only when you need it, and release any references to it as quickly as possible after you’re done with it. For example, before retrieving a new drawable, you might perform other work on the CPU or submit commands to the GPU that don’t require the drawable. Then, obtain the drawable and encode a command buffer to render into it, as described above. After you commit this command buffer, release all strong references to the drawable. If you don’t release drawables correctly, the layer runs out of drawables, and future calls to nextDrawable() return nil. Releasing the Drawable Don’t release the drawable explicitly; instead, embed your render loop within an autorelease pool block: This block releases drawables promptly and avoids possible deadlock situations with multiple drawables. Release drawables as soon as possible after committing your onscreen render pass. note: As of iOS 10 and tvOS 10, you can safely retain a drawable to query its properties, such as drawableID and presentedTime, after the system has presented it. If you don’t need to query these properties, release the drawable when you no longer need it.

## Topics

### Configuring the Metal Device

- [device](quartzcore/cametallayer/device.md)
- [preferredDevice](quartzcore/cametallayer/preferreddevice.md)

### Configuring the Layer’s Drawable Objects

- [pixelFormat](quartzcore/cametallayer/pixelformat.md)
- [colorspace](quartzcore/cametallayer/colorspace.md)
- [framebufferOnly](quartzcore/cametallayer/framebufferonly.md)
- [drawableSize](quartzcore/cametallayer/drawablesize.md)

### Configuring Presentation Behavior

- [presentsWithTransaction](quartzcore/cametallayer/presentswithtransaction.md)
- [displaySyncEnabled](quartzcore/cametallayer/displaysyncenabled.md)

### Configuring Extended Dynamic Range Behavior

- [wantsExtendedDynamicRangeContent](quartzcore/cametallayer/wantsextendeddynamicrangecontent.md)
- [edrMetadata](quartzcore/cametallayer/edrmetadata.md)

### Obtaining a Metal Drawable

- [nextDrawable()](quartzcore/cametallayer/nextdrawable().md)
- [maximumDrawableCount](quartzcore/cametallayer/maximumdrawablecount.md)
- [allowsNextDrawableTimeout](quartzcore/cametallayer/allowsnextdrawabletimeout.md)

### Configuring the Metal Performance HUD

- [developerHUDProperties](quartzcore/cametallayer/developerhudproperties.md)

### Instance Properties

- [residencySet](quartzcore/cametallayer/residencyset.md)

## Relationships

### Inherits From

- [CALayer](quartzcore/calayer.md)

### Conforms To

- [CAMediaTiming](quartzcore/camediatiming.md)
- [CVarArg](swift/cvararg.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)
- [NSCoding](foundation/nscoding.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
- [NSSecureCoding](foundation/nssecurecoding.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)

## See Also

### Metal and OpenGL

- [CAMetalDrawable](quartzcore/cametaldrawable.md)
- [CAEAGLLayer](quartzcore/caeagllayer.md)
- [CAEDRMetadata](quartzcore/caedrmetadata.md)
- [CAOpenGLLayer](quartzcore/caopengllayer.md)
- [CARenderer](quartzcore/carenderer.md)
