summaryrefslogtreecommitdiffstats
path: root/skia/ext/SkFontHost_fontconfig.cpp
diff options
context:
space:
mode:
authoragl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-04 00:04:07 +0000
committeragl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-04 00:04:07 +0000
commit998e781dfa85253775ac18ed6e1334ee13928d24 (patch)
tree31a4b3fef07141649fc8cca48ae56a053aee31f4 /skia/ext/SkFontHost_fontconfig.cpp
parent085bdc928b2ecdac1148b14539e079a85e96f4c3 (diff)
downloadchromium_src-998e781dfa85253775ac18ed6e1334ee13928d24.zip
chromium_src-998e781dfa85253775ac18ed6e1334ee13928d24.tar.gz
chromium_src-998e781dfa85253775ac18ed6e1334ee13928d24.tar.bz2
Linux: Add support for chrooted renderers.
http://code.google.com/p/chromium/wiki/LinuxSandboxIPC Without filesystem access from the renderers, we need another way of dealing with fontconfig and font loading. This add support for: * An "SBX_D" environment variable in the renderers which is used to signal the end of dynamic linking so that the chroot can be enforced. * A sandbox_host process, running outside the sandbox, to deal with fontconfig requests from the renderers. See the wiki page for the reasoning behind making it a separate process. * A new, custom SkFontHost for Skia. Because this is Chrome specific, it will live outside the upstream Skia tree. This FontHost can be configured either to drive fontconfig directly (for the browser process and for any unsandboxed renderers) or to use an IPC system. Since the same SkFontHost has to be linked into both the browser and renderer (they are the same binary), this switch has to be made at run time. Sandbox IPC calls are rare (a couple of dozen at page load time) and add about 50us of overhead for each call. http://codereview.chromium.org/112074 BUG=8081 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@17575 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'skia/ext/SkFontHost_fontconfig.cpp')
-rw-r--r--skia/ext/SkFontHost_fontconfig.cpp236
1 files changed, 236 insertions, 0 deletions
diff --git a/skia/ext/SkFontHost_fontconfig.cpp b/skia/ext/SkFontHost_fontconfig.cpp
new file mode 100644
index 0000000..780bc60
--- /dev/null
+++ b/skia/ext/SkFontHost_fontconfig.cpp
@@ -0,0 +1,236 @@
+/* libs/graphics/ports/SkFontHost_fontconfig.cpp
+**
+** Copyright 2008, Google Inc.
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+** http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+
+// -----------------------------------------------------------------------------
+// This file provides implementations of the font resolution members of
+// SkFontHost by using the fontconfig[1] library. Fontconfig is usually found
+// on Linux systems and handles configuration, parsing and caching issues
+// involved with enumerating and matching fonts.
+//
+// [1] http://fontconfig.org
+// -----------------------------------------------------------------------------
+
+#include <map>
+#include <string>
+
+#include <unistd.h>
+#include <sys/stat.h>
+
+#include "SkFontHost.h"
+#include "SkStream.h"
+#include "SkFontHost_fontconfig_impl.h"
+#include "SkFontHost_fontconfig_direct.h"
+#include "SkFontHost_fontconfig_ipc.h"
+
+static FontConfigInterface* global_fc_impl = NULL;
+
+void SkiaFontConfigUseDirectImplementation() {
+ if (global_fc_impl)
+ delete global_fc_impl;
+ global_fc_impl = new FontConfigDirect;
+}
+
+void SkiaFontConfigUseIPCImplementation(int fd) {
+ if (global_fc_impl)
+ delete global_fc_impl;
+ global_fc_impl = new FontConfigIPC(fd);
+}
+
+static void init() __attribute__((constructor));
+static void init() {
+ SkiaFontConfigUseDirectImplementation();
+}
+
+static SkMutex global_fc_map_lock;
+static std::map<uint32_t, SkTypeface *> global_fc_typefaces;
+
+// This is the maximum size of the font cache.
+static const unsigned kFontCacheMemoryBudget = 2 * 1024 * 1024; // 2MB
+
+// UniqueIds are encoded as (fileid << 8) | style
+
+static unsigned UniqueIdToFileId(unsigned uniqueid)
+{
+ return uniqueid >> 8;
+}
+
+static SkTypeface::Style UniqueIdToStyle(unsigned uniqueid)
+{
+ return static_cast<SkTypeface::Style>(uniqueid & 0xff);
+}
+
+static unsigned FileIdAndStyleToUniqueId(unsigned fileid,
+ SkTypeface::Style style)
+{
+ SkASSERT(style & 0xff == style);
+ return (fileid << 8) | static_cast<int>(style);
+}
+
+class FontConfigTypeface : public SkTypeface {
+public:
+ FontConfigTypeface(Style style, uint32_t id)
+ : SkTypeface(style, id)
+ { }
+};
+
+// static
+SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
+ const char familyName[],
+ SkTypeface::Style style)
+{
+ std::string resolved_family_name;
+
+ if (familyFace) {
+ // Given the fileid we can ask fontconfig for the familyname of the
+ // font.
+ const unsigned fileid = UniqueIdToFileId(familyFace->uniqueID());
+ if (!global_fc_impl->Match(
+ &resolved_family_name, NULL, true /* fileid valid */, fileid, "",
+ -1, -1)) {
+ return NULL;
+ }
+ } else if (familyName) {
+ resolved_family_name = familyName;
+ } else {
+ return NULL;
+ }
+
+ const bool bold = style & SkTypeface::kBold;
+ const bool italic = style & SkTypeface::kItalic;
+ unsigned fileid;
+ if (!global_fc_impl->Match(NULL, &fileid, false, -1, /* no fileid */
+ resolved_family_name, bold, italic)) {
+ return NULL;
+ }
+
+ const unsigned id = FileIdAndStyleToUniqueId(fileid, style);
+ SkTypeface* typeface = SkNEW_ARGS(FontConfigTypeface, (style, id));
+
+ {
+ SkAutoMutexAcquire ac(global_fc_map_lock);
+ global_fc_typefaces[id] = typeface;
+ }
+
+ return typeface;
+}
+
+// static
+SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream)
+{
+ SkASSERT(!"SkFontHost::CreateTypefaceFromStream unimplemented");
+ return NULL;
+}
+
+// static
+SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[])
+{
+ SkASSERT(!"SkFontHost::CreateTypefaceFromFile unimplemented");
+ return NULL;
+}
+
+// static
+bool SkFontHost::ValidFontID(SkFontID uniqueID) {
+ SkAutoMutexAcquire ac(global_fc_map_lock);
+ return global_fc_typefaces.find(uniqueID) != global_fc_typefaces.end();
+}
+
+void SkFontHost::Serialize(const SkTypeface*, SkWStream*) {
+ SkASSERT(!"SkFontHost::Serialize unimplemented");
+}
+
+SkTypeface* SkFontHost::Deserialize(SkStream* stream) {
+ SkASSERT(!"SkFontHost::Deserialize unimplemented");
+ return NULL;
+}
+
+// static
+uint32_t SkFontHost::NextLogicalFont(SkFontID fontID) {
+ // We don't handle font fallback, WebKit does.
+ return 0;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+class SkFileDescriptorStream : public SkStream {
+ public:
+ SkFileDescriptorStream(int fd)
+ : fd_(fd) {
+ }
+
+ ~SkFileDescriptorStream() {
+ close(fd_);
+ }
+
+ virtual bool rewind() {
+ if (lseek(fd_, 0, SEEK_SET) == -1)
+ return false;
+ return true;
+ }
+
+ // SkStream implementation.
+ virtual size_t read(void* buffer, size_t size) {
+ if (!buffer && !size) {
+ // This is request for the length of the stream.
+ struct stat st;
+ if (fstat(fd_, &st) == -1)
+ return 0;
+ return st.st_size;
+ }
+
+ if (!buffer) {
+ // This is a request to skip bytes.
+ const off_t current_position = lseek(fd_, 0, SEEK_CUR);
+ if (current_position == -1)
+ return 0;
+ const off_t new_position = lseek(fd_, size, SEEK_CUR);
+ if (new_position == -1)
+ return 0;
+ if (new_position < current_position) {
+ lseek(fd_, current_position, SEEK_SET);
+ return 0;
+ }
+ return new_position;
+ }
+
+ // This is a request to read bytes.
+ return ::read(fd_, buffer, size);
+ }
+
+ private:
+ const int fd_;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+// static
+SkStream* SkFontHost::OpenStream(uint32_t id)
+{
+ const unsigned fileid = UniqueIdToFileId(id);
+ const int fd = global_fc_impl->Open(fileid);
+ if (fd < 0)
+ return NULL;
+
+ return SkNEW_ARGS(SkFileDescriptorStream, (fd));
+}
+
+size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar)
+{
+ if (sizeAllocatedSoFar > kFontCacheMemoryBudget)
+ return sizeAllocatedSoFar - kFontCacheMemoryBudget;
+ else
+ return 0; // nothing to do
+}