Skip to content

Commit

Permalink
code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
koral-- committed Sep 11, 2016
1 parent bb482e6 commit 039f3e0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
19 changes: 10 additions & 9 deletions src/main/jni/opengl.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@ static void *slurp(void *pVoidInfo) {
long renderStartTime = getRealTime();
//TODO only advance frame index if cacheAnimation is enabled and frame is cached
DDGifSlurp(info, true, false);
TexImageDescriptor *texImageDescriptor = info->frameBufferDescriptor;
pthread_mutex_lock(&texImageDescriptor->renderMutex);
TexImageDescriptor *descriptor = info->frameBufferDescriptor;
pthread_mutex_lock(&descriptor->renderMutex);
if (info->currentIndex == 0) {
prepareCanvas(texImageDescriptor->frameBuffer, info);
prepareCanvas(descriptor->frameBuffer, info);
}
const uint_fast32_t frameDuration = getBitmap(texImageDescriptor->frameBuffer, info, true);
pthread_mutex_unlock(&texImageDescriptor->renderMutex);
const uint_fast32_t frameDuration = getBitmap(descriptor->frameBuffer, info, false);
pthread_mutex_unlock(&descriptor->renderMutex);

const long long invalidationDelayMillis = calculateInvalidationDelay(info, renderStartTime, frameDuration);
int pollResult = poll(&texImageDescriptor->eventPollFd, 1, (int) invalidationDelayMillis);
int pollResult = poll(&descriptor->eventPollFd, 1, (int) invalidationDelayMillis);
eventfd_t eventValue;
if (pollResult < 0) {
throwException(getEnv(), RUNTIME_EXCEPTION_ERRNO, "Could not poll on eventfd ");
break;
} else if (pollResult > 0) {
const int readResult = TEMP_FAILURE_RETRY(eventfd_read(texImageDescriptor->eventPollFd.fd, &eventValue));
const int readResult = TEMP_FAILURE_RETRY(eventfd_read(descriptor->eventPollFd.fd, &eventValue));
if (readResult != 0) {
throwException(getEnv(), RUNTIME_EXCEPTION_ERRNO, "Could not read from eventfd ");
}
Expand Down Expand Up @@ -186,9 +186,10 @@ Java_pl_droidsonroids_gif_GifInfoHandle_renderFrameGL(JNIEnv *__unused env, jcla
}
TexImageDescriptor *descriptor = info->frameBufferDescriptor;
pthread_mutex_lock(&descriptor->renderMutex);
const GLenum target = (const GLenum) rawTarget;
GLuint *currentTextureName = descriptor->glTextureNames + info->currentIndex;
if (*currentTextureName != 0) {
glBindTexture(GL_TEXTURE_2D, *currentTextureName);
glBindTexture(target, *currentTextureName);
} else {
GLuint *framebufferName = &descriptor->glFramebufferName;
if (*framebufferName == 0) {
Expand All @@ -197,7 +198,7 @@ Java_pl_droidsonroids_gif_GifInfoHandle_renderFrameGL(JNIEnv *__unused env, jcla
glGenTextures(1, currentTextureName);//TODO delete
const GLsizei width = (const GLsizei) info->gifFilePtr->SWidth;
const GLsizei height = (const GLsizei) info->gifFilePtr->SHeight;
const GLenum target = (const GLenum) rawTarget;

glBindTexture(target, *currentTextureName);
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
Expand Down
32 changes: 18 additions & 14 deletions src/main/jni/surface.c
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
#include "gif.h"
#include <android/native_window_jni.h>

#define HELPER_NOT_READY 0
#define HELPER_READY 1
#define HELPER_EXIT 2

typedef struct {
struct pollfd eventPollFd;
void *frameBuffer;
uint8_t slurpHelper;
pthread_mutex_t slurpMutex;
pthread_cond_t slurpCond;
uint8_t renderHelper;
pthread_mutex_t renderMutex;
pthread_cond_t renderCond;
pthread_t slurpThread;
void *frameBuffer;
u_int8_t renderHelper:1;
u_int8_t slurpHelper:2;
} SurfaceDescriptor;

static void *slurp(void *pVoidInfo) {
GifInfo *info = pVoidInfo;
SurfaceDescriptor *descriptor = info->frameBufferDescriptor;
while (1) {
pthread_mutex_lock(&descriptor->slurpMutex);
while (descriptor->slurpHelper == 0) {
while (descriptor->slurpHelper == HELPER_NOT_READY) {
pthread_cond_wait(&descriptor->slurpCond, &descriptor->slurpMutex);
}

if (descriptor->slurpHelper == 2) {
if (descriptor->slurpHelper == HELPER_EXIT) {
pthread_mutex_unlock(&descriptor->slurpMutex);
DetachCurrentThread();
return NULL;
}
descriptor->slurpHelper = 0;
descriptor->slurpHelper = HELPER_NOT_READY;
pthread_mutex_unlock(&descriptor->slurpMutex);
DDGifSlurp(info, true, false);
pthread_mutex_lock(&descriptor->renderMutex);
descriptor->renderHelper = 1;
descriptor->renderHelper = HELPER_READY;
pthread_cond_signal(&descriptor->renderCond);
pthread_mutex_unlock(&descriptor->renderMutex);
}
Expand Down Expand Up @@ -132,17 +136,17 @@ Java_pl_droidsonroids_gif_GifInfoHandle_bindSurface(JNIEnv *env, jclass __unused
if (descriptor->frameBuffer) {
memcpy(buffer.bits, descriptor->frameBuffer, bufferSize);
invalidationDelayMillis = 0;
descriptor->renderHelper = 1;
descriptor->slurpHelper = 0;
descriptor->renderHelper = HELPER_READY;
descriptor->slurpHelper = HELPER_NOT_READY;
} else {
if (savedState != NULL) {
invalidationDelayMillis = restoreSavedState(info, env, savedState, buffer.bits);
if (invalidationDelayMillis < 0)
invalidationDelayMillis = 0;
} else
invalidationDelayMillis = 0;
descriptor->renderHelper = 0;
descriptor->slurpHelper = 1;
descriptor->renderHelper = HELPER_NOT_READY;
descriptor->slurpHelper = HELPER_READY;
}

info->lastFrameRemainder = -1;
Expand Down Expand Up @@ -210,16 +214,16 @@ Java_pl_droidsonroids_gif_GifInfoHandle_bindSurface(JNIEnv *env, jclass __unused
memcpy(buffer.bits, oldBufferBits, bufferSize);

pthread_mutex_lock(&descriptor->renderMutex);
while (descriptor->renderHelper == 0) {
while (descriptor->renderHelper == HELPER_NOT_READY) {
pthread_cond_wait(&descriptor->renderCond, &descriptor->renderMutex);
}
descriptor->renderHelper = 0;
descriptor->renderHelper = HELPER_NOT_READY;
pthread_mutex_unlock(&descriptor->renderMutex);

const uint_fast32_t frameDuration = getBitmap(buffer.bits, info, false);

pthread_mutex_lock(&descriptor->slurpMutex);
descriptor->slurpHelper = 1;
descriptor->slurpHelper = HELPER_READY;
pthread_cond_signal(&descriptor->slurpCond);
pthread_mutex_unlock(&descriptor->slurpMutex);

Expand Down

0 comments on commit 039f3e0

Please sign in to comment.