1ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com/* 2ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * Copyright 2013 Google Inc. 3ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * 4ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * Use of this source code is governed by a BSD-style license that can be 5ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * found in the LICENSE file. 6ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com */ 7ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com 8ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com#ifndef SkImageGenerator_DEFINED 9ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com#define SkImageGenerator_DEFINED 10ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com 1100f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org#include "SkColor.h" 12abaef86f2b37d8a939506a2076da07f6db456951rileya#include "SkImageInfo.h" 13ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com 14edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.comclass SkBitmap; 15ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.comclass SkData; 16edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.comclass SkImageGenerator; 17edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.com 18edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.com/** 19edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.com * Takes ownership of SkImageGenerator. If this method fails for 20edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.com * whatever reason, it will return false and immediatetely delete 21edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.com * the generator. If it succeeds, it will modify destination 22edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.com * bitmap. 23edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.com * 243d50ea1b87132833d7eab38964f40315ba553205halcanary@google.com * If generator is NULL, will safely return false. 253d50ea1b87132833d7eab38964f40315ba553205halcanary@google.com * 26edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.com * If this fails or when the SkDiscardablePixelRef that is 27edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.com * installed into destination is destroyed, it will call 28edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.com * SkDELETE() on the generator. Therefore, generator should be 29edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.com * allocated with SkNEW() or SkNEW_ARGS(). 30edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.com * 31edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.com * @param destination Upon success, this bitmap will be 32edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.com * configured and have a pixelref installed. 33edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.com * 34edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.com * @return true iff successful. 35edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.com */ 362d970b5128f7270cd01a93e4ce68d0c3ea67ac71commit-bot@chromium.orgSK_API bool SkInstallDiscardablePixelRef(SkImageGenerator*, SkBitmap* destination); 37edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.com 38ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com 39ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com/** 40ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * An interface that allows a purgeable PixelRef (such as a 41ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * SkDiscardablePixelRef) to decode and re-decode an image as needed. 42ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com */ 43edd370f949a457f5d8f7a62efdaf685d4caf46fehalcanary@google.comclass SK_API SkImageGenerator { 44ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.compublic: 45ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com /** 46ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * The PixelRef which takes ownership of this SkImageGenerator 47ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * will call the image generator's destructor. 48ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com */ 49ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com virtual ~SkImageGenerator() { } 50ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com 5100f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org#ifdef SK_SUPPORT_LEGACY_IMAGEGENERATORAPI 5268d20ece8912c4260e9d9d1e8fdd683a3b8b4f26reed@google.com virtual SkData* refEncodedData() { return this->onRefEncodedData(); } 5300f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org virtual bool getInfo(SkImageInfo* info) { return this->onGetInfo(info); } 5400f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org virtual bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) { 5500f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org return this->onGetPixels(info, pixels, rowBytes, NULL, NULL); 5600f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org } 5700f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org#else 58ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com /** 59ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * Return a ref to the encoded (i.e. compressed) representation, 60ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * of this data. 61ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * 62ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * If non-NULL is returned, the caller is responsible for calling 63ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * unref() on the data when it is finished. 64ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com */ 6500f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org SkData* refEncodedData() { return this->onRefEncodedData(); } 66ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com 67ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com /** 68ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * Return some information about the image, allowing the owner of 69ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * this object to allocate pixels. 70ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * 71dd59799cf8eeb8e4263689e993a020b370d77991commit-bot@chromium.org * Repeated calls to this function should give the same results, 72dd59799cf8eeb8e4263689e993a020b370d77991commit-bot@chromium.org * allowing the PixelRef to be immutable. 73dd59799cf8eeb8e4263689e993a020b370d77991commit-bot@chromium.org * 74ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * @return false if anything goes wrong. 75ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com */ 7600f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org bool getInfo(SkImageInfo* info); 77ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com 78ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com /** 79ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * Decode into the given pixels, a block of memory of size at 80ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * least (info.fHeight - 1) * rowBytes + (info.fWidth * 81ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * bytesPerPixel) 82ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * 83dd59799cf8eeb8e4263689e993a020b370d77991commit-bot@chromium.org * Repeated calls to this function should give the same results, 84dd59799cf8eeb8e4263689e993a020b370d77991commit-bot@chromium.org * allowing the PixelRef to be immutable. 85dd59799cf8eeb8e4263689e993a020b370d77991commit-bot@chromium.org * 86ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * @param info A description of the format (config, size) 87ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * expected by the caller. This can simply be identical 88ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * to the info returned by getInfo(). 89ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * 90ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * This contract also allows the caller to specify 91ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * different output-configs, which the implementation can 92ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * decide to support or not. 93ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * 9400f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org * If info is kIndex8_SkColorType, then the caller must provide storage for up to 256 9500f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org * SkPMColor values in ctable. On success the generator must copy N colors into that storage, 9600f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org * (where N is the logical number of table entries) and set ctableCount to N. 9700f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org * 9800f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org * If info is not kIndex8_SkColorType, then the last two parameters may be NULL. If ctableCount 9900f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org * is not null, it will be set to 0. 10000f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org * 101ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * @return false if anything goes wrong or if the image info is 102ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com * unsupported. 103ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com */ 10400f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, 10500f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org SkPMColor ctable[], int* ctableCount); 10600f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org 10700f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org /** 10800f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org * Simplified version of getPixels() that asserts that info is NOT kIndex8_SkColorType. 10900f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org */ 11000f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes); 11100f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org#endif 11200f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org 113518d83dbc1c899e316e8c896af5defb58b83120fsugoi /** 114518d83dbc1c899e316e8c896af5defb58b83120fsugoi * If planes or rowBytes is NULL or if any entry in planes is NULL or if any entry in rowBytes 115518d83dbc1c899e316e8c896af5defb58b83120fsugoi * is 0, this imagegenerator should output the sizes and return true if it can efficiently 116518d83dbc1c899e316e8c896af5defb58b83120fsugoi * return YUV planar data. If it cannot, it should return false. Note that either planes and 117518d83dbc1c899e316e8c896af5defb58b83120fsugoi * rowBytes are both fully defined and non NULL/non 0 or they are both NULL or have NULL or 0 118518d83dbc1c899e316e8c896af5defb58b83120fsugoi * entries only. Having only partial planes/rowBytes information is not supported. 119518d83dbc1c899e316e8c896af5defb58b83120fsugoi * 120518d83dbc1c899e316e8c896af5defb58b83120fsugoi * If all planes and rowBytes entries are non NULL or non 0, then it should copy the 121518d83dbc1c899e316e8c896af5defb58b83120fsugoi * associated YUV data into those planes of memory supplied by the caller. It should validate 122518d83dbc1c899e316e8c896af5defb58b83120fsugoi * that the sizes match what it expected. If the sizes do not match, it should return false. 123518d83dbc1c899e316e8c896af5defb58b83120fsugoi */ 124abaef86f2b37d8a939506a2076da07f6db456951rileya bool getYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], 125abaef86f2b37d8a939506a2076da07f6db456951rileya SkYUVColorSpace* colorSpace); 126518d83dbc1c899e316e8c896af5defb58b83120fsugoi 12700f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.orgprotected: 12800f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org virtual SkData* onRefEncodedData(); 12900f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org virtual bool onGetInfo(SkImageInfo* info); 13000f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org virtual bool onGetPixels(const SkImageInfo& info, 13100f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org void* pixels, size_t rowBytes, 13200f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org SkPMColor ctable[], int* ctableCount); 133518d83dbc1c899e316e8c896af5defb58b83120fsugoi virtual bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3]); 134abaef86f2b37d8a939506a2076da07f6db456951rileya virtual bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], 135abaef86f2b37d8a939506a2076da07f6db456951rileya SkYUVColorSpace* colorSpace); 136ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com}; 137ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com 138ad04eb49f5d3f324e6b85411c776d7466c1fef92halcanary@google.com#endif // SkImageGenerator_DEFINED 139