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 | |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 8 | #include "SkBitmapRegionCodec.h" |
msarett | 5cb4885 | 2015-11-06 08:56:32 -0800 | [diff] [blame] | 9 | #include "SkBitmapRegionDecoder.h" |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 10 | #include "SkAndroidCodec.h" |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 11 | #include "SkCodec.h" |
msarett | 04965c6 | 2015-10-12 10:24:38 -0700 | [diff] [blame] | 12 | #include "SkCodecPriv.h" |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 13 | |
msarett | 5cb4885 | 2015-11-06 08:56:32 -0800 | [diff] [blame] | 14 | SkBitmapRegionDecoder* SkBitmapRegionDecoder::Create( |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 15 | sk_sp<SkData> data, Strategy strategy) { |
msarett | 5cb4885 | 2015-11-06 08:56:32 -0800 | [diff] [blame] | 16 | return SkBitmapRegionDecoder::Create(new SkMemoryStream(data), |
msarett | 3f65e93 | 2015-10-27 13:12:59 -0700 | [diff] [blame] | 17 | strategy); |
| 18 | } |
| 19 | |
msarett | 5cb4885 | 2015-11-06 08:56:32 -0800 | [diff] [blame] | 20 | SkBitmapRegionDecoder* SkBitmapRegionDecoder::Create( |
msarett | 3f65e93 | 2015-10-27 13:12:59 -0700 | [diff] [blame] | 21 | SkStreamRewindable* stream, Strategy strategy) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 22 | std::unique_ptr<SkStreamRewindable> streamDeleter(stream); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 23 | switch (strategy) { |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 24 | case kAndroidCodec_Strategy: { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 25 | std::unique_ptr<SkAndroidCodec> codec( |
| 26 | SkAndroidCodec::NewFromStream(streamDeleter.release())); |
msarett | 73d882e | 2015-11-16 13:30:20 -0800 | [diff] [blame] | 27 | if (nullptr == codec) { |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 28 | SkCodecPrintf("Error: Failed to create codec.\n"); |
mtklein | 5c05d10 | 2015-12-02 12:32:02 -0800 | [diff] [blame] | 29 | return NULL; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 30 | } |
msarett | 73d882e | 2015-11-16 13:30:20 -0800 | [diff] [blame] | 31 | |
Hal Canary | a2b4bdc | 2016-11-22 14:21:38 -0700 | [diff] [blame] | 32 | SkEncodedFormat format = codec->getEncodedFormat(); |
| 33 | switch (format) { |
| 34 | case SkEncodedFormat::kJPEG_SkEncodedFormat: |
| 35 | case SkEncodedFormat::kPNG_SkEncodedFormat: |
| 36 | case SkEncodedFormat::kWEBP_SkEncodedFormat: |
msarett | 73d882e | 2015-11-16 13:30:20 -0800 | [diff] [blame] | 37 | break; |
| 38 | default: |
| 39 | return nullptr; |
| 40 | } |
| 41 | |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 42 | return new SkBitmapRegionCodec(codec.release()); |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 43 | } |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 44 | default: |
| 45 | SkASSERT(false); |
| 46 | return nullptr; |
| 47 | } |
| 48 | } |