Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2014 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 #ifndef SkBitmapCache_DEFINED
      9 #define SkBitmapCache_DEFINED
     10 
     11 #include "SkBitmap.h"
     12 #include "SkMipMap.h"
     13 
     14 class SkImage;
     15 class SkResourceCache;
     16 
     17 uint64_t SkMakeResourceCacheSharedIDForBitmap(uint32_t bitmapGenID);
     18 
     19 void SkNotifyBitmapGenIDIsStale(uint32_t bitmapGenID);
     20 
     21 struct SkBitmapCacheDesc {
     22     uint32_t    fImageID;       // != 0
     23     int32_t     fScaledWidth;   // 0 for unscaled
     24     int32_t     fScaledHeight;  // 0 for unscaled
     25     SkIRect     fSubset;        // always set to a valid rect (entire or subset)
     26 
     27     void validate() const {
     28         SkASSERT(fImageID);
     29         if (fScaledWidth || fScaledHeight) {
     30             SkASSERT(fScaledWidth && fScaledHeight);
     31         }
     32         SkASSERT(fSubset.fLeft >= 0 && fSubset.fTop >= 0);
     33         SkASSERT(fSubset.width() > 0 && fSubset.height() > 0);
     34     }
     35 
     36     static SkBitmapCacheDesc Make(const SkBitmap&, int scaledWidth, int scaledHeight);
     37     static SkBitmapCacheDesc Make(const SkBitmap&);
     38     static SkBitmapCacheDesc Make(const SkImage*, int scaledWidth, int scaledHeight);
     39     static SkBitmapCacheDesc Make(const SkImage*);
     40 
     41     // Use with care -- width/height must match the original bitmap/image
     42     static SkBitmapCacheDesc Make(uint32_t genID, int origWidth, int origHeight);
     43 };
     44 
     45 class SkBitmapCache {
     46 public:
     47     /**
     48      *  Search based on the desc. If found, returns true and
     49      *  result will be set to the matching bitmap with its pixels already locked.
     50      */
     51     static bool Find(const SkBitmapCacheDesc&, SkBitmap* result);
     52 
     53     class Rec;
     54     struct RecDeleter { void operator()(Rec* r) { PrivateDeleteRec(r); } };
     55     typedef std::unique_ptr<Rec, RecDeleter> RecPtr;
     56 
     57     static RecPtr Alloc(const SkBitmapCacheDesc&, const SkImageInfo&, SkPixmap*);
     58     static void Add(RecPtr, SkBitmap*);
     59 
     60 private:
     61     static void PrivateDeleteRec(Rec*);
     62 };
     63 
     64 class SkMipMapCache {
     65 public:
     66     // Note: the scaled width/height in desc must be 0, as any other value would not make sense.
     67     static const SkMipMap* FindAndRef(const SkBitmapCacheDesc&, SkDestinationSurfaceColorMode,
     68                                       SkResourceCache* localCache = nullptr);
     69     static const SkMipMap* AddAndRef(const SkBitmap& src, SkDestinationSurfaceColorMode,
     70                                      SkResourceCache* localCache = nullptr);
     71 };
     72 
     73 #endif
     74