halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 1 | /* |
| 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 SkImageGenerator_DEFINED |
| 9 | #define SkImageGenerator_DEFINED |
| 10 | |
| 11 | #include "SkImageInfo.h" |
commit-bot@chromium.org | b263985 | 2014-05-28 16:01:55 +0000 | [diff] [blame^] | 12 | #include "SkColor.h" |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 13 | |
halcanary@google.com | edd370f | 2013-12-10 21:11:12 +0000 | [diff] [blame] | 14 | class SkBitmap; |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 15 | class SkData; |
halcanary@google.com | edd370f | 2013-12-10 21:11:12 +0000 | [diff] [blame] | 16 | class SkImageGenerator; |
| 17 | |
| 18 | /** |
| 19 | * Takes ownership of SkImageGenerator. If this method fails for |
| 20 | * whatever reason, it will return false and immediatetely delete |
| 21 | * the generator. If it succeeds, it will modify destination |
| 22 | * bitmap. |
| 23 | * |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 24 | * If generator is NULL, will safely return false. |
| 25 | * |
halcanary@google.com | edd370f | 2013-12-10 21:11:12 +0000 | [diff] [blame] | 26 | * If this fails or when the SkDiscardablePixelRef that is |
| 27 | * installed into destination is destroyed, it will call |
| 28 | * SkDELETE() on the generator. Therefore, generator should be |
| 29 | * allocated with SkNEW() or SkNEW_ARGS(). |
| 30 | * |
| 31 | * @param destination Upon success, this bitmap will be |
| 32 | * configured and have a pixelref installed. |
| 33 | * |
halcanary@google.com | edd370f | 2013-12-10 21:11:12 +0000 | [diff] [blame] | 34 | * @return true iff successful. |
| 35 | */ |
commit-bot@chromium.org | 2d970b5 | 2014-05-27 14:14:22 +0000 | [diff] [blame] | 36 | SK_API bool SkInstallDiscardablePixelRef(SkImageGenerator*, SkBitmap* destination); |
halcanary@google.com | edd370f | 2013-12-10 21:11:12 +0000 | [diff] [blame] | 37 | |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * An interface that allows a purgeable PixelRef (such as a |
| 41 | * SkDiscardablePixelRef) to decode and re-decode an image as needed. |
| 42 | */ |
halcanary@google.com | edd370f | 2013-12-10 21:11:12 +0000 | [diff] [blame] | 43 | class SK_API SkImageGenerator { |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 44 | public: |
| 45 | /** |
| 46 | * The PixelRef which takes ownership of this SkImageGenerator |
| 47 | * will call the image generator's destructor. |
| 48 | */ |
| 49 | virtual ~SkImageGenerator() { } |
| 50 | |
commit-bot@chromium.org | b263985 | 2014-05-28 16:01:55 +0000 | [diff] [blame^] | 51 | #ifdef SK_SUPPORT_LEGACY_IMAGEGENERATORAPI |
| 52 | virtual bool refEncodedData() { return this->onRefEncodedData(); } |
| 53 | virtual bool getInfo(SkImageInfo* info) { return this->onGetInfo(info); } |
| 54 | virtual bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 55 | return this->onGetPixels(info, pixels, rowBytes, NULL, NULL); |
| 56 | } |
| 57 | #else |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 58 | /** |
| 59 | * Return a ref to the encoded (i.e. compressed) representation, |
| 60 | * of this data. |
| 61 | * |
| 62 | * If non-NULL is returned, the caller is responsible for calling |
| 63 | * unref() on the data when it is finished. |
| 64 | */ |
commit-bot@chromium.org | b263985 | 2014-05-28 16:01:55 +0000 | [diff] [blame^] | 65 | SkData* refEncodedData() { return this->onRefEncodedData(); } |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * Return some information about the image, allowing the owner of |
| 69 | * this object to allocate pixels. |
| 70 | * |
commit-bot@chromium.org | dd59799 | 2013-12-02 22:32:54 +0000 | [diff] [blame] | 71 | * Repeated calls to this function should give the same results, |
| 72 | * allowing the PixelRef to be immutable. |
| 73 | * |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 74 | * @return false if anything goes wrong. |
| 75 | */ |
commit-bot@chromium.org | b263985 | 2014-05-28 16:01:55 +0000 | [diff] [blame^] | 76 | bool getInfo(SkImageInfo* info); |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * Decode into the given pixels, a block of memory of size at |
| 80 | * least (info.fHeight - 1) * rowBytes + (info.fWidth * |
| 81 | * bytesPerPixel) |
| 82 | * |
commit-bot@chromium.org | dd59799 | 2013-12-02 22:32:54 +0000 | [diff] [blame] | 83 | * Repeated calls to this function should give the same results, |
| 84 | * allowing the PixelRef to be immutable. |
| 85 | * |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 86 | * @param info A description of the format (config, size) |
| 87 | * expected by the caller. This can simply be identical |
| 88 | * to the info returned by getInfo(). |
| 89 | * |
| 90 | * This contract also allows the caller to specify |
| 91 | * different output-configs, which the implementation can |
| 92 | * decide to support or not. |
| 93 | * |
commit-bot@chromium.org | b263985 | 2014-05-28 16:01:55 +0000 | [diff] [blame^] | 94 | * If info is kIndex8_SkColorType, then the caller must provide storage for up to 256 |
| 95 | * SkPMColor values in ctable. On success the generator must copy N colors into that storage, |
| 96 | * (where N is the logical number of table entries) and set ctableCount to N. |
| 97 | * |
| 98 | * If info is not kIndex8_SkColorType, then the last two parameters may be NULL. If ctableCount |
| 99 | * is not null, it will be set to 0. |
| 100 | * |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 101 | * @return false if anything goes wrong or if the image info is |
| 102 | * unsupported. |
| 103 | */ |
commit-bot@chromium.org | b263985 | 2014-05-28 16:01:55 +0000 | [diff] [blame^] | 104 | bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 105 | SkPMColor ctable[], int* ctableCount); |
| 106 | |
| 107 | /** |
| 108 | * Simplified version of getPixels() that asserts that info is NOT kIndex8_SkColorType. |
| 109 | */ |
| 110 | bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes); |
| 111 | #endif |
| 112 | |
| 113 | protected: |
| 114 | virtual SkData* onRefEncodedData(); |
| 115 | virtual bool onGetInfo(SkImageInfo* info); |
| 116 | virtual bool onGetPixels(const SkImageInfo& info, |
| 117 | void* pixels, size_t rowBytes, |
| 118 | SkPMColor ctable[], int* ctableCount); |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | #endif // SkImageGenerator_DEFINED |