epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkImageRef.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkFlattenable.h" |
| 11 | #include "SkImageDecoder.h" |
| 12 | #include "SkStream.h" |
| 13 | #include "SkTemplates.h" |
| 14 | #include "SkThread.h" |
| 15 | |
reed@android.com | c6ddc11 | 2009-11-10 15:54:55 +0000 | [diff] [blame] | 16 | //#define DUMP_IMAGEREF_LIFECYCLE |
| 17 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | // can't be static, as SkImageRef_Pool needs to see it |
digit@google.com | 1771cbf | 2012-01-26 21:26:40 +0000 | [diff] [blame] | 19 | SK_DECLARE_GLOBAL_MUTEX(gImageRefMutex); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | |
| 21 | /////////////////////////////////////////////////////////////////////////////// |
| 22 | |
| 23 | SkImageRef::SkImageRef(SkStream* stream, SkBitmap::Config config, |
| 24 | int sampleSize) |
| 25 | : SkPixelRef(&gImageRefMutex), fErrorInDecoding(false) { |
| 26 | SkASSERT(stream); |
reed@android.com | 1337a7b | 2009-03-16 13:56:10 +0000 | [diff] [blame] | 27 | stream->ref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | fStream = stream; |
| 29 | fConfig = config; |
| 30 | fSampleSize = sampleSize; |
reed@android.com | bb9aea9 | 2009-09-24 17:21:05 +0000 | [diff] [blame] | 31 | fDoDither = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | fPrev = fNext = NULL; |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 33 | fFactory = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | |
| 35 | #ifdef DUMP_IMAGEREF_LIFECYCLE |
| 36 | SkDebugf("add ImageRef %p [%d] data=%d\n", |
| 37 | this, config, (int)stream->getLength()); |
| 38 | #endif |
| 39 | } |
| 40 | |
| 41 | SkImageRef::~SkImageRef() { |
| 42 | SkASSERT(&gImageRefMutex == this->mutex()); |
| 43 | |
| 44 | #ifdef DUMP_IMAGEREF_LIFECYCLE |
| 45 | SkDebugf("delete ImageRef %p [%d] data=%d\n", |
| 46 | this, fConfig, (int)fStream->getLength()); |
| 47 | #endif |
| 48 | |
reed@android.com | 1337a7b | 2009-03-16 13:56:10 +0000 | [diff] [blame] | 49 | fStream->unref(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 50 | SkSafeUnref(fFactory); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | bool SkImageRef::getInfo(SkBitmap* bitmap) { |
| 54 | SkAutoMutexAcquire ac(gImageRefMutex); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 55 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | if (!this->prepareBitmap(SkImageDecoder::kDecodeBounds_Mode)) { |
| 57 | return false; |
| 58 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 59 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | SkASSERT(SkBitmap::kNo_Config != fBitmap.config()); |
| 61 | if (bitmap) { |
| 62 | bitmap->setConfig(fBitmap.config(), fBitmap.width(), fBitmap.height()); |
| 63 | } |
| 64 | return true; |
| 65 | } |
| 66 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 67 | bool SkImageRef::isOpaque(SkBitmap* bitmap) { |
| 68 | if (bitmap && bitmap->pixelRef() == this) { |
| 69 | bitmap->lockPixels(); |
| 70 | bitmap->setIsOpaque(fBitmap.isOpaque()); |
| 71 | bitmap->unlockPixels(); |
| 72 | return true; |
| 73 | } |
| 74 | return false; |
| 75 | } |
| 76 | |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 77 | SkImageDecoderFactory* SkImageRef::setDecoderFactory( |
| 78 | SkImageDecoderFactory* fact) { |
| 79 | SkRefCnt_SafeAssign(fFactory, fact); |
| 80 | return fact; |
| 81 | } |
| 82 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | /////////////////////////////////////////////////////////////////////////////// |
| 84 | |
| 85 | bool SkImageRef::onDecode(SkImageDecoder* codec, SkStream* stream, |
| 86 | SkBitmap* bitmap, SkBitmap::Config config, |
| 87 | SkImageDecoder::Mode mode) { |
| 88 | return codec->decode(stream, bitmap, config, mode); |
| 89 | } |
| 90 | |
| 91 | bool SkImageRef::prepareBitmap(SkImageDecoder::Mode mode) { |
| 92 | SkASSERT(&gImageRefMutex == this->mutex()); |
| 93 | |
| 94 | if (fErrorInDecoding) { |
| 95 | return false; |
| 96 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 97 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | /* As soon as we really know our config, we record it, so that on |
| 99 | subsequent calls to the codec, we are sure we will always get the same |
| 100 | result. |
| 101 | */ |
| 102 | if (SkBitmap::kNo_Config != fBitmap.config()) { |
| 103 | fConfig = fBitmap.config(); |
| 104 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 105 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | if (NULL != fBitmap.getPixels() || |
| 107 | (SkBitmap::kNo_Config != fBitmap.config() && |
| 108 | SkImageDecoder::kDecodeBounds_Mode == mode)) { |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | SkASSERT(fBitmap.getPixels() == NULL); |
| 113 | |
| 114 | fStream->rewind(); |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 115 | |
| 116 | SkImageDecoder* codec; |
| 117 | if (fFactory) { |
| 118 | codec = fFactory->newDecoder(fStream); |
| 119 | } else { |
| 120 | codec = SkImageDecoder::Factory(fStream); |
| 121 | } |
| 122 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | if (codec) { |
| 124 | SkAutoTDelete<SkImageDecoder> ad(codec); |
| 125 | |
| 126 | codec->setSampleSize(fSampleSize); |
reed@android.com | bb9aea9 | 2009-09-24 17:21:05 +0000 | [diff] [blame] | 127 | codec->setDitherImage(fDoDither); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | if (this->onDecode(codec, fStream, &fBitmap, fConfig, mode)) { |
| 129 | return true; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | #ifdef DUMP_IMAGEREF_LIFECYCLE |
| 134 | if (NULL == codec) { |
| 135 | SkDebugf("--- ImageRef: <%s> failed to find codec\n", this->getURI()); |
| 136 | } else { |
| 137 | SkDebugf("--- ImageRef: <%s> failed in codec for %d mode\n", |
| 138 | this->getURI(), mode); |
| 139 | } |
| 140 | #endif |
| 141 | fErrorInDecoding = true; |
| 142 | fBitmap.reset(); |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | void* SkImageRef::onLockPixels(SkColorTable** ct) { |
| 147 | SkASSERT(&gImageRefMutex == this->mutex()); |
| 148 | |
| 149 | if (NULL == fBitmap.getPixels()) { |
| 150 | (void)this->prepareBitmap(SkImageDecoder::kDecodePixels_Mode); |
| 151 | } |
| 152 | |
| 153 | if (ct) { |
| 154 | *ct = fBitmap.getColorTable(); |
| 155 | } |
| 156 | return fBitmap.getPixels(); |
| 157 | } |
| 158 | |
| 159 | void SkImageRef::onUnlockPixels() { |
| 160 | // we're already have the mutex locked |
| 161 | SkASSERT(&gImageRefMutex == this->mutex()); |
| 162 | } |
| 163 | |
| 164 | size_t SkImageRef::ramUsed() const { |
| 165 | size_t size = 0; |
| 166 | |
| 167 | if (fBitmap.getPixels()) { |
| 168 | size = fBitmap.getSize(); |
| 169 | if (fBitmap.getColorTable()) { |
| 170 | size += fBitmap.getColorTable()->count() * sizeof(SkPMColor); |
| 171 | } |
| 172 | } |
| 173 | return size; |
| 174 | } |
| 175 | |
| 176 | /////////////////////////////////////////////////////////////////////////////// |
| 177 | |
| 178 | SkImageRef::SkImageRef(SkFlattenableReadBuffer& buffer) |
| 179 | : INHERITED(buffer, &gImageRefMutex), fErrorInDecoding(false) { |
| 180 | fConfig = (SkBitmap::Config)buffer.readU8(); |
| 181 | fSampleSize = buffer.readU8(); |
reed@android.com | c6ddc11 | 2009-11-10 15:54:55 +0000 | [diff] [blame] | 182 | fDoDither = buffer.readBool(); |
| 183 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | size_t length = buffer.readU32(); |
| 185 | fStream = SkNEW_ARGS(SkMemoryStream, (length)); |
| 186 | buffer.read((void*)fStream->getMemoryBase(), length); |
| 187 | |
| 188 | fPrev = fNext = NULL; |
reed@android.com | 797d51a | 2009-03-20 12:08:57 +0000 | [diff] [blame] | 189 | fFactory = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 190 | } |
| 191 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 192 | void SkImageRef::flatten(SkFlattenableWriteBuffer& buffer) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 193 | this->INHERITED::flatten(buffer); |
| 194 | |
| 195 | buffer.write8(fConfig); |
| 196 | buffer.write8(fSampleSize); |
reed@android.com | c6ddc11 | 2009-11-10 15:54:55 +0000 | [diff] [blame] | 197 | buffer.writeBool(fDoDither); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 198 | size_t length = fStream->getLength(); |
| 199 | buffer.write32(length); |
| 200 | fStream->rewind(); |
| 201 | buffer.readFromStream(fStream, length); |
| 202 | } |
| 203 | |