blob: 14dedf8bd3e7d502ab918a3cd16267a42503ba2a [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"
djsollen@google.com276a2952012-11-19 19:34:23 +000010#include "SkFlattenableBuffers.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkThread.h"
12
djsollen@google.com276a2952012-11-19 19:34:23 +000013#include "android/ashmem.h"
14
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include <sys/mman.h>
16#include <unistd.h>
reed@android.com8a1c16f2008-12-17 15:59:43 +000017
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
27static 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
reed@google.combf790232013-12-13 19:45:58 +000034SkImageRef_ashmem::SkImageRef_ashmem(const SkImageInfo& info,
35 SkStreamRewindable* stream,
36 int sampleSize)
37 : SkImageRef(info, stream, sampleSize)
38{
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 fRec.fFD = -1;
40 fRec.fAddr = NULL;
41 fRec.fSize = 0;
42 fRec.fPinned = false;
rmistry@google.comd6176b02012-08-23 18:14:13 +000043
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 fCT = NULL;
45}
46
47SkImageRef_ashmem::~SkImageRef_ashmem() {
djsollen@google.com57f49692011-02-23 20:46:31 +000048 SkSafeUnref(fCT);
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 this->closeFD();
50}
51
52void 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
67class AshmemAllocator : public SkBitmap::Allocator {
68public:
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.comd6176b02012-08-23 18:14:13 +000082
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 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.comd6176b02012-08-23 18:14:13 +000092
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 int err = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
94 if (err) {
djsollen@google.com60abb072012-02-15 18:49:15 +000095 SkDebugf("------ ashmem_set_prot_region(%d) failed %d\n",
96 fd, err);
97 close(fd);
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 return false;
99 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
102 if (-1 == (long)addr) {
djsollen@google.com60abb072012-02-15 18:49:15 +0000103 SkDebugf("---------- mmap failed for imageref_ashmem size=%d\n",
104 size);
105 close(fd);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 return false;
107 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000108
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 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.comd6176b02012-08-23 18:14:13 +0000122
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123private:
124 // we just point to our caller's memory, these are not copies
125 SkAshmemRec* fRec;
126 const char* fName;
127};
128
scroggo@google.comb5571b32013-09-25 21:34:24 +0000129bool SkImageRef_ashmem::onDecode(SkImageDecoder* codec, SkStreamRewindable* stream,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 SkBitmap* bitmap, SkBitmap::Config config,
131 SkImageDecoder::Mode mode) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000132
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
134 return this->INHERITED::onDecode(codec, stream, bitmap, config, mode);
135 }
136
scroggo@google.com8d239242013-10-01 17:27:15 +0000137 // Ashmem memory is guaranteed to be initialized to 0.
138 codec->setSkipWritingZeroes(true);
139
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 AshmemAllocator alloc(&fRec, this->getURI());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000141
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 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
reed@google.com92713892014-01-03 17:58:57 +0000162bool SkImageRef_ashmem::onNewLockPixels(LockRec* rec) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 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 {
djsollen@google.com60abb072012-02-15 18:49:15 +0000187 SkDebugf("===== ashmem pin_region(%d) returned %d\n", fRec.fFD, pin);
reed@google.com92713892014-01-03 17:58:57 +0000188 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189 }
190 } else {
191 // no FD, will create an ashmem region in allocator
192 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000193
reed@google.com92713892014-01-03 17:58:57 +0000194 return this->INHERITED::onNewLockPixels(rec);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195}
196
197void SkImageRef_ashmem::onUnlockPixels() {
198 this->INHERITED::onUnlockPixels();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000199
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200 if (-1 != fRec.fFD) {
201 SkASSERT(fRec.fAddr);
202 SkASSERT(fRec.fPinned);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000203
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204 ashmem_unpin_region(fRec.fFD, 0, 0);
205 fRec.fPinned = false;
206 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000207
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208 // we clear this with or without an error, since we've either closed or
209 // unpinned the region
210 fBitmap.setPixels(NULL, NULL);
211}
212
djsollen@google.com54924242012-03-29 15:18:04 +0000213void SkImageRef_ashmem::flatten(SkFlattenableWriteBuffer& buffer) const {
djsollen@google.com57f49692011-02-23 20:46:31 +0000214 this->INHERITED::flatten(buffer);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000215 buffer.writeString(getURI());
djsollen@google.com57f49692011-02-23 20:46:31 +0000216}
217
218SkImageRef_ashmem::SkImageRef_ashmem(SkFlattenableReadBuffer& buffer)
219 : INHERITED(buffer) {
220 fRec.fFD = -1;
221 fRec.fAddr = NULL;
222 fRec.fSize = 0;
223 fRec.fPinned = false;
224 fCT = NULL;
djsollen@google.com77687512013-05-21 16:17:14 +0000225
226 SkString uri;
227 buffer.readString(&uri);
228 this->setURI(uri);
djsollen@google.com57f49692011-02-23 20:46:31 +0000229}