Home | History | Annotate | Download | only in lazy
      1 /*
      2  * Copyright 2012 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 "SkBitmapFactory.h"
      9 
     10 #include "SkBitmap.h"
     11 #include "SkData.h"
     12 #include "SkImageCache.h"
     13 #include "SkImagePriv.h"
     14 #include "SkLazyPixelRef.h"
     15 
     16 SK_DEFINE_INST_COUNT(SkBitmapFactory::CacheSelector)
     17 
     18 SkBitmapFactory::SkBitmapFactory(SkBitmapFactory::DecodeProc proc)
     19     : fDecodeProc(proc)
     20     , fImageCache(NULL)
     21     , fCacheSelector(NULL) {
     22     SkASSERT(fDecodeProc != NULL);
     23 }
     24 
     25 SkBitmapFactory::~SkBitmapFactory() {
     26     SkSafeUnref(fImageCache);
     27     SkSafeUnref(fCacheSelector);
     28 }
     29 
     30 void SkBitmapFactory::setImageCache(SkImageCache *cache) {
     31     SkRefCnt_SafeAssign(fImageCache, cache);
     32     if (cache != NULL) {
     33         SkSafeUnref(fCacheSelector);
     34         fCacheSelector = NULL;
     35     }
     36 }
     37 
     38 void SkBitmapFactory::setCacheSelector(CacheSelector* selector) {
     39     SkRefCnt_SafeAssign(fCacheSelector, selector);
     40     if (selector != NULL) {
     41         SkSafeUnref(fImageCache);
     42         fImageCache = NULL;
     43     }
     44 }
     45 
     46 bool SkBitmapFactory::installPixelRef(SkData* data, SkBitmap* dst) {
     47     if (NULL == data || 0 == data->size() || dst == NULL) {
     48         return false;
     49     }
     50 
     51     SkImage::Info info;
     52     if (!fDecodeProc(data->data(), data->size(), &info, NULL)) {
     53         return false;
     54     }
     55 
     56     bool isOpaque = false;
     57     SkBitmap::Config config = SkImageInfoToBitmapConfig(info, &isOpaque);
     58 
     59     Target target;
     60     // FIMXE: There will be a problem if this rowbytes is calculated differently from
     61     // in SkLazyPixelRef.
     62     target.fRowBytes = SkImageMinRowBytes(info);
     63 
     64     dst->setConfig(config, info.fWidth, info.fHeight, target.fRowBytes);
     65     dst->setIsOpaque(isOpaque);
     66 
     67     // fImageCache and fCacheSelector are mutually exclusive.
     68     SkASSERT(NULL == fImageCache || NULL == fCacheSelector);
     69 
     70     SkImageCache* cache = NULL == fCacheSelector ? fImageCache : fCacheSelector->selectCache(info);
     71 
     72     if (cache != NULL) {
     73         // Now set a new LazyPixelRef on dst.
     74         SkAutoTUnref<SkLazyPixelRef> lazyRef(SkNEW_ARGS(SkLazyPixelRef,
     75                                                         (data, fDecodeProc, cache)));
     76         dst->setPixelRef(lazyRef);
     77         return true;
     78     } else {
     79         dst->allocPixels();
     80         target.fAddr = dst->getPixels();
     81         return fDecodeProc(data->data(), data->size(), &info, &target);
     82     }
     83 }
     84