Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2008 The Android Open Source Project
      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 
      9 #ifndef SkMallocPixelRef_DEFINED
     10 #define SkMallocPixelRef_DEFINED
     11 
     12 #include "SkPixelRef.h"
     13 
     14 /** We explicitly use the same allocator for our pixels that SkMask does,
     15     so that we can freely assign memory allocated by one class to the other.
     16 */
     17 class SK_API SkMallocPixelRef : public SkPixelRef {
     18 public:
     19     /**
     20      *  Return a new SkMallocPixelRef with the provided pixel storage, rowBytes,
     21      *  and optional colortable. The caller is responsible for managing the
     22      *  lifetime of the pixel storage buffer, as this pixelref will not try
     23      *  to delete it.
     24      *
     25      *  The pixelref will ref() the colortable (if not NULL).
     26      *
     27      *  Returns NULL on failure.
     28      */
     29     static SkMallocPixelRef* NewDirect(const SkImageInfo&, void* addr,
     30                                        size_t rowBytes, SkColorTable*);
     31 
     32     /**
     33      *  Return a new SkMallocPixelRef, automatically allocating storage for the
     34      *  pixels. If rowBytes are 0, an optimal value will be chosen automatically.
     35      *  If rowBytes is > 0, then it will be respected, or NULL will be returned
     36      *  if rowBytes is invalid for the specified info.
     37      *
     38      *  This pixelref will ref() the specified colortable (if not NULL).
     39      *
     40      *  Returns NULL on failure.
     41      */
     42     static SkMallocPixelRef* NewAllocate(const SkImageInfo& info,
     43                                          size_t rowBytes, SkColorTable*);
     44 
     45     /**
     46      *  Identical to NewAllocate, except all pixel bytes are zeroed.
     47      */
     48     static SkMallocPixelRef* NewZeroed(const SkImageInfo& info,
     49                                        size_t rowBytes, SkColorTable*);
     50 
     51     /**
     52      *  Return a new SkMallocPixelRef with the provided pixel storage,
     53      *  rowBytes, and optional colortable. On destruction, ReleaseProc
     54      *  will be called.
     55      *
     56      *  This pixelref will ref() the specified colortable (if not NULL).
     57      *
     58      *  If ReleaseProc is NULL, the pixels will never be released. This
     59      *  can be useful if the pixels were stack allocated. However, such an
     60      *  SkMallocPixelRef must not live beyond its pixels (e.g. by copying
     61      *  an SkBitmap pointing to it, or drawing to an SkPicture).
     62      *
     63      *  Returns NULL on failure.
     64      */
     65     typedef void (*ReleaseProc)(void* addr, void* context);
     66     static SkMallocPixelRef* NewWithProc(const SkImageInfo& info,
     67                                          size_t rowBytes, SkColorTable*,
     68                                          void* addr, ReleaseProc proc,
     69                                          void* context);
     70 
     71     /**
     72      *  Return a new SkMallocPixelRef that will use the provided
     73      *  SkData, rowBytes, and optional colortable as pixel storage.
     74      *  The SkData will be ref()ed and on destruction of the PielRef,
     75      *  the SkData will be unref()ed.
     76      *
     77      *  This pixelref will ref() the specified colortable (if not NULL).
     78      *
     79      *  Returns NULL on failure.
     80      */
     81     static SkMallocPixelRef* NewWithData(const SkImageInfo& info,
     82                                          size_t rowBytes,
     83                                          SkColorTable* ctable,
     84                                          SkData* data);
     85 
     86     void* getAddr() const { return fStorage; }
     87 
     88     class PRFactory : public SkPixelRefFactory {
     89     public:
     90         SkPixelRef* create(const SkImageInfo&, size_t rowBytes, SkColorTable*) override;
     91     };
     92 
     93     class ZeroedPRFactory : public SkPixelRefFactory {
     94     public:
     95         SkPixelRef* create(const SkImageInfo&, size_t rowBytes, SkColorTable*) override;
     96     };
     97 
     98 protected:
     99     // The ownPixels version of this constructor is deprecated.
    100     SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*,
    101                      bool ownPixels);
    102     ~SkMallocPixelRef() override;
    103 
    104     bool onNewLockPixels(LockRec*) override;
    105     void onUnlockPixels() override;
    106     size_t getAllocatedSizeInBytes() const override;
    107 
    108 private:
    109     // Uses alloc to implement NewAllocate or NewZeroed.
    110     static SkMallocPixelRef* NewUsing(void*(*alloc)(size_t),
    111                                       const SkImageInfo&,
    112                                       size_t rowBytes,
    113                                       SkColorTable*);
    114 
    115     void*           fStorage;
    116     SkColorTable*   fCTable;
    117     size_t          fRB;
    118     ReleaseProc     fReleaseProc;
    119     void*           fReleaseProcContext;
    120 
    121     SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*,
    122                      ReleaseProc proc, void* context);
    123 
    124     typedef SkPixelRef INHERITED;
    125 };
    126 
    127 
    128 #endif
    129