summaryrefslogtreecommitdiff
path: root/screen.c
blob: 5a36f9c9bf2c80fb32598ebcc7e01ffe69bb5806 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* Definitions for front buffer */

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

#include <nouveau_drm.h>
#include <nouveau_drmif.h>
#include <nouveau_bo.h>

#include "screen.h"
#include "object.h"

/* Global variables */

int screen_width;
int screen_height;
int screen_bpp;
int screen_offset;

int screen_pitch;

int viewport_x=32, viewport_y=32;
int viewport_w=512, viewport_h=512;

int drm_fd = -1;
struct nouveau_device *dev = NULL;

/* Functions */

int screen_open(int width, int height, int bpp)
{
	int ret;

	drm_fd = drmOpen("nouveau", 0);
	if (drm_fd < 0) {
		drmError(drm_fd, __func__);
		fprintf(stderr, "failed to open drm\n");
		return 1;
	}

	ret = nouveau_device_open_existing(&dev, 0, drm_fd, 0);
	if (ret) {
		screen_close();
		return 1;
	}

	printf("screen: chipset: 0x%08x\n", dev->chipset);
	printf("screen: vm_vram_base: 0x%" PRIu64 "\n", dev->vm_vram_base);
	printf("screen: vm_vram_size: %d MB\n", (int) (dev->vm_vram_size >> 20));
	printf("screen: vm_gart_size: %d MB\n", (int) (dev->vm_gart_size >> 20));

	screen_width = width;
	screen_height = height;
	screen_bpp = bpp;
	/* NOTE: find vt console fb in dmesg log:
[    9.435846] [drm] nouveau 0000:01:00.0: allocated 1680x1050 fb: 0x49000, bo ffff88007c0e8600
	*/
	screen_offset = 0x49000;

	screen_pitch = screen_width*(screen_bpp/8);
	if ((screen_pitch & 255)!=0) {
		screen_pitch = (screen_pitch | 255)+1;	/* Align on 256 bytes boundary */
	}

	printf("screen: %dx%dx%d, pitch=%d, offset=0x%08x\n",
		screen_width, screen_height, screen_bpp,
		screen_pitch, screen_offset
	);

	/* TODO: if running with X, try to find front buffer offset and resolution */

	return 0;
}

void screen_close(void)
{
	if (dev) {
		nouveau_device_close(&dev);
		dev = NULL;
	}
	if (drm_fd>=0) {
		drmClose(drm_fd);
		drm_fd = -1;
	}
}

struct nouveau_bo *screen_allocmem(int size)
{
	struct nouveau_bo *bo = NULL;
	uint32_t flags = NOUVEAU_BO_MAP;
	int ret;

	ret = nouveau_bo_new(dev, flags, 0x100, size, &bo);
	if (ret)
		return NULL;

	ret = nouveau_bo_map(bo, NOUVEAU_BO_WR);
	if (ret) {
		printf("screen: map failed: %d\n", ret);
		nouveau_bo_ref(NULL, &bo);
		return NULL;
	}

	return bo;
}