blob: 3c7d5c1bbf4c4a47143a54ff62c238b59d3cfa63 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.comc83a91f2013-12-13 13:41:14 +00007
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkImageRef_ashmem.h"
9#include "SkImageDecoder.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000010#include "SkReadBuffer.h"
11#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkThread.h"
13
djsollen@google.com276a2952012-11-19 19:34:23 +000014#include "android/ashmem.h"
15
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include <sys/mman.h>
17#include <unistd.h>
reed@android.com8a1c16f2008-12-17 15:59:43 +000018
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
28static 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.combf790232013-12-13 19:45:58 +000035SkImageRef_ashmem::SkImageRef_ashmem(const SkImageInfo& info,
36 SkStreamRewindable* stream,
37 int sampleSize)
38 : SkImageRef(info, stream, sampleSize)
39{
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 fRec.fFD = -1;
41 fRec.fAddr = NULL;
42 fRec.fSize = 0;
43 fRec.fPinned = false;
rmistry@google.comd6176b02012-08-23 18:14:13 +000044
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 fCT = NULL;
46}
47
48SkImageRef_ashmem::~SkImageRef_ashmem() {
djsollen@google.com57f49692011-02-23 20:46:31 +000049 SkSafeUnref(fCT);
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 this->closeFD();
51}
52
53void 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
68class AshmemAllocator : public SkBitmap::Allocator {
69public:
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.comd6176b02012-08-23 18:14:13 +000083
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 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.comd6176b02012-08-23 18:14:13 +000093
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 int err = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
95 if (err) {
djsollen@google.com60abb072012-02-15 18:49:15 +000096 SkDebugf("------ ashmem_set_prot_region(%d) failed %d\n",
97 fd, err);
98 close(fd);
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 return false;
100 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000101
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
103 if (-1 == (long)addr) {
djsollen@google.com60abb072012-02-15 18:49:15 +0000104 SkDebugf("---------- mmap failed for imageref_ashmem size=%d\n",
105 size);
106 close(fd);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 return false;
108 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000109
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 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.comd6176b02012-08-23 18:14:13 +0000123
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124private:
125 // we just point to our caller's memory, these are not copies
126 SkAshmemRec* fRec;
127 const char* fName;
128};
129
scroggo@google.comb5571b32013-09-25 21:34:24 +0000130bool SkImageRef_ashmem::onDecode(SkImageDecoder* codec, SkStreamRewindable* stream,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 SkBitmap* bitmap, SkBitmap::Config config,
132 SkImageDecoder::Mode mode) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000133
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
135 return this->INHERITED::onDecode(codec, stream, bitmap, config, mode);
136 }
137
scroggo@google.com8d239242013-10-01 17:27:15 +0000138 // Ashmem memory is guaranteed to be initialized to 0.
139 codec->setSkipWritingZeroes(true);
140
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 AshmemAllocator alloc(&fRec, this->getURI());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000142
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 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.comd0419b12014-01-06 17:08:27 +0000163bool SkImageRef_ashmem::onNewLockPixels(LockRec* rec) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164 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.com60abb072012-02-15 18:49:15 +0000188 SkDebugf("===== ashmem pin_region(%d) returned %d\n", fRec.fFD, pin);
reed@google.comd0419b12014-01-06 17:08:27 +0000189 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 }
191 } else {
192 // no FD, will create an ashmem region in allocator
193 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000194
reed@google.comd0419b12014-01-06 17:08:27 +0000195 return this->INHERITED::onNewLockPixels(rec);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196}
197
198void SkImageRef_ashmem::onUnlockPixels() {
199 this->INHERITED::onUnlockPixels();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000200
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201 if (-1 != fRec.fFD) {
202 SkASSERT(fRec.fAddr);
203 SkASSERT(fRec.fPinned);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000204
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 ashmem_unpin_region(fRec.fFD, 0, 0);
206 fRec.fPinned = false;
207 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000208
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209 // 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.org8b0e8ac2014-01-30 18:58:24 +0000214void SkImageRef_ashmem::flatten(SkWriteBuffer& buffer) const {
djsollen@google.com57f49692011-02-23 20:46:31 +0000215 this->INHERITED::flatten(buffer);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000216 buffer.writeString(getURI());
djsollen@google.com57f49692011-02-23 20:46:31 +0000217}
218
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000219SkImageRef_ashmem::SkImageRef_ashmem(SkReadBuffer& buffer)
djsollen@google.com57f49692011-02-23 20:46:31 +0000220 : INHERITED(buffer) {
221 fRec.fFD = -1;
222 fRec.fAddr = NULL;
223 fRec.fSize = 0;
224 fRec.fPinned = false;
225 fCT = NULL;
djsollen@google.com77687512013-05-21 16:17:14 +0000226
227 SkString uri;
228 buffer.readString(&uri);
229 this->setURI(uri);
djsollen@google.com57f49692011-02-23 20:46:31 +0000230}