| Index: ui/wayland/wayland_shm_buffer.cc
|
| diff --git a/ui/wayland/wayland_shm_buffer.cc b/ui/wayland/wayland_shm_buffer.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..200f49c7f3396eec46c6e77cdfc477567b03f0ee
|
| --- /dev/null
|
| +++ b/ui/wayland/wayland_shm_buffer.cc
|
| @@ -0,0 +1,61 @@
|
| +// Copyright (c) 2011 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 "wayland_shm_buffer.h"
|
| +
|
| +#include <cairo.h>
|
| +#include <fcntl.h>
|
| +#include <stdlib.h>
|
| +#include <stdio.h>
|
| +#include <sys/mman.h>
|
| +#include <unistd.h>
|
| +#include <wayland-client.h>
|
| +
|
| +WaylandShmBuffer::WaylandShmBuffer(WaylandDisplay* display,
|
| + uint32_t width, uint32_t height)
|
| + : cairo_data_surface_(NULL) {
|
| + int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
|
| + int allocation = stride * height;
|
| +
|
| + char filename[] = "/tmp/wayland-shm-XXXXXX";
|
| + int fd = mkstemp(filename);
|
| + if (fd < 0) {
|
| + // Failed to open
|
| + fprintf(stderr, "Faield to open\n");
|
| + return;
|
| + }
|
| + if (ftruncate(fd, allocation) < 0) {
|
| + // ftruncate failed
|
| + fprintf(stderr, "Faield to ftruncate\n");
|
| + close(fd);
|
| + return;
|
| + }
|
| +
|
| + unsigned char* data = static_cast<unsigned char*>(
|
| + mmap(NULL, allocation, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
|
| + unlink(filename);
|
| +
|
| + if (data == MAP_FAILED) {
|
| + // mmap /dev/zero failed
|
| + fprintf(stderr, "Failed to mmap /dev/zero\n");
|
| + close(fd);
|
| + return;
|
| + }
|
| + cairo_data_surface_ = cairo_image_surface_create_for_data(
|
| + data, CAIRO_FORMAT_ARGB32, width, height, stride);
|
| + buffer_ = wl_shm_create_buffer(display->GetShm(), fd,
|
| + width, height, stride, display->GetVisual());
|
| + close(fd);
|
| +}
|
| +
|
| +WaylandShmBuffer::~WaylandShmBuffer() {
|
| + if (buffer_) {
|
| + wl_buffer_destroy(buffer_);
|
| + buffer_ = NULL;
|
| + }
|
| +}
|
| +
|
| +cairo_surface_t* WaylandShmBuffer::GetDataSurface() const {
|
| + return cairo_data_surface_;
|
| +}
|
|
|