michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef SkCachingPixelRef_DEFINED michael@0: #define SkCachingPixelRef_DEFINED michael@0: michael@0: #include "SkImageInfo.h" michael@0: #include "SkImageGenerator.h" michael@0: #include "SkPixelRef.h" michael@0: michael@0: class SkColorTable; michael@0: michael@0: /** michael@0: * PixelRef which defers decoding until SkBitmap::lockPixels() is michael@0: * called. Caches the decoded images in the global michael@0: * SkScaledImageCache. When the pixels are unlocked, this cache may michael@0: * or be destroyed before the next lock. If so, onLockPixels will michael@0: * attempt to re-decode. michael@0: * michael@0: * Decoding is handled by the SkImageGenerator michael@0: */ michael@0: class SkCachingPixelRef : public SkPixelRef { michael@0: public: michael@0: SK_DECLARE_INST_COUNT(SkCachingPixelRef) michael@0: /** michael@0: * Takes ownership of SkImageGenerator. If this method fails for michael@0: * whatever reason, it will return false and immediatetely delete michael@0: * the generator. If it succeeds, it will modify destination michael@0: * bitmap. michael@0: * michael@0: * If Install fails or when the SkCachingPixelRef that is michael@0: * installed into destination is destroyed, it will call michael@0: * SkDELETE() on the generator. Therefore, generator should be michael@0: * allocated with SkNEW() or SkNEW_ARGS(). michael@0: */ michael@0: static bool Install(SkImageGenerator* gen, SkBitmap* dst); michael@0: michael@0: protected: michael@0: virtual ~SkCachingPixelRef(); michael@0: virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE; michael@0: virtual void onUnlockPixels() SK_OVERRIDE; michael@0: virtual bool onLockPixelsAreWritable() const SK_OVERRIDE { return false; } michael@0: michael@0: virtual SkData* onRefEncodedData() SK_OVERRIDE { michael@0: return fImageGenerator->refEncodedData(); michael@0: } michael@0: // No need to flatten this object. When flattening an SkBitmap, michael@0: // SkWriteBuffer will check the encoded data and write that michael@0: // instead. michael@0: // Future implementations of SkWriteBuffer will need to michael@0: // special case for onRefEncodedData as well. michael@0: SK_DECLARE_UNFLATTENABLE_OBJECT() michael@0: michael@0: private: michael@0: SkImageGenerator* const fImageGenerator; michael@0: bool fErrorInDecoding; michael@0: void* fScaledCacheId; michael@0: const size_t fRowBytes; michael@0: michael@0: SkCachingPixelRef(const SkImageInfo&, SkImageGenerator*, size_t rowBytes); michael@0: michael@0: typedef SkPixelRef INHERITED; michael@0: }; michael@0: michael@0: #endif // SkCachingPixelRef_DEFINED