1 #include "AutoDecodeCancel.h" 2 #include "SkMutex.h" 3 4 SK_DECLARE_STATIC_MUTEX(gAutoDecoderCancelMutex); 5 static AutoDecoderCancel* gAutoDecoderCancel; 6 #ifdef SK_DEBUG 7 static int gAutoDecoderCancelCount; 8 #endif 9 AutoDecoderCancel(jobject joptions,SkImageDecoder * decoder)10AutoDecoderCancel::AutoDecoderCancel(jobject joptions, 11 SkImageDecoder* decoder) { 12 fJOptions = joptions; 13 fDecoder = decoder; 14 15 if (NULL != joptions) { 16 SkAutoMutexAcquire ac(gAutoDecoderCancelMutex); 17 18 // Add us as the head of the list 19 fPrev = NULL; 20 fNext = gAutoDecoderCancel; 21 if (gAutoDecoderCancel) { 22 gAutoDecoderCancel->fPrev = this; 23 } 24 gAutoDecoderCancel = this; 25 26 SkDEBUGCODE(gAutoDecoderCancelCount += 1;) 27 Validate(); 28 } 29 } 30 ~AutoDecoderCancel()31AutoDecoderCancel::~AutoDecoderCancel() { 32 if (NULL != fJOptions) { 33 SkAutoMutexAcquire ac(gAutoDecoderCancelMutex); 34 35 // take us out of the dllist 36 AutoDecoderCancel* prev = fPrev; 37 AutoDecoderCancel* next = fNext; 38 39 if (prev) { 40 SkASSERT(prev->fNext == this); 41 prev->fNext = next; 42 } else { 43 SkASSERT(gAutoDecoderCancel == this); 44 gAutoDecoderCancel = next; 45 } 46 if (next) { 47 SkASSERT(next->fPrev == this); 48 next->fPrev = prev; 49 } 50 51 SkDEBUGCODE(gAutoDecoderCancelCount -= 1;) 52 Validate(); 53 } 54 } 55 RequestCancel(jobject joptions)56bool AutoDecoderCancel::RequestCancel(jobject joptions) { 57 SkAutoMutexAcquire ac(gAutoDecoderCancelMutex); 58 59 Validate(); 60 61 AutoDecoderCancel* pair = gAutoDecoderCancel; 62 while (pair != NULL) { 63 if (pair->fJOptions == joptions) { 64 pair->fDecoder->cancelDecode(); 65 return true; 66 } 67 pair = pair->fNext; 68 } 69 return false; 70 } 71 72 #ifdef SK_DEBUG 73 // can only call this inside a lock on gAutoDecoderCancelMutex Validate()74void AutoDecoderCancel::Validate() { 75 const int gCount = gAutoDecoderCancelCount; 76 77 if (gCount == 0) { 78 SkASSERT(gAutoDecoderCancel == NULL); 79 } else { 80 SkASSERT(gCount > 0); 81 82 AutoDecoderCancel* curr = gAutoDecoderCancel; 83 SkASSERT(curr); 84 SkASSERT(curr->fPrev == NULL); 85 86 int count = 0; 87 while (curr) { 88 count += 1; 89 SkASSERT(count <= gCount); 90 if (curr->fPrev) { 91 SkASSERT(curr->fPrev->fNext == curr); 92 } 93 if (curr->fNext) { 94 SkASSERT(curr->fNext->fPrev == curr); 95 } 96 curr = curr->fNext; 97 } 98 SkASSERT(count == gCount); 99 } 100 } 101 #endif 102