Use utility framebuffer in ViewDoneWritingSimulatorWorkaround
This code needs a FBO to bind the texture. Fixes invalid results when running under simulator. PiperOrigin-RevId: 499241867
This commit is contained in:
parent
9252a025e5
commit
2f4bb5d545
|
@ -74,6 +74,9 @@ GlTextureView GpuBufferStorageCvPixelBuffer::GetReadView(
|
||||||
static void ViewDoneWritingSimulatorWorkaround(CVPixelBufferRef pixel_buffer,
|
static void ViewDoneWritingSimulatorWorkaround(CVPixelBufferRef pixel_buffer,
|
||||||
const GlTextureView& view) {
|
const GlTextureView& view) {
|
||||||
CHECK(pixel_buffer);
|
CHECK(pixel_buffer);
|
||||||
|
auto ctx = GlContext::GetCurrent().get();
|
||||||
|
if (!ctx) ctx = view.gl_context();
|
||||||
|
ctx->Run([pixel_buffer, &view, ctx] {
|
||||||
CVReturn err = CVPixelBufferLockBaseAddress(pixel_buffer, 0);
|
CVReturn err = CVPixelBufferLockBaseAddress(pixel_buffer, 0);
|
||||||
CHECK(err == kCVReturnSuccess)
|
CHECK(err == kCVReturnSuccess)
|
||||||
<< "CVPixelBufferLockBaseAddress failed: " << err;
|
<< "CVPixelBufferLockBaseAddress failed: " << err;
|
||||||
|
@ -82,34 +85,40 @@ static void ViewDoneWritingSimulatorWorkaround(CVPixelBufferRef pixel_buffer,
|
||||||
uint8_t* pixel_ptr =
|
uint8_t* pixel_ptr =
|
||||||
static_cast<uint8_t*>(CVPixelBufferGetBaseAddress(pixel_buffer));
|
static_cast<uint8_t*>(CVPixelBufferGetBaseAddress(pixel_buffer));
|
||||||
if (pixel_format == kCVPixelFormatType_32BGRA) {
|
if (pixel_format == kCVPixelFormatType_32BGRA) {
|
||||||
// TODO: restore previous framebuffer? Move this to helper so we
|
glBindFramebuffer(GL_FRAMEBUFFER, kUtilityFramebuffer.Get(*ctx));
|
||||||
// can use BindFramebuffer?
|
|
||||||
glViewport(0, 0, view.width(), view.height());
|
glViewport(0, 0, view.width(), view.height());
|
||||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, view.target(),
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
|
||||||
view.name(), 0);
|
view.target(), view.name(), 0);
|
||||||
|
|
||||||
size_t contiguous_bytes_per_row = view.width() * 4;
|
size_t contiguous_bytes_per_row = view.width() * 4;
|
||||||
if (bytes_per_row == contiguous_bytes_per_row) {
|
if (bytes_per_row == contiguous_bytes_per_row) {
|
||||||
glReadPixels(0, 0, view.width(), view.height(), GL_BGRA, GL_UNSIGNED_BYTE,
|
glReadPixels(0, 0, view.width(), view.height(), GL_BGRA,
|
||||||
pixel_ptr);
|
GL_UNSIGNED_BYTE, pixel_ptr);
|
||||||
} else {
|
} else {
|
||||||
|
// TODO: use GL_PACK settings for row length. We can expect
|
||||||
|
// GLES 3.0 on iOS now.
|
||||||
std::vector<uint8_t> contiguous_buffer(contiguous_bytes_per_row *
|
std::vector<uint8_t> contiguous_buffer(contiguous_bytes_per_row *
|
||||||
view.height());
|
view.height());
|
||||||
uint8_t* temp_ptr = contiguous_buffer.data();
|
uint8_t* temp_ptr = contiguous_buffer.data();
|
||||||
glReadPixels(0, 0, view.width(), view.height(), GL_BGRA, GL_UNSIGNED_BYTE,
|
glReadPixels(0, 0, view.width(), view.height(), GL_BGRA,
|
||||||
temp_ptr);
|
GL_UNSIGNED_BYTE, temp_ptr);
|
||||||
for (int i = 0; i < view.height(); ++i) {
|
for (int i = 0; i < view.height(); ++i) {
|
||||||
memcpy(pixel_ptr, temp_ptr, contiguous_bytes_per_row);
|
memcpy(pixel_ptr, temp_ptr, contiguous_bytes_per_row);
|
||||||
temp_ptr += contiguous_bytes_per_row;
|
temp_ptr += contiguous_bytes_per_row;
|
||||||
pixel_ptr += bytes_per_row;
|
pixel_ptr += bytes_per_row;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// TODO: restore previous framebuffer?
|
||||||
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
|
||||||
|
view.target(), 0, 0);
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
} else {
|
} else {
|
||||||
LOG(ERROR) << "unsupported pixel format: " << pixel_format;
|
LOG(ERROR) << "unsupported pixel format: " << pixel_format;
|
||||||
}
|
}
|
||||||
err = CVPixelBufferUnlockBaseAddress(pixel_buffer, 0);
|
err = CVPixelBufferUnlockBaseAddress(pixel_buffer, 0);
|
||||||
CHECK(err == kCVReturnSuccess)
|
CHECK(err == kCVReturnSuccess)
|
||||||
<< "CVPixelBufferUnlockBaseAddress failed: " << err;
|
<< "CVPixelBufferUnlockBaseAddress failed: " << err;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
#endif // TARGET_IPHONE_SIMULATOR
|
#endif // TARGET_IPHONE_SIMULATOR
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user