Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Side by Side Diff: src/codec/SkAndroidCodec.cpp

Issue 1406223002: Create an SkAndroidCodec API separate from SkCodec (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 #include "SkAndroidCodec.h"
9 #include "SkScaledCodec.h"
10 #include "SkCodec.h"
11 #include "SkCodecPriv.h"
12 #include "SkWebpAdapterCodec.h"
13
14 static bool is_valid_sample_size(int sampleSize) {
15 // FIXME: As Leon has mentioned elsewhere, surely there is also a maximum sa mpleSize?
scroggo 2015/10/16 21:13:55 I've seen some really high ones. We just need to m
16 return sampleSize > 0;
17 }
18
19 SkAndroidCodec::SkAndroidCodec(const SkImageInfo& info)
20 : fInfo(info)
21 {}
22
23 SkAndroidCodec* SkAndroidCodec::NewFromStream(SkStream* stream) {
24 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream));
25 if (nullptr == codec) {
26 return nullptr;
27 }
28
29 switch (codec->getEncodedFormat()) {
30 case kWEBP_SkEncodedFormat:
31 return new SkWebpAdapterCodec((SkWebpCodec*) codec.detach());
32 case kPNG_SkEncodedFormat:
33 case kJPEG_SkEncodedFormat:
34 return new SkSampledCodec(codec.detach());
35 default:
36 // FIXME: SkSampledCodec is temporarily disabled for other formats
37 // while focusing on the formats that are supported by
38 // BitmapRegionDecoder.
39 return nullptr;
40 }
41 }
42
43 SkAndroidCodec* SkAndroidCodec::NewFromData(SkData* data) {
44 if (!data) {
45 return nullptr;
46 }
47
48 return NewFromStream(new SkMemoryStream(data));
49 }
50
51 SkISize SkAndroidCodec::getSampledDimensions(int sampleSize) const {
52 if (!is_valid_sample_size(sampleSize)) {
scroggo 2015/10/16 21:13:55 Should we instead treat 0 as 1, as Android does?
msarett 2015/10/19 16:06:10 I'm not sure. It's annoying to catch...I've added
scroggo 2015/10/19 20:03:20 Haha, agreed, it is annoying to catch it in three
msarett 2015/10/19 21:39:21 Let's plan to handle it upstack for now.
53 return SkISize::Make(0, 0);
54 }
55
56 return this->onGetSampledDimensions(sampleSize);
57 }
58
59 bool SkAndroidCodec::getSubset(SkIRect* desiredSubset) const {
60 if (!desiredSubset || !is_valid_subset(*desiredSubset, fInfo.dimensions())) {
61 return false;
62 }
63
64 return this->onGetSubset(desiredSubset);
65 }
66
67 SkISize SkAndroidCodec::getSampledSubsetDimensions(int sampleSize, const SkIRect & subset) const {
68 if (!is_valid_sample_size(sampleSize)) {
69 return SkISize::Make(0, 0);
70 }
71
72 // We require that the input subset is a subset that is supported by SkAndro idCodec.
73 // We test this by calling getSubset() and verifying that no modifications a re made
74 // to the subset.
75 SkIRect copySubset = subset;
76 if (!this->getSubset(&copySubset) || copySubset != subset) {
77 return SkISize::Make(0, 0);
78 }
79
80 return this->onGetSampledSubsetDimensions(sampleSize, subset);
81 }
82
83 SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels,
84 size_t rowBytes, AndroidOptions* options) {
85 if (!pixels) {
msarett 2015/10/16 18:42:16 There are some error cases that we don't check her
scroggo 2015/10/16 21:13:55 Seems fine to me.
86 return SkCodec::kInvalidParameters;
87 }
88 if (rowBytes < info.minRowBytes()) {
89 return SkCodec::kInvalidParameters;
90 }
91
92 AndroidOptions defaultOptions;
93 if (!options) {
94 options = &defaultOptions;
95 }
96
97 SkISize supportedSize;
98 if (!options->fSubset) {
99 supportedSize = this->getSampledDimensions(options->fSampleSize);
scroggo 2015/10/16 21:13:55 I hope these calls are cheap - it seems like a cli
msarett 2015/10/19 16:06:10 They are not trivial, but I *think* they are cheap
100 } else {
101 supportedSize = this->getSampledSubsetDimensions(options->fSampleSize, * options->fSubset);
102 }
103 if (supportedSize != info.dimensions()) {
104 return SkCodec::kInvalidParameters;
105 }
106
107 return this->onGetAndroidPixels(info, pixels, rowBytes, *options);
108 }
109
110 SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels,
111 size_t rowBytes) {
112 return this->getAndroidPixels(info, pixels, rowBytes, nullptr);
113 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698