summaryrefslogtreecommitdiff
path: root/src/surface/ram.c
blob: da769b6e40b4a848dcd901ffbae17d38b2f8c16e (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Copyright 2009 Vincent Sanders <vince@simtec.co.uk>
 *
 * This file is part of libnsfb, http://www.netsurf-browser.org/
 * Licenced under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 */

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include "libnsfb.h"
#include "libnsfb_plot.h"
#include "libnsfb_event.h"

#include "nsfb.h"
#include "surface.h"
#include "plot.h"

#define UNUSED(x) ((x) = (x))

static int ram_defaults(nsfb_t *nsfb)
{
    nsfb->width = 0;
    nsfb->height = 0;
    nsfb->format = NSFB_FMT_ABGR8888;

    /* select default sw plotters for bpp */
    select_plotters(nsfb);

    return 0;
}


static int ram_initialise(nsfb_t *nsfb)
{
    size_t size;
    uint8_t *fbptr;

    size = (nsfb->width * nsfb->height * nsfb->bpp) / 8;
    fbptr = realloc(nsfb->ptr, size);
    if (fbptr == NULL) {
        return -1;
    }

    nsfb->ptr = fbptr;
    nsfb->linelen = (nsfb->width * nsfb->bpp) / 8;

    return 0;
}

static int
ram_set_geometry(nsfb_t *nsfb, int width, int height, enum nsfb_format_e format)
{
    int startsize; 
    int endsize;

    int prev_width;
    int prev_height;
    enum nsfb_format_e prev_format;

    prev_width = nsfb->width;
    prev_height = nsfb->height;
    prev_format = nsfb->format;

    startsize = (nsfb->width * nsfb->height * nsfb->bpp) / 8;

    if (width > 0) {
	nsfb->width = width;
    }

    if (height > 0) {
	nsfb->height = height;
    }

    if (format != NSFB_FMT_ANY) {
	nsfb->format = format;
    }

    /* select soft plotters appropriate for format */
    select_plotters(nsfb);

    /* reallocate surface memory if necessary */
    endsize = (nsfb->width * nsfb->height * nsfb->bpp) / 8;
    if ((nsfb->ptr != NULL) && (startsize != endsize)) {
        uint8_t *fbptr;
        fbptr = realloc(nsfb->ptr, endsize);
        if (fbptr == NULL) {
            /* allocation failed so put everything back as it was */
            nsfb->width = prev_width;
            nsfb->height = prev_height;
            nsfb->format = prev_format;
            select_plotters(nsfb);

            return -1;
        }
        nsfb->ptr = fbptr;
    }

    nsfb->linelen = (nsfb->width * nsfb->bpp) / 8;

    return 0;
}


static int ram_finalise(nsfb_t *nsfb)
{
    free(nsfb->ptr);

    return 0;
}

static bool ram_input(nsfb_t *nsfb, nsfb_event_t *event, int timeout)
{
    UNUSED(nsfb);
    UNUSED(event);
    UNUSED(timeout);
    return false;
}

const nsfb_surface_rtns_t ram_rtns = {
    .defaults = ram_defaults,
    .initialise = ram_initialise,
    .finalise = ram_finalise,
    .input = ram_input,
    .geometry = ram_set_geometry,
};

NSFB_SURFACE_DEF(ram, NSFB_SURFACE_RAM, &ram_rtns)

/*
 * Local variables:
 *  c-basic-offset: 4
 *  tab-width: 8
 * End:
 */