Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(177)

Side by Side Diff: src/ports/SkPurgeableMemoryBlock_android.cpp

Issue 12433020: Improvements/additions to SkImageCache/SkLazyPixelRef. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/ports/SkAshmemImageCache.cpp ('k') | src/ports/SkPurgeableMemoryBlock_mac.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2013 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 "SkPurgeableMemoryBlock.h"
9
10 #include "android/ashmem.h"
11 #include <sys/mman.h>
12 #include <unistd.h>
13
14 bool SkPurgeableMemoryBlock::PlatformSupportsPurgeableMemory() {
15 return true;
16 }
17
18 #ifdef SK_DEBUG
19 bool SkPurgeableMemoryBlock::PlatformSupportsPurgingAllUnpinnedBlocks() {
20 return false;
21 }
22
23 bool SkPurgeableMemoryBlock::PurgeAllUnpinnedBlocks() {
24 return false;
25 }
26
27 bool SkPurgeableMemoryBlock::purge() {
28 SkASSERT(!fPinned);
29 if (-1 != fFD) {
30 munmap(fAddr, fSize);
31 close(fFD);
32 fFD = -1;
33 fAddr = NULL;
34 return true;
35 } else {
36 return false;
37 }
38 }
39 #endif
40
41 // ashmem likes lengths on page boundaries.
42 static size_t round_to_page_size(size_t size) {
43 const size_t mask = getpagesize() - 1;
44 size_t newSize = (size + mask) & ~mask;
45 return newSize;
46 }
47
48 SkPurgeableMemoryBlock::SkPurgeableMemoryBlock(size_t size)
49 : fAddr(NULL)
50 , fSize(round_to_page_size(size))
51 , fPinned(false)
52 , fFD(-1) {
53 }
54
55 SkPurgeableMemoryBlock::~SkPurgeableMemoryBlock() {
56 if (-1 != fFD) {
57 munmap(fAddr, fSize);
58 close(fFD);
59 }
60 }
61
62 void* SkPurgeableMemoryBlock::pin(SkPurgeableMemoryBlock::PinResult* pinResult) {
63 SkASSERT(!fPinned);
64 if (-1 == fFD) {
65 int fd = ashmem_create_region(NULL, fSize);
66 if (-1 == fd) {
67 SkDebugf("ashmem_create_region failed\n");
68 return NULL;
69 }
70
71 int err = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
72 if (err != 0) {
73 SkDebugf("ashmem_set_prot_region failed\n");
74 close(fd);
75 return NULL;
76 }
77
78 void* addr = mmap(NULL, fSize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
79 if (-1 == (long) addr) {
80 SkDebugf("mmap failed\n");
81 close(fd);
82 return NULL;
83 }
84 fAddr = addr;
85 fFD = fd;
86 (void) ashmem_pin_region(fd, 0, 0);
87 *pinResult = kUninitialized_PinResult;
88 fPinned = true;
89 } else {
90 int pin = ashmem_pin_region(fFD, 0, 0);
91 if (ASHMEM_NOT_PURGED == pin) {
92 fPinned = true;
93 *pinResult = kRetained_PinResult;
94 } else if (ASHMEM_WAS_PURGED == pin) {
95 fPinned = true;
96 *pinResult = kUninitialized_PinResult;
97 } else {
98 // Failed.
99 munmap(fAddr, fSize);
100 close(fFD);
101 fFD = -1;
102 fAddr = NULL;
103 }
104 }
105 return fAddr;
106 }
107
108 void SkPurgeableMemoryBlock::unpin() {
109 if (-1 != fFD) {
110 ashmem_unpin_region(fFD, 0, 0);
111 fPinned = false;
112 }
113 }
114
OLDNEW
« no previous file with comments | « src/ports/SkAshmemImageCache.cpp ('k') | src/ports/SkPurgeableMemoryBlock_mac.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698