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 */ 7 8 #include "SkImageRef_ashmem.h" 9 #include "SkImageDecoder.h" 10 #include "SkFlattenableBuffers.h" 11 #include "SkThread.h" 12 13 #include "android/ashmem.h" 14 15 #include <sys/mman.h> 16 #include <unistd.h> 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 34 SkImageRef_ashmem::SkImageRef_ashmem(const SkImageInfo& info, 35 SkStreamRewindable* stream, 36 int sampleSize) 37 : SkImageRef(info, stream, sampleSize) 38 { 39 fRec.fFD = -1; 40 fRec.fAddr = NULL; 41 fRec.fSize = 0; 42 fRec.fPinned = false; 43 44 fCT = NULL; 45 } 46 47 SkImageRef_ashmem::~SkImageRef_ashmem() { 48 SkSafeUnref(fCT); 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); 82 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 } 92 93 int err = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE); 94 if (err) { 95 SkDebugf("------ ashmem_set_prot_region(%d) failed %d\n", 96 fd, err); 97 close(fd); 98 return false; 99 } 100 101 addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); 102 if (-1 == (long)addr) { 103 SkDebugf("---------- mmap failed for imageref_ashmem size=%d\n", 104 size); 105 close(fd); 106 return false; 107 } 108 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 } 122 123 private: 124 // we just point to our caller's memory, these are not copies 125 SkAshmemRec* fRec; 126 const char* fName; 127 }; 128 129 bool SkImageRef_ashmem::onDecode(SkImageDecoder* codec, SkStreamRewindable* stream, 130 SkBitmap* bitmap, SkBitmap::Config config, 131 SkImageDecoder::Mode mode) { 132 133 if (SkImageDecoder::kDecodeBounds_Mode == mode) { 134 return this->INHERITED::onDecode(codec, stream, bitmap, config, mode); 135 } 136 137 // Ashmem memory is guaranteed to be initialized to 0. 138 codec->setSkipWritingZeroes(true); 139 140 AshmemAllocator alloc(&fRec, this->getURI()); 141 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 162 void* SkImageRef_ashmem::onLockPixels(SkColorTable** ct) { 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 { 187 SkDebugf("===== ashmem pin_region(%d) returned %d\n", fRec.fFD, pin); 188 // return null result for failure 189 if (ct) { 190 *ct = NULL; 191 } 192 return NULL; 193 } 194 } else { 195 // no FD, will create an ashmem region in allocator 196 } 197 198 return this->INHERITED::onLockPixels(ct); 199 } 200 201 void SkImageRef_ashmem::onUnlockPixels() { 202 this->INHERITED::onUnlockPixels(); 203 204 if (-1 != fRec.fFD) { 205 SkASSERT(fRec.fAddr); 206 SkASSERT(fRec.fPinned); 207 208 ashmem_unpin_region(fRec.fFD, 0, 0); 209 fRec.fPinned = false; 210 } 211 212 // we clear this with or without an error, since we've either closed or 213 // unpinned the region 214 fBitmap.setPixels(NULL, NULL); 215 } 216 217 void SkImageRef_ashmem::flatten(SkFlattenableWriteBuffer& buffer) const { 218 this->INHERITED::flatten(buffer); 219 buffer.writeString(getURI()); 220 } 221 222 SkImageRef_ashmem::SkImageRef_ashmem(SkFlattenableReadBuffer& buffer) 223 : INHERITED(buffer) { 224 fRec.fFD = -1; 225 fRec.fAddr = NULL; 226 fRec.fSize = 0; 227 fRec.fPinned = false; 228 fCT = NULL; 229 230 SkString uri; 231 buffer.readString(&uri); 232 this->setURI(uri); 233 } 234