Initial version of libomxil-vc4 for RPI3
[platform/adaptation/broadcom/libomxil-vc4.git] / host_applications / linux / apps / hello_pi / hello_font / main.c
1 /*
2 Copyright (c) 2012, Broadcom Europe Ltd
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in the
11       documentation and/or other materials provided with the distribution.
12     * Neither the name of the copyright holder nor the
13       names of its contributors may be used to endorse or promote products
14       derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 // Test app for VG font library.
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <assert.h>
34 #include <unistd.h>
35
36 #include "bcm_host.h"
37 #include "vgfont.h"
38
39 static const char *strnchr(const char *str, size_t len, char c)
40 {
41    const char *e = str + len;
42    do {
43       if (*str == c) {
44          return str;
45       }
46    } while (++str < e);
47    return NULL;
48 }
49
50 int32_t render_subtitle(GRAPHICS_RESOURCE_HANDLE img, const char *text, const int skip, const uint32_t text_size, const uint32_t y_offset)
51 {
52    uint32_t text_length = strlen(text)-skip;
53    uint32_t width=0, height=0;
54    const char *split = text;
55    int32_t s=0;
56    int len = 0; // length of pre-subtitle
57    uint32_t img_w, img_h;
58
59    graphics_get_resource_size(img, &img_w, &img_h);
60
61    if (text_length==0)
62       return 0;
63    while (split[0]) {
64       s = graphics_resource_text_dimensions_ext(img, split, text_length-(split-text), &width, &height, text_size);
65       if (s != 0) return s;
66       if (width > img_w) {
67          const char *space = strnchr(split, text_length-(split-text), ' ');
68          if (!space) {
69             len = split+1-text;
70             split = split+1;
71          } else {
72             len = space-text;
73             split = space+1;
74          }
75       } else {
76          break;
77       }
78    }
79    // split now points to last line of text. split-text = length of initial text. text_length-(split-text) is length of last line
80    if (width) {
81       s = graphics_resource_render_text_ext(img, (img_w - width)>>1, y_offset-height,
82                                      GRAPHICS_RESOURCE_WIDTH,
83                                      GRAPHICS_RESOURCE_HEIGHT,
84                                      GRAPHICS_RGBA32(0xff,0xff,0xff,0xff), /* fg */
85                                      GRAPHICS_RGBA32(0,0,0,0x80), /* bg */
86                                      split, text_length-(split-text), text_size);
87       if (s!=0) return s;
88    }
89    return render_subtitle(img, text, skip+text_length-len, text_size, y_offset - height);
90 }
91
92 int main(void)
93 {
94    GRAPHICS_RESOURCE_HANDLE img;
95    uint32_t width, height;
96    int LAYER=1;
97    bcm_host_init();
98    int s;
99
100    s = gx_graphics_init(".");
101    assert(s == 0);
102
103    s = graphics_get_display_size(0, &width, &height);
104    assert(s == 0);
105
106    s = gx_create_window(0, width, height, GRAPHICS_RESOURCE_RGBA32, &img);
107    assert(s == 0);
108
109    // transparent before display to avoid screen flash
110    graphics_resource_fill(img, 0, 0, width, height, GRAPHICS_RGBA32(0,0,0,0x00));
111
112    graphics_display_resource(img, 0, LAYER, 0, 0, GRAPHICS_RESOURCE_WIDTH, GRAPHICS_RESOURCE_HEIGHT, VC_DISPMAN_ROT0, 1);
113
114    uint32_t text_size = 10;
115    while (1) {
116       const char *text = "The quick brown fox jumps over the lazy dog";
117       uint32_t y_offset = height-60+text_size/2;
118       graphics_resource_fill(img, 0, 0, width, height, GRAPHICS_RGBA32(0,0,0,0x00));
119       // blue, at the top (y=40)
120       graphics_resource_fill(img, 0, 40, width, 1, GRAPHICS_RGBA32(0,0,0xff,0xff));
121
122       // green, at the bottom (y=height-40)
123       graphics_resource_fill(img, 0, height-40, width, 1, GRAPHICS_RGBA32(0,0xff,0,0xff));
124
125       // draw the subtitle text
126       render_subtitle(img, text, 0, text_size,  y_offset);
127       graphics_update_displayed_resource(img, 0, 0, 0, 0);
128       text_size += 1;
129       if (text_size > 50)
130          text_size = 10;
131    }
132
133    graphics_display_resource(img, 0, LAYER, 0, 0, GRAPHICS_RESOURCE_WIDTH, GRAPHICS_RESOURCE_HEIGHT, VC_DISPMAN_ROT0, 0);
134    graphics_delete_resource(img);
135
136    return 0;
137 }
138