epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
reed@google.com | c83a91f | 2013-12-13 13:41:14 +0000 | [diff] [blame] | 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkImageRef_ashmem.h" |
| 9 | #include "SkImageDecoder.h" |
djsollen@google.com | 276a295 | 2012-11-19 19:34:23 +0000 | [diff] [blame] | 10 | #include "SkFlattenableBuffers.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #include "SkThread.h" |
| 12 | |
djsollen@google.com | 276a295 | 2012-11-19 19:34:23 +0000 | [diff] [blame] | 13 | #include "android/ashmem.h" |
| 14 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include <sys/mman.h> |
| 16 | #include <unistd.h> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | |
| 18 | //#define TRACE_ASH_PURGE // just trace purges |
| 19 | |
| 20 | #ifdef DUMP_IMAGEREF_LIFECYCLE |
| 21 | #define DUMP_ASHMEM_LIFECYCLE |
| 22 | #else |
| 23 | // #define DUMP_ASHMEM_LIFECYCLE |
| 24 | #endif |
| 25 | |
| 26 | // ashmem likes lengths on page boundaries |
| 27 | static size_t roundToPageSize(size_t size) { |
| 28 | const size_t mask = getpagesize() - 1; |
| 29 | size_t newsize = (size + mask) & ~mask; |
| 30 | // SkDebugf("---- oldsize %d newsize %d\n", size, newsize); |
| 31 | return newsize; |
| 32 | } |
| 33 | |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 34 | SkImageRef_ashmem::SkImageRef_ashmem(const SkImageInfo& info, |
| 35 | SkStreamRewindable* stream, |
| 36 | int sampleSize) |
| 37 | : SkImageRef(info, stream, sampleSize) |
| 38 | { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | fRec.fFD = -1; |
| 40 | fRec.fAddr = NULL; |
| 41 | fRec.fSize = 0; |
| 42 | fRec.fPinned = false; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 43 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | fCT = NULL; |
| 45 | } |
| 46 | |
| 47 | SkImageRef_ashmem::~SkImageRef_ashmem() { |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 48 | SkSafeUnref(fCT); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | this->closeFD(); |
| 50 | } |
| 51 | |
| 52 | void SkImageRef_ashmem::closeFD() { |
| 53 | if (-1 != fRec.fFD) { |
| 54 | #ifdef DUMP_ASHMEM_LIFECYCLE |
| 55 | SkDebugf("=== ashmem close %d\n", fRec.fFD); |
| 56 | #endif |
| 57 | SkASSERT(fRec.fAddr); |
| 58 | SkASSERT(fRec.fSize); |
| 59 | munmap(fRec.fAddr, fRec.fSize); |
| 60 | close(fRec.fFD); |
| 61 | fRec.fFD = -1; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /////////////////////////////////////////////////////////////////////////////// |
| 66 | |
| 67 | class AshmemAllocator : public SkBitmap::Allocator { |
| 68 | public: |
| 69 | AshmemAllocator(SkAshmemRec* rec, const char name[]) |
| 70 | : fRec(rec), fName(name) {} |
| 71 | |
| 72 | virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) { |
| 73 | const size_t size = roundToPageSize(bm->getSize()); |
| 74 | int fd = fRec->fFD; |
| 75 | void* addr = fRec->fAddr; |
| 76 | |
| 77 | SkASSERT(!fRec->fPinned); |
| 78 | |
| 79 | if (-1 == fd) { |
| 80 | SkASSERT(NULL == addr); |
| 81 | SkASSERT(0 == fRec->fSize); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 82 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | fd = ashmem_create_region(fName, size); |
| 84 | #ifdef DUMP_ASHMEM_LIFECYCLE |
| 85 | SkDebugf("=== ashmem_create_region %s size=%d fd=%d\n", fName, size, fd); |
| 86 | #endif |
| 87 | if (-1 == fd) { |
| 88 | SkDebugf("------- imageref_ashmem create failed <%s> %d\n", |
| 89 | fName, size); |
| 90 | return false; |
| 91 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 92 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | int err = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE); |
| 94 | if (err) { |
djsollen@google.com | 60abb07 | 2012-02-15 18:49:15 +0000 | [diff] [blame] | 95 | SkDebugf("------ ashmem_set_prot_region(%d) failed %d\n", |
| 96 | fd, err); |
| 97 | close(fd); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | return false; |
| 99 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 100 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
| 102 | if (-1 == (long)addr) { |
djsollen@google.com | 60abb07 | 2012-02-15 18:49:15 +0000 | [diff] [blame] | 103 | SkDebugf("---------- mmap failed for imageref_ashmem size=%d\n", |
| 104 | size); |
| 105 | close(fd); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | return false; |
| 107 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 108 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | fRec->fFD = fd; |
| 110 | fRec->fAddr = addr; |
| 111 | fRec->fSize = size; |
| 112 | } else { |
| 113 | SkASSERT(addr); |
| 114 | SkASSERT(size == fRec->fSize); |
| 115 | (void)ashmem_pin_region(fd, 0, 0); |
| 116 | } |
| 117 | |
| 118 | bm->setPixels(addr, ct); |
| 119 | fRec->fPinned = true; |
| 120 | return true; |
| 121 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 122 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | private: |
| 124 | // we just point to our caller's memory, these are not copies |
| 125 | SkAshmemRec* fRec; |
| 126 | const char* fName; |
| 127 | }; |
| 128 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 129 | bool SkImageRef_ashmem::onDecode(SkImageDecoder* codec, SkStreamRewindable* stream, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | SkBitmap* bitmap, SkBitmap::Config config, |
| 131 | SkImageDecoder::Mode mode) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 132 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | if (SkImageDecoder::kDecodeBounds_Mode == mode) { |
| 134 | return this->INHERITED::onDecode(codec, stream, bitmap, config, mode); |
| 135 | } |
| 136 | |
scroggo@google.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame] | 137 | // Ashmem memory is guaranteed to be initialized to 0. |
| 138 | codec->setSkipWritingZeroes(true); |
| 139 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | AshmemAllocator alloc(&fRec, this->getURI()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 141 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 142 | codec->setAllocator(&alloc); |
| 143 | bool success = this->INHERITED::onDecode(codec, stream, bitmap, config, |
| 144 | mode); |
| 145 | // remove the allocator, since its on the stack |
| 146 | codec->setAllocator(NULL); |
| 147 | |
| 148 | if (success) { |
| 149 | // remember the colortable (if any) |
| 150 | SkRefCnt_SafeAssign(fCT, bitmap->getColorTable()); |
| 151 | return true; |
| 152 | } else { |
| 153 | if (fRec.fPinned) { |
| 154 | ashmem_unpin_region(fRec.fFD, 0, 0); |
| 155 | fRec.fPinned = false; |
| 156 | } |
| 157 | this->closeFD(); |
| 158 | return false; |
| 159 | } |
| 160 | } |
| 161 | |
reed@google.com | 9271389 | 2014-01-03 17:58:57 +0000 | [diff] [blame] | 162 | bool SkImageRef_ashmem::onNewLockPixels(LockRec* rec) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 163 | SkASSERT(fBitmap.getPixels() == NULL); |
| 164 | SkASSERT(fBitmap.getColorTable() == NULL); |
| 165 | |
| 166 | // fast case: check if we can just pin and get the cached data |
| 167 | if (-1 != fRec.fFD) { |
| 168 | SkASSERT(fRec.fAddr); |
| 169 | SkASSERT(!fRec.fPinned); |
| 170 | int pin = ashmem_pin_region(fRec.fFD, 0, 0); |
| 171 | |
| 172 | if (ASHMEM_NOT_PURGED == pin) { // yea, fast case! |
| 173 | fBitmap.setPixels(fRec.fAddr, fCT); |
| 174 | fRec.fPinned = true; |
| 175 | } else if (ASHMEM_WAS_PURGED == pin) { |
| 176 | ashmem_unpin_region(fRec.fFD, 0, 0); |
| 177 | // let go of our colortable if we lost the pixels. Well get it back |
| 178 | // again when we re-decode |
| 179 | if (fCT) { |
| 180 | fCT->unref(); |
| 181 | fCT = NULL; |
| 182 | } |
| 183 | #if defined(DUMP_ASHMEM_LIFECYCLE) || defined(TRACE_ASH_PURGE) |
| 184 | SkDebugf("===== ashmem purged %d\n", fBitmap.getSize()); |
| 185 | #endif |
| 186 | } else { |
djsollen@google.com | 60abb07 | 2012-02-15 18:49:15 +0000 | [diff] [blame] | 187 | SkDebugf("===== ashmem pin_region(%d) returned %d\n", fRec.fFD, pin); |
reed@google.com | 9271389 | 2014-01-03 17:58:57 +0000 | [diff] [blame] | 188 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 189 | } |
| 190 | } else { |
| 191 | // no FD, will create an ashmem region in allocator |
| 192 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 193 | |
reed@google.com | 9271389 | 2014-01-03 17:58:57 +0000 | [diff] [blame] | 194 | return this->INHERITED::onNewLockPixels(rec); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void SkImageRef_ashmem::onUnlockPixels() { |
| 198 | this->INHERITED::onUnlockPixels(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 199 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 200 | if (-1 != fRec.fFD) { |
| 201 | SkASSERT(fRec.fAddr); |
| 202 | SkASSERT(fRec.fPinned); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 203 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 204 | ashmem_unpin_region(fRec.fFD, 0, 0); |
| 205 | fRec.fPinned = false; |
| 206 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 207 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 208 | // we clear this with or without an error, since we've either closed or |
| 209 | // unpinned the region |
| 210 | fBitmap.setPixels(NULL, NULL); |
| 211 | } |
| 212 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 213 | void SkImageRef_ashmem::flatten(SkFlattenableWriteBuffer& buffer) const { |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 214 | this->INHERITED::flatten(buffer); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 215 | buffer.writeString(getURI()); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | SkImageRef_ashmem::SkImageRef_ashmem(SkFlattenableReadBuffer& buffer) |
| 219 | : INHERITED(buffer) { |
| 220 | fRec.fFD = -1; |
| 221 | fRec.fAddr = NULL; |
| 222 | fRec.fSize = 0; |
| 223 | fRec.fPinned = false; |
| 224 | fCT = NULL; |
djsollen@google.com | 7768751 | 2013-05-21 16:17:14 +0000 | [diff] [blame] | 225 | |
| 226 | SkString uri; |
| 227 | buffer.readString(&uri); |
| 228 | this->setURI(uri); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 229 | } |