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" |
msarett | 3f65e93 | 2015-10-27 13:12:59 -0700 | [diff] [blame] | 13 | #include "SkEncodedFormat.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 | kCanvas_Strategy, // Draw to the canvas, uses SkCodec |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 25 | kAndroidCodec_Strategy, // Uses SkAndroidCodec for scaling and subsetting |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | /* |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 29 | * @param data Refs the data while this object exists, unrefs on destruction |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 30 | * @param strategy Strategy used for scaling and subsetting |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 31 | * @return Tries to create an SkBitmapRegionDecoder, returns NULL on failure |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 32 | */ |
msarett | 5cb4885 | 2015-11-06 08:56:32 -0800 | [diff] [blame] | 33 | static SkBitmapRegionDecoder* Create( |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 34 | SkData* data, Strategy strategy); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 35 | |
| 36 | /* |
msarett | 3f65e93 | 2015-10-27 13:12:59 -0700 | [diff] [blame] | 37 | * @param stream Takes ownership of the stream |
| 38 | * @param strategy Strategy used for scaling and subsetting |
| 39 | * @return Tries to create an SkBitmapRegionDecoder, returns NULL on failure |
| 40 | */ |
msarett | 5cb4885 | 2015-11-06 08:56:32 -0800 | [diff] [blame] | 41 | static SkBitmapRegionDecoder* Create( |
msarett | 3f65e93 | 2015-10-27 13:12:59 -0700 | [diff] [blame] | 42 | SkStreamRewindable* stream, Strategy strategy); |
| 43 | |
| 44 | /* |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 45 | * Decode a scaled region of the encoded image stream |
| 46 | * |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 47 | * @param bitmap Container for decoded pixels. It is assumed that the pixels |
| 48 | * are initially unallocated and will be allocated by this function. |
| 49 | * @param allocator Allocator for the pixels. If this is NULL, the default |
| 50 | * allocator (HeapAllocator) will be used. |
| 51 | * @param desiredSubset Subset of the original image to decode. |
| 52 | * @param sampleSize An integer downscaling factor for the decode. |
| 53 | * @param colorType Preferred output colorType. |
| 54 | * New implementations should return NULL if they do not support |
| 55 | * decoding to this color type. |
| 56 | * The old kOriginal_Strategy will decode to a default color type |
| 57 | * if this color type is unsupported. |
| 58 | * @param requireUnpremul If the image is not opaque, we will use this to determine the |
| 59 | * alpha type to use. |
| 60 | * |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 61 | */ |
msarett | cb8d719 | 2015-11-11 13:30:43 -0800 | [diff] [blame] | 62 | virtual bool decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator, |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 63 | const SkIRect& desiredSubset, int sampleSize, |
| 64 | SkColorType colorType, bool requireUnpremul) = 0; |
msarett | 04965c6 | 2015-10-12 10:24:38 -0700 | [diff] [blame] | 65 | /* |
| 66 | * @param Requested destination color type |
| 67 | * @return true if we support the requested color type and false otherwise |
| 68 | */ |
| 69 | virtual bool conversionSupported(SkColorType colorType) = 0; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 70 | |
msarett | 3f65e93 | 2015-10-27 13:12:59 -0700 | [diff] [blame] | 71 | virtual SkEncodedFormat getEncodedFormat() = 0; |
| 72 | |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 73 | int width() const { return fWidth; } |
| 74 | int height() const { return fHeight; } |
| 75 | |
msarett | 5cb4885 | 2015-11-06 08:56:32 -0800 | [diff] [blame] | 76 | virtual ~SkBitmapRegionDecoder() {} |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 77 | |
| 78 | protected: |
| 79 | |
msarett | 5cb4885 | 2015-11-06 08:56:32 -0800 | [diff] [blame] | 80 | SkBitmapRegionDecoder(int width, int height) |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 81 | : fWidth(width) |
| 82 | , fHeight(height) |
| 83 | {} |
| 84 | |
| 85 | private: |
| 86 | const int fWidth; |
| 87 | const int fHeight; |
| 88 | }; |
| 89 | |
| 90 | #endif |