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 "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 "wayland_display.h" |
| 16 |
| 17 WaylandShmBuffer::WaylandShmBuffer(WaylandDisplay* display, |
| 18 uint32_t width, uint32_t height) |
| 19 : cairo_data_surface_(NULL) { |
| 20 int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width); |
| 21 int allocation = stride * height; |
| 22 |
| 23 char filename[] = "/tmp/wayland-shm-XXXXXX"; |
| 24 int fd = mkstemp(filename); |
| 25 if (fd < 0) { |
| 26 LOG(ERROR) << "Failed to open"; |
| 27 return; |
| 28 } |
| 29 if (ftruncate(fd, allocation) < 0) { |
| 30 LOG(ERROR) << "Failed to ftruncate"; |
| 31 close(fd); |
| 32 return; |
| 33 } |
| 34 |
| 35 unsigned char* data = static_cast<unsigned char*>( |
| 36 mmap(NULL, allocation, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)); |
| 37 unlink(filename); |
| 38 |
| 39 if (data == MAP_FAILED) { |
| 40 LOG(ERROR) << "Failed to mmap /dev/zero"; |
| 41 close(fd); |
| 42 return; |
| 43 } |
| 44 cairo_data_surface_ = cairo_image_surface_create_for_data( |
| 45 data, CAIRO_FORMAT_ARGB32, width, height, stride); |
| 46 buffer_ = wl_shm_create_buffer(display->GetShm(), fd, |
| 47 width, height, stride, display->GetVisual()); |
| 48 close(fd); |
| 49 } |
| 50 |
| 51 WaylandShmBuffer::~WaylandShmBuffer() { |
| 52 if (buffer_) { |
| 53 wl_buffer_destroy(buffer_); |
| 54 buffer_ = NULL; |
| 55 } |
| 56 if (cairo_data_surface_) { |
| 57 cairo_surface_destroy(cairo_data_surface_); |
| 58 cairo_data_surface_ = NULL; |
| 59 } |
| 60 } |
| 61 |
| 62 cairo_surface_t* WaylandShmBuffer::GetDataSurface() const { |
| 63 return cairo_data_surface_; |
| 64 } |
OLD | NEW |