aboutsummaryrefslogtreecommitdiffstats
path: root/framebuffer.c
blob: d0f9b4073c506370660bc52bda8a2a19b0926c4c (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* Copyright (C) 2007-2008 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
*/
#include "framebuffer.h"
#include <memory.h>
#include <stdlib.h>

typedef struct {
    /* client fields, these correspond to code that waits for updates before displaying them */
    /* at the moment, only one client is supported */
    void*                        fb_opaque;
    QFrameBufferUpdateFunc       fb_update;
    QFrameBufferRotateFunc       fb_rotate;
    QFrameBufferDoneFunc         fb_done;

    void*                        pr_opaque;
    QFrameBufferCheckUpdateFunc  pr_check;
    QFrameBufferInvalidateFunc   pr_invalidate;
    QFrameBufferDetachFunc       pr_detach;

} QFrameBufferExtra;


static int
_get_pitch( int  width, QFrameBufferFormat  format )
{

    switch (format) {
        case QFRAME_BUFFER_RGB565:
            return width*2;
        default:
            return -1;
    }
}


int
qframebuffer_init( QFrameBuffer*       qfbuff,
                   int                 width,
                   int                 height,
                   int                 rotation,
                   QFrameBufferFormat  format )
{
    int   pitch;

    rotation &= 3;

    if (!qfbuff || width < 0 || height < 0)
        return -1;

    pitch = _get_pitch( width, format );
    if (pitch < 0)
        return -1;

    memset( qfbuff, 0, sizeof(*qfbuff) );

    qfbuff->extra = calloc( 1, sizeof(QFrameBufferExtra) );
    if (qfbuff->extra == NULL)
        return -1;

    qfbuff->pixels = calloc( pitch, height );
    if (qfbuff->pixels == NULL && (height > 0 && pitch > 0)) {
        free( qfbuff->extra );
        return -1;
    }

    qfbuff->width  = width;
    qfbuff->height = height;
    qfbuff->pitch  = pitch;
    qfbuff->format = format;

    qframebuffer_set_dpi( qfbuff, DEFAULT_FRAMEBUFFER_DPI, DEFAULT_FRAMEBUFFER_DPI );
    return 0;
}


void
qframebuffer_set_dpi( QFrameBuffer*   qfbuff,
                      int             x_dpi,
                      int             y_dpi )
{
    /* dpi = dots / inch
    ** inch = dots / dpi
    ** mm / 25.4 = dots / dpi
    ** mm = (dots * 25.4)/dpi
    */
    qfbuff->phys_width_mm  = (int)(0.5 + 25.4 * qfbuff->width  / x_dpi);
    qfbuff->phys_height_mm = (int)(0.5 + 25.4 * qfbuff->height / y_dpi);
}

/* alternative to qframebuffer_set_dpi where one can set the physical dimensions directly */
/* in millimeters. for the record 1 inch = 25.4 mm */
void
qframebuffer_set_mm( QFrameBuffer*   qfbuff,
                     int             width_mm,
                     int             height_mm )
{
    qfbuff->phys_width_mm  = width_mm;
    qfbuff->phys_height_mm = height_mm;
}

void
qframebuffer_update( QFrameBuffer*  qfbuff, int  x, int  y, int  w, int  h )
{
    QFrameBufferExtra*  extra = qfbuff->extra;

    if (extra->fb_update)
        extra->fb_update( extra->fb_opaque, x, y, w, h );
}


void
qframebuffer_add_client( QFrameBuffer*           qfbuff,
                         void*                   fb_opaque,
                         QFrameBufferUpdateFunc  fb_update,
                         QFrameBufferRotateFunc  fb_rotate,
                         QFrameBufferDoneFunc    fb_done )
{
    QFrameBufferExtra*  extra = qfbuff->extra;

    extra->fb_opaque = fb_opaque;
    extra->fb_update = fb_update;
    extra->fb_rotate = fb_rotate;
    extra->fb_done   = fb_done;
}

void
qframebuffer_set_producer( QFrameBuffer*                qfbuff,
                           void*                        opaque,
                           QFrameBufferCheckUpdateFunc  pr_check,
                           QFrameBufferInvalidateFunc   pr_invalidate,
                           QFrameBufferDetachFunc       pr_detach )
{
    QFrameBufferExtra*  extra = qfbuff->extra;

    extra->pr_opaque     = opaque;
    extra->pr_check      = pr_check;
    extra->pr_invalidate = pr_invalidate;
    extra->pr_detach     = pr_detach;
}


void
qframebuffer_rotate( QFrameBuffer*  qfbuff, int  rotation )
{
    QFrameBufferExtra*  extra = qfbuff->extra;

    if ((rotation ^ qfbuff->rotation) & 1) {
        /* swap width and height if new rotation requires it */
        int  temp = qfbuff->width;
        qfbuff->width  = qfbuff->height;
        qfbuff->height = temp;
        qfbuff->pitch  = _get_pitch( qfbuff->width, qfbuff->format );

        temp = qfbuff->phys_width_mm;
        qfbuff->phys_width_mm  = qfbuff->phys_height_mm;
        qfbuff->phys_height_mm = temp;
    }
    qfbuff->rotation = rotation;

    if (extra->fb_rotate)
        extra->fb_rotate( extra->fb_opaque, rotation );
}


extern void
qframebuffer_done( QFrameBuffer*   qfbuff )
{
    QFrameBufferExtra*  extra = qfbuff->extra;

    if (extra) {
        if (extra->pr_detach)
            extra->pr_detach( extra->pr_opaque );

        if (extra->fb_done)
            extra->fb_done( extra->fb_opaque );
    }

    free( qfbuff->pixels );
    free( qfbuff->extra );
    memset( qfbuff, 0, sizeof(*qfbuff) );
}


#define  MAX_FRAME_BUFFERS  8

static QFrameBuffer* framebuffer_fifo[ MAX_FRAME_BUFFERS ];
static int           framebuffer_fifo_rpos;
static int           framebuffer_fifo_count;

void
qframebuffer_fifo_add( QFrameBuffer*  qfbuff )
{
    if (framebuffer_fifo_count >= MAX_FRAME_BUFFERS)
        return;

    framebuffer_fifo[ framebuffer_fifo_count++ ] = qfbuff;
}


QFrameBuffer*
qframebuffer_fifo_get( void )
{
    if (framebuffer_fifo_rpos >= framebuffer_fifo_count)
        return NULL;

    return framebuffer_fifo[ framebuffer_fifo_rpos++ ];
}


void
qframebuffer_check_updates( void )
{
    int  nn;
    for (nn = 0; nn < framebuffer_fifo_count; nn++) {
        QFrameBuffer*       q     = framebuffer_fifo[nn];
        QFrameBufferExtra*  extra = q->extra;

        if (extra->pr_check)
            extra->pr_check( extra->pr_opaque );
    }
}

void
qframebuffer_invalidate_all( void )
{
    int  nn;
    for (nn = 0; nn < framebuffer_fifo_count; nn++) {
        QFrameBuffer*       q     = framebuffer_fifo[nn];
        QFrameBufferExtra*  extra = q->extra;

        if (extra->pr_invalidate)
            extra->pr_invalidate( extra->pr_opaque );
    }
}