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 
validateSkBitmapCacheDesc27     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      * Use this allocator for bitmaps, so they can use ashmem when available.
49      * Returns nullptr if the ResourceCache has not been initialized with a DiscardableFactory.
50      */
51     static SkBitmap::Allocator* GetAllocator();
52 
53     /**
54      *  Search based on the desc. If found, returns true and
55      *  result will be set to the matching bitmap with its pixels already locked.
56      */
57     static bool Find(const SkBitmapCacheDesc&, SkBitmap* result,
58                     SkResourceCache* localCache = nullptr);
59 
60     /*
61      *  result must be marked isImmutable()
62      */
63     static bool Add(const SkBitmapCacheDesc&, const SkBitmap& result,
64                     SkResourceCache* localCache = nullptr);
65 };
66 
67 class SkMipMapCache {
68 public:
69     // Note: the scaled width/height in desc must be 0, as any other value would not make sense.
70     static const SkMipMap* FindAndRef(const SkBitmapCacheDesc&, SkDestinationSurfaceColorMode,
71                                       SkResourceCache* localCache = nullptr);
72     static const SkMipMap* AddAndRef(const SkBitmap& src, SkDestinationSurfaceColorMode,
73                                      SkResourceCache* localCache = nullptr);
74 };
75 
76 #endif
77