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 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 * @param Requested destination color type 68 * @return true if we support the requested color type and false otherwise 69 */ 70 virtual bool conversionSupported(SkColorType colorType) = 0; 71 72 virtual SkEncodedImageFormat getEncodedFormat() = 0; 73 74 virtual SkColorType computeOutputColorType(SkColorType requestedColorType) = 0; 75 76 virtual sk_sp<SkColorSpace> computeOutputColorSpace(SkColorType outputColorType, 77 sk_sp<SkColorSpace> prefColorSpace = nullptr) = 0; 78 79 width()80 int width() const { return fWidth; } height()81 int height() const { return fHeight; } 82 ~SkBitmapRegionDecoder()83 virtual ~SkBitmapRegionDecoder() {} 84 85 protected: 86 SkBitmapRegionDecoder(int width,int height)87 SkBitmapRegionDecoder(int width, int height) 88 : fWidth(width) 89 , fHeight(height) 90 {} 91 92 private: 93 const int fWidth; 94 const int fHeight; 95 }; 96 97 #endif 98