OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2013 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #include "Sk64.h" |
| 9 #include "SkColorTable.h" |
| 10 #include "SkData.h" |
| 11 #include "SkImageDecoder.h" |
| 12 #include "SkImagePriv.h" |
| 13 #include "SkLazyCachingPixelRef.h" |
| 14 #include "SkScaledImageCache.h" |
| 15 #include "SkPostConfig.h" |
| 16 |
| 17 SkLazyCachingPixelRef::SkLazyCachingPixelRef(SkData* data, |
| 18 SkBitmapFactory::DecodeProc proc, |
| 19 SkScaledImageCache* cache) |
| 20 : INHERITED(cache) |
| 21 , fDecodeProc(proc) { |
| 22 if (NULL == data) { |
| 23 fData = SkData::NewEmpty(); |
| 24 } else { |
| 25 fData = data; |
| 26 fData->ref(); |
| 27 } |
| 28 memset(&fLazilyCachedInfo, 0xFF, sizeof(fLazilyCachedInfo)); |
| 29 |
| 30 if (NULL == fDecodeProc) { // use a reasonable default. |
| 31 fDecodeProc = SkImageDecoder::DecodeMemoryToTarget; |
| 32 } |
| 33 this->setImmutable(); |
| 34 } |
| 35 |
| 36 SkLazyCachingPixelRef::~SkLazyCachingPixelRef() { |
| 37 SkASSERT(fData != NULL); |
| 38 fData->unref(); |
| 39 } |
| 40 |
| 41 bool SkLazyCachingPixelRef::onDecodeInfo(SkISize* size, |
| 42 SkBitmap::Config* config, |
| 43 SkAlphaType* alphaType) { |
| 44 SkASSERT(size && config &&alphaType); |
| 45 if (fLazilyCachedInfo.fWidth < 0) { |
| 46 SkImage::Info info; |
| 47 if (!fDecodeProc(fData->data(), fData->size(), &info, NULL)) { |
| 48 return false; |
| 49 } |
| 50 fLazilyCachedInfo = info; |
| 51 } |
| 52 size->set(fLazilyCachedInfo.fWidth, fLazilyCachedInfo.fHeight); |
| 53 *config = SkImageInfoToBitmapConfig(fLazilyCachedInfo); |
| 54 *alphaType = fLazilyCachedInfo.fAlphaType; |
| 55 return true; |
| 56 } |
| 57 |
| 58 bool SkLazyCachingPixelRef::onDecode(void* pixels, size_t rowBytes) { |
| 59 SkASSERT(pixels); |
| 60 if (fLazilyCachedInfo.fWidth <= 0) { |
| 61 SkISize size; |
| 62 SkBitmap::Config config; |
| 63 SkAlphaType alphaType; |
| 64 // The side effect of this is to set fLazilyCachedInfo |
| 65 this->onDecodeInfo(&size, &config, &alphaType); |
| 66 } |
| 67 SkBitmapFactory::Target target = {pixels, rowBytes}; |
| 68 return fDecodeProc(fData->data(), fData->size(), |
| 69 &fLazilyCachedInfo, &target); |
| 70 } |
| 71 |
| 72 bool SkLazyCachingPixelRef::Install(SkBitmapFactory::DecodeProc proc, |
| 73 SkData* data, |
| 74 SkBitmap* destination, |
| 75 SkScaledImageCache* cache) { |
| 76 SkAutoTUnref<SkLazyCachingPixelRef> ref( |
| 77 SkNEW_ARGS(SkLazyCachingPixelRef, (data, proc, cache))); |
| 78 return ref->configure(destination) && destination->setPixelRef(ref); |
| 79 } |
| 80 |
OLD | NEW |