Skip to content

Commit

Permalink
surface rendering fixed, native code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
koral-- committed Apr 15, 2015
1 parent c39dd6c commit 84ba5cb
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 38 deletions.
26 changes: 7 additions & 19 deletions src/main/jni/drawing.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ static bool checkIfCover(const SavedImage *target, const SavedImage *covered) {
return false;
}

static inline void disposeFrameIfNeeded(argb *bm, GifInfo *info, uint_fast32_t idx) {
static inline void disposeFrameIfNeeded(argb *bm, GifInfo *info) {
GifFileType *fGif = info->gifFilePtr;
SavedImage *cur = &fGif->SavedImages[idx - 1];
SavedImage *next = &fGif->SavedImages[idx];
SavedImage *cur = &fGif->SavedImages[info->currentIndex - 1];
SavedImage *next = &fGif->SavedImages[info->currentIndex];
// We can skip disposal process if next frame is not transparent
// and completely covers current area
uint_fast8_t curDisposal = info->infos[idx - 1].DisposalMode;
bool nextTrans = info->infos[idx].TransparentColor != NO_TRANSPARENT_COLOR;
unsigned char nextDisposal = info->infos[idx].DisposalMode;
uint_fast8_t curDisposal = info->infos[info->currentIndex - 1].DisposalMode;
bool nextTrans = info->infos[info->currentIndex].TransparentColor != NO_TRANSPARENT_COLOR;
unsigned char nextDisposal = info->infos[info->currentIndex].DisposalMode;

if ((curDisposal == DISPOSE_PREVIOUS || nextDisposal == DISPOSE_PREVIOUS) && info->backupPtr == NULL) {
info->backupPtr = malloc(info->stride * fGif->SHeight * sizeof(argb));
Expand Down Expand Up @@ -148,7 +148,7 @@ uint_fast32_t const getBitmap(argb *bm, GifInfo *info) {
}
}
else {
disposeFrameIfNeeded(bm, info, info->currentIndex);
disposeFrameIfNeeded(bm, info);
}
drawFrame(bm, info, info->gifFilePtr->SavedImages + info->currentIndex);
uint_fast32_t frameDuration = info->infos[info->currentIndex].DelayTime;
Expand All @@ -170,15 +170,3 @@ uint_fast32_t const getBitmap(argb *bm, GifInfo *info) {
return frameDuration;
}

ColorMapObject *genDefColorMap(void) {
ColorMapObject *cmap = GifMakeMapObject(8, NULL);
if (cmap != NULL) {
uint_fast16_t iColor;
for (iColor = 0; iColor < 256; iColor++) {
cmap->Colors[iColor].Red = (GifByteType) iColor;
cmap->Colors[iColor].Green = (GifByteType) iColor;
cmap->Colors[iColor].Blue = (GifByteType) iColor;
}
}
return cmap;
}
15 changes: 12 additions & 3 deletions src/main/jni/gif.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,19 @@ JNI_OnLoad(JavaVM *vm, void *__unused reserved) {
return -1;
}
g_jvm = vm;
defaultCmap = genDefColorMap();
if (defaultCmap == NULL) {
throwException(env, OUT_OF_MEMORY_ERROR, OOME_MESSAGE);

defaultCmap = GifMakeMapObject(8, NULL);
if (defaultCmap != NULL) {
uint_fast16_t iColor;
for (iColor = 0; iColor < 256; iColor++) {
defaultCmap->Colors[iColor].Red = (GifByteType) iColor;
defaultCmap->Colors[iColor].Green = (GifByteType) iColor;
defaultCmap->Colors[iColor].Blue = (GifByteType) iColor;
}
}
else
throwException(env, OUT_OF_MEMORY_ERROR, OOME_MESSAGE);

struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) == -1) {
//sanity check here instead of on each clock_gettime() call
Expand Down
9 changes: 1 addition & 8 deletions src/main/jni/gif.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,6 @@ static ColorMapObject *defaultCmap;
*/
static JavaVM *g_jvm;

/**
* Generates default color map, used when there is no color map defined in GIF file.
* Upon successful allocation in JNI_OnLoad it is stored for further use.
*
*/
ColorMapObject *genDefColorMap(void);

/**
* @return the real time, in ms
*/
Expand Down Expand Up @@ -199,7 +192,7 @@ static void drawFrame(argb *bm, GifInfo *info, SavedImage *frame);

static bool checkIfCover(const SavedImage *target, const SavedImage *covered);

static void disposeFrameIfNeeded(argb *bm, GifInfo *info, uint_fast32_t idx);
static void disposeFrameIfNeeded(argb *bm, GifInfo *info);

uint_fast32_t const getBitmap(argb *bm, GifInfo *info);

Expand Down
4 changes: 1 addition & 3 deletions src/main/jni/metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ Java_pl_droidsonroids_gif_GifInfoHandle_getSavedState(JNIEnv *env, jclass __unus
throwException(env, ILLEGAL_STATE_EXCEPTION_BARE, "Could not create state array");
return NULL;
}

jlong nativeState[4] = {(jlong) info->currentIndex, info->currentLoop, info->lastFrameRemainder};
memcpy(nativeState + 3, &info->speedFactor, sizeof(info->speedFactor));
(*env)->SetLongArrayRegion(env, state, 0, 4, nativeState);
Expand All @@ -144,8 +143,8 @@ jint restoreSavedState(GifInfo *info, JNIEnv *env, jlongArray state, void *pixel
jlong nativeState[4];
(*env)->GetLongArrayRegion(env, state, 0, 4, nativeState);

const uint_fast8_t savedLoop = (uint_fast8_t) nativeState[1];
const uint_fast32_t savedIndex = (uint_fast32_t) nativeState[0];
const uint_fast8_t savedLoop = (uint_fast8_t) nativeState[1];

if (savedIndex >= info->gifFilePtr->ImageCount || info->currentLoop > info->loopCount)
return -1;
Expand All @@ -157,7 +156,6 @@ jint restoreSavedState(GifInfo *info, JNIEnv *env, jlongArray state, void *pixel

uint_fast32_t lastFrameDuration = info->infos[info->currentIndex].DelayTime;
if (info->currentIndex < savedIndex) {

while (info->currentIndex < savedIndex) {
DDGifSlurp(info, true);
lastFrameDuration = getBitmap((argb *) pixels, info);
Expand Down
17 changes: 12 additions & 5 deletions src/main/jni/surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ static void *slurp(void *pVoidInfo) {
while (1) {
pthread_mutex_lock(&info->surfaceDescriptor->slurpMutex);
while (info->surfaceDescriptor->slurpHelper == 0)

pthread_cond_wait(&info->surfaceDescriptor->slurpCond, &info->surfaceDescriptor->slurpMutex);

if (info->surfaceDescriptor->slurpHelper == 2) {
pthread_mutex_unlock(&info->surfaceDescriptor->slurpMutex);
return NULL;
Expand Down Expand Up @@ -125,28 +125,35 @@ Java_pl_droidsonroids_gif_GifInfoHandle_bindSurface(JNIEnv *env, jclass __unused
else if (info->surfaceDescriptor->surfaceBackupPtr) {
memcpy(buffer.bits, info->surfaceDescriptor->surfaceBackupPtr, bufferSize);
info->lastFrameRemainder = -1;
info->surfaceDescriptor->renderHelper = 1;
info->surfaceDescriptor->slurpHelper = 0;
}
else {
if (savedState != NULL)
info->lastFrameRemainder = restoreSavedState(info, env, savedState, buffer.bits);
else
info->lastFrameRemainder = -1;
info->surfaceDescriptor->renderHelper = 0;
info->surfaceDescriptor->slurpHelper = 1;
}
ANativeWindow_unlockAndPost(window);

if (info->loopCount != 0 && info->currentLoop == info->loopCount) {
ANativeWindow_release(window);
pollResult = poll(&info->surfaceDescriptor->eventPollFd, 1, -1);
if (pollResult < 0) {
throwException(env, ILLEGAL_STATE_EXCEPTION_ERRNO, "Poll failed");
}
return;
}

info->surfaceDescriptor->renderHelper = 0;
info->surfaceDescriptor->slurpHelper = 1;

pthread_t thread;
if (pthread_create(&thread, NULL, slurp, info) != 0) {
ANativeWindow_release(window);
throwException(env, ILLEGAL_STATE_EXCEPTION_ERRNO, "pthread_create failed");
return;
}

while (1) {
long renderingStartTime = getRealTime();

Expand Down

0 comments on commit 84ba5cb

Please sign in to comment.