Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2013 Google Inc.
      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 #ifndef SkImageGenerator_DEFINED
      9 #define SkImageGenerator_DEFINED
     10 
     11 #include "SkImageInfo.h"
     12 #include "SkColor.h"
     13 
     14 class SkBitmap;
     15 class SkData;
     16 class SkImageGenerator;
     17 
     18 /**
     19  *  Takes ownership of SkImageGenerator.  If this method fails for
     20  *  whatever reason, it will return false and immediatetely delete
     21  *  the generator.  If it succeeds, it will modify destination
     22  *  bitmap.
     23  *
     24  *  If generator is NULL, will safely return false.
     25  *
     26  *  If this fails or when the SkDiscardablePixelRef that is
     27  *  installed into destination is destroyed, it will call
     28  *  SkDELETE() on the generator.  Therefore, generator should be
     29  *  allocated with SkNEW() or SkNEW_ARGS().
     30  *
     31  *  @param destination Upon success, this bitmap will be
     32  *  configured and have a pixelref installed.
     33  *
     34  *  @return true iff successful.
     35  */
     36 SK_API bool SkInstallDiscardablePixelRef(SkImageGenerator*, SkBitmap* destination);
     37 
     38 /**
     39  *  Purges all unlocked discardable memory in Skia's global
     40  *  discardable memory pool.
     41  */
     42 SK_API void SkPurgeGlobalDiscardableMemoryPool();
     43 
     44 
     45 /**
     46  *  An interface that allows a purgeable PixelRef (such as a
     47  *  SkDiscardablePixelRef) to decode and re-decode an image as needed.
     48  */
     49 class SK_API SkImageGenerator {
     50 public:
     51     /**
     52      *  The PixelRef which takes ownership of this SkImageGenerator
     53      *  will call the image generator's destructor.
     54      */
     55     virtual ~SkImageGenerator() { }
     56 
     57 #ifdef SK_SUPPORT_LEGACY_IMAGEGENERATORAPI
     58     virtual SkData* refEncodedData() { return this->onRefEncodedData(); }
     59     virtual bool getInfo(SkImageInfo* info) { return this->onGetInfo(info); }
     60     virtual bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
     61         return this->onGetPixels(info, pixels, rowBytes, NULL, NULL);
     62     }
     63 #else
     64     /**
     65      *  Return a ref to the encoded (i.e. compressed) representation,
     66      *  of this data.
     67      *
     68      *  If non-NULL is returned, the caller is responsible for calling
     69      *  unref() on the data when it is finished.
     70      */
     71     SkData* refEncodedData() { return this->onRefEncodedData(); }
     72 
     73     /**
     74      *  Return some information about the image, allowing the owner of
     75      *  this object to allocate pixels.
     76      *
     77      *  Repeated calls to this function should give the same results,
     78      *  allowing the PixelRef to be immutable.
     79      *
     80      *  @return false if anything goes wrong.
     81      */
     82     bool getInfo(SkImageInfo* info);
     83 
     84     /**
     85      *  Decode into the given pixels, a block of memory of size at
     86      *  least (info.fHeight - 1) * rowBytes + (info.fWidth *
     87      *  bytesPerPixel)
     88      *
     89      *  Repeated calls to this function should give the same results,
     90      *  allowing the PixelRef to be immutable.
     91      *
     92      *  @param info A description of the format (config, size)
     93      *         expected by the caller.  This can simply be identical
     94      *         to the info returned by getInfo().
     95      *
     96      *         This contract also allows the caller to specify
     97      *         different output-configs, which the implementation can
     98      *         decide to support or not.
     99      *
    100      *  If info is kIndex8_SkColorType, then the caller must provide storage for up to 256
    101      *  SkPMColor values in ctable. On success the generator must copy N colors into that storage,
    102      *  (where N is the logical number of table entries) and set ctableCount to N.
    103      *
    104      *  If info is not kIndex8_SkColorType, then the last two parameters may be NULL. If ctableCount
    105      *  is not null, it will be set to 0.
    106      *
    107      *  @return false if anything goes wrong or if the image info is
    108      *          unsupported.
    109      */
    110     bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
    111                    SkPMColor ctable[], int* ctableCount);
    112 
    113     /**
    114      *  Simplified version of getPixels() that asserts that info is NOT kIndex8_SkColorType.
    115      */
    116     bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
    117 #endif
    118 
    119 protected:
    120     virtual SkData* onRefEncodedData();
    121     virtual bool onGetInfo(SkImageInfo* info);
    122     virtual bool onGetPixels(const SkImageInfo& info,
    123                              void* pixels, size_t rowBytes,
    124                              SkPMColor ctable[], int* ctableCount);
    125 };
    126 
    127 #endif  // SkImageGenerator_DEFINED
    128