blob: 575ad9dc01a1c5ab538d339537ce1d4f536ab163 [file] [log] [blame]
msaretta5783ae2015-09-08 15:35:32 -07001/*
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"
msarettcb8d7192015-11-11 13:30:43 -080012#include "SkBRDAllocator.h"
msarett3f65e932015-10-27 13:12:59 -070013#include "SkEncodedFormat.h"
msaretta5783ae2015-09-08 15:35:32 -070014#include "SkStream.h"
15
16/*
17 * This class aims to provide an interface to test multiple implementations of
18 * SkBitmapRegionDecoder.
19 */
msarett5cb48852015-11-06 08:56:32 -080020class SkBitmapRegionDecoder {
msaretta5783ae2015-09-08 15:35:32 -070021public:
22
23 enum Strategy {
msarett26ad17b2015-10-22 07:29:19 -070024 kCanvas_Strategy, // Draw to the canvas, uses SkCodec
msarett26ad17b2015-10-22 07:29:19 -070025 kAndroidCodec_Strategy, // Uses SkAndroidCodec for scaling and subsetting
msaretta5783ae2015-09-08 15:35:32 -070026 };
27
28 /*
msarett26ad17b2015-10-22 07:29:19 -070029 * @param data Refs the data while this object exists, unrefs on destruction
msaretta5783ae2015-09-08 15:35:32 -070030 * @param strategy Strategy used for scaling and subsetting
msarett26ad17b2015-10-22 07:29:19 -070031 * @return Tries to create an SkBitmapRegionDecoder, returns NULL on failure
msaretta5783ae2015-09-08 15:35:32 -070032 */
msarett5cb48852015-11-06 08:56:32 -080033 static SkBitmapRegionDecoder* Create(
msarett26ad17b2015-10-22 07:29:19 -070034 SkData* data, Strategy strategy);
msaretta5783ae2015-09-08 15:35:32 -070035
36 /*
msarett3f65e932015-10-27 13:12:59 -070037 * @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 */
msarett5cb48852015-11-06 08:56:32 -080041 static SkBitmapRegionDecoder* Create(
msarett3f65e932015-10-27 13:12:59 -070042 SkStreamRewindable* stream, Strategy strategy);
43
44 /*
msaretta5783ae2015-09-08 15:35:32 -070045 * Decode a scaled region of the encoded image stream
46 *
msarett35e5d1b2015-10-27 12:50:25 -070047 * @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 *
msaretta5783ae2015-09-08 15:35:32 -070061 */
msarettcb8d7192015-11-11 13:30:43 -080062 virtual bool decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator,
msarett35e5d1b2015-10-27 12:50:25 -070063 const SkIRect& desiredSubset, int sampleSize,
64 SkColorType colorType, bool requireUnpremul) = 0;
msarett04965c62015-10-12 10:24:38 -070065 /*
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;
msaretta5783ae2015-09-08 15:35:32 -070070
msarett3f65e932015-10-27 13:12:59 -070071 virtual SkEncodedFormat getEncodedFormat() = 0;
72
msaretta5783ae2015-09-08 15:35:32 -070073 int width() const { return fWidth; }
74 int height() const { return fHeight; }
75
msarett5cb48852015-11-06 08:56:32 -080076 virtual ~SkBitmapRegionDecoder() {}
msaretta5783ae2015-09-08 15:35:32 -070077
78protected:
79
msarett5cb48852015-11-06 08:56:32 -080080 SkBitmapRegionDecoder(int width, int height)
msaretta5783ae2015-09-08 15:35:32 -070081 : fWidth(width)
82 , fHeight(height)
83 {}
84
85private:
86 const int fWidth;
87 const int fHeight;
88};
89
90#endif