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 <memory>
13
14class SkBitmap;
15class SkImage;
16class SkImage_Base;
17struct SkImageInfo;
18class SkMipMap;
19class SkPixmap;
20class SkResourceCache;
21
22uint64_t SkMakeResourceCacheSharedIDForBitmap(uint32_t bitmapGenID);
23
24void SkNotifyBitmapGenIDIsStale(uint32_t bitmapGenID);
25
26struct SkBitmapCacheDesc {
27 uint32_t fImageID; // != 0
28 SkIRect fSubset; // always set to a valid rect (entire or subset)
29
30 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
40class SkBitmapCache {
41public:
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;
49 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
55private:
56 static void PrivateDeleteRec(Rec*);
57};
58
59class SkMipMapCache {
60public:
61 static const SkMipMap* FindAndRef(const SkBitmapCacheDesc&,
62 SkResourceCache* localCache = nullptr);
63 static const SkMipMap* AddAndRef(const SkImage_Base*,
64 SkResourceCache* localCache = nullptr);
65};
66
67#endif
68