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" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 10 | #include "SkReadBuffer.h" |
| 11 | #include "SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkThread.h" |
| 13 | |
djsollen@google.com | 276a295 | 2012-11-19 19:34:23 +0000 | [diff] [blame] | 14 | #include "android/ashmem.h" |
| 15 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #include <sys/mman.h> |
| 17 | #include <unistd.h> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | |
| 19 | //#define TRACE_ASH_PURGE // just trace purges |
| 20 | |
| 21 | #ifdef DUMP_IMAGEREF_LIFECYCLE |
| 22 | #define DUMP_ASHMEM_LIFECYCLE |
| 23 | #else |
| 24 | // #define DUMP_ASHMEM_LIFECYCLE |
| 25 | #endif |
| 26 | |
| 27 | // ashmem likes lengths on page boundaries |
| 28 | static size_t roundToPageSize(size_t size) { |
| 29 | const size_t mask = getpagesize() - 1; |
| 30 | size_t newsize = (size + mask) & ~mask; |
| 31 | // SkDebugf("---- oldsize %d newsize %d\n", size, newsize); |
| 32 | return newsize; |
| 33 | } |
| 34 | |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 35 | SkImageRef_ashmem::SkImageRef_ashmem(const SkImageInfo& info, |
| 36 | SkStreamRewindable* stream, |
| 37 | int sampleSize) |
| 38 | : SkImageRef(info, stream, sampleSize) |
| 39 | { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | fRec.fFD = -1; |
| 41 | fRec.fAddr = NULL; |
| 42 | fRec.fSize = 0; |
| 43 | fRec.fPinned = false; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 44 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | fCT = NULL; |
| 46 | } |
| 47 | |
| 48 | SkImageRef_ashmem::~SkImageRef_ashmem() { |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 49 | SkSafeUnref(fCT); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | this->closeFD(); |
| 51 | } |
| 52 | |
| 53 | void SkImageRef_ashmem::closeFD() { |
| 54 | if (-1 != fRec.fFD) { |
| 55 | #ifdef DUMP_ASHMEM_LIFECYCLE |
| 56 | SkDebugf("=== ashmem close %d\n", fRec.fFD); |
| 57 | #endif |
| 58 | SkASSERT(fRec.fAddr); |
| 59 | SkASSERT(fRec.fSize); |
| 60 | munmap(fRec.fAddr, fRec.fSize); |
| 61 | close(fRec.fFD); |
| 62 | fRec.fFD = -1; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /////////////////////////////////////////////////////////////////////////////// |
| 67 | |
| 68 | class AshmemAllocator : public SkBitmap::Allocator { |
| 69 | public: |
| 70 | AshmemAllocator(SkAshmemRec* rec, const char name[]) |
| 71 | : fRec(rec), fName(name) {} |
| 72 | |
| 73 | virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) { |
| 74 | const size_t size = roundToPageSize(bm->getSize()); |
| 75 | int fd = fRec->fFD; |
| 76 | void* addr = fRec->fAddr; |
| 77 | |
| 78 | SkASSERT(!fRec->fPinned); |
| 79 | |
| 80 | if (-1 == fd) { |
| 81 | SkASSERT(NULL == addr); |
| 82 | SkASSERT(0 == fRec->fSize); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 83 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | fd = ashmem_create_region(fName, size); |
| 85 | #ifdef DUMP_ASHMEM_LIFECYCLE |
| 86 | SkDebugf("=== ashmem_create_region %s size=%d fd=%d\n", fName, size, fd); |
| 87 | #endif |
| 88 | if (-1 == fd) { |
| 89 | SkDebugf("------- imageref_ashmem create failed <%s> %d\n", |
| 90 | fName, size); |
| 91 | return false; |
| 92 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 93 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | int err = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE); |
| 95 | if (err) { |
djsollen@google.com | 60abb07 | 2012-02-15 18:49:15 +0000 | [diff] [blame] | 96 | SkDebugf("------ ashmem_set_prot_region(%d) failed %d\n", |
| 97 | fd, err); |
| 98 | close(fd); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | return false; |
| 100 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 101 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
| 103 | if (-1 == (long)addr) { |
djsollen@google.com | 60abb07 | 2012-02-15 18:49:15 +0000 | [diff] [blame] | 104 | SkDebugf("---------- mmap failed for imageref_ashmem size=%d\n", |
| 105 | size); |
| 106 | close(fd); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | return false; |
| 108 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 109 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 110 | fRec->fFD = fd; |
| 111 | fRec->fAddr = addr; |
| 112 | fRec->fSize = size; |
| 113 | } else { |
| 114 | SkASSERT(addr); |
| 115 | SkASSERT(size == fRec->fSize); |
| 116 | (void)ashmem_pin_region(fd, 0, 0); |
| 117 | } |
| 118 | |
| 119 | bm->setPixels(addr, ct); |
| 120 | fRec->fPinned = true; |
| 121 | return true; |
| 122 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 123 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | private: |
| 125 | // we just point to our caller's memory, these are not copies |
| 126 | SkAshmemRec* fRec; |
| 127 | const char* fName; |
| 128 | }; |
| 129 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 130 | bool SkImageRef_ashmem::onDecode(SkImageDecoder* codec, SkStreamRewindable* stream, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | SkBitmap* bitmap, SkBitmap::Config config, |
| 132 | SkImageDecoder::Mode mode) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 133 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | if (SkImageDecoder::kDecodeBounds_Mode == mode) { |
| 135 | return this->INHERITED::onDecode(codec, stream, bitmap, config, mode); |
| 136 | } |
| 137 | |
scroggo@google.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame] | 138 | // Ashmem memory is guaranteed to be initialized to 0. |
| 139 | codec->setSkipWritingZeroes(true); |
| 140 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | AshmemAllocator alloc(&fRec, this->getURI()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 142 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | codec->setAllocator(&alloc); |
| 144 | bool success = this->INHERITED::onDecode(codec, stream, bitmap, config, |
| 145 | mode); |
| 146 | // remove the allocator, since its on the stack |
| 147 | codec->setAllocator(NULL); |
| 148 | |
| 149 | if (success) { |
| 150 | // remember the colortable (if any) |
| 151 | SkRefCnt_SafeAssign(fCT, bitmap->getColorTable()); |
| 152 | return true; |
| 153 | } else { |
| 154 | if (fRec.fPinned) { |
| 155 | ashmem_unpin_region(fRec.fFD, 0, 0); |
| 156 | fRec.fPinned = false; |
| 157 | } |
| 158 | this->closeFD(); |
| 159 | return false; |
| 160 | } |
| 161 | } |
| 162 | |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 163 | bool SkImageRef_ashmem::onNewLockPixels(LockRec* rec) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | SkASSERT(fBitmap.getPixels() == NULL); |
| 165 | SkASSERT(fBitmap.getColorTable() == NULL); |
| 166 | |
| 167 | // fast case: check if we can just pin and get the cached data |
| 168 | if (-1 != fRec.fFD) { |
| 169 | SkASSERT(fRec.fAddr); |
| 170 | SkASSERT(!fRec.fPinned); |
| 171 | int pin = ashmem_pin_region(fRec.fFD, 0, 0); |
| 172 | |
| 173 | if (ASHMEM_NOT_PURGED == pin) { // yea, fast case! |
| 174 | fBitmap.setPixels(fRec.fAddr, fCT); |
| 175 | fRec.fPinned = true; |
| 176 | } else if (ASHMEM_WAS_PURGED == pin) { |
| 177 | ashmem_unpin_region(fRec.fFD, 0, 0); |
| 178 | // let go of our colortable if we lost the pixels. Well get it back |
| 179 | // again when we re-decode |
| 180 | if (fCT) { |
| 181 | fCT->unref(); |
| 182 | fCT = NULL; |
| 183 | } |
| 184 | #if defined(DUMP_ASHMEM_LIFECYCLE) || defined(TRACE_ASH_PURGE) |
| 185 | SkDebugf("===== ashmem purged %d\n", fBitmap.getSize()); |
| 186 | #endif |
| 187 | } else { |
djsollen@google.com | 60abb07 | 2012-02-15 18:49:15 +0000 | [diff] [blame] | 188 | SkDebugf("===== ashmem pin_region(%d) returned %d\n", fRec.fFD, pin); |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 189 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 195 | return this->INHERITED::onNewLockPixels(rec); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 214 | void SkImageRef_ashmem::flatten(SkWriteBuffer& 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 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 219 | SkImageRef_ashmem::SkImageRef_ashmem(SkReadBuffer& buffer) |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 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 | } |