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 "SkDiscardableMemory.h"
     12 #include "SkImageInfo.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 this fails or when the SkDiscardablePixelRef that is
     25  *  installed into destination is destroyed, it will call
     26  *  SkDELETE() on the generator.  Therefore, generator should be
     27  *  allocated with SkNEW() or SkNEW_ARGS().
     28  *
     29  *  @param destination Upon success, this bitmap will be
     30  *  configured and have a pixelref installed.
     31  *
     32  *  @param factory If not NULL, this object will be used as a
     33  *  source of discardable memory when decoding.  If NULL, then
     34  *  SkDiscardableMemory::Create() will be called.
     35  *
     36  *  @return true iff successful.
     37  */
     38 SK_API bool SkInstallDiscardablePixelRef(SkImageGenerator* generator,
     39                                          SkBitmap* destination,
     40                                          SkDiscardableMemory::Factory* factory = NULL);
     41 
     42 
     43 /**
     44  *  An interface that allows a purgeable PixelRef (such as a
     45  *  SkDiscardablePixelRef) to decode and re-decode an image as needed.
     46  */
     47 class SK_API SkImageGenerator {
     48 public:
     49     /**
     50      *  The PixelRef which takes ownership of this SkImageGenerator
     51      *  will call the image generator's destructor.
     52      */
     53     virtual ~SkImageGenerator() { }
     54 
     55     /**
     56      *  Return a ref to the encoded (i.e. compressed) representation,
     57      *  of this data.
     58      *
     59      *  If non-NULL is returned, the caller is responsible for calling
     60      *  unref() on the data when it is finished.
     61      */
     62     virtual SkData* refEncodedData() { return NULL; }
     63 
     64     /**
     65      *  Return some information about the image, allowing the owner of
     66      *  this object to allocate pixels.
     67      *
     68      *  Repeated calls to this function should give the same results,
     69      *  allowing the PixelRef to be immutable.
     70      *
     71      *  @return false if anything goes wrong.
     72      */
     73     virtual bool getInfo(SkImageInfo* info) = 0;
     74 
     75     /**
     76      *  Decode into the given pixels, a block of memory of size at
     77      *  least (info.fHeight - 1) * rowBytes + (info.fWidth *
     78      *  bytesPerPixel)
     79      *
     80      *  Repeated calls to this function should give the same results,
     81      *  allowing the PixelRef to be immutable.
     82      *
     83      *  @param info A description of the format (config, size)
     84      *         expected by the caller.  This can simply be identical
     85      *         to the info returned by getInfo().
     86      *
     87      *         This contract also allows the caller to specify
     88      *         different output-configs, which the implementation can
     89      *         decide to support or not.
     90      *
     91      *  @return false if anything goes wrong or if the image info is
     92      *          unsupported.
     93      */
     94     virtual bool getPixels(const SkImageInfo& info,
     95                            void* pixels,
     96                            size_t rowBytes) = 0;
     97 };
     98 
     99 #endif  // SkImageGenerator_DEFINED
    100