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_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 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame^] | 34 | SkImageRef_ashmem::SkImageRef_ashmem(SkStreamRewindable* stream, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | SkBitmap::Config config, |
| 36 | int sampleSize) |
| 37 | : SkImageRef(stream, config, sampleSize) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 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 | |
| 137 | AshmemAllocator alloc(&fRec, this->getURI()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 138 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | codec->setAllocator(&alloc); |
| 140 | bool success = this->INHERITED::onDecode(codec, stream, bitmap, config, |
| 141 | mode); |
| 142 | // remove the allocator, since its on the stack |
| 143 | codec->setAllocator(NULL); |
| 144 | |
| 145 | if (success) { |
| 146 | // remember the colortable (if any) |
| 147 | SkRefCnt_SafeAssign(fCT, bitmap->getColorTable()); |
| 148 | return true; |
| 149 | } else { |
| 150 | if (fRec.fPinned) { |
| 151 | ashmem_unpin_region(fRec.fFD, 0, 0); |
| 152 | fRec.fPinned = false; |
| 153 | } |
| 154 | this->closeFD(); |
| 155 | return false; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void* SkImageRef_ashmem::onLockPixels(SkColorTable** ct) { |
| 160 | SkASSERT(fBitmap.getPixels() == NULL); |
| 161 | SkASSERT(fBitmap.getColorTable() == NULL); |
| 162 | |
| 163 | // fast case: check if we can just pin and get the cached data |
| 164 | if (-1 != fRec.fFD) { |
| 165 | SkASSERT(fRec.fAddr); |
| 166 | SkASSERT(!fRec.fPinned); |
| 167 | int pin = ashmem_pin_region(fRec.fFD, 0, 0); |
| 168 | |
| 169 | if (ASHMEM_NOT_PURGED == pin) { // yea, fast case! |
| 170 | fBitmap.setPixels(fRec.fAddr, fCT); |
| 171 | fRec.fPinned = true; |
| 172 | } else if (ASHMEM_WAS_PURGED == pin) { |
| 173 | ashmem_unpin_region(fRec.fFD, 0, 0); |
| 174 | // let go of our colortable if we lost the pixels. Well get it back |
| 175 | // again when we re-decode |
| 176 | if (fCT) { |
| 177 | fCT->unref(); |
| 178 | fCT = NULL; |
| 179 | } |
| 180 | #if defined(DUMP_ASHMEM_LIFECYCLE) || defined(TRACE_ASH_PURGE) |
| 181 | SkDebugf("===== ashmem purged %d\n", fBitmap.getSize()); |
| 182 | #endif |
| 183 | } else { |
djsollen@google.com | 60abb07 | 2012-02-15 18:49:15 +0000 | [diff] [blame] | 184 | SkDebugf("===== ashmem pin_region(%d) returned %d\n", fRec.fFD, pin); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 185 | // return null result for failure |
| 186 | if (ct) { |
| 187 | *ct = NULL; |
| 188 | } |
| 189 | return NULL; |
| 190 | } |
| 191 | } else { |
| 192 | // no FD, will create an ashmem region in allocator |
| 193 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 194 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 195 | return this->INHERITED::onLockPixels(ct); |
| 196 | } |
| 197 | |
| 198 | void SkImageRef_ashmem::onUnlockPixels() { |
| 199 | this->INHERITED::onUnlockPixels(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 200 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 201 | if (-1 != fRec.fFD) { |
| 202 | SkASSERT(fRec.fAddr); |
| 203 | SkASSERT(fRec.fPinned); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 204 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 205 | ashmem_unpin_region(fRec.fFD, 0, 0); |
| 206 | fRec.fPinned = false; |
| 207 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 208 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 209 | // we clear this with or without an error, since we've either closed or |
| 210 | // unpinned the region |
| 211 | fBitmap.setPixels(NULL, NULL); |
| 212 | } |
| 213 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 214 | void SkImageRef_ashmem::flatten(SkFlattenableWriteBuffer& buffer) const { |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 215 | this->INHERITED::flatten(buffer); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 216 | buffer.writeString(getURI()); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | SkImageRef_ashmem::SkImageRef_ashmem(SkFlattenableReadBuffer& buffer) |
| 220 | : INHERITED(buffer) { |
| 221 | fRec.fFD = -1; |
| 222 | fRec.fAddr = NULL; |
| 223 | fRec.fSize = 0; |
| 224 | fRec.fPinned = false; |
| 225 | fCT = NULL; |
djsollen@google.com | 7768751 | 2013-05-21 16:17:14 +0000 | [diff] [blame] | 226 | |
| 227 | SkString uri; |
| 228 | buffer.readString(&uri); |
| 229 | this->setURI(uri); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 230 | } |