[client/obs] improve frambuffer_read functions to support copy pitch

Fixes #244
This commit is contained in:
Geoffrey McRae 2020-04-14 13:27:07 +10:00
parent ead09ed110
commit f6691a90c0
6 changed files with 68 additions and 36 deletions

View File

@ -1 +1 @@
B1-173-g2e760c09b4+1 B1-173-gead09ed110+1

View File

@ -33,7 +33,8 @@ Place, Suite 330, Boston, MA 02111-1307 USA
struct EGL_Texture struct EGL_Texture
{ {
enum EGL_PixelFormat pixFmt; enum EGL_PixelFormat pixFmt;
size_t width, height; size_t width, height, stride;
size_t bpp;
bool streaming; bool streaming;
bool ready; bool ready;
@ -106,6 +107,7 @@ bool egl_texture_setup(EGL_Texture * texture, enum EGL_PixelFormat pixFmt, size_
texture->pixFmt = pixFmt; texture->pixFmt = pixFmt;
texture->width = width; texture->width = width;
texture->height = height; texture->height = height;
texture->stride = stride;
texture->streaming = streaming; texture->streaming = streaming;
texture->ready = false; texture->ready = false;
@ -113,6 +115,7 @@ bool egl_texture_setup(EGL_Texture * texture, enum EGL_PixelFormat pixFmt, size_
{ {
case EGL_PF_BGRA: case EGL_PF_BGRA:
textureCount = 1; textureCount = 1;
texture->bpp = 4;
texture->format = GL_BGRA; texture->format = GL_BGRA;
texture->planes[0][0] = width; texture->planes[0][0] = width;
texture->planes[0][1] = height; texture->planes[0][1] = height;
@ -125,6 +128,7 @@ bool egl_texture_setup(EGL_Texture * texture, enum EGL_PixelFormat pixFmt, size_
case EGL_PF_RGBA: case EGL_PF_RGBA:
textureCount = 1; textureCount = 1;
texture->bpp = 4;
texture->format = GL_RGBA; texture->format = GL_RGBA;
texture->planes[0][0] = width; texture->planes[0][0] = width;
texture->planes[0][1] = height; texture->planes[0][1] = height;
@ -137,6 +141,7 @@ bool egl_texture_setup(EGL_Texture * texture, enum EGL_PixelFormat pixFmt, size_
case EGL_PF_RGBA10: case EGL_PF_RGBA10:
textureCount = 1; textureCount = 1;
texture->bpp = 4;
texture->format = GL_RGBA; texture->format = GL_RGBA;
texture->planes[0][0] = width; texture->planes[0][0] = width;
texture->planes[0][1] = height; texture->planes[0][1] = height;
@ -149,6 +154,7 @@ bool egl_texture_setup(EGL_Texture * texture, enum EGL_PixelFormat pixFmt, size_
case EGL_PF_YUV420: case EGL_PF_YUV420:
textureCount = 3; textureCount = 3;
texture->bpp = 4;
texture->format = GL_RED; texture->format = GL_RED;
texture->planes[0][0] = width; texture->planes[0][0] = width;
texture->planes[0][1] = height; texture->planes[0][1] = height;
@ -287,7 +293,16 @@ bool egl_texture_update_from_frame(EGL_Texture * texture, const FrameBuffer * fr
if (texture->pboCount == 2) if (texture->pboCount == 2)
return true; return true;
framebuffer_read(frame, texture->pboMap[texture->pboWIndex], texture->pboBufferSize); framebuffer_read(
frame,
texture->pboMap[texture->pboWIndex],
texture->stride,
texture->height,
texture->width,
texture->bpp,
texture->stride
);
texture->pboSync[texture->pboWIndex] = 0; texture->pboSync[texture->pboWIndex] = 0;
if (++texture->pboWIndex == 2) if (++texture->pboWIndex == 2)
@ -383,4 +398,4 @@ enum EGL_TexStatus egl_texture_bind(EGL_Texture * texture)
int egl_texture_count(EGL_Texture * texture) int egl_texture_count(EGL_Texture * texture)
{ {
return texture->textureCount; return texture->textureCount;
} }

View File

@ -1274,14 +1274,19 @@ static bool draw_frame(struct Inst * this)
glBindTexture(GL_TEXTURE_2D, this->frames[this->texIndex]); glBindTexture(GL_TEXTURE_2D, this->frames[this->texIndex]);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vboID[this->texIndex]); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vboID[this->texIndex]);
glPixelStorei(GL_UNPACK_ALIGNMENT , 4); const int bpp = this->format.bpp / 8;
glPixelStorei(GL_UNPACK_ROW_LENGTH , this->format.stride); glPixelStorei(GL_UNPACK_ALIGNMENT , bpp);
glPixelStorei(GL_UNPACK_ROW_LENGTH, this->format.width);
this->texPos = 0; this->texPos = 0;
framebuffer_read_fn( framebuffer_read_fn(
this->frame, this->frame,
this->format.height,
this->format.width,
bpp,
this->format.pitch,
opengl_buffer_fn, opengl_buffer_fn,
this->format.height * this->format.stride * 4,
this this
); );

View File

@ -40,12 +40,14 @@ void framebuffer_wait(const FrameBuffer * frame, size_t size);
/** /**
* Read data from the KVMFRFrame into the dst buffer * Read data from the KVMFRFrame into the dst buffer
*/ */
bool framebuffer_read(const FrameBuffer * frame, void * dst, size_t size); bool framebuffer_read(const FrameBuffer * frame, void * dst, size_t dstpitch,
size_t height, size_t width, size_t bpp, size_t pitch);
/** /**
* Read data from the KVMFRFrame using a callback * Read data from the KVMFRFrame using a callback
*/ */
bool framebuffer_read_fn(const FrameBuffer * frame, FrameBufferReadFn fn, size_t size, void * opaque); bool framebuffer_read_fn(const FrameBuffer * frame, size_t height, size_t width,
size_t bpp, size_t pitch, FrameBufferReadFn fn, void * opaque);
/** /**
* Prepare the framebuffer for writing * Prepare the framebuffer for writing

View File

@ -38,53 +38,55 @@ void framebuffer_wait(const FrameBuffer * frame, size_t size)
while(atomic_load_explicit(&frame->wp, memory_order_relaxed) != size) {} while(atomic_load_explicit(&frame->wp, memory_order_relaxed) != size) {}
} }
bool framebuffer_read(const FrameBuffer * frame, void * dst, size_t size)
bool framebuffer_read(const FrameBuffer * frame, void * dst, size_t dstpitch,
size_t height, size_t width, size_t bpp, size_t pitch)
{ {
uint8_t *d = (uint8_t*)dst; uint8_t *d = (uint8_t*)dst;
uint64_t rp = 0; uint_least32_t rp = 0;
while(rp < size) size_t y = 0;
const size_t linewidth = width * bpp;
while(y < height)
{ {
uint_least32_t wp; uint_least32_t wp;
/* spinlock */ /* spinlock */
do do
wp = atomic_load_explicit(&frame->wp, memory_order_relaxed); wp = atomic_load_explicit(&frame->wp, memory_order_relaxed);
while(rp == wp); while(wp - rp < pitch);
/* copy what we can */ memcpy(d, frame->data + rp, linewidth);
uint64_t avail = wp - rp;
avail = avail > size ? size : avail;
memcpy(d, frame->data + rp, avail); rp += pitch;
d += dstpitch;
rp += avail; ++y;
d += avail;
size -= avail;
} }
return true; return true;
} }
bool framebuffer_read_fn(const FrameBuffer * frame, FrameBufferReadFn fn, size_t size, void * opaque) bool framebuffer_read_fn(const FrameBuffer * frame, size_t height, size_t width,
size_t bpp, size_t pitch, FrameBufferReadFn fn, void * opaque)
{ {
uint64_t rp = 0; uint_least32_t rp = 0;
while(rp < size) size_t y = 0;
const size_t linewidth = width * bpp;
while(y < height)
{ {
uint_least32_t wp; uint_least32_t wp;
/* spinlock */ /* spinlock */
do do
wp = atomic_load_explicit(&frame->wp, memory_order_relaxed); wp = atomic_load_explicit(&frame->wp, memory_order_relaxed);
while(rp == wp); while(wp - rp < pitch);
/* copy what we can */ if (!fn(opaque, frame->data + rp, linewidth))
uint64_t avail = wp - rp;
avail = avail > size ? size : avail;
if (!fn(opaque, frame->data + rp, avail))
return false; return false;
rp += avail; rp += pitch;
size -= avail; ++y;
} }
return true; return true;

View File

@ -169,8 +169,16 @@ static void lgVideoTick(void * data, float seconds)
uint8_t *texData; uint8_t *texData;
uint32_t linesize; uint32_t linesize;
gs_texture_map(this->texture, &texData, &linesize); gs_texture_map(this->texture, &texData, &linesize);
if (linesize == frame->pitch)
framebuffer_read(fb, texData, frame->height * frame->pitch); framebuffer_read(
fb,
texData, // dst
linesize, // dstpitch
frame->height, // height
frame->width, // width
4, // bpp
frame->pitch // linepitch
);
gs_texture_unmap(this->texture); gs_texture_unmap(this->texture);
// gs_texture_set_image(this->texture, frameData, frame->pitch, false); // gs_texture_set_image(this->texture, frameData, frame->pitch, false);
@ -229,4 +237,4 @@ struct obs_source_info lg_source =
.get_width = lgGetWidth, .get_width = lgGetWidth,
.get_height = lgGetHeight, .get_height = lgGetHeight,
// .icon_type = OBS_ICON_TYPE_DESKTOP_CAPTURE // .icon_type = OBS_ICON_TYPE_DESKTOP_CAPTURE
}; };