blob: d009b27f3a3dd5cbc160a1a23906557878248de0 [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#include "SkBitmapRegionCanvas.h"
msarett26ad17b2015-10-22 07:29:19 -07009#include "SkBitmapRegionCodec.h"
msaretta5783ae2015-09-08 15:35:32 -070010#include "SkBitmapRegionDecoderInterface.h"
11#include "SkBitmapRegionSampler.h"
msarett26ad17b2015-10-22 07:29:19 -070012#include "SkAndroidCodec.h"
scroggo46c57472015-09-30 08:57:13 -070013#include "SkCodec.h"
msarett04965c62015-10-12 10:24:38 -070014#include "SkCodecPriv.h"
msaretta5783ae2015-09-08 15:35:32 -070015#include "SkImageDecoder.h"
16
17SkBitmapRegionDecoderInterface* SkBitmapRegionDecoderInterface::CreateBitmapRegionDecoder(
msarett26ad17b2015-10-22 07:29:19 -070018 SkData* data, Strategy strategy) {
msarett3f65e932015-10-27 13:12:59 -070019 return SkBitmapRegionDecoderInterface::CreateBitmapRegionDecoder(new SkMemoryStream(data),
20 strategy);
21}
22
23SkBitmapRegionDecoderInterface* SkBitmapRegionDecoderInterface::CreateBitmapRegionDecoder(
24 SkStreamRewindable* stream, Strategy strategy) {
25 SkAutoTDelete<SkStreamRewindable> streamDeleter(stream);
msaretta5783ae2015-09-08 15:35:32 -070026 switch (strategy) {
27 case kOriginal_Strategy: {
msarett3f65e932015-10-27 13:12:59 -070028 SkImageDecoder* decoder = SkImageDecoder::Factory(streamDeleter);
msaretta5783ae2015-09-08 15:35:32 -070029 int width, height;
30 if (nullptr == decoder) {
msarett04965c62015-10-12 10:24:38 -070031 SkCodecPrintf("Error: Could not create image decoder.\n");
msaretta5783ae2015-09-08 15:35:32 -070032 return nullptr;
33 }
msarett3f65e932015-10-27 13:12:59 -070034 if (!decoder->buildTileIndex(streamDeleter.detach(), &width, &height)) {
msarett04965c62015-10-12 10:24:38 -070035 SkCodecPrintf("Error: Could not build tile index.\n");
msaretta5783ae2015-09-08 15:35:32 -070036 delete decoder;
37 return nullptr;
38 }
39 return new SkBitmapRegionSampler(decoder, width, height);
40 }
41 case kCanvas_Strategy: {
msarett3f65e932015-10-27 13:12:59 -070042 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(streamDeleter.detach()));
msarette6dd0042015-10-09 11:07:34 -070043 if (nullptr == codec) {
msarett04965c62015-10-12 10:24:38 -070044 SkCodecPrintf("Error: Failed to create decoder.\n");
msaretta5783ae2015-09-08 15:35:32 -070045 return nullptr;
46 }
msarettc3779922015-10-27 13:28:25 -070047
48 if (SkEncodedFormat::kWEBP_SkEncodedFormat == codec->getEncodedFormat()) {
49 // FIXME: Support webp using a special case. Webp does not support
50 // scanline decoding.
51 return nullptr;
52 }
53
msarette6dd0042015-10-09 11:07:34 -070054 switch (codec->getScanlineOrder()) {
scroggo46c57472015-09-30 08:57:13 -070055 case SkCodec::kTopDown_SkScanlineOrder:
56 case SkCodec::kNone_SkScanlineOrder:
msaretta5783ae2015-09-08 15:35:32 -070057 break;
58 default:
msarett04965c62015-10-12 10:24:38 -070059 SkCodecPrintf("Error: Scanline ordering not supported.\n");
msaretta5783ae2015-09-08 15:35:32 -070060 return nullptr;
61 }
msarette6dd0042015-10-09 11:07:34 -070062 return new SkBitmapRegionCanvas(codec.detach());
msaretta5783ae2015-09-08 15:35:32 -070063 }
msarett26ad17b2015-10-22 07:29:19 -070064 case kAndroidCodec_Strategy: {
msarett3f65e932015-10-27 13:12:59 -070065 SkAutoTDelete<SkAndroidCodec> codec =
66 SkAndroidCodec::NewFromStream(streamDeleter.detach());
msarett26ad17b2015-10-22 07:29:19 -070067 if (NULL == codec) {
68 SkCodecPrintf("Error: Failed to create codec.\n");
69 return NULL;
70 }
71 return new SkBitmapRegionCodec(codec.detach());
72 }
msaretta5783ae2015-09-08 15:35:32 -070073 default:
74 SkASSERT(false);
75 return nullptr;
76 }
77}