00001 /* 00002 * Copyright (C) 2008 The Android Open Source Project 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef SkImageRef_DEFINED 00018 #define SkImageRef_DEFINED 00019 00020 #include "SkPixelRef.h" 00021 #include "SkBitmap.h" 00022 #include "SkImageDecoder.h" 00023 #include "SkString.h" 00024 00025 class SkImageRefPool; 00026 class SkStream; 00027 00028 // define this to enable dumping whenever we add/remove/purge an imageref 00029 //#define DUMP_IMAGEREF_LIFECYCLE 00030 00031 class SkImageRef : public SkPixelRef { 00032 public: 00044 SkImageRef(SkStream*, SkBitmap::Config config, int sampleSize = 1); 00045 virtual ~SkImageRef(); 00046 00049 void setDitherImage(bool dither) { fDoDither = dither; } 00050 00058 bool getInfo(SkBitmap* bm); 00059 00060 SkImageDecoderFactory* getDecoderFactory() const { return fFactory; } 00061 // returns the factory parameter 00062 SkImageDecoderFactory* setDecoderFactory(SkImageDecoderFactory*); 00063 00064 // overrides 00065 virtual void flatten(SkFlattenableWriteBuffer&) const; 00066 00067 protected: 00071 virtual bool onDecode(SkImageDecoder* codec, SkStream*, SkBitmap*, 00072 SkBitmap::Config, SkImageDecoder::Mode); 00073 00074 /* Overrides from SkPixelRef 00075 When these are called, we will have already acquired the mutex! 00076 */ 00077 00078 virtual void* onLockPixels(SkColorTable**); 00079 // override this in your subclass to clean up when we're unlocking pixels 00080 virtual void onUnlockPixels(); 00081 00082 SkImageRef(SkFlattenableReadBuffer&); 00083 00084 SkBitmap fBitmap; 00085 00086 private: 00087 SkStream* setStream(SkStream*); 00088 // called with mutex already held. returns true if the bitmap is in the 00089 // requested state (or further, i.e. has pixels) 00090 bool prepareBitmap(SkImageDecoder::Mode); 00091 00092 SkImageDecoderFactory* fFactory; // may be null 00093 SkStream* fStream; 00094 SkBitmap::Config fConfig; 00095 int fSampleSize; 00096 bool fDoDither; 00097 bool fErrorInDecoding; 00098 00099 friend class SkImageRefPool; 00100 00101 SkImageRef* fPrev, *fNext; 00102 size_t ramUsed() const; 00103 00104 typedef SkPixelRef INHERITED; 00105 }; 00106 00107 #endif