scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame^] | 1 | /* |
| 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 | |
| 14 | class SkBitmap; |
| 15 | class SkData; |
| 16 | class SkImageCache; |
| 17 | |
| 18 | /** |
| 19 | * Factory for creating a bitmap from encoded data. |
| 20 | */ |
| 21 | class SkBitmapFactory { |
| 22 | |
| 23 | public: |
| 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 | /** |
| 70 | * A function for selecting an SkImageCache to use based on an SkImage::Info. |
| 71 | */ |
| 72 | typedef SkImageCache* (*CacheSelector)(const SkImage::Info&); |
| 73 | |
| 74 | /** |
| 75 | * Set the function to be used to select which SkImageCache to use. Mutually exclusive with |
| 76 | * fImageCache. |
| 77 | */ |
| 78 | void setCacheSelector(CacheSelector); |
| 79 | |
| 80 | private: |
| 81 | DecodeProc fDecodeProc; |
| 82 | SkImageCache* fImageCache; |
| 83 | CacheSelector fCacheSelector; |
| 84 | }; |
| 85 | |
| 86 | #endif // SkBitmapFactory_DEFINED |