Home | History | Annotate | Download | only in android
      1 /*
      2  * Copyright 2015 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 SkBitmapRegionDecoder_DEFINED
      9 #define SkBitmapRegionDecoder_DEFINED
     10 
     11 #include "SkBitmap.h"
     12 #include "SkBRDAllocator.h"
     13 #include "SkEncodedImageFormat.h"
     14 #include "SkStream.h"
     15 
     16 /*
     17  * This class aims to provide an interface to test multiple implementations of
     18  * SkBitmapRegionDecoder.
     19  */
     20 class SK_API SkBitmapRegionDecoder {
     21 public:
     22 
     23     enum Strategy {
     24         kAndroidCodec_Strategy, // Uses SkAndroidCodec for scaling and subsetting
     25     };
     26 
     27     /*
     28      * @param data     Refs the data while this object exists, unrefs on destruction
     29      * @param strategy Strategy used for scaling and subsetting
     30      * @return         Tries to create an SkBitmapRegionDecoder, returns NULL on failure
     31      */
     32     static SkBitmapRegionDecoder* Create(sk_sp<SkData>, Strategy strategy);
     33 
     34     /*
     35      * @param stream   Takes ownership of the stream
     36      * @param strategy Strategy used for scaling and subsetting
     37      * @return         Tries to create an SkBitmapRegionDecoder, returns NULL on failure
     38      */
     39     static SkBitmapRegionDecoder* Create(
     40             SkStreamRewindable* stream, Strategy strategy);
     41 
     42     /*
     43      * Decode a scaled region of the encoded image stream
     44      *
     45      * @param bitmap          Container for decoded pixels.  It is assumed that the pixels
     46      *                        are initially unallocated and will be allocated by this function.
     47      * @param allocator       Allocator for the pixels.  If this is NULL, the default
     48      *                        allocator (HeapAllocator) will be used.
     49      * @param desiredSubset   Subset of the original image to decode.
     50      * @param sampleSize      An integer downscaling factor for the decode.
     51      * @param colorType       Preferred output colorType.
     52      *                        New implementations should return NULL if they do not support
     53      *                        decoding to this color type.
     54      *                        The old kOriginal_Strategy will decode to a default color type
     55      *                        if this color type is unsupported.
     56      * @param requireUnpremul If the image is not opaque, we will use this to determine the
     57      *                        alpha type to use.
     58      * @param prefColorSpace  If non-null and supported, this is the color space that we will
     59      *                        decode into.  Otherwise, we will choose a default.
     60      *
     61      */
     62     virtual bool decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator,
     63                               const SkIRect& desiredSubset, int sampleSize,
     64                               SkColorType colorType, bool requireUnpremul,
     65                               sk_sp<SkColorSpace> prefColorSpace = nullptr) = 0;
     66 
     67     virtual SkEncodedImageFormat getEncodedFormat() = 0;
     68 
     69     virtual SkColorType computeOutputColorType(SkColorType requestedColorType) = 0;
     70 
     71     virtual sk_sp<SkColorSpace> computeOutputColorSpace(SkColorType outputColorType,
     72             sk_sp<SkColorSpace> prefColorSpace = nullptr) = 0;
     73 
     74 
     75     int width() const { return fWidth; }
     76     int height() const { return fHeight; }
     77 
     78     virtual ~SkBitmapRegionDecoder() {}
     79 
     80 protected:
     81 
     82     SkBitmapRegionDecoder(int width, int height)
     83         : fWidth(width)
     84         , fHeight(height)
     85     {}
     86 
     87 private:
     88     const int fWidth;
     89     const int fHeight;
     90 };
     91 
     92 #endif
     93