reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* libs/graphics/ports/SkImageDecoder_Factory.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include "SkImageDecoder.h" |
| 19 | #include "SkMovie.h" |
| 20 | #include "SkStream.h" |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 21 | #include "SkTRegistry.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 23 | typedef SkTRegistry<SkImageDecoder*, SkStream*> DecodeReg; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | |
agl@chromium.org | 4f9cdcc | 2010-06-10 21:32:00 +0000 | [diff] [blame^] | 25 | // N.B. You can't use "DecodeReg::gHead here" due to complex C++ |
| 26 | // corner cases. |
| 27 | template DecodeReg* SkTRegistry<SkImageDecoder*, SkStream*>::gHead; |
reed@android.com | 3c2b7e8 | 2009-01-27 01:43:36 +0000 | [diff] [blame] | 28 | |
reed@android.com | dfee579 | 2010-04-15 14:24:50 +0000 | [diff] [blame] | 29 | #ifdef SK_ENABLE_LIBPNG |
| 30 | extern SkImageDecoder* sk_libpng_dfactory(SkStream*); |
| 31 | #endif |
| 32 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | SkImageDecoder* SkImageDecoder::Factory(SkStream* stream) { |
reed@android.com | dfee579 | 2010-04-15 14:24:50 +0000 | [diff] [blame] | 34 | SkImageDecoder* codec = NULL; |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 35 | const DecodeReg* curr = DecodeReg::Head(); |
| 36 | while (curr) { |
reed@android.com | dfee579 | 2010-04-15 14:24:50 +0000 | [diff] [blame] | 37 | codec = curr->factory()(stream); |
reed@android.com | a16cb97 | 2009-06-18 20:26:58 +0000 | [diff] [blame] | 38 | // we rewind here, because we promise later when we call "decode", that |
| 39 | // the stream will be at its beginning. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | stream->rewind(); |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 41 | if (codec) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | return codec; |
| 43 | } |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 44 | curr = curr->next(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | } |
reed@android.com | dfee579 | 2010-04-15 14:24:50 +0000 | [diff] [blame] | 46 | #ifdef SK_ENABLE_LIBPNG |
| 47 | codec = sk_libpng_dfactory(stream); |
| 48 | stream->rewind(); |
| 49 | if (codec) { |
| 50 | return codec; |
| 51 | } |
| 52 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | return NULL; |
| 54 | } |
| 55 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | ///////////////////////////////////////////////////////////////////////// |
| 57 | |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 58 | typedef SkTRegistry<SkMovie*, SkStream*> MovieReg; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | |
| 60 | SkMovie* SkMovie::DecodeStream(SkStream* stream) { |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 61 | const MovieReg* curr = MovieReg::Head(); |
| 62 | while (curr) { |
| 63 | SkMovie* movie = curr->factory()(stream); |
| 64 | if (movie) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | return movie; |
| 66 | } |
reed@android.com | a16cb97 | 2009-06-18 20:26:58 +0000 | [diff] [blame] | 67 | // we must rewind only if we got NULL, since we gave the stream to the |
| 68 | // movie, who may have already started reading from it |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | stream->rewind(); |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 70 | curr = curr->next(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | } |
| 72 | return NULL; |
| 73 | } |
| 74 | |