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