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 "SkScalar.h"
12 #include "SkBitmap.h"
13 
14 class SkImage;
15 class SkResourceCache;
16 class SkMipMap;
17 
18 uint64_t SkMakeResourceCacheSharedIDForBitmap(uint32_t bitmapGenID);
19 
20 void SkNotifyBitmapGenIDIsStale(uint32_t bitmapGenID);
21 
22 struct SkBitmapCacheDesc {
23     uint32_t    fImageID;
24     int32_t     fWidth;
25     int32_t     fHeight;
26     SkIRect     fBounds;
27 
28     static SkBitmapCacheDesc Make(const SkBitmap&, int width, int height);
29     static SkBitmapCacheDesc Make(const SkBitmap&);
30     static SkBitmapCacheDesc Make(const SkImage*, int width, int height);
31     static SkBitmapCacheDesc Make(const SkImage*);
32 };
33 
34 class SkBitmapCache {
35 public:
36     /**
37      * Use this allocator for bitmaps, so they can use ashmem when available.
38      * Returns nullptr if the ResourceCache has not been initialized with a DiscardableFactory.
39      */
40     static SkBitmap::Allocator* GetAllocator();
41 
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 FindWH(const SkBitmapCacheDesc&, SkBitmap* result,
47                        SkResourceCache* localCache = nullptr);
48 
49     /*
50      *  result must be marked isImmutable()
51      */
52     static bool AddWH(const SkBitmapCacheDesc&, const SkBitmap& result,
53                       SkResourceCache* localCache = nullptr);
54 
55     /**
56      *  Search based on the bitmap's genID and subset. If found, returns true and
57      *  result will be set to the matching bitmap with its pixels already locked.
58      */
59     static bool Find(uint32_t genID, const SkIRect& subset, SkBitmap* result,
60                      SkResourceCache* localCache = nullptr);
61 
62     /**
63      * The width and the height of the provided subset must be the same as the result bitmap ones.
64      * result must be marked isImmutable()
65      */
66     static bool Add(SkPixelRef*, const SkIRect& subset, const SkBitmap& result,
67                     SkResourceCache* localCache = nullptr);
68 
69     static bool Find(uint32_t genID, SkBitmap* result, SkResourceCache* localCache = nullptr);
70     // todo: eliminate the need to specify ID, since it should == the bitmap's
71     static void Add(uint32_t genID, const SkBitmap&, SkResourceCache* localCache = nullptr);
72 };
73 
74 class SkMipMapCache {
75 public:
76     static const SkMipMap* FindAndRef(const SkBitmapCacheDesc&,
77                                       SkResourceCache* localCache = nullptr);
78     static const SkMipMap* AddAndRef(const SkBitmap& src, SkResourceCache* localCache = nullptr);
79 };
80 
81 #endif
82