Skip to content

Commit

Permalink
fixes for #240
Browse files Browse the repository at this point in the history
  • Loading branch information
koral-- committed Jan 8, 2016
1 parent a832928 commit bf7b97d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
12 changes: 0 additions & 12 deletions src/main/java/pl/droidsonroids/gif/GifTextureView.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,6 @@ void dispose(@NonNull final GifTextureView gifTextureView) {
gifTextureView.setSuperSurfaceTextureListener(null);
mGifInfoHandle.postUnbindSurface();
interrupt();
final boolean isCallerInterrupted = Thread.currentThread().isInterrupted();
if (isCallerInterrupted) {
interrupted();
}
try {
join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (isCallerInterrupted) {
Thread.currentThread().interrupt();
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/jni/gif.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#ifndef _GIF
#define _GIF

#define _POSIX_C_SOURCE 200809L
#define _GNU_SOURCE
#ifdef __clang__
#pragma clang system_header
#else
#pragma clang diagnostic ignored "-Wgnu"
#elif __GNUC__
#pragma GCC system_header
#pragma GCC diagnostic ignored "-Wgnu"
#endif

#include <unistd.h>
Expand Down
33 changes: 21 additions & 12 deletions src/main/jni/surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
typedef uint64_t POLL_TYPE;
#define POLL_TYPE_SIZE sizeof(POLL_TYPE)

#define THROW_AND_BREAK_ON_NONZERO_RESULT(fun, message) if (fun !=0) {throwException(env, RUNTIME_EXCEPTION_ERRNO, message); break;}

static void *slurp(void *pVoidInfo) {
GifInfo *info = pVoidInfo;
while (1) {
Expand Down Expand Up @@ -68,11 +66,13 @@ Java_pl_droidsonroids_gif_GifInfoHandle_bindSurface(JNIEnv *env, jclass __unused
int pollResult;

while (1) {
pollResult = poll(&info->surfaceDescriptor->eventPollFd, 1, 0);
pollResult = TEMP_FAILURE_RETRY(poll(&info->surfaceDescriptor->eventPollFd, 1, 0));
if (pollResult == 0)
break;
else if (pollResult > 0) {
if (read(info->surfaceDescriptor->eventPollFd.fd, &eftd_ctr, POLL_TYPE_SIZE) != POLL_TYPE_SIZE) {
ssize_t bytesRead = TEMP_FAILURE_RETRY(
read(info->surfaceDescriptor->eventPollFd.fd, &eftd_ctr, POLL_TYPE_SIZE));
if (bytesRead != POLL_TYPE_SIZE) {
throwException(env, RUNTIME_EXCEPTION_ERRNO, "Could not read from eventfd ");
return;
}
Expand All @@ -99,8 +99,10 @@ Java_pl_droidsonroids_gif_GifInfoHandle_bindSurface(JNIEnv *env, jclass __unused
void *oldBufferBits;

if (ANativeWindow_lock(window, &buffer, NULL) != 0) {
#ifdef DEBUG
LOGE("Window lock failed %d", errno);
#endif
ANativeWindow_release(window);
throwException(env, RUNTIME_EXCEPTION_ERRNO, "Window lock failed ");
return;
}
const size_t bufferSize = buffer.stride * buffer.height * sizeof(argb);
Expand All @@ -114,10 +116,10 @@ Java_pl_droidsonroids_gif_GifInfoHandle_bindSurface(JNIEnv *env, jclass __unused
info->surfaceDescriptor->slurpHelper = 0;
}
else {
if (savedState != NULL){
if (savedState != NULL) {
invalidationDelayMillis = restoreSavedState(info, env, savedState, buffer.bits);
if (invalidationDelayMillis <0)
invalidationDelayMillis =0;
if (invalidationDelayMillis < 0)
invalidationDelayMillis = 0;
}
else
invalidationDelayMillis = 0;
Expand All @@ -130,7 +132,7 @@ Java_pl_droidsonroids_gif_GifInfoHandle_bindSurface(JNIEnv *env, jclass __unused

if (info->loopCount != 0 && info->currentLoop == info->loopCount) {
ANativeWindow_release(window);
pollResult = poll(&info->surfaceDescriptor->eventPollFd, 1, -1);
pollResult = TEMP_FAILURE_RETRY(poll(&info->surfaceDescriptor->eventPollFd, 1, -1));
if (pollResult < 0) {
throwException(env, RUNTIME_EXCEPTION_ERRNO, "Animation end poll failed ");
}
Expand All @@ -145,7 +147,7 @@ Java_pl_droidsonroids_gif_GifInfoHandle_bindSurface(JNIEnv *env, jclass __unused
}

while (1) {
pollResult = poll(&info->surfaceDescriptor->eventPollFd, 1, (int) invalidationDelayMillis);
pollResult = TEMP_FAILURE_RETRY(poll(&info->surfaceDescriptor->eventPollFd, 1, (int) invalidationDelayMillis));
long renderingStartTime = getRealTime();

if (pollResult < 0) {
Expand All @@ -164,7 +166,12 @@ Java_pl_droidsonroids_gif_GifInfoHandle_bindSurface(JNIEnv *env, jclass __unused
break;
}
oldBufferBits = buffer.bits;
THROW_AND_BREAK_ON_NONZERO_RESULT(ANativeWindow_lock(window, &buffer, NULL), "Window lock failed ");
if (ANativeWindow_lock(window, &buffer, NULL) != 0) {
#ifdef DEBUG
LOGE("Window lock failed %d", errno);
#endif
break;
}

if (info->currentIndex == 0)
prepareCanvas(buffer.bits, info);
Expand Down Expand Up @@ -210,7 +217,9 @@ Java_pl_droidsonroids_gif_GifInfoHandle_postUnbindSurface(JNIEnv *env, jclass __
return;
}
POLL_TYPE eftd_ctr;
if (write(info->surfaceDescriptor->eventPollFd.fd, &eftd_ctr, POLL_TYPE_SIZE) != POLL_TYPE_SIZE) {
ssize_t bytesWritten = TEMP_FAILURE_RETRY(
write(info->surfaceDescriptor->eventPollFd.fd, &eftd_ctr, POLL_TYPE_SIZE));
if (bytesWritten != POLL_TYPE_SIZE && errno != EBADF) {
throwException(env, RUNTIME_EXCEPTION_ERRNO, "Could not write to eventfd ");
}
}
5 changes: 4 additions & 1 deletion src/main/jni/surface_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ void releaseSurfaceDescriptor(SurfaceDescriptor *surfaceDescriptor, JNIEnv *env)
return;
free(surfaceDescriptor->surfaceBackupPtr);
surfaceDescriptor->surfaceBackupPtr = NULL;
THROW_ON_NONZERO_RESULT(close(surfaceDescriptor->eventPollFd.fd), "Eventfd close failed ");
int closeResult = close(surfaceDescriptor->eventPollFd.fd);
if (closeResult !=0 && errno != EINTR) {
throwException(env, RUNTIME_EXCEPTION_ERRNO, "Eventfd close failed ");
}
THROW_ON_NONZERO_RESULT(pthread_mutex_destroy(&surfaceDescriptor->slurpMutex), "Slurp mutex destroy failed ");
THROW_ON_NONZERO_RESULT(pthread_mutex_destroy(&surfaceDescriptor->renderMutex), "Render mutex destroy failed ");
THROW_ON_NONZERO_RESULT(pthread_cond_destroy(&surfaceDescriptor->slurpCond), "Slurp cond destroy failed ");
Expand Down

0 comments on commit bf7b97d

Please sign in to comment.