blob: c13a2cd02b4e4efbf183d7298dad457e9c47d2f8 [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 SkLazyCachingPixelRef_DEFINED
9#define SkLazyCachingPixelRef_DEFINED
10
11#include "SkBitmapFactory.h"
12#include "SkCachingPixelRef.h"
13
14class SkData;
15
16/**
17 * PixelRef which defers decoding until SkBitmap::lockPixels() is
18 * called. Makes use of a supplied decode procedure. Will decode at
19 * the procedure's preferred size.
20 */
21class SkLazyCachingPixelRef : public SkCachingPixelRef {
22public:
23 /**
24 * @param data Encoded data representing the pixels. NULL is
25 * equivalent to an empty data, and will be passed to
26 * DecodeProc with length zero.
27 *
28 * @param procedure Called to decode the pixels when
29 * needed. If NULL, use SkImageDecoder::DecodeMemoryToTarget.
30 */
31 SkLazyCachingPixelRef(SkData* data,
32 SkBitmapFactory::DecodeProc procedure);
33
34 virtual ~SkLazyCachingPixelRef();
35
36 virtual SkData* onRefEncodedData() SK_OVERRIDE { return SkSafeRef(fData); }
37
38 /**
39 * A simplified version of SkBitmapFactory. Installs a new
40 * SkLazyCachingPixelRef into the provided bitmap. Will
41 * immediately call onDecodeInfo() to configure the bitmap, but
42 * will defer decoding until the first time the bitmap's pixels
43 * are locked.
44 *
45 * @param data Encoded data representing the pixels. NULL is
46 * equivalent to an empty data, and will be passed to
47 * DecodeProc with length zero.
48 *
49 * @param procedure Called to decode the pixels when
50 * needed. If NULL, use SkImageDecoder::DecodeMemoryToTarget.
51 *
52 * @param destination Bitmap that will be modified on success.
53 *
54 * @returns true on success.
55 */
56 static bool Install(SkBitmapFactory::DecodeProc procedure,
57 SkData* data,
58 SkBitmap* destination);
59
60 // No need to flatten this object. When flattening an SkBitmap,
61 // SkOrderedWriteBuffer will check the encoded data and write that
62 // instead.
63 // Future implementations of SkFlattenableWriteBuffer will need to
64 // special case for onRefEncodedData as well.
65 SK_DECLARE_UNFLATTENABLE_OBJECT()
66
67protected:
68 /**
69 * Return some information about the pixels, allowing this class
70 * to allocate pixels. @return false if anything goes wrong.
71 *
72 * This implementation calls SkBitmapFactory::DecodeProc with a
73 * NULL target.
74 */
75 virtual bool onDecodeInfo(SkImageInfo* info) SK_OVERRIDE;
76 /**
77 * Decode into the given pixels, a block of memory of size
78 * (info.fHeight * rowBytes) bytes.
79 *
80 * @param info Should be identical to the info returned by
81 * onDecodeInfo so that the implementation can confirm
82 * that the caller knows what its asking for (config,
83 * size).
84 *
85 * @return false if anything goes wrong.
86 */
87 virtual bool onDecodePixels(const SkImageInfo& info,
88 void* pixels,
89 size_t rowBytes) SK_OVERRIDE;
90
91private:
92 SkData* fData;
93 SkBitmapFactory::DecodeProc fDecodeProc;
94
95 typedef SkCachingPixelRef INHERITED;
96};
97
98#endif // SkLazyCachingPixelRef_DEFINED
99