Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* libs/graphics/ports/SkFontHost_fontconfig.cpp | 1 /* libs/graphics/ports/SkFontHost_fontconfig.cpp |
| 2 ** | 2 ** |
| 3 ** Copyright 2008, Google Inc. | 3 ** Copyright 2008, Google Inc. |
| 4 ** | 4 ** |
| 5 ** Licensed under the Apache License, Version 2.0 (the "License"); | 5 ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 ** you may not use this file except in compliance with the License. | 6 ** you may not use this file except in compliance with the License. |
| 7 ** You may obtain a copy of the License at | 7 ** You may obtain a copy of the License at |
| 8 ** | 8 ** |
| 9 ** http://www.apache.org/licenses/LICENSE-2.0 | 9 ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 ** | 10 ** |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 // involved with enumerating and matching fonts. | 22 // involved with enumerating and matching fonts. |
| 23 // | 23 // |
| 24 // [1] http://fontconfig.org | 24 // [1] http://fontconfig.org |
| 25 // ----------------------------------------------------------------------------- | 25 // ----------------------------------------------------------------------------- |
| 26 | 26 |
| 27 #include <map> | 27 #include <map> |
| 28 #include <string> | 28 #include <string> |
| 29 | 29 |
| 30 #include <unistd.h> | 30 #include <unistd.h> |
| 31 #include <sys/stat.h> | 31 #include <sys/stat.h> |
| 32 #include <sys/mman.h> | |
|
tony
2009/08/28 20:11:29
Nit: Sort these?
| |
| 32 | 33 |
| 33 #include "SkFontHost.h" | 34 #include "SkFontHost.h" |
| 34 #include "SkStream.h" | 35 #include "SkStream.h" |
| 35 #include "SkFontHost_fontconfig_impl.h" | 36 #include "SkFontHost_fontconfig_impl.h" |
| 36 #include "SkFontHost_fontconfig_direct.h" | 37 #include "SkFontHost_fontconfig_direct.h" |
| 37 #include "SkFontHost_fontconfig_ipc.h" | 38 #include "SkFontHost_fontconfig_ipc.h" |
| 38 | 39 |
| 39 static FontConfigInterface* global_fc_impl = NULL; | 40 static FontConfigInterface* global_fc_impl = NULL; |
| 40 | 41 |
| 41 void SkiaFontConfigUseDirectImplementation() { | 42 void SkiaFontConfigUseDirectImplementation() { |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 228 // static | 229 // static |
| 229 uint32_t SkFontHost::NextLogicalFont(SkFontID fontID) { | 230 uint32_t SkFontHost::NextLogicalFont(SkFontID fontID) { |
| 230 // We don't handle font fallback, WebKit does. | 231 // We don't handle font fallback, WebKit does. |
| 231 return 0; | 232 return 0; |
| 232 } | 233 } |
| 233 | 234 |
| 234 /////////////////////////////////////////////////////////////////////////////// | 235 /////////////////////////////////////////////////////////////////////////////// |
| 235 | 236 |
| 236 class SkFileDescriptorStream : public SkStream { | 237 class SkFileDescriptorStream : public SkStream { |
| 237 public: | 238 public: |
| 238 SkFileDescriptorStream(int fd) | 239 SkFileDescriptorStream(int fd) { |
| 239 : fd_(fd) { | 240 memory_ = NULL; |
| 241 offset_ = 0; | |
| 242 | |
| 243 struct stat st; | |
| 244 if (fstat(fd, &st)) | |
| 245 return; | |
| 246 | |
| 247 void* memory = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); | |
| 248 if (memory == MAP_FAILED) | |
| 249 return; | |
| 250 | |
| 251 memory_ = reinterpret_cast<uint8_t*>(memory); | |
| 252 length_ = st.st_size; | |
| 240 } | 253 } |
| 241 | 254 |
| 242 ~SkFileDescriptorStream() { | 255 ~SkFileDescriptorStream() { |
| 243 close(fd_); | 256 munmap(const_cast<uint8_t*>(memory_), length_); |
| 244 } | 257 } |
| 245 | 258 |
| 246 virtual bool rewind() { | 259 virtual bool rewind() { |
| 247 if (lseek(fd_, 0, SEEK_SET) == -1) | 260 offset_ = 0; |
| 248 return false; | |
| 249 return true; | 261 return true; |
| 250 } | 262 } |
| 251 | 263 |
| 252 // SkStream implementation. | 264 // SkStream implementation. |
| 253 virtual size_t read(void* buffer, size_t size) { | 265 virtual size_t read(void* buffer, size_t size) { |
| 254 if (!buffer && !size) { | 266 if (!buffer && !size) { |
| 255 // This is request for the length of the stream. | 267 // This is request for the length of the stream. |
| 256 struct stat st; | 268 return length_; |
| 257 if (fstat(fd_, &st) == -1) | |
| 258 return 0; | |
| 259 return st.st_size; | |
| 260 } | 269 } |
| 261 | 270 |
| 262 if (!buffer) { | 271 if (!buffer) { |
| 263 // This is a request to skip bytes. | 272 // This is a request to skip bytes. |
| 264 const off_t current_position = lseek(fd_, 0, SEEK_CUR); | 273 if (offset_ + size < offset_) |
| 265 if (current_position == -1) | 274 return offset_; |
| 266 return 0; | 275 offset_ += size; |
| 267 const off_t new_position = lseek(fd_, size, SEEK_CUR); | 276 if (offset_ > length_) |
| 268 if (new_position == -1) | 277 offset_ = length_; |
| 269 return 0; | 278 return offset_; |
| 270 if (new_position < current_position) { | |
| 271 lseek(fd_, current_position, SEEK_SET); | |
| 272 return 0; | |
| 273 } | |
| 274 return new_position; | |
| 275 } | 279 } |
| 276 | 280 |
| 277 // This is a request to read bytes. | 281 size_t remaining = length_ - offset_; |
| 278 return ::read(fd_, buffer, size); | 282 if (size > remaining) |
| 283 size = remaining; | |
| 284 memcpy(buffer, memory_ + offset_, size); | |
| 285 offset_ += size; | |
| 286 return size; | |
| 287 } | |
| 288 | |
| 289 virtual const void* getMemoryBase() { | |
| 290 return memory_; | |
| 279 } | 291 } |
| 280 | 292 |
| 281 private: | 293 private: |
| 282 const int fd_; | 294 const uint8_t* memory_; |
| 295 size_t offset_, length_; | |
| 283 }; | 296 }; |
| 284 | 297 |
| 285 /////////////////////////////////////////////////////////////////////////////// | 298 /////////////////////////////////////////////////////////////////////////////// |
| 286 | 299 |
| 287 // static | 300 // static |
| 288 SkStream* SkFontHost::OpenStream(uint32_t id) | 301 SkStream* SkFontHost::OpenStream(uint32_t id) |
| 289 { | 302 { |
| 290 const unsigned fileid = UniqueIdToFileId(id); | 303 const unsigned fileid = UniqueIdToFileId(id); |
| 291 | 304 |
| 292 if (IsRemoteFont(fileid)) { | 305 if (IsRemoteFont(fileid)) { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 308 return SkNEW_ARGS(SkFileDescriptorStream, (fd)); | 321 return SkNEW_ARGS(SkFileDescriptorStream, (fd)); |
| 309 } | 322 } |
| 310 | 323 |
| 311 size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar) | 324 size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar) |
| 312 { | 325 { |
| 313 if (sizeAllocatedSoFar > kFontCacheMemoryBudget) | 326 if (sizeAllocatedSoFar > kFontCacheMemoryBudget) |
| 314 return sizeAllocatedSoFar - kFontCacheMemoryBudget; | 327 return sizeAllocatedSoFar - kFontCacheMemoryBudget; |
| 315 else | 328 else |
| 316 return 0; // nothing to do | 329 return 0; // nothing to do |
| 317 } | 330 } |
| OLD | NEW |