aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/pdf/SkBitmapKey.h
blob: 4723debac26b49c7c1459f11cddcdf7898c51ecc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#ifndef SkBitmapKey_DEFINED
#define SkBitmapKey_DEFINED

#include "SkBitmap.h"
#include "SkImage.h"
#include "SkCanvas.h"

class SkBitmapKey {
public:
    SkBitmapKey() : fSubset(SkIRect::MakeEmpty()), fID(0) {}
    explicit SkBitmapKey(const SkBitmap& bm)
        : fSubset(bm.getSubset()), fID(bm.getGenerationID()) {}
    explicit SkBitmapKey(const SkImage* img)
        : fSubset(img ? img->bounds() : SkIRect::MakeEmpty())
        , fID(img ? img->uniqueID() : 0) {}
    explicit SkBitmapKey(const sk_sp<SkImage> img)
        : fSubset(img->bounds()), fID(img->uniqueID()) {}
    bool operator==(const SkBitmapKey& rhs) const {
        return fID == rhs.fID && fSubset == rhs.fSubset;
    }
    bool operator!=(const SkBitmapKey& rhs) const { return !(*this == rhs); }

private:
    SkIRect fSubset;
    uint32_t fID;
};

/**
   This wraps a thing that could either be a bitmap or a image and
   abstracts out some common tasks.
 */
class SkImageBitmap {
public:
    explicit SkImageBitmap(const SkBitmap& b) : fBitmap(b), fImage(nullptr) {}
    explicit SkImageBitmap(SkImage* i) : fImage(i) { SkASSERT(fImage); }
    SkIRect bounds() const { return fImage ? fImage->bounds() : fBitmap.bounds(); }
    SkISize dimensions() const {
        return fImage ? fImage->dimensions() : fBitmap.dimensions();
    }
    sk_sp<SkImage> makeImage() const {
        return fImage ? sk_ref_sp(fImage) : SkImage::MakeFromBitmap(fBitmap);
    }
    SkBitmapKey getKey() const {
        return fImage ? SkBitmapKey(fImage) : SkBitmapKey(fBitmap);
    }
    void draw(SkCanvas* canvas, SkPaint* paint) const {
        if (fImage) {
            canvas->drawImage(fImage, 0, 0, paint);
        } else {
            canvas->drawBitmap(fBitmap, 0, 0, paint);
        }
    }

private:
    SkBitmap fBitmap;
    SkImage* fImage; // non-owning; when drawImage starts passing a sk_sp<>,
                     // we can take a const ref to that sk_sp<>.
};

#endif  // SkBitmapKey_DEFINED