blob: dcc4c0e240aef42a1db4f3b3d2d38bfb0bb45bf3 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2008 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkImageRef_DEFINED
11#define SkImageRef_DEFINED
12
13#include "SkPixelRef.h"
14#include "SkBitmap.h"
15#include "SkImageDecoder.h"
16#include "SkString.h"
17
18class SkImageRefPool;
19class SkStream;
20
21// define this to enable dumping whenever we add/remove/purge an imageref
22//#define DUMP_IMAGEREF_LIFECYCLE
23
24class SkImageRef : public SkPixelRef {
25public:
26 /** Create a new imageref from a stream. NOTE: the stream is not copied, but
27 since it may be accessed from another thread, the caller must ensure
28 that this imageref is the only owner of the stream. i.e. - sole
29 ownership of the stream object is transferred to this imageref object.
rmistry@google.comfbfcd562012-08-23 18:09:54 +000030
reed@android.com1337a7b2009-03-16 13:56:10 +000031 @param stream The stream containing the encoded image data. This may be
32 retained (by calling ref()), so the caller should not
33 explicitly delete it.
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 @param config The preferred config of the decoded bitmap.
35 @param sampleSize Requested sampleSize for decoding. Defaults to 1.
36 */
37 SkImageRef(SkStream*, SkBitmap::Config config, int sampleSize = 1);
38 virtual ~SkImageRef();
reed@android.combb9aea92009-09-24 17:21:05 +000039
40 /** this value is passed onto the decoder. Default is true
41 */
42 void setDitherImage(bool dither) { fDoDither = dither; }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000043
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 /** Return true if the image can be decoded. If so, and bitmap is non-null,
45 call its setConfig() with the corresponding values, but explicitly will
46 not set its pixels or colortable. Use SkPixelRef::lockPixels() for that.
rmistry@google.comfbfcd562012-08-23 18:09:54 +000047
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 If there has been an error decoding the bitmap, this will return false
49 and ignore the bitmap parameter.
50 */
51 bool getInfo(SkBitmap* bm);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000052
djsollen@google.com57f49692011-02-23 20:46:31 +000053 /** Return true if the image can be decoded and is opaque. Calling this
54 method will decode and set the pixels in the specified bitmap and
55 sets the isOpaque flag.
56 */
57 bool isOpaque(SkBitmap* bm);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000058
reed@android.coma14ea0e2009-03-17 17:59:53 +000059 SkImageDecoderFactory* getDecoderFactory() const { return fFactory; }
60 // returns the factory parameter
61 SkImageDecoderFactory* setDecoderFactory(SkImageDecoderFactory*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000062
reed@android.com8a1c16f2008-12-17 15:59:43 +000063protected:
64 /** Override if you want to install a custom allocator.
65 When this is called we will have already acquired the mutex!
66 */
67 virtual bool onDecode(SkImageDecoder* codec, SkStream*, SkBitmap*,
68 SkBitmap::Config, SkImageDecoder::Mode);
69
70 /* Overrides from SkPixelRef
71 When these are called, we will have already acquired the mutex!
72 */
73
74 virtual void* onLockPixels(SkColorTable**);
75 // override this in your subclass to clean up when we're unlocking pixels
76 virtual void onUnlockPixels();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000077
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 SkImageRef(SkFlattenableReadBuffer&);
djsollen@google.com54924242012-03-29 15:18:04 +000079 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000080
81 SkBitmap fBitmap;
82
rmistry@google.comfbfcd562012-08-23 18:09:54 +000083private:
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 SkStream* setStream(SkStream*);
85 // called with mutex already held. returns true if the bitmap is in the
86 // requested state (or further, i.e. has pixels)
87 bool prepareBitmap(SkImageDecoder::Mode);
88
reed@android.coma14ea0e2009-03-17 17:59:53 +000089 SkImageDecoderFactory* fFactory; // may be null
90 SkStream* fStream;
91 SkBitmap::Config fConfig;
92 int fSampleSize;
reed@android.combb9aea92009-09-24 17:21:05 +000093 bool fDoDither;
reed@android.coma14ea0e2009-03-17 17:59:53 +000094 bool fErrorInDecoding;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000095
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 friend class SkImageRefPool;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000097
98 SkImageRef* fPrev, *fNext;
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 size_t ramUsed() const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 typedef SkPixelRef INHERITED;
102};
103
104#endif