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 "SkRect.h"
12 #include <memory>
13 
14 class SkBitmap;
15 class SkBitmapProvider;
16 class SkImage;
17 struct SkImageInfo;
18 class SkMipMap;
19 class SkPixmap;
20 class SkResourceCache;
21 
22 uint64_t SkMakeResourceCacheSharedIDForBitmap(uint32_t bitmapGenID);
23 
24 void SkNotifyBitmapGenIDIsStale(uint32_t bitmapGenID);
25 
26 struct SkBitmapCacheDesc {
27     uint32_t    fImageID;       // != 0
28     SkIRect     fSubset;        // always set to a valid rect (entire or subset)
29 
validateSkBitmapCacheDesc30     void validate() const {
31         SkASSERT(fImageID);
32         SkASSERT(fSubset.fLeft >= 0 && fSubset.fTop >= 0);
33         SkASSERT(fSubset.width() > 0 && fSubset.height() > 0);
34     }
35 
36     static SkBitmapCacheDesc Make(const SkImage*);
37     static SkBitmapCacheDesc Make(uint32_t genID, const SkIRect& subset);
38 };
39 
40 class SkBitmapCache {
41 public:
42     /**
43      *  Search based on the desc. If found, returns true and
44      *  result will be set to the matching bitmap with its pixels already locked.
45      */
46     static bool Find(const SkBitmapCacheDesc&, SkBitmap* result);
47 
48     class Rec;
operatorRecDeleter49     struct RecDeleter { void operator()(Rec* r) { PrivateDeleteRec(r); } };
50     typedef std::unique_ptr<Rec, RecDeleter> RecPtr;
51 
52     static RecPtr Alloc(const SkBitmapCacheDesc&, const SkImageInfo&, SkPixmap*);
53     static void Add(RecPtr, SkBitmap*);
54 
55 private:
56     static void PrivateDeleteRec(Rec*);
57 };
58 
59 class SkMipMapCache {
60 public:
61     static const SkMipMap* FindAndRef(const SkBitmapCacheDesc&,
62                                       SkResourceCache* localCache = nullptr);
63     static const SkMipMap* AddAndRef(const SkBitmapProvider&,
64                                      SkResourceCache* localCache = nullptr);
65 };
66 
67 #endif
68