OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/wayland/wayland_shm_buffer.h" |
| 6 |
| 7 #include <cairo.h> |
| 8 #include <fcntl.h> |
| 9 #include <stdlib.h> |
| 10 #include <sys/mman.h> |
| 11 #include <unistd.h> |
| 12 #include <wayland-client.h> |
| 13 |
| 14 #include "base/logging.h" |
| 15 #include "ui/wayland/wayland_display.h" |
| 16 |
| 17 namespace ui { |
| 18 |
| 19 WaylandShmBuffer::WaylandShmBuffer(WaylandDisplay* display, |
| 20 uint32_t width, |
| 21 uint32_t height) |
| 22 : data_surface_(NULL) { |
| 23 int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width); |
| 24 int allocation = stride * height; |
| 25 |
| 26 char filename[] = "/tmp/wayland-shm-XXXXXX"; |
| 27 int fd = mkstemp(filename); |
| 28 if (fd < 0) { |
| 29 PLOG(ERROR) << "Failed to open"; |
| 30 return; |
| 31 } |
| 32 if (ftruncate(fd, allocation) < 0) { |
| 33 PLOG(ERROR) << "Failed to ftruncate"; |
| 34 close(fd); |
| 35 return; |
| 36 } |
| 37 |
| 38 unsigned char* data = static_cast<unsigned char*>( |
| 39 mmap(NULL, allocation, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)); |
| 40 unlink(filename); |
| 41 |
| 42 if (data == MAP_FAILED) { |
| 43 PLOG(ERROR) << "Failed to mmap /dev/zero"; |
| 44 close(fd); |
| 45 return; |
| 46 } |
| 47 data_surface_ = cairo_image_surface_create_for_data( |
| 48 data, CAIRO_FORMAT_ARGB32, width, height, stride); |
| 49 buffer_ = wl_shm_create_buffer(display->shm(), fd, |
| 50 width, height, stride, display->visual()); |
| 51 close(fd); |
| 52 } |
| 53 |
| 54 WaylandShmBuffer::~WaylandShmBuffer() { |
| 55 if (buffer_) { |
| 56 wl_buffer_destroy(buffer_); |
| 57 buffer_ = NULL; |
| 58 } |
| 59 if (data_surface_) { |
| 60 cairo_surface_destroy(data_surface_); |
| 61 data_surface_ = NULL; |
| 62 } |
| 63 } |
| 64 |
| 65 } // namespace ui |
OLD | NEW |