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     SK_DECLARE_INST_COUNT(SkMallocPixelRef)
     20     /**
     21      *  Return a new SkMallocPixelRef with the provided pixel storage, rowBytes,
     22      *  and optional colortable. The caller is responsible for managing the
     23      *  lifetime of the pixel storage buffer, as this pixelref will not try
     24      *  to delete it.
     25      *
     26      *  The pixelref will ref() the colortable (if not NULL).
     27      *
     28      *  Returns NULL on failure.
     29      */
     30     static SkMallocPixelRef* NewDirect(const SkImageInfo&, void* addr,
     31                                        size_t rowBytes, SkColorTable*);
     32 
     33     /**
     34      *  Return a new SkMallocPixelRef, automatically allocating storage for the
     35      *  pixels. If rowBytes are 0, an optimal value will be chosen automatically.
     36      *  If rowBytes is > 0, then it will be respected, or NULL will be returned
     37      *  if rowBytes is invalid for the specified info.
     38      *
     39      *  This pixelref will ref() the specified colortable (if not NULL).
     40      *
     41      *  Returns NULL on failure.
     42      */
     43     static SkMallocPixelRef* NewAllocate(const SkImageInfo& info,
     44                                          size_t rowBytes, SkColorTable*);
     45 
     46     /**
     47      *  Return a new SkMallocPixelRef with the provided pixel storage,
     48      *  rowBytes, and optional colortable. On destruction, ReleaseProc
     49      *  will be called.
     50      *
     51      *  This pixelref will ref() the specified colortable (if not NULL).
     52      *
     53      *  Returns NULL on failure.
     54      */
     55     typedef void (*ReleaseProc)(void* addr, void* context);
     56     static SkMallocPixelRef* NewWithProc(const SkImageInfo& info,
     57                                          size_t rowBytes, SkColorTable*,
     58                                          void* addr, ReleaseProc proc,
     59                                          void* context);
     60 
     61     /**
     62      *  Return a new SkMallocPixelRef that will use the provided
     63      *  SkData, rowBytes, and optional colortable as pixel storage.
     64      *  The SkData will be ref()ed and on destruction of the PielRef,
     65      *  the SkData will be unref()ed.
     66      *
     67      *  This pixelref will ref() the specified colortable (if not NULL).
     68      *
     69      *  Returns NULL on failure.
     70      */
     71     static SkMallocPixelRef* NewWithData(const SkImageInfo& info,
     72                                          size_t rowBytes,
     73                                          SkColorTable* ctable,
     74                                          SkData* data);
     75 
     76     void* getAddr() const { return fStorage; }
     77 
     78     class PRFactory : public SkPixelRefFactory {
     79     public:
     80         virtual SkPixelRef* create(const SkImageInfo&,
     81                                    size_t rowBytes,
     82                                    SkColorTable*) SK_OVERRIDE;
     83     };
     84 
     85 protected:
     86     // The ownPixels version of this constructor is deprecated.
     87     SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*,
     88                      bool ownPixels);
     89     virtual ~SkMallocPixelRef();
     90 
     91     virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE;
     92     virtual void onUnlockPixels() SK_OVERRIDE;
     93     virtual size_t getAllocatedSizeInBytes() const SK_OVERRIDE;
     94 
     95 private:
     96     void*           fStorage;
     97     SkColorTable*   fCTable;
     98     size_t          fRB;
     99     ReleaseProc     fReleaseProc;
    100     void*           fReleaseProcContext;
    101 
    102     SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*,
    103                      ReleaseProc proc, void* context);
    104 
    105     typedef SkPixelRef INHERITED;
    106 };
    107 
    108 
    109 #endif
    110