Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Side by Side Diff: ui/wayland/wayland_shm_buffer.cc

Issue 7457023: Adding a Wayland basic toolkit (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 <stdio.h>
11 #include <sys/mman.h>
12 #include <unistd.h>
13 #include <wayland-client.h>
14
15 WaylandShmBuffer::WaylandShmBuffer(WaylandDisplay* display,
16 uint32_t width, uint32_t height)
17 : cairo_data_surface_(NULL) {
18 int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
19 int allocation = stride * height;
20
21 char filename[] = "/tmp/wayland-shm-XXXXXX";
22 int fd = mkstemp(filename);
23 if (fd < 0) {
24 // Failed to open
25 fprintf(stderr, "Faield to open\n");
26 return;
27 }
28 if (ftruncate(fd, allocation) < 0) {
29 // ftruncate failed
30 fprintf(stderr, "Faield to ftruncate\n");
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 // mmap /dev/zero failed
41 fprintf(stderr, "Failed to mmap /dev/zero\n");
42 close(fd);
43 return;
44 }
45 cairo_data_surface_ = cairo_image_surface_create_for_data(
46 data, CAIRO_FORMAT_ARGB32, width, height, stride);
47 buffer_ = wl_shm_create_buffer(display->GetShm(), fd,
48 width, height, stride, display->GetVisual());
49 close(fd);
50 }
51
52 WaylandShmBuffer::~WaylandShmBuffer() {
53 if (buffer_) {
54 wl_buffer_destroy(buffer_);
55 buffer_ = NULL;
56 }
57 }
58
59 cairo_surface_t* WaylandShmBuffer::GetDataSurface() const {
60 return cairo_data_surface_;
61 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698