Home | History | Annotate | Download | only in images
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef SkImageRef_DEFINED
     18 #define SkImageRef_DEFINED
     19 
     20 #include "SkPixelRef.h"
     21 #include "SkBitmap.h"
     22 #include "SkImageDecoder.h"
     23 #include "SkString.h"
     24 
     25 class SkImageRefPool;
     26 class SkStream;
     27 
     28 // define this to enable dumping whenever we add/remove/purge an imageref
     29 //#define DUMP_IMAGEREF_LIFECYCLE
     30 
     31 class SkImageRef : public SkPixelRef {
     32 public:
     33     /** Create a new imageref from a stream. NOTE: the stream is not copied, but
     34         since it may be accessed from another thread, the caller must ensure
     35         that this imageref is the only owner of the stream. i.e. - sole
     36         ownership of the stream object is transferred to this imageref object.
     37 
     38         @param stream The stream containing the encoded image data. This may be
     39                       retained (by calling ref()), so the caller should not
     40                       explicitly delete it.
     41         @param config The preferred config of the decoded bitmap.
     42         @param sampleSize Requested sampleSize for decoding. Defaults to 1.
     43     */
     44     SkImageRef(SkStream*, SkBitmap::Config config, int sampleSize = 1);
     45     virtual ~SkImageRef();
     46 
     47     /** this value is passed onto the decoder. Default is true
     48      */
     49     void setDitherImage(bool dither) { fDoDither = dither; }
     50 
     51     /** Return true if the image can be decoded. If so, and bitmap is non-null,
     52         call its setConfig() with the corresponding values, but explicitly will
     53         not set its pixels or colortable. Use SkPixelRef::lockPixels() for that.
     54 
     55         If there has been an error decoding the bitmap, this will return false
     56         and ignore the bitmap parameter.
     57     */
     58     bool getInfo(SkBitmap* bm);
     59 
     60     /** Return true if the image can be decoded and is opaque. Calling this
     61         method will decode and set the pixels in the specified bitmap and
     62         sets the isOpaque flag.
     63      */
     64     bool isOpaque(SkBitmap* bm);
     65 
     66     SkImageDecoderFactory* getDecoderFactory() const { return fFactory; }
     67     // returns the factory parameter
     68     SkImageDecoderFactory* setDecoderFactory(SkImageDecoderFactory*);
     69 
     70     // overrides
     71     virtual void flatten(SkFlattenableWriteBuffer&) const;
     72 
     73 protected:
     74     /** Override if you want to install a custom allocator.
     75         When this is called we will have already acquired the mutex!
     76     */
     77     virtual bool onDecode(SkImageDecoder* codec, SkStream*, SkBitmap*,
     78                           SkBitmap::Config, SkImageDecoder::Mode);
     79 
     80     /*  Overrides from SkPixelRef
     81         When these are called, we will have already acquired the mutex!
     82      */
     83 
     84     virtual void* onLockPixels(SkColorTable**);
     85     // override this in your subclass to clean up when we're unlocking pixels
     86     virtual void onUnlockPixels();
     87 
     88     SkImageRef(SkFlattenableReadBuffer&);
     89 
     90     SkBitmap fBitmap;
     91 
     92 private:
     93     SkStream* setStream(SkStream*);
     94     // called with mutex already held. returns true if the bitmap is in the
     95     // requested state (or further, i.e. has pixels)
     96     bool prepareBitmap(SkImageDecoder::Mode);
     97 
     98     SkImageDecoderFactory*  fFactory;    // may be null
     99     SkStream*               fStream;
    100     SkBitmap::Config        fConfig;
    101     int                     fSampleSize;
    102     bool                    fDoDither;
    103     bool                    fErrorInDecoding;
    104 
    105     friend class SkImageRefPool;
    106 
    107     SkImageRef*  fPrev, *fNext;
    108     size_t ramUsed() const;
    109 
    110     typedef SkPixelRef INHERITED;
    111 };
    112 
    113 #endif
    114