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: michael@0: #ifndef SkMallocPixelRef_DEFINED michael@0: #define SkMallocPixelRef_DEFINED michael@0: michael@0: #include "SkPixelRef.h" michael@0: michael@0: /** We explicitly use the same allocator for our pixels that SkMask does, michael@0: so that we can freely assign memory allocated by one class to the other. michael@0: */ michael@0: class SK_API SkMallocPixelRef : public SkPixelRef { michael@0: public: michael@0: SK_DECLARE_INST_COUNT(SkMallocPixelRef) michael@0: /** michael@0: * Return a new SkMallocPixelRef with the provided pixel storage, rowBytes, michael@0: * and optional colortable. The caller is responsible for managing the michael@0: * lifetime of the pixel storage buffer, as this pixelref will not try michael@0: * to delete it. michael@0: * michael@0: * The pixelref will ref() the colortable (if not NULL). michael@0: * michael@0: * Returns NULL on failure. michael@0: */ michael@0: static SkMallocPixelRef* NewDirect(const SkImageInfo&, void* addr, michael@0: size_t rowBytes, SkColorTable*); michael@0: michael@0: /** michael@0: * Return a new SkMallocPixelRef, automatically allocating storage for the michael@0: * pixels. If rowBytes are 0, an optimal value will be chosen automatically. michael@0: * If rowBytes is > 0, then it will be respected, or NULL will be returned michael@0: * if rowBytes is invalid for the specified info. michael@0: * michael@0: * This pixelref will ref() the specified colortable (if not NULL). michael@0: * michael@0: * Returns NULL on failure. michael@0: */ michael@0: static SkMallocPixelRef* NewAllocate(const SkImageInfo& info, michael@0: size_t rowBytes, SkColorTable*); michael@0: michael@0: /** michael@0: * Return a new SkMallocPixelRef with the provided pixel storage, michael@0: * rowBytes, and optional colortable. On destruction, ReleaseProc michael@0: * will be called. michael@0: * michael@0: * This pixelref will ref() the specified colortable (if not NULL). michael@0: * michael@0: * Returns NULL on failure. michael@0: */ michael@0: typedef void (*ReleaseProc)(void* addr, void* context); michael@0: static SkMallocPixelRef* NewWithProc(const SkImageInfo& info, michael@0: size_t rowBytes, SkColorTable*, michael@0: void* addr, ReleaseProc proc, michael@0: void* context); michael@0: michael@0: /** michael@0: * Return a new SkMallocPixelRef that will use the provided michael@0: * SkData, rowBytes, and optional colortable as pixel storage. michael@0: * The SkData will be ref()ed and on destruction of the PielRef, michael@0: * the SkData will be unref()ed. michael@0: * michael@0: * @param offset (in bytes) into the provided SkData that the michael@0: * first pixel is located at. michael@0: * michael@0: * This pixelref will ref() the specified colortable (if not NULL). michael@0: * michael@0: * Returns NULL on failure. michael@0: */ michael@0: static SkMallocPixelRef* NewWithData(const SkImageInfo& info, michael@0: size_t rowBytes, michael@0: SkColorTable* ctable, michael@0: SkData* data, michael@0: size_t offset = 0); michael@0: michael@0: void* getAddr() const { return fStorage; } michael@0: michael@0: class PRFactory : public SkPixelRefFactory { michael@0: public: michael@0: virtual SkPixelRef* create(const SkImageInfo&, michael@0: SkColorTable*) SK_OVERRIDE; michael@0: }; michael@0: michael@0: SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMallocPixelRef) michael@0: michael@0: protected: michael@0: // The ownPixels version of this constructor is deprecated. michael@0: SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*, michael@0: bool ownPixels); michael@0: SkMallocPixelRef(SkReadBuffer& buffer); michael@0: virtual ~SkMallocPixelRef(); michael@0: michael@0: virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE; michael@0: virtual void onUnlockPixels() SK_OVERRIDE; michael@0: virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; michael@0: virtual size_t getAllocatedSizeInBytes() const SK_OVERRIDE; michael@0: michael@0: private: michael@0: void* fStorage; michael@0: SkColorTable* fCTable; michael@0: size_t fRB; michael@0: ReleaseProc fReleaseProc; michael@0: void* fReleaseProcContext; michael@0: michael@0: SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*, michael@0: ReleaseProc proc, void* context); michael@0: michael@0: typedef SkPixelRef INHERITED; michael@0: }; michael@0: michael@0: michael@0: #endif