Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/common/font_config_ipc_linux.h" | 5 #include "content/common/font_config_ipc_linux.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <sys/mman.h> | |
| 9 #include <sys/socket.h> | 10 #include <sys/socket.h> |
| 11 #include <sys/stat.h> | |
| 10 #include <sys/uio.h> | 12 #include <sys/uio.h> |
| 11 #include <unistd.h> | 13 #include <unistd.h> |
| 12 | 14 |
| 13 #include "base/pickle.h" | 15 #include "base/pickle.h" |
| 14 #include "base/posix/unix_domain_socket_linux.h" | 16 #include "base/posix/unix_domain_socket_linux.h" |
| 15 #include "skia/ext/skia_utils_base.h" | 17 #include "skia/ext/skia_utils_base.h" |
| 16 #include "third_party/skia/include/core/SkStream.h" | 18 #include "third_party/skia/include/core/SkStream.h" |
| 17 | 19 |
| 18 namespace content { | 20 namespace content { |
| 19 | 21 |
| 22 // Takes "ownership" of the file descript, and will call close on it. | |
| 23 class SkFileDescriptorStream : public SkStream { | |
| 24 public: | |
| 25 SkFileDescriptorStream(int fd) { | |
| 26 memory_ = NULL; | |
| 27 offset_ = 0; | |
| 28 | |
| 29 // this ensures that if we fail in the constructor, we will safely | |
| 30 // ignore all subsequent calls to read() because we will always trim | |
| 31 // the requested size down to 0 | |
| 32 length_ = 0; | |
| 33 | |
| 34 struct stat st; | |
| 35 if (fstat(fd, &st)) | |
| 36 return; | |
| 37 | |
| 38 void* memory = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); | |
| 39 close(fd); | |
|
jln (very slow on Chromium)
2013/03/18 16:26:06
You need to HANDLE_EINTR and DCHECK (or CHECK) her
| |
| 40 if (memory == MAP_FAILED) | |
| 41 return; | |
| 42 | |
| 43 memory_ = reinterpret_cast<uint8_t*>(memory); | |
| 44 length_ = st.st_size; | |
| 45 } | |
| 46 | |
| 47 virtual ~SkFileDescriptorStream() { | |
| 48 munmap(const_cast<uint8_t*>(memory_), length_); | |
| 49 } | |
| 50 | |
| 51 virtual bool rewind() OVERRIDE { | |
| 52 offset_ = 0; | |
| 53 return true; | |
| 54 } | |
| 55 | |
| 56 // SkStream implementation. | |
| 57 virtual size_t read(void* buffer, size_t size) OVERRIDE { | |
| 58 if (!buffer && !size) { | |
| 59 // This is request for the length of the stream. | |
| 60 return length_; | |
| 61 } | |
| 62 | |
| 63 size_t remaining = length_ - offset_; | |
| 64 if (size > remaining) | |
| 65 size = remaining; | |
| 66 if (buffer) | |
| 67 memcpy(buffer, memory_ + offset_, size); | |
| 68 | |
| 69 offset_ += size; | |
| 70 return size; | |
| 71 } | |
| 72 | |
| 73 virtual const void* getMemoryBase() OVERRIDE { | |
| 74 return memory_; | |
| 75 } | |
| 76 | |
| 77 private: | |
| 78 const uint8_t* memory_; | |
| 79 size_t offset_, length_; | |
| 80 }; | |
| 81 | |
| 20 FontConfigIPC::FontConfigIPC(int fd) | 82 FontConfigIPC::FontConfigIPC(int fd) |
| 21 : fd_(fd) { | 83 : fd_(fd) { |
| 22 } | 84 } |
| 23 | 85 |
| 24 FontConfigIPC::~FontConfigIPC() { | 86 FontConfigIPC::~FontConfigIPC() { |
| 25 close(fd_); | 87 close(fd_); |
| 26 } | 88 } |
| 27 | 89 |
| 28 bool FontConfigIPC::matchFamilyName(const char familyName[], | 90 bool FontConfigIPC::matchFamilyName(const char familyName[], |
| 29 SkTypeface::Style requestedStyle, | 91 SkTypeface::Style requestedStyle, |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 Pickle reply(reinterpret_cast<char*>(reply_buf), r); | 152 Pickle reply(reinterpret_cast<char*>(reply_buf), r); |
| 91 bool result; | 153 bool result; |
| 92 PickleIterator iter(reply); | 154 PickleIterator iter(reply); |
| 93 if (!reply.ReadBool(&iter, &result) || | 155 if (!reply.ReadBool(&iter, &result) || |
| 94 !result) { | 156 !result) { |
| 95 if (result_fd) | 157 if (result_fd) |
| 96 close(result_fd); | 158 close(result_fd); |
| 97 return NULL; | 159 return NULL; |
| 98 } | 160 } |
| 99 | 161 |
| 100 return new SkFDStream(result_fd, true); | 162 return new SkFileDescriptorStream(result_fd); |
| 101 } | 163 } |
| 102 | 164 |
| 103 } // namespace content | 165 } // namespace content |
| OLD | NEW |