Will CVPixelBuffer created from CVPixelBufferPoolCreatePixelBuffer keep existing pixels

We have a pixel buffer pool managed by the system(created using CVPixelBufferPoolCreate API). And each time when we need a pixel buffer, we call CVPixelBufferPoolCreatePixelBuffer to created one from the pool. Then we override all pixels of the buffer, getting IOSurface from the buffer, and then set the IOSurface as CALayer's contents property in another process to show it, everything works fine.

Now we want to do some optimization by only override pixels that's changed between frames. The way we'd like to do is that after we call CVPixelBufferPoolCreatePixelBuffer to create a buffer, we get the underlying IOSurface id map it with a frame info. Next time if we get the same IOSurface id, we just compare the current frame info with the one we stored and only update the changed pixels in CVPixelBuffer.

However, there is no document mentioning whether the CVPixelBuffer created using CVPixelBufferPoolCreatePixelBuffer will contain previous pixels(content before it's returned to the pool). Do we have this guarantee? If not, is there any way we can know whether the created buffer contains the previous pixels or not?

There is no guarantee. It would be better to keep a reference to the previous CVPixelBuffer, so will be sure it's the one you want.

@galad87 Thanks for the reply. Tthe reason why we don't keep a reference to previous buffer is that we don't know when CALayer will finish using the buffer, so we rely on the CVPixelBufferPool to return an unused buffer to us. Is there any way we can get whether a buffer is still used by CALayer?

Will CVPixelBuffer created from CVPixelBufferPoolCreatePixelBuffer keep existing pixels
 
 
Q