michael@0: /* michael@0: * Copyright 2008 The Android Open Source Project 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 SkPixelRef_DEFINED michael@0: #define SkPixelRef_DEFINED michael@0: michael@0: #include "SkBitmap.h" michael@0: #include "SkRefCnt.h" michael@0: #include "SkString.h" michael@0: #include "SkFlattenable.h" michael@0: #include "SkImageInfo.h" michael@0: #include "SkTDArray.h" michael@0: michael@0: //#define xed michael@0: michael@0: #ifdef SK_DEBUG michael@0: /** michael@0: * Defining SK_IGNORE_PIXELREF_SETPRELOCKED will force all pixelref michael@0: * subclasses to correctly handle lock/unlock pixels. For performance michael@0: * reasons, simple malloc-based subclasses call setPreLocked() to skip michael@0: * the overhead of implementing these calls. michael@0: * michael@0: * This build-flag disables that optimization, to add in debugging our michael@0: * call-sites, to ensure that they correctly balance their calls of michael@0: * lock and unlock. michael@0: */ michael@0: // #define SK_IGNORE_PIXELREF_SETPRELOCKED michael@0: #endif michael@0: michael@0: class SkColorTable; michael@0: class SkData; michael@0: struct SkIRect; michael@0: class SkMutex; michael@0: michael@0: class GrTexture; michael@0: michael@0: /** \class SkPixelRef michael@0: michael@0: This class is the smart container for pixel memory, and is used with michael@0: SkBitmap. A pixelref is installed into a bitmap, and then the bitmap can michael@0: access the actual pixel memory by calling lockPixels/unlockPixels. michael@0: michael@0: This class can be shared/accessed between multiple threads. michael@0: */ michael@0: class SK_API SkPixelRef : public SkFlattenable { michael@0: public: michael@0: SK_DECLARE_INST_COUNT(SkPixelRef) michael@0: michael@0: explicit SkPixelRef(const SkImageInfo&); michael@0: SkPixelRef(const SkImageInfo&, SkBaseMutex* mutex); michael@0: virtual ~SkPixelRef(); michael@0: michael@0: const SkImageInfo& info() const { michael@0: return fInfo; michael@0: } michael@0: michael@0: /** Return the pixel memory returned from lockPixels, or null if the michael@0: lockCount is 0. michael@0: */ michael@0: void* pixels() const { return fRec.fPixels; } michael@0: michael@0: /** Return the current colorTable (if any) if pixels are locked, or null. michael@0: */ michael@0: SkColorTable* colorTable() const { return fRec.fColorTable; } michael@0: michael@0: size_t rowBytes() const { return fRec.fRowBytes; } michael@0: michael@0: /** michael@0: * To access the actual pixels of a pixelref, it must be "locked". michael@0: * Calling lockPixels returns a LockRec struct (on success). michael@0: */ michael@0: struct LockRec { michael@0: void* fPixels; michael@0: SkColorTable* fColorTable; michael@0: size_t fRowBytes; michael@0: michael@0: void zero() { sk_bzero(this, sizeof(*this)); } michael@0: michael@0: bool isZero() const { michael@0: return NULL == fPixels && NULL == fColorTable && 0 == fRowBytes; michael@0: } michael@0: }; michael@0: michael@0: /** michael@0: * Returns true if the lockcount > 0 michael@0: */ michael@0: bool isLocked() const { return fLockCount > 0; } michael@0: michael@0: SkDEBUGCODE(int getLockCount() const { return fLockCount; }) michael@0: michael@0: /** michael@0: * Call to access the pixel memory. Return true on success. Balance this michael@0: * with a call to unlockPixels(). michael@0: */ michael@0: bool lockPixels(); michael@0: michael@0: /** michael@0: * Call to access the pixel memory. On success, return true and fill out michael@0: * the specified rec. On failure, return false and ignore the rec parameter. michael@0: * Balance this with a call to unlockPixels(). michael@0: */ michael@0: bool lockPixels(LockRec* rec); michael@0: michael@0: /** Call to balanace a previous call to lockPixels(). Returns the pixels michael@0: (or null) after the unlock. NOTE: lock calls can be nested, but the michael@0: matching number of unlock calls must be made in order to free the michael@0: memory (if the subclass implements caching/deferred-decoding.) michael@0: */ michael@0: void unlockPixels(); michael@0: michael@0: /** michael@0: * Some bitmaps can return a copy of their pixels for lockPixels(), but michael@0: * that copy, if modified, will not be pushed back. These bitmaps should michael@0: * not be used as targets for a raster device/canvas (since all pixels michael@0: * modifications will be lost when unlockPixels() is called.) michael@0: */ michael@0: bool lockPixelsAreWritable() const; michael@0: michael@0: /** Returns a non-zero, unique value corresponding to the pixels in this michael@0: pixelref. Each time the pixels are changed (and notifyPixelsChanged is michael@0: called), a different generation ID will be returned. michael@0: */ michael@0: uint32_t getGenerationID() const; michael@0: michael@0: /** michael@0: * Call this if you have changed the contents of the pixels. This will in- michael@0: * turn cause a different generation ID value to be returned from michael@0: * getGenerationID(). michael@0: */ michael@0: void notifyPixelsChanged(); michael@0: michael@0: /** michael@0: * Change the info's AlphaType. Note that this does not automatically michael@0: * invalidate the generation ID. If the pixel values themselves have michael@0: * changed, then you must explicitly call notifyPixelsChanged() as well. michael@0: */ michael@0: void changeAlphaType(SkAlphaType at); michael@0: michael@0: /** Returns true if this pixelref is marked as immutable, meaning that the michael@0: contents of its pixels will not change for the lifetime of the pixelref. michael@0: */ michael@0: bool isImmutable() const { return fIsImmutable; } michael@0: michael@0: /** Marks this pixelref is immutable, meaning that the contents of its michael@0: pixels will not change for the lifetime of the pixelref. This state can michael@0: be set on a pixelref, but it cannot be cleared once it is set. michael@0: */ michael@0: void setImmutable(); michael@0: michael@0: /** Return the optional URI string associated with this pixelref. May be michael@0: null. michael@0: */ michael@0: const char* getURI() const { return fURI.size() ? fURI.c_str() : NULL; } michael@0: michael@0: /** Copy a URI string to this pixelref, or clear the URI if the uri is null michael@0: */ michael@0: void setURI(const char uri[]) { michael@0: fURI.set(uri); michael@0: } michael@0: michael@0: /** Copy a URI string to this pixelref michael@0: */ michael@0: void setURI(const char uri[], size_t len) { michael@0: fURI.set(uri, len); michael@0: } michael@0: michael@0: /** Assign a URI string to this pixelref. michael@0: */ michael@0: void setURI(const SkString& uri) { fURI = uri; } michael@0: michael@0: /** michael@0: * If the pixelRef has an encoded (i.e. compressed) representation, michael@0: * return a ref to its data. If the pixelRef michael@0: * is uncompressed or otherwise does not have this form, return NULL. michael@0: * michael@0: * If non-null is returned, the caller is responsible for calling unref() michael@0: * on the data when it is finished. michael@0: */ michael@0: SkData* refEncodedData() { michael@0: return this->onRefEncodedData(); michael@0: } michael@0: michael@0: /** michael@0: * Experimental -- tells the caller if it is worth it to call decodeInto(). michael@0: * Just an optimization at this point, to avoid checking the cache first. michael@0: * We may remove/change this call in the future. michael@0: */ michael@0: bool implementsDecodeInto() { michael@0: return this->onImplementsDecodeInto(); michael@0: } michael@0: michael@0: /** michael@0: * Return a decoded instance of this pixelRef in bitmap. If this cannot be michael@0: * done, return false and the bitmap parameter is ignored/unchanged. michael@0: * michael@0: * pow2 is the requeste power-of-two downscale that the caller needs. This michael@0: * can be ignored, and the "original" size can be returned, but if the michael@0: * underlying codec can efficiently return a smaller size, that should be michael@0: * done. Some examples: michael@0: * michael@0: * To request the "base" version (original scale), pass 0 for pow2 michael@0: * To request 1/2 scale version (1/2 width, 1/2 height), pass 1 for pow2 michael@0: * To request 1/4 scale version (1/4 width, 1/4 height), pass 2 for pow2 michael@0: * ... michael@0: * michael@0: * If this returns true, then bitmap must be "locked" such that michael@0: * bitmap->getPixels() will return the correct address. michael@0: */ michael@0: bool decodeInto(int pow2, SkBitmap* bitmap) { michael@0: SkASSERT(pow2 >= 0); michael@0: return this->onDecodeInto(pow2, bitmap); michael@0: } michael@0: michael@0: /** Are we really wrapping a texture instead of a bitmap? michael@0: */ michael@0: virtual GrTexture* getTexture() { return NULL; } michael@0: michael@0: bool readPixels(SkBitmap* dst, const SkIRect* subset = NULL); michael@0: michael@0: /** michael@0: * Makes a deep copy of this PixelRef, respecting the requested config. michael@0: * @param config Desired config. michael@0: * @param subset Subset of this PixelRef to copy. Must be fully contained within the bounds of michael@0: * of this PixelRef. michael@0: * @return A new SkPixelRef, or NULL if either there is an error (e.g. the destination could michael@0: * not be created with the given config), or this PixelRef does not support deep michael@0: * copies. michael@0: */ michael@0: virtual SkPixelRef* deepCopy(SkBitmap::Config config, const SkIRect* subset = NULL) { michael@0: return NULL; michael@0: } michael@0: michael@0: #ifdef SK_BUILD_FOR_ANDROID michael@0: /** michael@0: * Acquire a "global" ref on this object. michael@0: * The default implementation just calls ref(), but subclasses can override michael@0: * this method to implement additional behavior. michael@0: */ michael@0: virtual void globalRef(void* data=NULL); michael@0: michael@0: /** michael@0: * Release a "global" ref on this object. michael@0: * The default implementation just calls unref(), but subclasses can override michael@0: * this method to implement additional behavior. michael@0: */ michael@0: virtual void globalUnref(); michael@0: #endif michael@0: michael@0: SK_DEFINE_FLATTENABLE_TYPE(SkPixelRef) michael@0: michael@0: // Register a listener that may be called the next time our generation ID changes. michael@0: // michael@0: // We'll only call the listener if we're confident that we are the only SkPixelRef with this michael@0: // generation ID. If our generation ID changes and we decide not to call the listener, we'll michael@0: // never call it: you must add a new listener for each generation ID change. We also won't call michael@0: // the listener when we're certain no one knows what our generation ID is. michael@0: // michael@0: // This can be used to invalidate caches keyed by SkPixelRef generation ID. michael@0: struct GenIDChangeListener { michael@0: virtual ~GenIDChangeListener() {} michael@0: virtual void onChange() = 0; michael@0: }; michael@0: michael@0: // Takes ownership of listener. michael@0: void addGenIDChangeListener(GenIDChangeListener* listener); michael@0: michael@0: protected: michael@0: /** michael@0: * On success, returns true and fills out the LockRec for the pixels. On michael@0: * failure returns false and ignores the LockRec parameter. michael@0: * michael@0: * The caller will have already acquired a mutex for thread safety, so this michael@0: * method need not do that. michael@0: */ michael@0: virtual bool onNewLockPixels(LockRec*) = 0; michael@0: michael@0: /** michael@0: * Balancing the previous successful call to onNewLockPixels. The locked michael@0: * pixel address will no longer be referenced, so the subclass is free to michael@0: * move or discard that memory. michael@0: * michael@0: * The caller will have already acquired a mutex for thread safety, so this michael@0: * method need not do that. michael@0: */ michael@0: virtual void onUnlockPixels() = 0; michael@0: michael@0: /** Default impl returns true */ michael@0: virtual bool onLockPixelsAreWritable() const; michael@0: michael@0: // returns false; michael@0: virtual bool onImplementsDecodeInto(); michael@0: // returns false; michael@0: virtual bool onDecodeInto(int pow2, SkBitmap* bitmap); michael@0: michael@0: /** michael@0: * For pixelrefs that don't have access to their raw pixels, they may be michael@0: * able to make a copy of them (e.g. if the pixels are on the GPU). michael@0: * michael@0: * The base class implementation returns false; michael@0: */ michael@0: virtual bool onReadPixels(SkBitmap* dst, const SkIRect* subsetOrNull); michael@0: michael@0: // default impl returns NULL. michael@0: virtual SkData* onRefEncodedData(); michael@0: michael@0: /** michael@0: * Returns the size (in bytes) of the internally allocated memory. michael@0: * This should be implemented in all serializable SkPixelRef derived classes. michael@0: * SkBitmap::fPixelRefOffset + SkBitmap::getSafeSize() should never overflow this value, michael@0: * otherwise the rendering code may attempt to read memory out of bounds. michael@0: * michael@0: * @return default impl returns 0. michael@0: */ michael@0: virtual size_t getAllocatedSizeInBytes() const; michael@0: michael@0: /** Return the mutex associated with this pixelref. This value is assigned michael@0: in the constructor, and cannot change during the lifetime of the object. michael@0: */ michael@0: SkBaseMutex* mutex() const { return fMutex; } michael@0: michael@0: // serialization michael@0: SkPixelRef(SkReadBuffer&, SkBaseMutex*); michael@0: virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; michael@0: michael@0: // only call from constructor. Flags this to always be locked, removing michael@0: // the need to grab the mutex and call onLockPixels/onUnlockPixels. michael@0: // Performance tweak to avoid those calls (esp. in multi-thread use case). michael@0: void setPreLocked(void*, size_t rowBytes, SkColorTable*); michael@0: michael@0: private: michael@0: SkBaseMutex* fMutex; // must remain in scope for the life of this object michael@0: michael@0: // mostly const. fInfo.fAlpahType can be changed at runtime. michael@0: const SkImageInfo fInfo; michael@0: michael@0: // LockRec is only valid if we're in a locked state (isLocked()) michael@0: LockRec fRec; michael@0: int fLockCount; michael@0: michael@0: mutable uint32_t fGenerationID; michael@0: mutable bool fUniqueGenerationID; michael@0: michael@0: SkTDArray fGenIDChangeListeners; // pointers are owned michael@0: michael@0: SkString fURI; michael@0: michael@0: // can go from false to true, but never from true to false michael@0: bool fIsImmutable; michael@0: // only ever set in constructor, const after that michael@0: bool fPreLocked; michael@0: michael@0: void needsNewGenID(); michael@0: void callGenIDChangeListeners(); michael@0: michael@0: void setMutex(SkBaseMutex* mutex); michael@0: michael@0: // When copying a bitmap to another with the same shape and config, we can safely michael@0: // clone the pixelref generation ID too, which makes them equivalent under caching. michael@0: friend class SkBitmap; // only for cloneGenID michael@0: void cloneGenID(const SkPixelRef&); michael@0: michael@0: typedef SkFlattenable INHERITED; michael@0: }; michael@0: michael@0: class SkPixelRefFactory : public SkRefCnt { michael@0: public: michael@0: /** michael@0: * Allocate a new pixelref matching the specified ImageInfo, allocating michael@0: * the memory for the pixels. If the ImageInfo requires a ColorTable, michael@0: * the pixelref will ref() the colortable. michael@0: * On failure return NULL. michael@0: */ michael@0: virtual SkPixelRef* create(const SkImageInfo&, SkColorTable*) = 0; michael@0: }; michael@0: michael@0: #endif