blob: e7c71e52c9ce91983a9187c8bcaa772b1eebdd84 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/* 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.com00bf85a2009-01-22 13:04:56 +000021#include "SkTRegistry.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022
reed@android.com00bf85a2009-01-22 13:04:56 +000023typedef SkTRegistry<SkImageDecoder*, SkStream*> DecodeReg;
reed@android.com8a1c16f2008-12-17 15:59:43 +000024
reed@android.com3c2b7e82009-01-27 01:43:36 +000025template DecodeReg* DecodeReg::gHead;
26
reed@android.comdfee5792010-04-15 14:24:50 +000027#ifdef SK_ENABLE_LIBPNG
28 extern SkImageDecoder* sk_libpng_dfactory(SkStream*);
29#endif
30
reed@android.com8a1c16f2008-12-17 15:59:43 +000031SkImageDecoder* SkImageDecoder::Factory(SkStream* stream) {
reed@android.comdfee5792010-04-15 14:24:50 +000032 SkImageDecoder* codec = NULL;
reed@android.com00bf85a2009-01-22 13:04:56 +000033 const DecodeReg* curr = DecodeReg::Head();
34 while (curr) {
reed@android.comdfee5792010-04-15 14:24:50 +000035 codec = curr->factory()(stream);
reed@android.coma16cb972009-06-18 20:26:58 +000036 // we rewind here, because we promise later when we call "decode", that
37 // the stream will be at its beginning.
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 stream->rewind();
reed@android.com00bf85a2009-01-22 13:04:56 +000039 if (codec) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 return codec;
41 }
reed@android.com00bf85a2009-01-22 13:04:56 +000042 curr = curr->next();
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 }
reed@android.comdfee5792010-04-15 14:24:50 +000044#ifdef SK_ENABLE_LIBPNG
45 codec = sk_libpng_dfactory(stream);
46 stream->rewind();
47 if (codec) {
48 return codec;
49 }
50#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 return NULL;
52}
53
reed@android.com8a1c16f2008-12-17 15:59:43 +000054/////////////////////////////////////////////////////////////////////////
55
reed@android.com00bf85a2009-01-22 13:04:56 +000056typedef SkTRegistry<SkMovie*, SkStream*> MovieReg;
reed@android.com8a1c16f2008-12-17 15:59:43 +000057
58SkMovie* SkMovie::DecodeStream(SkStream* stream) {
reed@android.com00bf85a2009-01-22 13:04:56 +000059 const MovieReg* curr = MovieReg::Head();
60 while (curr) {
61 SkMovie* movie = curr->factory()(stream);
62 if (movie) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 return movie;
64 }
reed@android.coma16cb972009-06-18 20:26:58 +000065 // we must rewind only if we got NULL, since we gave the stream to the
66 // movie, who may have already started reading from it
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 stream->rewind();
reed@android.com00bf85a2009-01-22 13:04:56 +000068 curr = curr->next();
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 }
70 return NULL;
71}
72