aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/lazy/SkCachingPixelRef.h
blob: a46df14496d63afb0272933424088357286177f3 (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
/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkCachingPixelRef_DEFINED
#define SkCachingPixelRef_DEFINED

#include "SkBitmapCache.h"
#include "SkImageInfo.h"
#include "SkImageGenerator.h"
#include "SkPixelRef.h"

class SkColorTable;

/**
 *  PixelRef which defers decoding until SkBitmap::lockPixels() is
 *  called.  Caches the decoded images in the global
 *  SkScaledImageCache.  When the pixels are unlocked, this cache may
 *  or be destroyed before the next lock.  If so, onLockPixels will
 *  attempt to re-decode.
 *
 *  Decoding is handled by the SkImageGenerator
 */
class SkCachingPixelRef : public SkPixelRef {
public:
    SK_DECLARE_INST_COUNT(SkCachingPixelRef)
    /**
     *  Takes ownership of SkImageGenerator.  If this method fails for
     *  whatever reason, it will return false and immediatetely delete
     *  the generator.  If it succeeds, it will modify destination
     *  bitmap.
     *
     *  If Install fails or when the SkCachingPixelRef that is
     *  installed into destination is destroyed, it will call
     *  SkDELETE() on the generator.  Therefore, generator should be
     *  allocated with SkNEW() or SkNEW_ARGS().
     */
    static bool Install(SkImageGenerator* gen, SkBitmap* dst);

protected:
    virtual ~SkCachingPixelRef();
    bool onNewLockPixels(LockRec*) SK_OVERRIDE;
    void onUnlockPixels() SK_OVERRIDE;
    bool onLockPixelsAreWritable() const SK_OVERRIDE { return false; }

    SkData* onRefEncodedData() SK_OVERRIDE {
        return fImageGenerator->refEncodedData();
    }

private:
    SkImageGenerator* const fImageGenerator;
    bool                    fErrorInDecoding;
    const size_t            fRowBytes;

    SkBitmap                fLockedBitmap;

    SkCachingPixelRef(const SkImageInfo&, SkImageGenerator*, size_t rowBytes);

    typedef SkPixelRef INHERITED;
};

#endif  // SkCachingPixelRef_DEFINED