diff options
author | Kissaki <kissaki@gmx.de> | 2011-10-02 20:44:08 +0400 |
---|---|---|
committer | Kissaki <kissaki@gmx.de> | 2013-06-12 01:03:31 +0400 |
commit | f09e943b6cc6965578dc41ae5dbde89b11729aa0 (patch) | |
tree | 25b1746dd7cab7f1ef1709f8832a6cdc7a95e60f /overlay_gl | |
parent | f8f4b0fdec6b1336f59ad70804390af99fc0f698 (diff) |
* Commenting, varnaming, fix indent
* commenting and adjusting debug output
* more descriptive varnaming
Diffstat (limited to 'overlay_gl')
-rw-r--r-- | overlay_gl/overlay.c | 55 |
1 files changed, 37 insertions, 18 deletions
diff --git a/overlay_gl/overlay.c b/overlay_gl/overlay.c index 0321341d7..b20f47091 100644 --- a/overlay_gl/overlay.c +++ b/overlay_gl/overlay.c @@ -77,9 +77,12 @@ typedef struct _Context { struct sockaddr_un saName; int iSocket; + // overlay message, temporary variable for processing from socket struct OverlayMsg omMsg; + // opengl overlay texture GLuint texture; + // overlay texture in shared memory unsigned char *a_ucTexture; unsigned int uiMappedLength; @@ -101,7 +104,7 @@ static const char vshader[] = "" static const char fshader[] = "" "uniform sampler2D tex;" "void main() {" - "gl_FragColor = texture2D(tex, gl_TexCoord[0].st);" + "gl_FragColor = texture2D(tex, gl_TexCoord[0].st);" "}"; const GLfloat fBorder[] = {0.125f, 0.250f, 0.5f, 0.75f}; @@ -221,7 +224,7 @@ static bool sendMessage(Context *ctx, struct OverlayMsg *om) { ssize_t sent = send(ctx->iSocket, om, wantsend, MSG_DONTWAIT); if (wantsend == sent) return true; - ods("Short write"); + ods("Short write. Disconnecting pipe."); } disconnect(ctx); return false; @@ -243,6 +246,7 @@ static void regenTexture(Context *ctx) { } static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { + // if no socket is active, initialize and connect to socket if (ctx->iSocket == -1) { releaseMem(ctx); if (! ctx->saName.sun_path[0]) @@ -259,11 +263,12 @@ static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { ods("connect() failure %s", ctx->saName.sun_path); return; } - ods("Connected"); + ods("Socket connected"); } + // if overlay size (width or height) is not up-to-date create and send an overlay initialization message if ((ctx->uiWidth != width) || (ctx->uiHeight != height)) { - ods("Sent init %i %i", width, height); + ods("Sending init overlay msg with w h %i %i", width, height); releaseMem(ctx); ctx->uiWidth = width; @@ -280,8 +285,10 @@ static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { return; } + // receive and process overlay messages while (1) { if (ctx->omMsg.omh.iLength == -1) { + // receive the overlay message header ssize_t length = recv(ctx->iSocket, ctx->omMsg.headerbuffer, sizeof(struct OverlayMsgHeader), 0); if (length < 0) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) @@ -289,11 +296,12 @@ static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { disconnect(ctx); return; } else if (length != sizeof(struct OverlayMsgHeader)) { - ods("Short header read"); + ods("Short header read on overlay message"); disconnect(ctx); return; } } else { + // receive the overlay message body ssize_t length = recv(ctx->iSocket, ctx->omMsg.msgbuffer, ctx->omMsg.omh.iLength, 0); if (length < 0) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) @@ -301,13 +309,15 @@ static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { disconnect(ctx); return; } else if (length != ctx->omMsg.omh.iLength) { - ods("Short message read %x %ld/%d", ctx->omMsg.omh.uiType, length, ctx->omMsg.omh.iLength); + ods("Short overlay message read %x %ld/%d", ctx->omMsg.omh.uiType, length, ctx->omMsg.omh.iLength); disconnect(ctx); return; } + // set len to -1 again for a clean state on next receive ctx->omMsg.omh.iLength = -1; switch (ctx->omMsg.omh.uiType) { + // shared memory overlay message: case OVERLAY_MSGTYPE_SHMEM: { struct OverlayMsgShmem *oms = (struct OverlayMsgShmem *) & ctx->omMsg.omi; ods("SHMEM %s", oms->a_cName); @@ -319,7 +329,9 @@ static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { if (buf.st_size >= ctx->uiWidth * ctx->uiHeight * 4) { ctx->uiMappedLength = buf.st_size; ctx->a_ucTexture = mmap(NULL, buf.st_size, PROT_READ, MAP_SHARED, fd, 0); + //TODO shouldn't -1 be MAP_FAILED ? if (ctx->a_ucTexture != (unsigned char *) -1) { + // mmap successfull; send a new bodyless sharedmemory overlay message and regenerate the overlay texture struct OverlayMsg om; om.omh.uiMagic = OVERLAY_MAGIC_NUMBER; om.omh.uiType = OVERLAY_MSGTYPE_SHMEM; @@ -339,6 +351,7 @@ static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { ods("Failed to map memory"); } break; + // blit overlay message: blit overlay texture from shared memory to gl-texture var case OVERLAY_MSGTYPE_BLIT: { struct OverlayMsgBlit *omb = & ctx->omMsg.omb; ods("BLIT %d %d %d %d", omb->x, omb->y, omb->w, omb->h); @@ -349,20 +362,23 @@ static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { ods("Optimzied fullscreen blit"); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ctx->uiWidth, ctx->uiHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, ctx->a_ucTexture); } else { + // allocate temporary memory unsigned int x = omb->x; unsigned int y = omb->y; unsigned int w = omb->w; unsigned int h = omb->h; unsigned char *ptr = (unsigned char *) malloc(w*h*4); - unsigned int r; + unsigned int row; memset(ptr, 0, w * h * 4); - for (r = 0; r < h; ++r) { - const unsigned char *sptr = ctx->a_ucTexture + 4 * ((y+r) * ctx->uiWidth + x); - unsigned char *dptr = ptr + 4 * w * r; + // copy overlay texture to temporary memory to adapt to full opengl ui size (overlay at correct place) + for (row = 0; row < h; ++row) { + const unsigned char *sptr = ctx->a_ucTexture + 4 * ((y+row) * ctx->uiWidth + x); + unsigned char *dptr = ptr + 4 * w * row; memcpy(dptr, sptr, w * 4); } + // copy temporary texture to opengl glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_BGRA, GL_UNSIGNED_BYTE, ptr); free(ptr); } @@ -387,6 +403,7 @@ static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { if ((ctx->a_ucTexture == NULL) || (ctx->texture == ~0U)) return; + // texture checks, that our gltexture is still valid and sane if (! glIsTexture(ctx->texture)) { ctx->texture = ~0; ods("Lost texture"); @@ -396,7 +413,7 @@ static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { GLfloat bordercolor[4]; glGetTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, bordercolor); if (bordercolor[0] != fBorder[0] || bordercolor[1] != fBorder[1] || bordercolor[2] != fBorder[2] || bordercolor[3] != fBorder[3]) { - ods("Texture hijacked"); + ods("Texture was hijacked! Texture will be regenerated."); regenTexture(ctx); } } @@ -412,16 +429,16 @@ static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { float right = (float)(ctx->uiRight); float bottom = (float)(ctx->uiBottom); - float xm = (left) / w; - float ym = (top) / h; - float xmx = (right) / w; - float ymx = (bottom) / h; + float xm = left / w; + float ym = top / h; + float xmx = right / w; + float ymx = bottom / h; GLfloat vertex[] = {left, bottom, - left, top, - right, top, - right, bottom}; + left, top, + right, top, + right, bottom}; GLfloat tex[] = {xm, ymx, xm, ym, xmx, ym, xmx, ymx}; glVertexPointer(2, GL_FLOAT, 0, vertex); glTexCoordPointer(2, GL_FLOAT, 0, tex); @@ -431,6 +448,7 @@ static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { } static void drawContext(Context * ctx, int width, int height) { + // calculate FPS and send it as an overlay message clock_t t = clock(); float elapsed = (float)(t - ctx->timeT) / CLOCKS_PER_SEC; ++(ctx->frameCount); @@ -586,6 +604,7 @@ static void drawContext(Context * ctx, int width, int height) { glViewport(viewport[0], viewport[1], viewport[2], viewport[3]); glUseProgram(program); + // drain opengl error queue while (glGetError() != GL_NO_ERROR); } |