blob: cb5ff2143f6053ae0b2638f869fc3a2afeef6bf0 [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
agl@chromium.org4f9cdcc2010-06-10 21:32:00 +000025// N.B. You can't use "DecodeReg::gHead here" due to complex C++
26// corner cases.
27template DecodeReg* SkTRegistry<SkImageDecoder*, SkStream*>::gHead;
reed@android.com3c2b7e82009-01-27 01:43:36 +000028
reed@android.comdfee5792010-04-15 14:24:50 +000029#ifdef SK_ENABLE_LIBPNG
30 extern SkImageDecoder* sk_libpng_dfactory(SkStream*);
31#endif
32
reed@android.com8a1c16f2008-12-17 15:59:43 +000033SkImageDecoder* SkImageDecoder::Factory(SkStream* stream) {
reed@android.comdfee5792010-04-15 14:24:50 +000034 SkImageDecoder* codec = NULL;
reed@android.com00bf85a2009-01-22 13:04:56 +000035 const DecodeReg* curr = DecodeReg::Head();
36 while (curr) {
reed@android.comdfee5792010-04-15 14:24:50 +000037 codec = curr->factory()(stream);
reed@android.coma16cb972009-06-18 20:26:58 +000038 // we rewind here, because we promise later when we call "decode", that
39 // the stream will be at its beginning.
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 stream->rewind();
reed@android.com00bf85a2009-01-22 13:04:56 +000041 if (codec) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 return codec;
43 }
reed@android.com00bf85a2009-01-22 13:04:56 +000044 curr = curr->next();
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 }
reed@android.comdfee5792010-04-15 14:24:50 +000046#ifdef SK_ENABLE_LIBPNG
47 codec = sk_libpng_dfactory(stream);
48 stream->rewind();
49 if (codec) {
50 return codec;
51 }
52#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 return NULL;
54}
55
reed@android.com8a1c16f2008-12-17 15:59:43 +000056/////////////////////////////////////////////////////////////////////////
57
reed@android.com00bf85a2009-01-22 13:04:56 +000058typedef SkTRegistry<SkMovie*, SkStream*> MovieReg;
reed@android.com8a1c16f2008-12-17 15:59:43 +000059
60SkMovie* SkMovie::DecodeStream(SkStream* stream) {
reed@android.com00bf85a2009-01-22 13:04:56 +000061 const MovieReg* curr = MovieReg::Head();
62 while (curr) {
63 SkMovie* movie = curr->factory()(stream);
64 if (movie) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 return movie;
66 }
reed@android.coma16cb972009-06-18 20:26:58 +000067 // 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.com8a1c16f2008-12-17 15:59:43 +000069 stream->rewind();
reed@android.com00bf85a2009-01-22 13:04:56 +000070 curr = curr->next();
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 }
72 return NULL;
73}
74