OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 The Android Open Source Project | 2 * Copyright 2013 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkErrorInternals.h" | 8 #include "SkErrorInternals.h" |
9 #include "SkImageDecoder.h" | 9 #include "SkImageDecoder.h" |
10 #include "SkStream.h" | 10 #include "SkStream.h" |
11 #include "SkTRegistry.h" | 11 #include "SkTRegistry.h" |
12 | 12 |
13 // This file is used for registration of SkImageDecoders. It also holds a functi
on | 13 // This file is used for registration of SkImageDecoders. It also holds a functi
on |
14 // for checking all the the registered SkImageDecoders for one that matches an | 14 // for checking all the the registered SkImageDecoders for one that matches an |
15 // input SkStream. | 15 // input SkStreamRewindable. |
16 | 16 |
17 typedef SkTRegistry<SkImageDecoder*, SkStream*> DecodeReg; | 17 typedef SkTRegistry<SkImageDecoder*, SkStreamRewindable*> DecodeReg; |
18 | 18 |
19 // N.B. You can't use "DecodeReg::gHead here" due to complex C++ | 19 // N.B. You can't use "DecodeReg::gHead here" due to complex C++ |
20 // corner cases. | 20 // corner cases. |
21 template DecodeReg* SkTRegistry<SkImageDecoder*, SkStream*>::gHead; | 21 template DecodeReg* SkTRegistry<SkImageDecoder*, SkStreamRewindable*>::gHead; |
22 | 22 |
23 SkImageDecoder* image_decoder_from_stream(SkStream*); | 23 SkImageDecoder* image_decoder_from_stream(SkStreamRewindable*); |
24 | 24 |
25 SkImageDecoder* image_decoder_from_stream(SkStream* stream) { | 25 SkImageDecoder* image_decoder_from_stream(SkStreamRewindable* stream) { |
26 SkImageDecoder* codec = NULL; | 26 SkImageDecoder* codec = NULL; |
27 const DecodeReg* curr = DecodeReg::Head(); | 27 const DecodeReg* curr = DecodeReg::Head(); |
28 while (curr) { | 28 while (curr) { |
29 codec = curr->factory()(stream); | 29 codec = curr->factory()(stream); |
30 // we rewind here, because we promise later when we call "decode", that | 30 // we rewind here, because we promise later when we call "decode", that |
31 // the stream will be at its beginning. | 31 // the stream will be at its beginning. |
32 bool rewindSuceeded = stream->rewind(); | 32 bool rewindSuceeded = stream->rewind(); |
33 | 33 |
34 // our image decoder's require that rewind is supported so we fail early | 34 // our image decoder's require that rewind is supported so we fail early |
35 // if we are given a stream that does not support rewinding. | 35 // if we are given a stream that does not support rewinding. |
36 if (!rewindSuceeded) { | 36 if (!rewindSuceeded) { |
37 SkDEBUGF(("Unable to rewind the image stream.")); | 37 SkDEBUGF(("Unable to rewind the image stream.")); |
38 SkDELETE(codec); | 38 SkDELETE(codec); |
39 return NULL; | 39 return NULL; |
40 } | 40 } |
41 | 41 |
42 if (codec) { | 42 if (codec) { |
43 return codec; | 43 return codec; |
44 } | 44 } |
45 curr = curr->next(); | 45 curr = curr->next(); |
46 } | 46 } |
47 return NULL; | 47 return NULL; |
48 } | 48 } |
49 | 49 |
50 typedef SkTRegistry<SkImageDecoder::Format, SkStream*> FormatReg; | 50 typedef SkTRegistry<SkImageDecoder::Format, SkStreamRewindable*> FormatReg; |
51 | 51 |
52 template FormatReg* SkTRegistry<SkImageDecoder::Format, SkStream*>::gHead; | 52 template FormatReg* SkTRegistry<SkImageDecoder::Format, SkStreamRewindable*>::gH
ead; |
53 | 53 |
54 SkImageDecoder::Format SkImageDecoder::GetStreamFormat(SkStream* stream) { | 54 SkImageDecoder::Format SkImageDecoder::GetStreamFormat(SkStreamRewindable* strea
m) { |
55 const FormatReg* curr = FormatReg::Head(); | 55 const FormatReg* curr = FormatReg::Head(); |
56 while (curr != NULL) { | 56 while (curr != NULL) { |
57 Format format = curr->factory()(stream); | 57 Format format = curr->factory()(stream); |
58 if (!stream->rewind()) { | 58 if (!stream->rewind()) { |
59 SkErrorInternals::SetError(kInvalidOperation_SkError, | 59 SkErrorInternals::SetError(kInvalidOperation_SkError, |
60 "Unable to rewind the image stream\n"); | 60 "Unable to rewind the image stream\n"); |
61 return kUnknown_Format; | 61 return kUnknown_Format; |
62 } | 62 } |
63 if (format != kUnknown_Format) { | 63 if (format != kUnknown_Format) { |
64 return format; | 64 return format; |
65 } | 65 } |
66 curr = curr->next(); | 66 curr = curr->next(); |
67 } | 67 } |
68 return kUnknown_Format; | 68 return kUnknown_Format; |
69 } | 69 } |
OLD | NEW |