1// Copyright 2017 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <dirent.h>
6#include <errno.h>
7#include <fcntl.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <unistd.h>
12
13#include <zircon/process.h>
14#include <zircon/syscalls.h>
15#include <zircon/types.h>
16
17#include <gfx/gfx.h>
18
19#include <lib/framebuffer/framebuffer.h>
20
21int main(int argc, char* argv[]) {
22    const char* err;
23    zx_status_t status = fb_bind(true, &err);
24    if (status != ZX_OK) {
25        printf("failed to open framebuffer: %d (%s)\n", status, err);
26        return -1;
27    }
28    uint32_t width;
29    uint32_t height;
30    uint32_t stride;
31    zx_pixel_format_t format;
32
33    fb_get_config(&width, &height, &stride, &format);
34
35    size_t size = stride * ZX_PIXEL_FORMAT_BYTES(format) * height;
36    uintptr_t fbo;
37    status = zx_vmar_map(zx_vmar_root_self(),
38                         ZX_VM_PERM_READ | ZX_VM_PERM_WRITE,
39                         0, fb_get_single_buffer(), 0, size, &fbo);
40    if (status < 0) {
41        printf("failed to map fb (%d)\n", status);
42        return -1;
43    }
44
45    gfx_surface* gfx = gfx_create_surface((void*)fbo, width, height, stride,
46                                          format, GFX_FLAG_FLUSH_CPU_CACHE);
47    if (!gfx) {
48        printf("failed to create gfx surface\n");
49        return -1;
50    }
51    gfx_fillrect(gfx, 0, 0, gfx->width, gfx->height, 0xffffffff);
52    gfx_flush(gfx);
53
54    double a,b, dx, dy, mag, c, ci;
55    uint32_t color,iter,x,y;
56
57    bool rotate = (gfx->height > gfx->width);
58
59    dx= 3.0/((double)gfx->width);
60    dy= 3.0/((double)gfx->height);
61    c = -2.0;
62    ci = -1.5;
63    for (y = 0; y < gfx->height; y++) {
64        if (rotate) {
65            ci = -1.5;
66        } else {
67            c = -2.0;
68        }
69        for (x = 0; x < gfx->width; x++) {
70            a=0;
71            b=0;
72            mag=0;
73            iter = 0;
74            while ((mag < 4.0) && (iter < 200) ){
75                double a1;
76                a1 = a*a - b*b + c;
77                b = 2.0 * a * b + ci;
78                a=a1;
79                mag = a*a + b*b;
80                iter++;
81            }
82            if (rotate) {
83                ci = ci + dx;
84            } else {
85                c = c + dx;
86            }
87            if (iter == 200) {
88                color = 0;
89            } else {
90                color = 0x231AF9 * iter;
91            }
92            color= color | 0xff000000;
93            gfx_putpixel(gfx, x, y, color);
94
95        }
96        if ((y%50) == 0)
97            gfx_flush(gfx);
98        if (rotate) {
99            c = c + dy;
100        } else {
101            ci = ci + dy;
102        }
103    }
104    gfx_flush(gfx);
105    zx_nanosleep(zx_deadline_after(ZX_SEC(10)));
106
107    gfx_surface_destroy(gfx);
108    fb_release();
109}
110