SkBitmapCache.h revision 96fcdcc219d2a0d3579719b84b28bede76efba64
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
14class SkResourceCache;
15class SkMipMap;
16
17uint64_t SkMakeResourceCacheSharedIDForBitmap(uint32_t bitmapGenID);
18
19void SkNotifyBitmapGenIDIsStale(uint32_t bitmapGenID);
20
21class SkBitmapCache {
22public:
23    /**
24     * Use this allocator for bitmaps, so they can use ashmem when available.
25     * Returns nullptr if the ResourceCache has not been initialized with a DiscardableFactory.
26     */
27    static SkBitmap::Allocator* GetAllocator();
28
29    /**
30     *  Search based on the src bitmap and inverse scales in X and Y. If found, returns true and
31     *  result will be set to the matching bitmap with its pixels already locked.
32     */
33    static bool Find(const SkBitmap& src, SkScalar invScaleX, SkScalar invScaleY, SkBitmap* result,
34                     SkResourceCache* localCache = nullptr);
35
36    /*
37     *  result must be marked isImmutable()
38     */
39    static void Add(const SkBitmap& src, SkScalar invScaleX, SkScalar invScaleY,
40            const SkBitmap& result, SkResourceCache* localCache = nullptr);
41
42    /**
43     *  Search based on the bitmap's genID and subset. If found, returns true and
44     *  result will be set to the matching bitmap with its pixels already locked.
45     */
46    static bool Find(uint32_t genID, const SkIRect& subset, SkBitmap* result,
47                     SkResourceCache* localCache = nullptr);
48
49    /**
50     * The width and the height of the provided subset must be the same as the result bitmap ones.
51     * result must be marked isImmutable()
52     */
53    static bool Add(SkPixelRef*, const SkIRect& subset, const SkBitmap& result,
54                    SkResourceCache* localCache = nullptr);
55
56    static bool Find(uint32_t genID, SkBitmap* result, SkResourceCache* localCache = nullptr);
57    // todo: eliminate the need to specify ID, since it should == the bitmap's
58    static void Add(uint32_t genID, const SkBitmap&, SkResourceCache* localCache = nullptr);
59};
60
61class SkMipMapCache {
62public:
63    static const SkMipMap* FindAndRef(const SkBitmap& src, SkResourceCache* localCache = nullptr);
64    static const SkMipMap* AddAndRef(const SkBitmap& src, SkResourceCache* localCache = nullptr);
65};
66
67#endif
68