summaryrefslogtreecommitdiffstats
path: root/tests/tegra/vic.c
blob: 4163e1846c323a5baf342618fac6f292d6da559b (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
/*
 * Copyright © 2018 NVIDIA Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include <errno.h>
#include <stdio.h> /* XXX remove */
#include <stdlib.h>

#include "util_math.h"

#include "tegra.h"
#include "host1x.h"
#include "vic.h"

#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

const struct vic_format_info *vic_format_get_info(unsigned int format)
{
    static const struct vic_format_info formats[] = {
        { .format = VIC_PIXEL_FORMAT_A8R8G8B8, .cpp = 4 },
    };
    unsigned int i;

    for (i = 0; i < ARRAY_SIZE(formats); i++) {
        if (formats[i].format == format)
            return &formats[i];
    }

    return 0;
}

int vic_image_new(struct vic *vic, unsigned int width, unsigned int height,
                  unsigned int format, unsigned int kind, uint32_t flags,
                  struct vic_image **imagep)
{
    const struct vic_format_info *info = vic_format_get_info(format);
    struct vic_image *image;
    int err;

    if (!info)
        return -EINVAL;

    image = calloc(1, sizeof(*image));
    if (!image)
        return -ENOMEM;

    if (kind == VIC_BLK_KIND_PITCH)
        image->align = 256;
    else
        image->align = 256; /* XXX */

    image->width = width;
    image->stride = ALIGN(width, image->align);
    image->pitch = image->stride * info->cpp;
    image->height = height;
    image->format = format;
    image->kind = kind;

    image->size = image->pitch * image->height;

    printf("image: %ux%u align: %zu stride: %u pitch: %u size: %zu\n",
           image->width, image->height, image->align, image->stride,
           image->pitch, image->size);

    err = drm_tegra_bo_new(vic->drm, 0, image->size, &image->bo);
    if (err < 0) {
        free(image);
        return err;
    }

    err = drm_tegra_channel_map(vic->channel, image->bo, flags, &image->map);
    if (err < 0) {
        drm_tegra_bo_unref(image->bo);
        free(image);
        return err;
    }

    *imagep = image;
    return 0;
}

void vic_image_free(struct vic_image *image)
{
    if (image) {
        drm_tegra_channel_unmap(image->map);
        drm_tegra_bo_unref(image->bo);
        free(image);
    }
}

void vic_image_dump(struct vic_image *image, FILE *fp)
{
    unsigned int i, j;
    void *ptr;
    int err;

    err = drm_tegra_bo_map(image->bo, &ptr);
    if (err < 0)
        return;

    for (j = 0; j < image->height; j++) {
        uint32_t *pixels = (uint32_t *)((unsigned long)ptr + j * image->pitch);

        printf("   ");

        for (i = 0; i < image->width; i++)
            printf(" %08x", pixels[i]);

        printf("\n");
    }

    drm_tegra_bo_unmap(image->bo);
}

/* from vic30.c */
int vic30_new(struct drm_tegra *drm, struct drm_tegra_channel *channel,
              struct vic **vicp);

/* from vic40.c */
int vic40_new(struct drm_tegra *drm, struct drm_tegra_channel *channel,
              struct vic **vicp);

/* from vic41.c */
int vic41_new(struct drm_tegra *drm, struct drm_tegra_channel *channel,
              struct vic **vicp);

/* from vic42.c */
int vic42_new(struct drm_tegra *drm, struct drm_tegra_channel *channel,
              struct vic **vicp);

int vic_new(struct drm_tegra *drm, struct drm_tegra_channel *channel,
            struct vic **vicp)
{
    unsigned int version;

    version = drm_tegra_channel_get_version(channel);

    switch (version) {
    case 0x40:
        return vic30_new(drm, channel, vicp);

    case 0x21:
        return vic40_new(drm, channel, vicp);

    case 0x18:
        return vic41_new(drm, channel, vicp);

    case 0x19:
        return vic42_new(drm, channel, vicp);
    }

    return -ENOTSUP;
}

void vic_free(struct vic *vic)
{
    if (vic)
        vic->ops->free(vic);
}

int vic_clear(struct vic *vic, struct vic_image *output, unsigned int alpha,
              unsigned int red, unsigned int green, unsigned int blue)
{
    return vic->ops->fill(vic, output, 0, 0, output->width - 1,
                          output->height - 1, alpha, red, green, blue);
}