Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2015 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 "SkBitmapProvider.h"
      9 #include "SkImage_Base.h"
     10 #include "SkPixelRef.h"
     11 
     12 int SkBitmapProvider::width() const {
     13     return fImage ? fImage->width() : fBitmap.width();
     14 }
     15 
     16 int SkBitmapProvider::height() const {
     17     return fImage ? fImage->height() : fBitmap.height();
     18 }
     19 
     20 uint32_t SkBitmapProvider::getID() const {
     21     return fImage ? fImage->uniqueID() : fBitmap.getGenerationID();
     22 }
     23 
     24 bool SkBitmapProvider::validForDrawing() const {
     25     if (!fImage) {
     26         if (0 == fBitmap.width() || 0 == fBitmap.height()) {
     27             return false;
     28         }
     29         if (nullptr == fBitmap.pixelRef()) {
     30             return false;   // no pixels to read
     31         }
     32         if (fBitmap.getTexture()) {
     33             // we can handle texture (ugh) since lockPixels will perform a read-back
     34             return true;
     35         }
     36         if (kIndex_8_SkColorType == fBitmap.colorType()) {
     37             SkAutoLockPixels alp(fBitmap); // but we need to call it before getColorTable() is safe.
     38             if (!fBitmap.getColorTable()) {
     39                 return false;
     40             }
     41         }
     42     }
     43     return true;
     44 }
     45 
     46 SkImageInfo SkBitmapProvider::info() const {
     47     if (fImage) {
     48         SkAlphaType at = fImage->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
     49         return SkImageInfo::MakeN32(fImage->width(), fImage->height(), at);
     50     } else {
     51         return fBitmap.info();
     52     }
     53 }
     54 
     55 bool SkBitmapProvider::isVolatile() const {
     56     if (fImage) {
     57         return false;   // add flag to images?
     58     } else {
     59         return fBitmap.isVolatile();
     60     }
     61 }
     62 
     63 SkBitmapCacheDesc SkBitmapProvider::makeCacheDesc(int w, int h) const {
     64     return fImage ? SkBitmapCacheDesc::Make(fImage, w, h) : SkBitmapCacheDesc::Make(fBitmap, w, h);
     65 }
     66 
     67 SkBitmapCacheDesc SkBitmapProvider::makeCacheDesc() const {
     68     return fImage ? SkBitmapCacheDesc::Make(fImage) : SkBitmapCacheDesc::Make(fBitmap);
     69 }
     70 
     71 void SkBitmapProvider::notifyAddedToCache() const {
     72     if (fImage) {
     73         as_IB(fImage)->notifyAddedToCache();
     74     } else {
     75         fBitmap.pixelRef()->notifyAddedToCache();
     76     }
     77 }
     78 
     79 bool SkBitmapProvider::asBitmap(SkBitmap* bm) const {
     80     if (fImage) {
     81         return as_IB(fImage)->getROPixels(bm, SkImage::kAllow_CachingHint);
     82     } else {
     83         *bm = fBitmap;
     84         return true;
     85     }
     86 }
     87 
     88