Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #include "SkStream.h" | 10 #include "SkStream.h" |
| 11 #include "SkData.h" | 11 #include "SkData.h" |
| 12 #include "SkFixed.h" | 12 #include "SkFixed.h" |
| 13 #include "SkString.h" | 13 #include "SkString.h" |
| 14 #include "SkOSFile.h" | 14 #include "SkOSFile.h" |
| 15 | 15 |
| 16 #if SK_MMAP_SUPPORT | |
| 17 #include <unistd.h> | |
| 18 #include <sys/mman.h> | |
| 19 #include <fcntl.h> | |
| 20 #include <errno.h> | |
| 21 #include <unistd.h> | |
| 22 #endif | |
| 23 | |
| 24 SK_DEFINE_INST_COUNT(SkStream) | 16 SK_DEFINE_INST_COUNT(SkStream) |
| 25 SK_DEFINE_INST_COUNT(SkWStream) | 17 SK_DEFINE_INST_COUNT(SkWStream) |
| 26 SK_DEFINE_INST_COUNT(SkFILEStream) | 18 SK_DEFINE_INST_COUNT(SkFILEStream) |
| 27 SK_DEFINE_INST_COUNT(SkFDStream) | 19 SK_DEFINE_INST_COUNT(SkFDStream) |
| 28 SK_DEFINE_INST_COUNT(SkMemoryStream) | 20 SK_DEFINE_INST_COUNT(SkMemoryStream) |
| 29 SK_DEFINE_INST_COUNT(SkBufferStream) | 21 SK_DEFINE_INST_COUNT(SkBufferStream) |
| 30 SK_DEFINE_INST_COUNT(SkFILEWStream) | 22 SK_DEFINE_INST_COUNT(SkFILEWStream) |
| 31 SK_DEFINE_INST_COUNT(SkMemoryWStream) | 23 SK_DEFINE_INST_COUNT(SkMemoryWStream) |
| 32 SK_DEFINE_INST_COUNT(SkDynamicMemoryWStream) | 24 SK_DEFINE_INST_COUNT(SkDynamicMemoryWStream) |
| 33 SK_DEFINE_INST_COUNT(SkDebugWStream) | 25 SK_DEFINE_INST_COUNT(SkDebugWStream) |
| (...skipping 755 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 789 s[size] = 0; | 781 s[size] = 0; |
| 790 SkDebugf("%s", s); | 782 SkDebugf("%s", s); |
| 791 delete[] s; | 783 delete[] s; |
| 792 #endif | 784 #endif |
| 793 return true; | 785 return true; |
| 794 } | 786 } |
| 795 | 787 |
| 796 /////////////////////////////////////////////////////////////////////////////// | 788 /////////////////////////////////////////////////////////////////////////////// |
| 797 /////////////////////////////////////////////////////////////////////////////// | 789 /////////////////////////////////////////////////////////////////////////////// |
| 798 | 790 |
| 799 static bool mmap_filename(const char path[], void** addrPtr, size_t* sizePtr) { | 791 |
| 800 #if SK_MMAP_SUPPORT | 792 static SkData* mmap_filename(const char path[]) { |
|
reed1
2013/04/24 19:07:49
A nice illustration of the utility of SkData::NewF
| |
| 801 int fd = open(path, O_RDONLY); | 793 SkFILE* file = sk_fopen(path, kRead_SkFILE_Flag); |
| 802 if (fd < 0) { | 794 if (NULL == file) { |
| 803 return false; | 795 return NULL; |
| 804 } | 796 } |
| 805 | 797 |
| 806 off_t offset = lseek(fd, 0, SEEK_END); // find the file size | 798 SkData* data = SkData::NewFromFILE(file); |
| 807 if (offset == -1) { | 799 sk_fclose(file); |
| 808 close(fd); | 800 return data; |
| 809 return false; | |
| 810 } | |
| 811 (void)lseek(fd, 0, SEEK_SET); // restore file offset to beginning | |
| 812 | |
| 813 // to avoid a 64bit->32bit warning, I explicitly create a size_t size | |
| 814 size_t size = static_cast<size_t>(offset); | |
| 815 | |
| 816 void* addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); | |
| 817 close(fd); | |
| 818 | |
| 819 if (MAP_FAILED == addr) { | |
| 820 return false; | |
| 821 } | |
| 822 | |
| 823 *addrPtr = addr; | |
| 824 *sizePtr = size; | |
| 825 return true; | |
| 826 #else | |
| 827 return false; | |
| 828 #endif | |
| 829 } | 801 } |
| 830 | 802 |
| 831 SkStream* SkStream::NewFromFile(const char path[]) { | 803 SkStream* SkStream::NewFromFile(const char path[]) { |
| 832 void* addr; | 804 SkAutoTUnref<SkData> data(mmap_filename(path)); |
| 833 size_t size; | 805 if (data.get()) { |
| 834 if (mmap_filename(path, &addr, &size)) { | 806 return SkNEW_ARGS(SkMemoryStream, (data.get())); |
| 835 SkAutoTUnref<SkData> data(SkData::NewFromMMap(addr, size)); | |
| 836 if (data.get()) { | |
| 837 return SkNEW_ARGS(SkMemoryStream, (data.get())); | |
| 838 } | |
| 839 } | 807 } |
| 840 | 808 |
| 841 // If we get here, then our attempt at using mmap failed, so try normal | 809 // If we get here, then our attempt at using mmap failed, so try normal |
| 842 // file access. | 810 // file access. |
| 843 SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path)); | 811 SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path)); |
| 844 if (!stream->isValid()) { | 812 if (!stream->isValid()) { |
| 845 stream->unref(); | 813 stream->unref(); |
| 846 stream = NULL; | 814 stream = NULL; |
| 847 } | 815 } |
| 848 return stream; | 816 return stream; |
| 849 } | 817 } |
| OLD | NEW |