1/*
2 * © Copyright2018-2019 Alyssa Rosenzweig
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 */
24
25
26#ifndef PAN_RESOURCE_H
27#define PAN_RESOURCE_H
28
29#include "pan_screen.h"
30#include "pan_minmax_cache.h"
31#include "pan_texture.h"
32#include "drm-uapi/drm.h"
33#include "util/u_range.h"
34
35#define LAYOUT_CONVERT_THRESHOLD 8
36#define PAN_MAX_BATCHES 32
37
38#define PAN_BIND_SHARED_MASK (PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT | \
39                              PIPE_BIND_SHARED)
40
41struct panfrost_resource {
42        struct pipe_resource base;
43        struct {
44                struct pipe_scissor_state extent;
45                struct {
46                        bool enable;
47                        unsigned stride;
48                        unsigned size;
49                        BITSET_WORD *data;
50                } tile_map;
51        } damage;
52
53        struct {
54                /** Number of batches accessing this resource. Used to check if
55                 * a resource is in use. */
56                _Atomic unsigned nr_users;
57
58                /** Number of batches writing this resource. Note that only one
59                 * batch per context may write a resource, so this is the
60                 * number of contexts that have an active writer. */
61                _Atomic unsigned nr_writers;
62        } track;
63
64        struct renderonly_scanout *scanout;
65
66        struct panfrost_resource *separate_stencil;
67
68        struct util_range valid_buffer_range;
69
70        /* Description of the resource layout */
71        struct pan_image image;
72
73        struct {
74                /* Is the checksum for this image valid? Implicitly refers to
75                 * the first slice; we only checksum non-mipmapped 2D images */
76                bool crc;
77
78                /* Has anything been written to this slice? */
79                BITSET_DECLARE(data, MAX_MIP_LEVELS);
80        } valid;
81
82        /* Whether the modifier can be changed */
83        bool modifier_constant;
84
85        /* Used to decide when to convert to another modifier */
86        uint16_t modifier_updates;
87
88        /* Do all pixels have the same stencil value? */
89        bool constant_stencil;
90
91        /* The stencil value if constant_stencil is set */
92        uint8_t stencil_value;
93
94        /* Cached min/max values for index buffers */
95        struct panfrost_minmax_cache *index_cache;
96};
97
98static inline struct panfrost_resource *
99pan_resource(struct pipe_resource *p)
100{
101        return (struct panfrost_resource *)p;
102}
103
104struct panfrost_transfer {
105        struct pipe_transfer base;
106        void *map;
107        struct {
108                struct pipe_resource *rsrc;
109                struct pipe_box box;
110        } staging;
111};
112
113static inline struct panfrost_transfer *
114pan_transfer(struct pipe_transfer *p)
115{
116        return (struct panfrost_transfer *)p;
117}
118
119void panfrost_resource_screen_init(struct pipe_screen *screen);
120
121void panfrost_resource_screen_destroy(struct pipe_screen *screen);
122
123void panfrost_resource_context_init(struct pipe_context *pctx);
124
125/* Blitting */
126
127void
128panfrost_blitter_save(struct panfrost_context *ctx, bool render_cond);
129
130void
131panfrost_blit(struct pipe_context *pipe,
132              const struct pipe_blit_info *info);
133
134void
135panfrost_resource_set_damage_region(struct pipe_screen *screen,
136                                    struct pipe_resource *res,
137                                    unsigned int nrects,
138                                    const struct pipe_box *rects);
139
140static inline enum mali_texture_dimension
141panfrost_translate_texture_dimension(enum pipe_texture_target t) {
142        switch (t)
143        {
144        case PIPE_BUFFER:
145        case PIPE_TEXTURE_1D:
146        case PIPE_TEXTURE_1D_ARRAY:
147                return MALI_TEXTURE_DIMENSION_1D;
148
149        case PIPE_TEXTURE_2D:
150        case PIPE_TEXTURE_2D_ARRAY:
151        case PIPE_TEXTURE_RECT:
152                return MALI_TEXTURE_DIMENSION_2D;
153
154        case PIPE_TEXTURE_3D:
155                return MALI_TEXTURE_DIMENSION_3D;
156
157        case PIPE_TEXTURE_CUBE:
158        case PIPE_TEXTURE_CUBE_ARRAY:
159                return MALI_TEXTURE_DIMENSION_CUBE;
160
161        default:
162                unreachable("Unknown target");
163        }
164}
165
166void
167pan_resource_modifier_convert(struct panfrost_context *ctx,
168                              struct panfrost_resource *rsrc,
169                              uint64_t modifier, const char *reason);
170
171void
172pan_legalize_afbc_format(struct panfrost_context *ctx,
173                         struct panfrost_resource *rsrc,
174                         enum pipe_format format);
175
176#endif /* PAN_RESOURCE_H */
177