summaryrefslogtreecommitdiffstats
path: root/chrome/common/transport_dib_linux.cc
diff options
context:
space:
mode:
authoragl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-20 02:00:04 +0000
committeragl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-20 02:00:04 +0000
commite68e62fa169c45bd779bfe890aa4fcdaa24d267d (patch)
treeefdb18adec880e7f780d8cde4e12893d3a20234f /chrome/common/transport_dib_linux.cc
parent7fe07d0726bad485fa40150aa9f7cecb1318217e (diff)
downloadchromium_src-e68e62fa169c45bd779bfe890aa4fcdaa24d267d.zip
chromium_src-e68e62fa169c45bd779bfe890aa4fcdaa24d267d.tar.gz
chromium_src-e68e62fa169c45bd779bfe890aa4fcdaa24d267d.tar.bz2
Bitmap transport
This patch reworks bitmap transport on all platforms. Linux and Mac are switched from serialising bitmaps over the IPC channel to using shared memory. All platforms gain a shared memory mapping cache on the host side. The concept of a TransportDIB (device independent bitmap) is added to encapsulate most of the platform specifics. On Linux, we use SysV shared memory. This is because X shared pixmaps, which predate POSIX SHM, can only use SysV. By using SysV between renderer and browser, we open up the possibility to map the shared memory directly from the renderer to the X server. On Mac, we use POSIX shared memory. However, since this needs filesystem access and the Mac renderer is sandboxed from the filesystem, we add two new messages from renderer -> browser: The first, AllocTransportDIB, synchronously creates a transport DIB in the browser and passes a handle back to the renderer. The second, FreeTransportDIB, asynchronously, notifies the browser that it may close its handle to the shared memory region. On Mac, the shared memory regions are identified by their inode numbers on the wire. This means that the browser must keep handles open to all the allocated shared memory regions (since an inode number is insufficient to map the region). The alternative design is that the renderer passes the file descriptor with each paint operation. Since passing file descriptors is special case in the code, I felt that it would be best to minimise their use. Creating and freeing transport DIBs are relatively rare operations relative to paints and scrolls. On Windows, most of the code remains the same, except that Windows now uses the mapping cache added in this patch. This allows the browser to maintain a shared memory mapping for a transport DIB over several paints. Previously it mapped and unmapped for every operation, causing lots of TLB and VM churn. Review URL: http://codereview.chromium.org/21485 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10071 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/transport_dib_linux.cc')
-rw-r--r--chrome/common/transport_dib_linux.cc85
1 files changed, 85 insertions, 0 deletions
diff --git a/chrome/common/transport_dib_linux.cc b/chrome/common/transport_dib_linux.cc
new file mode 100644
index 0000000..adecf1d
--- /dev/null
+++ b/chrome/common/transport_dib_linux.cc
@@ -0,0 +1,85 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <errno.h>
+#include <stdlib.h>
+#include <sys/ipc.h>
+#include <sys/shm.h>
+
+#include "base/logging.h"
+#include "chrome/common/transport_dib.h"
+
+// The shmat system call uses this as it's invalid return address
+static void *const kInvalidAddress = (void*) -1;
+
+TransportDIB::TransportDIB()
+ : key_(-1),
+ address_(kInvalidAddress),
+ size_(0) {
+}
+
+TransportDIB::~TransportDIB() {
+ if (address_ != kInvalidAddress) {
+ shmdt(address_);
+ address_ = kInvalidAddress;
+ }
+}
+
+// static
+TransportDIB* TransportDIB::Create(size_t size, uint32 sequence_num) {
+ // We use a mode of 0666 since the X server won't attach to memory which is
+ // 0600 since it can't know if it (as a root process) is being asked to map
+ // someone else's private shared memory region.
+ const int shmkey = shmget(IPC_PRIVATE, size, 0666);
+ if (shmkey == -1) {
+ DLOG(ERROR) << "Failed to create SysV shared memory region"
+ << " errno:" << errno;
+ return false;
+ }
+
+ void* address = shmat(shmkey, NULL /* desired address */, 0 /* flags */);
+ // Here we mark the shared memory for deletion. Since we attached it in the
+ // line above, it doesn't actually get deleted but, if we crash, this means
+ // that the kernel will automatically clean it up for us.
+ shmctl(shmkey, IPC_RMID, 0);
+ if (address == kInvalidAddress)
+ return false;
+
+ TransportDIB* dib = new TransportDIB;
+
+ dib->key_ = shmkey;
+ dib->address_ = address;
+ dib->size_ = size;
+ return dib;
+}
+
+TransportDIB* TransportDIB::Map(Handle shmkey) {
+ struct shmid_ds shmst;
+ if (shmctl(shmkey, IPC_STAT, &shmst) == -1)
+ return NULL;
+
+ void* address = shmat(shmkey, NULL /* desired address */, 0 /* flags */);
+ if (address == kInvalidAddress)
+ return NULL;
+
+ TransportDIB* dib = new TransportDIB;
+
+ dib->address_ = address;
+ dib->size_ = shmst.shm_segsz;
+ dib->key_ = shmkey;
+ return dib;
+}
+
+void* TransportDIB::memory() const {
+ DCHECK_NE(address_, kInvalidAddress);
+ return address_;
+}
+
+TransportDIB::Id TransportDIB::id() const {
+ return key_;
+}
+
+TransportDIB::Handle TransportDIB::handle() const {
+ return key_;
+}