1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/ports/SkPurgeableMemoryBlock_android.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,110 @@ 1.4 +/* 1.5 + * Copyright 2013 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#include "SkPurgeableMemoryBlock.h" 1.12 + 1.13 +#include "android/ashmem.h" 1.14 +#include <sys/mman.h> 1.15 +#include <unistd.h> 1.16 + 1.17 +bool SkPurgeableMemoryBlock::IsSupported() { 1.18 + return true; 1.19 +} 1.20 + 1.21 +#ifdef SK_DEBUG 1.22 +bool SkPurgeableMemoryBlock::PlatformSupportsPurgingAllUnpinnedBlocks() { 1.23 + return false; 1.24 +} 1.25 + 1.26 +bool SkPurgeableMemoryBlock::PurgeAllUnpinnedBlocks() { 1.27 + return false; 1.28 +} 1.29 + 1.30 +bool SkPurgeableMemoryBlock::purge() { 1.31 + SkASSERT(!fPinned); 1.32 + if (-1 != fFD) { 1.33 + ashmem_purge_all_caches(fFD); 1.34 + return true; 1.35 + } else { 1.36 + return false; 1.37 + } 1.38 +} 1.39 +#endif 1.40 + 1.41 +// ashmem likes lengths on page boundaries. 1.42 +static size_t round_to_page_size(size_t size) { 1.43 + const size_t mask = getpagesize() - 1; 1.44 + size_t newSize = (size + mask) & ~mask; 1.45 + return newSize; 1.46 +} 1.47 + 1.48 +SkPurgeableMemoryBlock::SkPurgeableMemoryBlock(size_t size) 1.49 + : fAddr(NULL) 1.50 + , fSize(round_to_page_size(size)) 1.51 + , fPinned(false) 1.52 + , fFD(-1) { 1.53 +} 1.54 + 1.55 +SkPurgeableMemoryBlock::~SkPurgeableMemoryBlock() { 1.56 + if (-1 != fFD) { 1.57 + munmap(fAddr, fSize); 1.58 + close(fFD); 1.59 + } 1.60 +} 1.61 + 1.62 +void* SkPurgeableMemoryBlock::pin(SkPurgeableMemoryBlock::PinResult* pinResult) { 1.63 + SkASSERT(!fPinned); 1.64 + if (-1 == fFD) { 1.65 + int fd = ashmem_create_region(NULL, fSize); 1.66 + if (-1 == fd) { 1.67 + SkDebugf("ashmem_create_region failed\n"); 1.68 + return NULL; 1.69 + } 1.70 + 1.71 + int err = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE); 1.72 + if (err != 0) { 1.73 + SkDebugf("ashmem_set_prot_region failed\n"); 1.74 + close(fd); 1.75 + return NULL; 1.76 + } 1.77 + 1.78 + void* addr = mmap(NULL, fSize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); 1.79 + if (-1 == (long) addr) { 1.80 + SkDebugf("mmap failed\n"); 1.81 + close(fd); 1.82 + return NULL; 1.83 + } 1.84 + fAddr = addr; 1.85 + fFD = fd; 1.86 + (void) ashmem_pin_region(fd, 0, 0); 1.87 + *pinResult = kUninitialized_PinResult; 1.88 + fPinned = true; 1.89 + } else { 1.90 + int pin = ashmem_pin_region(fFD, 0, 0); 1.91 + if (ASHMEM_NOT_PURGED == pin) { 1.92 + fPinned = true; 1.93 + *pinResult = kRetained_PinResult; 1.94 + } else if (ASHMEM_WAS_PURGED == pin) { 1.95 + fPinned = true; 1.96 + *pinResult = kUninitialized_PinResult; 1.97 + } else { 1.98 + // Failed. 1.99 + munmap(fAddr, fSize); 1.100 + close(fFD); 1.101 + fFD = -1; 1.102 + fAddr = NULL; 1.103 + } 1.104 + } 1.105 + return fAddr; 1.106 +} 1.107 + 1.108 +void SkPurgeableMemoryBlock::unpin() { 1.109 + if (-1 != fFD) { 1.110 + ashmem_unpin_region(fFD, 0, 0); 1.111 + fPinned = false; 1.112 + } 1.113 +}