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

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
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 fSize = 0;
35 return true;
36 } else {
37 return false;
38 }
39 }
40 #endif
41
42 // ashmem likes lengths on page boundaries.
43 static size_t round_to_page_size(size_t size) {
44 const size_t mask = getpagesize() - 1;
45 size_t newSize = (size + mask) & ~mask;
46 return newSize;
47 }
48
49 SkPurgeableMemoryBlock::SkPurgeableMemoryBlock(size_t size)
50 : fAddr(NULL)
51 , fSize(0)
52 , fPinned(false)
53 , fFD(-1) {
54 size = round_to_page_size(size);
55 int fd = ashmem_create_region(NULL, size);
56 if (-1 == fd) {
57 SkDebugf("ashmem_create_region failed\n");
58 return;
59 }
60
61 int err = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
62 if (err != 0) {
63 SkDebugf("ashmem_set_prot_region failed\n");
64 close(fd);
65 return;
66 }
67
68 void* addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
69 if (-1 == (long) addr) {
70 SkDebugf("mmap failed\n");
71 close(fd);
72 return;
73 }
74 fAddr = addr;
75 fSize = size;
76 fFD = fd;
77 (void) ashmem_pin_region(fd, 0, 0);
78 fPinned = true;
79 }
80
81 SkPurgeableMemoryBlock::~SkPurgeableMemoryBlock() {
82 if (-1 != fFD) {
83 munmap(fAddr, fSize);
84 close(fFD);
85 }
86 }
87
88 SkPurgeableMemoryBlock::PinResult SkPurgeableMemoryBlock::pin() {
89 SkASSERT(!fPinned);
90 if (-1 != fFD) {
91 int pin = ashmem_pin_region(fFD, 0, 0);
92 if (ASHMEM_NOT_PURGED == pin) {
93 fPinned = true;
94 return kRetained_PinResult;
95 } else if (ASHMEM_WAS_PURGED == pin) {
96 fPinned = true;
97 return kUninitialized_PinResult;
98 } else {
99 // Failed.
100 munmap(fAddr, fSize);
101 close(fFD);
102 fFD = -1;
103 fAddr = NULL;
104 fSize = 0;
105 // Fall through
106 }
107 }
108 return kFailed_PinResult;
109 }
110
111 void SkPurgeableMemoryBlock::unpin() {
112 if (-1 != fFD) {
113 ashmem_unpin_region(fFD, 0, 0);
114 fPinned = false;
115 }
116 }
117
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698