blob: 7caba45138ab91f0db1fce1c863c926786f26a3a [file] [log] [blame]
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +00001/*
2 * Copyright 2013 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 SkCachingPixelRef_DEFINED
9#define SkCachingPixelRef_DEFINED
10
11#include "SkImage.h"
12#include "SkPixelRef.h"
13
14class SkColorTable;
15
16/**
17 * PixelRef which defers decoding until SkBitmap::lockPixels() is
18 * called. Caches the decoded images in the global
19 * SkScaledImageCache. When the pixels are unlocked, this cache may
20 * or be destroyed before the next lock. If so, onLockPixels will
21 * attempt to re-decode.
22 *
23 * Decoding is handled by the pure-virtual functions onDecodeInfo()
24 * and onDecodePixels(). Subclasses of this class need only provide
25 * those two functions.
26 */
27class SkCachingPixelRef : public SkPixelRef {
28public:
29 SkCachingPixelRef();
30 virtual ~SkCachingPixelRef();
31
32protected:
33 virtual void* onLockPixels(SkColorTable** colorTable) SK_OVERRIDE;
34 virtual void onUnlockPixels() SK_OVERRIDE;
35 virtual bool onLockPixelsAreWritable() const SK_OVERRIDE { return false; }
36 virtual bool onImplementsDecodeInto() SK_OVERRIDE { return true; }
37 virtual bool onDecodeInto(int pow2, SkBitmap*) SK_OVERRIDE;
38
39 /**
40 * Configure the supplied bitmap for this pixelRef, based on
41 * information provided by onDecodeInfo(). Does not set the
42 * bitmap's pixelRef. */
43 bool configure(SkBitmap* bitmap);
44
45 /**
46 * Cache info from onDecodeInfo(). Returns false on failure.
47 */
48 bool getInfo(SkImageInfo* info);
49
50 /**
51 * Return some information about the pixels, allowing this class
52 * to allocate pixels. @return false if anything goes wrong.
53 */
54 virtual bool onDecodeInfo(SkImageInfo* info) = 0;
55 /**
56 * Decode into the given pixels, a block of memory of size
57 * (info.fHeight - 1) * rowBytes + (info.fWidth * bytesPerPixel)
58 *
59 * @param info Should be identical to the info returned by
60 * onDecodeInfo so that the implementation can confirm
61 * that the caller knows what it is asking for (config,
62 * size). Thiscontract also allows the caller to specify
63 * different output-configs, which the implementation can
64 * decide to support or not.
65 *
66 * @return false if anything goes wrong.
67 */
68 virtual bool onDecodePixels(const SkImageInfo& info,
69 void* pixels,
70 size_t rowBytes) = 0;
71
72private:
73 bool fErrorInDecoding;
74 void* fScaledCacheId;
75 SkImageInfo fInfo;
76
77 typedef SkPixelRef INHERITED;
78};
79
80#endif // SkCachingPixelRef_DEFINED
81