aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/lazy/SkLazyPixelRef.h
blob: c51675dd5f301e648e20db0daa40db3a1d7faae6 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkLazyPixelRef_DEFINED
#define SkLazyPixelRef_DEFINED

#include "SkBitmapFactory.h"
#include "SkImage.h"
#include "SkPixelRef.h"
#include "SkFlattenable.h"

class SkColorTable;
class SkData;
class SkImageCache;

#ifdef SK_DEBUG
    #define LAZY_CACHE_STATS 1
#elif !defined(LAZY_CACHE_STATS)
    #define LAZY_CACHE_STATS 0
#endif

/**
 *  PixelRef which defers decoding until SkBitmap::lockPixels() is called.
 */
class SkLazyPixelRef : public SkPixelRef {

public:
    /**
     *  Create a new SkLazyPixelRef.
     *  @param SkData Encoded data representing the pixels.
     *  @param DecodeProc Called to decode the pixels when needed. Must be non-NULL.
     *  @param SkImageCache Object that handles allocating and freeing the pixel memory, as needed.
     *         Must not be NULL.
     */
    SkLazyPixelRef(SkData*, SkBitmapFactory::DecodeProc, SkImageCache*);

    virtual ~SkLazyPixelRef();

#ifdef SK_DEBUG
    intptr_t getCacheId() const { return fCacheId; }
#endif

#if LAZY_CACHE_STATS
    static int32_t GetCacheHits() { return gCacheHits; }
    static int32_t GetCacheMisses() { return gCacheMisses; }
    static void ResetCacheStats() { gCacheHits = gCacheMisses = 0; }
#endif

    // No need to flatten this object. When flattening an SkBitmap, SkOrderedWriteBuffer will check
    // the encoded data and write that instead.
    // Future implementations of SkFlattenableWriteBuffer will need to special case for
    // onRefEncodedData as well.
    SK_DECLARE_UNFLATTENABLE_OBJECT()

protected:
    virtual void* onLockPixels(SkColorTable**) SK_OVERRIDE;
    virtual void onUnlockPixels() SK_OVERRIDE;
    virtual bool onLockPixelsAreWritable() const SK_OVERRIDE { return false; }
    virtual SkData* onRefEncodedData() SK_OVERRIDE;
    virtual bool onImplementsDecodeInto() SK_OVERRIDE;
    virtual bool onDecodeInto(int pow2, SkBitmap*) SK_OVERRIDE;

private:
    bool                        fErrorInDecoding;
    SkData*                     fData;
    SkBitmapFactory::DecodeProc fDecodeProc;
    SkImageCache*               fImageCache;
    intptr_t                    fCacheId;
    size_t                      fRowBytes;

#if LAZY_CACHE_STATS
    static int32_t              gCacheHits;
    static int32_t              gCacheMisses;
#endif

    typedef SkPixelRef INHERITED;
};

#endif  // SkLazyPixelRef_DEFINED