Changeset e0545de in mainline
- Timestamp:
- 2019-10-19T10:07:32Z (5 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- d18f3b7
- Parents:
- dcc4cb31
- Location:
- uspace
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/gfxdemo/gfxdemo.c
rdcc4cb31 re0545de 39 39 #include <fibril.h> 40 40 #include <guigfx/canvas.h> 41 #include <gfx/bitmap.h> 41 42 #include <gfx/color.h> 42 43 #include <gfx/render.h> 43 44 #include <io/console.h> 45 #include <io/pixelmap.h> 44 46 #include <stdlib.h> 45 47 #include <str.h> … … 49 51 * 50 52 * @param gc Graphic context 53 * @param w Width 54 * @param h Height 51 55 */ 52 56 static errno_t demo_rects(gfx_context_t *gc, int w, int h) … … 54 58 gfx_color_t *color = NULL; 55 59 gfx_rect_t rect; 56 int i ;57 errno_t rc; 58 59 while (true) {60 int i, j; 61 errno_t rc; 62 63 for (j = 0; j < 10; j++) { 60 64 rc = gfx_color_new_rgb_i16(rand() % 0x10000, rand() % 0x10000, 61 65 rand() % 0x10000, &color); … … 82 86 fibril_usleep(500 * 1000); 83 87 } 88 89 return EOK; 90 } 91 92 /** Run bitmap demo on a graphic context. 93 * 94 * @param gc Graphic context 95 * @param w Width 96 * @param h Height 97 */ 98 static errno_t demo_bitmap(gfx_context_t *gc, int w, int h) 99 { 100 gfx_bitmap_t *bitmap; 101 gfx_bitmap_params_t params; 102 gfx_bitmap_alloc_t alloc; 103 int i, j; 104 gfx_coord2_t offs; 105 gfx_rect_t srect; 106 errno_t rc; 107 pixelmap_t pixelmap; 108 109 params.rect.p0.x = 0; 110 params.rect.p0.y = 0; 111 params.rect.p1.x = w; 112 params.rect.p1.y = h; 113 114 rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap); 115 if (rc != EOK) 116 return rc; 117 118 rc = gfx_bitmap_get_alloc(bitmap, &alloc); 119 if (rc != EOK) 120 return rc; 121 122 /* Fill bitmap with something. In absence of anything else, use pixelmap */ 123 pixelmap.width = w; 124 pixelmap.height = h; 125 pixelmap.data = alloc.pixels; 126 127 for (i = 0; i < w; i++) { 128 for (j = 0; j < h; j++) { 129 pixelmap_put_pixel(&pixelmap, i, j, 130 PIXEL(255, (i % 30) < 3 ? 255 : 0, 131 (j % 30) < 3 ? 255 : 0, i / 2)); 132 } 133 } 134 135 for (j = 0; j < 10; j++) { 136 for (i = 0; i < 5; i++) { 137 srect.p0.x = rand() % (w - 40); 138 srect.p0.y = rand() % (h - 40); 139 srect.p1.x = srect.p0.x + rand() % (w - srect.p0.x); 140 srect.p1.y = srect.p0.y + rand() % (h - srect.p0.y); 141 offs.x = rand() % (w - srect.p1.x); 142 offs.y = rand() % (h - srect.p1.y); 143 offs.x = 0; 144 offs.y = 0; 145 146 gfx_bitmap_render(bitmap, &srect, &offs); 147 fibril_usleep(500 * 1000); 148 } 149 } 150 151 gfx_bitmap_destroy(bitmap); 152 153 return EOK; 154 } 155 156 /** Run demo loop on a graphic context. 157 * 158 * @param gc Graphic context 159 * @param w Width 160 * @param h Height 161 */ 162 static errno_t demo_loop(gfx_context_t *gc, int w, int h) 163 { 164 errno_t rc; 165 166 while (true) { 167 rc = demo_rects(gc, w, h); 168 if (rc != EOK) 169 return rc; 170 171 rc = demo_bitmap(gc, w, h); 172 if (rc != EOK) 173 return rc; 174 } 84 175 } 85 176 … … 104 195 gc = console_gc_get_ctx(cgc); 105 196 106 rc = demo_ rects(gc, 80, 25);197 rc = demo_loop(gc, 80, 25); 107 198 if (rc != EOK) 108 199 return rc; … … 168 259 gc = canvas_gc_get_ctx(cgc); 169 260 170 rc = demo_ rects(gc, 400, 300);261 rc = demo_loop(gc, 400, 300); 171 262 if (rc != EOK) 172 263 return rc; … … 206 297 } 207 298 208 rc = demo_ rects(gc, 400, 300);299 rc = demo_loop(gc, 400, 300); 209 300 if (rc != EOK) 210 301 return rc; -
uspace/lib/guigfx/private/canvas.h
rdcc4cb31 re0545de 40 40 #include <canvas.h> 41 41 #include <draw/surface.h> 42 #include <gfx/bitmap.h> 42 43 #include <gfx/context.h> 44 #include <gfx/coord.h> 43 45 #include <io/pixel.h> 44 46 … … 58 60 }; 59 61 62 /** Bitmap in canvas GC */ 63 typedef struct { 64 /** Containing canvas GC */ 65 struct canvas_gc *cgc; 66 /** Allocation info */ 67 gfx_bitmap_alloc_t alloc; 68 /** @c true if we allocated the bitmap, @c false if allocated by caller */ 69 bool myalloc; 70 /** Surface */ 71 surface_t *surface; 72 /** Rectangle covered by bitmap */ 73 gfx_rect_t rect; 74 } canvas_gc_bitmap_t; 75 60 76 #endif 61 77 -
uspace/lib/guigfx/src/canvas.c
rdcc4cb31 re0545de 37 37 */ 38 38 39 #include <draw/drawctx.h> 40 #include <draw/source.h> 39 41 #include <gfx/color.h> 40 42 #include <gfx/context.h> … … 43 45 #include <io/pixel.h> 44 46 #include <stdlib.h> 47 #include <transform.h> 45 48 #include "../private/canvas.h" 46 49 //#include "../../private/color.h" … … 48 51 static errno_t canvas_gc_set_color(void *, gfx_color_t *); 49 52 static errno_t canvas_gc_fill_rect(void *, gfx_rect_t *); 53 static errno_t canvas_gc_bitmap_create(void *, gfx_bitmap_params_t *, 54 gfx_bitmap_alloc_t *, void **); 55 static errno_t canvas_gc_bitmap_destroy(void *); 56 static errno_t canvas_gc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *); 57 static errno_t canvas_gc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *); 50 58 51 59 gfx_context_ops_t canvas_gc_ops = { 52 60 .set_color = canvas_gc_set_color, 53 .fill_rect = canvas_gc_fill_rect 61 .fill_rect = canvas_gc_fill_rect, 62 .bitmap_create = canvas_gc_bitmap_create, 63 .bitmap_destroy = canvas_gc_bitmap_destroy, 64 .bitmap_render = canvas_gc_bitmap_render, 65 .bitmap_get_alloc = canvas_gc_bitmap_get_alloc 54 66 }; 55 67 … … 163 175 } 164 176 177 /** Create bitmap in canvas GC. 178 * 179 * @param arg Canvas GC 180 * @param params Bitmap params 181 * @param alloc Bitmap allocation info or @c NULL 182 * @param rbm Place to store pointer to new bitmap 183 * @return EOK on success or an error code 184 */ 185 errno_t canvas_gc_bitmap_create(void *arg, gfx_bitmap_params_t *params, 186 gfx_bitmap_alloc_t *alloc, void **rbm) 187 { 188 canvas_gc_t *cgc = (canvas_gc_t *) arg; 189 canvas_gc_bitmap_t *cbm = NULL; 190 int w, h; 191 errno_t rc; 192 193 cbm = calloc(1, sizeof(canvas_gc_bitmap_t)); 194 if (cbm == NULL) 195 return ENOMEM; 196 197 w = params->rect.p1.x - params->rect.p0.x; 198 h = params->rect.p1.y - params->rect.p0.y; 199 cbm->rect = params->rect; 200 201 if (alloc == NULL) { 202 cbm->surface = surface_create(w, h, NULL, 0); 203 if (cbm->surface == NULL) { 204 rc = ENOMEM; 205 goto error; 206 } 207 208 cbm->alloc.pitch = w * sizeof(uint32_t); 209 cbm->alloc.off0 = 0; 210 cbm->alloc.pixels = surface_direct_access(cbm->surface); 211 cbm->myalloc = true; 212 } else { 213 cbm->surface = surface_create(w, h, alloc->pixels, 0); 214 if (cbm->surface == NULL) { 215 rc = ENOMEM; 216 goto error; 217 } 218 219 cbm->alloc = *alloc; 220 } 221 222 cbm->cgc = cgc; 223 *rbm = (void *)cbm; 224 return EOK; 225 error: 226 if (cbm != NULL) 227 free(cbm); 228 return rc; 229 } 230 231 /** Destroy bitmap in canvas GC. 232 * 233 * @param bm Bitmap 234 * @return EOK on success or an error code 235 */ 236 static errno_t canvas_gc_bitmap_destroy(void *bm) 237 { 238 canvas_gc_bitmap_t *cbm = (canvas_gc_bitmap_t *)bm; 239 if (cbm->myalloc) 240 surface_destroy(cbm->surface); 241 // XXX if !cbm->myalloc, surface is leaked - no way to destroy it 242 // without destroying the pixel buffer 243 free(cbm); 244 return EOK; 245 } 246 247 /** Render bitmap in canvas GC. 248 * 249 * @param bm Bitmap 250 * @param srect0 Source rectangle or @c NULL 251 * @param offs0 Offset or @c NULL 252 * @return EOK on success or an error code 253 */ 254 static errno_t canvas_gc_bitmap_render(void *bm, gfx_rect_t *srect0, 255 gfx_coord2_t *offs0) 256 { 257 canvas_gc_bitmap_t *cbm = (canvas_gc_bitmap_t *)bm; 258 gfx_rect_t srect; 259 gfx_rect_t drect; 260 gfx_coord2_t offs; 261 262 if (srect0 != NULL) 263 srect = *srect0; 264 else 265 srect = cbm->rect; 266 267 if (offs0 != NULL) { 268 offs = *offs0; 269 } else { 270 offs.x = 0; 271 offs.y = 0; 272 } 273 274 // XXX Add function to translate rectangle 275 drect.p0.x = srect.p0.x + offs.x; 276 drect.p0.y = srect.p0.y + offs.y; 277 drect.p1.x = srect.p1.x + offs.x; 278 drect.p1.y = srect.p1.y + offs.y; 279 280 transform_t transform; 281 transform_identity(&transform); 282 transform_translate(&transform, offs.x - cbm->rect.p0.x, 283 offs.y - cbm->rect.p0.y); 284 285 source_t source; 286 source_init(&source); 287 source_set_transform(&source, transform); 288 source_set_texture(&source, cbm->surface, 289 PIXELMAP_EXTEND_TRANSPARENT_BLACK); 290 291 drawctx_t drawctx; 292 drawctx_init(&drawctx, cbm->cgc->surface); 293 294 drawctx_set_source(&drawctx, &source); 295 drawctx_transfer(&drawctx, drect.p0.x, drect.p0.y, 296 drect.p1.x - drect.p0.x, drect.p1.y - drect.p0.y); 297 298 update_canvas(cbm->cgc->canvas, cbm->cgc->surface); 299 return EOK; 300 } 301 302 /** Get allocation info for bitmap in canvas GC. 303 * 304 * @param bm Bitmap 305 * @param alloc Place to store allocation info 306 * @return EOK on success or an error code 307 */ 308 static errno_t canvas_gc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc) 309 { 310 canvas_gc_bitmap_t *cbm = (canvas_gc_bitmap_t *)bm; 311 *alloc = cbm->alloc; 312 return EOK; 313 } 314 165 315 /** @} 166 316 */
Note:
See TracChangeset
for help on using the changeset viewer.