OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "SkPixelRef.h" | 8 #include "SkPixelRef.h" |
9 #include "SkFlattenableBuffers.h" | 9 #include "SkFlattenableBuffers.h" |
10 #include "SkThread.h" | 10 #include "SkThread.h" |
11 | 11 |
12 SK_DEFINE_INST_COUNT(SkPixelRef) | 12 SK_DEFINE_INST_COUNT(SkPixelRef) |
13 | 13 |
14 // must be a power-of-2. undef to just use 1 mutex | |
15 #define PIXELREF_MUTEX_RING_COUNT 32 | |
16 | 14 |
17 #ifdef PIXELREF_MUTEX_RING_COUNT | 15 #ifdef SK_USE_POSIX_THREADS |
18 static int32_t gPixelRefMutexRingIndex; | 16 |
19 static SK_DECLARE_MUTEX_ARRAY(gPixelRefMutexRing, PIXELREF_MUTEX_RING_COUNT)
; | 17 static SkBaseMutex gPixelRefMutexRing[] = { |
20 #else | 18 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER }, |
21 SK_DECLARE_STATIC_MUTEX(gPixelRefMutex); | 19 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER }, |
| 20 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER }, |
| 21 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER }, |
| 22 |
| 23 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER }, |
| 24 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER }, |
| 25 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER }, |
| 26 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER }, |
| 27 |
| 28 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER }, |
| 29 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER }, |
| 30 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER }, |
| 31 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER }, |
| 32 |
| 33 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER }, |
| 34 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER }, |
| 35 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER }, |
| 36 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER }, |
| 37 }; |
| 38 |
| 39 // must be a power-of-2. undef to just use 1 mutex |
| 40 #define PIXELREF_MUTEX_RING_COUNT SK_ARRAY_COUNT(gPixelRefMutexRing) |
| 41 |
| 42 #else // not pthreads |
| 43 |
| 44 // must be a power-of-2. undef to just use 1 mutex |
| 45 #define PIXELREF_MUTEX_RING_COUNT 32 |
| 46 static SkBaseMutex gPixelRefMutexRing[PIXELREF_MUTEX_RING_COUNT]; |
| 47 |
22 #endif | 48 #endif |
23 | 49 |
24 static SkBaseMutex* get_default_mutex() { | 50 static SkBaseMutex* get_default_mutex() { |
25 #ifdef PIXELREF_MUTEX_RING_COUNT | 51 static int32_t gPixelRefMutexRingIndex; |
| 52 |
| 53 SkASSERT(SkIsPow2(PIXELREF_MUTEX_RING_COUNT)); |
| 54 |
26 // atomic_inc might be overkill here. It may be fine if once in a while | 55 // atomic_inc might be overkill here. It may be fine if once in a while |
27 // we hit a race-condition and two subsequent calls get the same index... | 56 // we hit a race-condition and two subsequent calls get the same index... |
28 int index = sk_atomic_inc(&gPixelRefMutexRingIndex); | 57 int index = sk_atomic_inc(&gPixelRefMutexRingIndex); |
29 return &gPixelRefMutexRing[index & (PIXELREF_MUTEX_RING_COUNT - 1)]; | 58 return &gPixelRefMutexRing[index & (PIXELREF_MUTEX_RING_COUNT - 1)]; |
30 #else | |
31 return &gPixelRefMutex; | |
32 #endif | |
33 } | 59 } |
34 | 60 |
35 /////////////////////////////////////////////////////////////////////////////// | 61 /////////////////////////////////////////////////////////////////////////////// |
36 | 62 |
37 int32_t SkNextPixelRefGenerationID(); | 63 int32_t SkNextPixelRefGenerationID(); |
38 | 64 |
39 int32_t SkNextPixelRefGenerationID() { | 65 int32_t SkNextPixelRefGenerationID() { |
40 static int32_t gPixelRefGenerationID; | 66 static int32_t gPixelRefGenerationID; |
41 // do a loop in case our global wraps around, as we never want to | 67 // do a loop in case our global wraps around, as we never want to |
42 // return a 0 | 68 // return a 0 |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 | 204 |
179 #ifdef SK_BUILD_FOR_ANDROID | 205 #ifdef SK_BUILD_FOR_ANDROID |
180 void SkPixelRef::globalRef(void* data) { | 206 void SkPixelRef::globalRef(void* data) { |
181 this->ref(); | 207 this->ref(); |
182 } | 208 } |
183 | 209 |
184 void SkPixelRef::globalUnref() { | 210 void SkPixelRef::globalUnref() { |
185 this->unref(); | 211 this->unref(); |
186 } | 212 } |
187 #endif | 213 #endif |
OLD | NEW |