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