blob: eb427ee390c8a776db7c652699a69ea682f8a7e0 [file] [log] [blame]
scroggo@google.comf8d7d272013-02-22 21:38:35 +00001/*
2 * Copyright 2012 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 SkBitmapFactory_DEFINED
9#define SkBitmapFactory_DEFINED
10
11#include "SkImage.h"
12#include "SkTypes.h"
13
14class SkBitmap;
15class SkData;
16class SkImageCache;
17
18/**
19 * Factory for creating a bitmap from encoded data.
20 */
21class SkBitmapFactory {
22
23public:
24 /**
25 * Struct containing information about a pixel destination.
26 */
27 struct Target {
28 /**
29 * Pre-allocated memory.
30 */
31 void* fAddr;
32
33 /**
34 * Rowbytes of the allocated memory.
35 */
36 size_t fRowBytes;
37 };
38
39 /**
40 * Signature for a function to decode an image from encoded data.
41 */
42 typedef bool (*DecodeProc)(const void* data, size_t length, SkImage::Info*, const Target*);
43
44 /**
45 * Create a bitmap factory which uses DecodeProc for decoding.
46 * @param DecodeProc Must not be NULL.
47 */
48 SkBitmapFactory(DecodeProc);
49
50 ~SkBitmapFactory();
51
52 /**
53 * Set an image cache to use on pixelrefs provided by installPixelRef. Mutually exclusive
54 * with fCacheSelector.
55 */
56 void setImageCache(SkImageCache* cache);
57
58 /**
59 * Sets up an SkBitmap from encoded data. On success, the SkBitmap will have its Config,
60 * width, height, rowBytes and pixelref set. If fImageCache is non-NULL, or if fCacheSelector
61 * is set and returns non-NULL, the pixelref will lazily decode, and that SkImageCache will
62 * handle the pixel memory. Otherwise installPixelRef will do an immediate decode.
63 * @param SkData Encoded data.
64 * @param SkBitmap to install the pixel ref on.
65 * @return bool Whether or not a pixel ref was successfully installed.
66 */
67 bool installPixelRef(SkData*, SkBitmap*);
68
69 /**
scroggo@google.combb281f72013-03-18 21:37:39 +000070 * An object for selecting an SkImageCache to use based on an SkImage::Info.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000071 */
scroggo@google.combb281f72013-03-18 21:37:39 +000072 class CacheSelector : public SkRefCnt {
73
74 public:
75 /**
76 * Return an SkImageCache to use based on the provided SkImage::Info. If the caller decides
77 * to hang on to the result, it will call ref, so the implementation should not add a ref
78 * as a result of this call.
79 */
80 virtual SkImageCache* selectCache(const SkImage::Info&) = 0;
81 };
scroggo@google.comf8d7d272013-02-22 21:38:35 +000082
83 /**
84 * Set the function to be used to select which SkImageCache to use. Mutually exclusive with
85 * fImageCache.
86 */
scroggo@google.combb281f72013-03-18 21:37:39 +000087 void setCacheSelector(CacheSelector*);
scroggo@google.comf8d7d272013-02-22 21:38:35 +000088
89private:
scroggo@google.combb281f72013-03-18 21:37:39 +000090 DecodeProc fDecodeProc;
91 SkImageCache* fImageCache;
92 CacheSelector* fCacheSelector;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000093};
94
95#endif // SkBitmapFactory_DEFINED