msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 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" |
msarett | cb8d719 | 2015-11-11 13:30:43 -0800 | [diff] [blame] | 12 | #include "SkBRDAllocator.h" |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 13 | #include "SkEncodedImageFormat.h" |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 14 | #include "SkStream.h" |
| 15 | |
| 16 | /* |
| 17 | * This class aims to provide an interface to test multiple implementations of |
| 18 | * SkBitmapRegionDecoder. |
| 19 | */ |
msarett | 5cb4885 | 2015-11-06 08:56:32 -0800 | [diff] [blame] | 20 | class SkBitmapRegionDecoder { |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 21 | public: |
| 22 | |
| 23 | enum Strategy { |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 24 | kAndroidCodec_Strategy, // Uses SkAndroidCodec for scaling and subsetting |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | /* |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 28 | * @param data Refs the data while this object exists, unrefs on destruction |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 29 | * @param strategy Strategy used for scaling and subsetting |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 30 | * @return Tries to create an SkBitmapRegionDecoder, returns NULL on failure |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 31 | */ |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 32 | static SkBitmapRegionDecoder* Create(sk_sp<SkData>, Strategy strategy); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 33 | |
| 34 | /* |
msarett | 3f65e93 | 2015-10-27 13:12:59 -0700 | [diff] [blame] | 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 | */ |
msarett | 5cb4885 | 2015-11-06 08:56:32 -0800 | [diff] [blame] | 39 | static SkBitmapRegionDecoder* Create( |
msarett | 3f65e93 | 2015-10-27 13:12:59 -0700 | [diff] [blame] | 40 | SkStreamRewindable* stream, Strategy strategy); |
| 41 | |
| 42 | /* |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 43 | * Decode a scaled region of the encoded image stream |
| 44 | * |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 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 | * |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 59 | */ |
msarett | cb8d719 | 2015-11-11 13:30:43 -0800 | [diff] [blame] | 60 | virtual bool decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator, |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 61 | const SkIRect& desiredSubset, int sampleSize, |
| 62 | SkColorType colorType, bool requireUnpremul) = 0; |
msarett | 04965c6 | 2015-10-12 10:24:38 -0700 | [diff] [blame] | 63 | /* |
| 64 | * @param Requested destination color type |
| 65 | * @return true if we support the requested color type and false otherwise |
| 66 | */ |
| 67 | virtual bool conversionSupported(SkColorType colorType) = 0; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 68 | |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 69 | virtual SkEncodedImageFormat getEncodedFormat() = 0; |
msarett | 3f65e93 | 2015-10-27 13:12:59 -0700 | [diff] [blame] | 70 | |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 71 | int width() const { return fWidth; } |
| 72 | int height() const { return fHeight; } |
| 73 | |
msarett | 5cb4885 | 2015-11-06 08:56:32 -0800 | [diff] [blame] | 74 | virtual ~SkBitmapRegionDecoder() {} |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 75 | |
| 76 | protected: |
| 77 | |
msarett | 5cb4885 | 2015-11-06 08:56:32 -0800 | [diff] [blame] | 78 | SkBitmapRegionDecoder(int width, int height) |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 79 | : fWidth(width) |
| 80 | , fHeight(height) |
| 81 | {} |
| 82 | |
| 83 | private: |
| 84 | const int fWidth; |
| 85 | const int fHeight; |
| 86 | }; |
| 87 | |
| 88 | #endif |