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 | /** |
scroggo@google.com | bb281f7 | 2013-03-18 21:37:39 +0000 | [diff] [blame^] | 70 | * An object for selecting an SkImageCache to use based on an SkImage::Info. |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 71 | */ |
scroggo@google.com | bb281f7 | 2013-03-18 21:37:39 +0000 | [diff] [blame^] | 72 | 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.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Set the function to be used to select which SkImageCache to use. Mutually exclusive with |
| 85 | * fImageCache. |
| 86 | */ |
scroggo@google.com | bb281f7 | 2013-03-18 21:37:39 +0000 | [diff] [blame^] | 87 | void setCacheSelector(CacheSelector*); |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 88 | |
| 89 | private: |
scroggo@google.com | bb281f7 | 2013-03-18 21:37:39 +0000 | [diff] [blame^] | 90 | DecodeProc fDecodeProc; |
| 91 | SkImageCache* fImageCache; |
| 92 | CacheSelector* fCacheSelector; |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | #endif // SkBitmapFactory_DEFINED |