OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkGradientShaderPriv.h" | 8 #include "SkGradientShaderPriv.h" |
9 #include "SkLinearGradient.h" | 9 #include "SkLinearGradient.h" |
10 #include "SkRadialGradient.h" | 10 #include "SkRadialGradient.h" |
(...skipping 552 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
563 if (!fCache || fCache->getAlpha() != alpha) { | 563 if (!fCache || fCache->getAlpha() != alpha) { |
564 fCache.reset(SkNEW_ARGS(GradientShaderCache, (alpha, *this))); | 564 fCache.reset(SkNEW_ARGS(GradientShaderCache, (alpha, *this))); |
565 } | 565 } |
566 // Increment the ref counter inside the mutex to ensure the returned pointer
is still valid. | 566 // Increment the ref counter inside the mutex to ensure the returned pointer
is still valid. |
567 // Otherwise, the pointer may have been overwritten on a different thread be
fore the object's | 567 // Otherwise, the pointer may have been overwritten on a different thread be
fore the object's |
568 // ref count was incremented. | 568 // ref count was incremented. |
569 fCache.get()->ref(); | 569 fCache.get()->ref(); |
570 return fCache; | 570 return fCache; |
571 } | 571 } |
572 | 572 |
| 573 SK_DECLARE_STATIC_MUTEX(gGradientCacheMutex); |
573 /* | 574 /* |
574 * Because our caller might rebuild the same (logically the same) gradient | 575 * Because our caller might rebuild the same (logically the same) gradient |
575 * over and over, we'd like to return exactly the same "bitmap" if possible, | 576 * over and over, we'd like to return exactly the same "bitmap" if possible, |
576 * allowing the client to utilize a cache of our bitmap (e.g. with a GPU). | 577 * allowing the client to utilize a cache of our bitmap (e.g. with a GPU). |
577 * To do that, we maintain a private cache of built-bitmaps, based on our | 578 * To do that, we maintain a private cache of built-bitmaps, based on our |
578 * colors and positions. Note: we don't try to flatten the fMapper, so if one | 579 * colors and positions. Note: we don't try to flatten the fMapper, so if one |
579 * is present, we skip the cache for now. | 580 * is present, we skip the cache for now. |
580 */ | 581 */ |
581 void SkGradientShaderBase::getGradientTableBitmap(SkBitmap* bitmap) const { | 582 void SkGradientShaderBase::getGradientTableBitmap(SkBitmap* bitmap) const { |
582 // our caller assumes no external alpha, so we ensure that our cache is | 583 // our caller assumes no external alpha, so we ensure that our cache is |
(...skipping 15 matching lines...) Expand all Loading... |
598 if (fColorCount > 2) { | 599 if (fColorCount > 2) { |
599 for (int i = 1; i < fColorCount; i++) { | 600 for (int i = 1; i < fColorCount; i++) { |
600 *buffer++ = fRecs[i].fPos; | 601 *buffer++ = fRecs[i].fPos; |
601 } | 602 } |
602 } | 603 } |
603 *buffer++ = fGradFlags; | 604 *buffer++ = fGradFlags; |
604 SkASSERT(buffer - storage.get() == count); | 605 SkASSERT(buffer - storage.get() == count); |
605 | 606 |
606 /////////////////////////////////// | 607 /////////////////////////////////// |
607 | 608 |
608 SK_DECLARE_STATIC_MUTEX(gMutex); | |
609 static SkBitmapCache* gCache; | 609 static SkBitmapCache* gCache; |
610 // each cache cost 1K of RAM, since each bitmap will be 1x256 at 32bpp | 610 // each cache cost 1K of RAM, since each bitmap will be 1x256 at 32bpp |
611 static const int MAX_NUM_CACHED_GRADIENT_BITMAPS = 32; | 611 static const int MAX_NUM_CACHED_GRADIENT_BITMAPS = 32; |
612 SkAutoMutexAcquire ama(gMutex); | 612 SkAutoMutexAcquire ama(gGradientCacheMutex); |
613 | 613 |
614 if (NULL == gCache) { | 614 if (NULL == gCache) { |
615 gCache = SkNEW_ARGS(SkBitmapCache, (MAX_NUM_CACHED_GRADIENT_BITMAPS)); | 615 gCache = SkNEW_ARGS(SkBitmapCache, (MAX_NUM_CACHED_GRADIENT_BITMAPS)); |
616 } | 616 } |
617 size_t size = count * sizeof(int32_t); | 617 size_t size = count * sizeof(int32_t); |
618 | 618 |
619 if (!gCache->find(storage.get(), size, bitmap)) { | 619 if (!gCache->find(storage.get(), size, bitmap)) { |
620 // force our cahce32pixelref to be built | 620 // force our cahce32pixelref to be built |
621 (void)cache->getCache32(); | 621 (void)cache->getCache32(); |
622 bitmap->setInfo(SkImageInfo::MakeN32Premul(kCache32Count, 1)); | 622 bitmap->setInfo(SkImageInfo::MakeN32Premul(kCache32Count, 1)); |
(...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1148 (*stops)[i] = stop; | 1148 (*stops)[i] = stop; |
1149 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - st
op) : 1.f; | 1149 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - st
op) : 1.f; |
1150 } | 1150 } |
1151 } | 1151 } |
1152 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileM
odeCount)); | 1152 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileM
odeCount)); |
1153 | 1153 |
1154 return outColors; | 1154 return outColors; |
1155 } | 1155 } |
1156 | 1156 |
1157 #endif | 1157 #endif |
OLD | NEW |