blob: 21ebcbb3f26ba835b16cadfdc28874cf7731bb80 [file] [log] [blame]
halcanary@google.comad04eb42013-11-21 15:32:08 +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 SkImageGenerator_DEFINED
9#define SkImageGenerator_DEFINED
10
11#include "SkImageInfo.h"
commit-bot@chromium.orgb2639852014-05-28 16:01:55 +000012#include "SkColor.h"
halcanary@google.comad04eb42013-11-21 15:32:08 +000013
halcanary@google.comedd370f2013-12-10 21:11:12 +000014class SkBitmap;
halcanary@google.comad04eb42013-11-21 15:32:08 +000015class SkData;
halcanary@google.comedd370f2013-12-10 21:11:12 +000016class 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.com3d50ea12014-01-02 13:15:13 +000024 * If generator is NULL, will safely return false.
25 *
halcanary@google.comedd370f2013-12-10 21:11:12 +000026 * 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.comedd370f2013-12-10 21:11:12 +000034 * @return true iff successful.
35 */
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +000036SK_API bool SkInstallDiscardablePixelRef(SkImageGenerator*, SkBitmap* destination);
halcanary@google.comedd370f2013-12-10 21:11:12 +000037
halcanary@google.comad04eb42013-11-21 15:32:08 +000038
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.comedd370f2013-12-10 21:11:12 +000043class SK_API SkImageGenerator {
halcanary@google.comad04eb42013-11-21 15:32:08 +000044public:
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.orgb2639852014-05-28 16:01:55 +000051#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.comad04eb42013-11-21 15:32:08 +000058 /**
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.orgb2639852014-05-28 16:01:55 +000065 SkData* refEncodedData() { return this->onRefEncodedData(); }
halcanary@google.comad04eb42013-11-21 15:32:08 +000066
67 /**
68 * Return some information about the image, allowing the owner of
69 * this object to allocate pixels.
70 *
commit-bot@chromium.orgdd597992013-12-02 22:32:54 +000071 * Repeated calls to this function should give the same results,
72 * allowing the PixelRef to be immutable.
73 *
halcanary@google.comad04eb42013-11-21 15:32:08 +000074 * @return false if anything goes wrong.
75 */
commit-bot@chromium.orgb2639852014-05-28 16:01:55 +000076 bool getInfo(SkImageInfo* info);
halcanary@google.comad04eb42013-11-21 15:32:08 +000077
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.orgdd597992013-12-02 22:32:54 +000083 * Repeated calls to this function should give the same results,
84 * allowing the PixelRef to be immutable.
85 *
halcanary@google.comad04eb42013-11-21 15:32:08 +000086 * @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.orgb2639852014-05-28 16:01:55 +000094 * 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.comad04eb42013-11-21 15:32:08 +0000101 * @return false if anything goes wrong or if the image info is
102 * unsupported.
103 */
commit-bot@chromium.orgb2639852014-05-28 16:01:55 +0000104 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
113protected:
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.comad04eb42013-11-21 15:32:08 +0000119};
120
121#endif // SkImageGenerator_DEFINED