1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (c) 2012-2015 Etnaviv Project
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sub license,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
12bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
13bf215546Sopenharmony_ci * of the Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Wladimir J. van der Laan <laanwj@gmail.com>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#ifndef H_ETNAVIV_RESOURCE
28bf215546Sopenharmony_ci#define H_ETNAVIV_RESOURCE
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "etnaviv_internal.h"
31bf215546Sopenharmony_ci#include "etnaviv_tiling.h"
32bf215546Sopenharmony_ci#include "pipe/p_state.h"
33bf215546Sopenharmony_ci#include "util/format/u_format.h"
34bf215546Sopenharmony_ci#include "util/list.h"
35bf215546Sopenharmony_ci#include "util/hash_table.h"
36bf215546Sopenharmony_ci#include "util/u_helpers.h"
37bf215546Sopenharmony_ci#include "util/u_range.h"
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_cistruct etna_context;
40bf215546Sopenharmony_cistruct pipe_screen;
41bf215546Sopenharmony_cistruct util_dynarray;
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_cistruct etna_resource_level {
44bf215546Sopenharmony_ci   unsigned width, padded_width; /* in pixels */
45bf215546Sopenharmony_ci   unsigned height, padded_height; /* in samples */
46bf215546Sopenharmony_ci   unsigned depth;
47bf215546Sopenharmony_ci   unsigned offset; /* offset into memory area */
48bf215546Sopenharmony_ci   uint32_t stride; /* row stride */
49bf215546Sopenharmony_ci   uint32_t layer_stride; /* layer stride */
50bf215546Sopenharmony_ci   unsigned size; /* total size of memory area */
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci   uint32_t ts_offset;
53bf215546Sopenharmony_ci   uint32_t ts_layer_stride;
54bf215546Sopenharmony_ci   uint32_t ts_size;
55bf215546Sopenharmony_ci   uint64_t clear_value; /* clear value of resource level (mainly for TS) */
56bf215546Sopenharmony_ci   bool ts_valid;
57bf215546Sopenharmony_ci   uint8_t ts_mode;
58bf215546Sopenharmony_ci   int8_t ts_compress_fmt; /* COLOR_COMPRESSION_FORMAT_* (-1 = disable) */
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci   /* keep track if we have done some per block patching */
61bf215546Sopenharmony_ci   bool patched;
62bf215546Sopenharmony_ci   struct util_dynarray *patch_offsets;
63bf215546Sopenharmony_ci};
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci/* status of queued up but not flushed reads and write operations.
66bf215546Sopenharmony_ci * In _transfer_map() we need to know if queued up rendering needs
67bf215546Sopenharmony_ci * to be flushed to preserve the order of cpu and gpu access. */
68bf215546Sopenharmony_cienum etna_resource_status {
69bf215546Sopenharmony_ci   ETNA_PENDING_WRITE = 0x01,
70bf215546Sopenharmony_ci   ETNA_PENDING_READ = 0x02,
71bf215546Sopenharmony_ci};
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_cistruct etna_resource {
74bf215546Sopenharmony_ci   struct pipe_resource base;
75bf215546Sopenharmony_ci   struct renderonly_scanout *scanout;
76bf215546Sopenharmony_ci   uint32_t seqno;
77bf215546Sopenharmony_ci   uint32_t flush_seqno;
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci   /* only lod 0 used for non-texture buffers */
80bf215546Sopenharmony_ci   /* Layout for surface (tiled, multitiled, split tiled, ...) */
81bf215546Sopenharmony_ci   enum etna_surface_layout layout;
82bf215546Sopenharmony_ci   /* Horizontal alignment for texture unit (TEXTURE_HALIGN_*) */
83bf215546Sopenharmony_ci   unsigned halign;
84bf215546Sopenharmony_ci   struct etna_bo *bo; /* Surface video memory */
85bf215546Sopenharmony_ci   struct etna_bo *ts_bo; /* Tile status video memory */
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci   struct etna_resource_level levels[ETNA_NUM_LOD];
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci   /* buffer range that has been initialized */
90bf215546Sopenharmony_ci   struct util_range valid_buffer_range;
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci   /* for when TE doesn't support the base layout */
93bf215546Sopenharmony_ci   struct pipe_resource *texture;
94bf215546Sopenharmony_ci   /* for when PE doesn't support the base layout */
95bf215546Sopenharmony_ci   struct pipe_resource *render;
96bf215546Sopenharmony_ci   /* frontend flushes resource via an explicit call to flush_resource */
97bf215546Sopenharmony_ci   bool explicit_flush;
98bf215546Sopenharmony_ci};
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci/* returns TRUE if a is newer than b */
101bf215546Sopenharmony_cistatic inline bool
102bf215546Sopenharmony_cietna_resource_newer(struct etna_resource *a, struct etna_resource *b)
103bf215546Sopenharmony_ci{
104bf215546Sopenharmony_ci   return (int)(a->seqno - b->seqno) > 0;
105bf215546Sopenharmony_ci}
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci/* returns TRUE if a is older than b */
108bf215546Sopenharmony_cistatic inline bool
109bf215546Sopenharmony_cietna_resource_older(struct etna_resource *a, struct etna_resource *b)
110bf215546Sopenharmony_ci{
111bf215546Sopenharmony_ci   return (int)(a->seqno - b->seqno) < 0;
112bf215546Sopenharmony_ci}
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci/* returns TRUE if a resource has a TS, and it is valid for at least one level */
115bf215546Sopenharmony_cibool
116bf215546Sopenharmony_cietna_resource_has_valid_ts(struct etna_resource *res);
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci/* returns TRUE if the resource needs a resolve to itself */
119bf215546Sopenharmony_cistatic inline bool
120bf215546Sopenharmony_cietna_resource_needs_flush(struct etna_resource *res)
121bf215546Sopenharmony_ci{
122bf215546Sopenharmony_ci   return etna_resource_has_valid_ts(res) && ((int)(res->seqno - res->flush_seqno) > 0);
123bf215546Sopenharmony_ci}
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci/* is the resource only used on the sampler? */
126bf215546Sopenharmony_cistatic inline bool
127bf215546Sopenharmony_cietna_resource_sampler_only(const struct pipe_resource *pres)
128bf215546Sopenharmony_ci{
129bf215546Sopenharmony_ci   return (pres->bind & (PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET |
130bf215546Sopenharmony_ci                         PIPE_BIND_DEPTH_STENCIL | PIPE_BIND_BLENDABLE)) ==
131bf215546Sopenharmony_ci          PIPE_BIND_SAMPLER_VIEW;
132bf215546Sopenharmony_ci}
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_cistatic inline bool
135bf215546Sopenharmony_cietna_resource_hw_tileable(bool use_blt, const struct pipe_resource *pres)
136bf215546Sopenharmony_ci{
137bf215546Sopenharmony_ci   if (use_blt)
138bf215546Sopenharmony_ci      return true;
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci   /* RS can only tile 16bpp or 32bpp formats */
141bf215546Sopenharmony_ci   return util_format_get_blocksize(pres->format) == 2 ||
142bf215546Sopenharmony_ci          util_format_get_blocksize(pres->format) == 4;
143bf215546Sopenharmony_ci}
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_cistatic inline struct etna_resource *
146bf215546Sopenharmony_cietna_resource(struct pipe_resource *p)
147bf215546Sopenharmony_ci{
148bf215546Sopenharmony_ci   return (struct etna_resource *)p;
149bf215546Sopenharmony_ci}
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_civoid
152bf215546Sopenharmony_cietna_resource_used(struct etna_context *ctx, struct pipe_resource *prsc,
153bf215546Sopenharmony_ci                   enum etna_resource_status status);
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_cistatic inline void
156bf215546Sopenharmony_ciresource_read(struct etna_context *ctx, struct pipe_resource *prsc)
157bf215546Sopenharmony_ci{
158bf215546Sopenharmony_ci   etna_resource_used(ctx, prsc, ETNA_PENDING_READ);
159bf215546Sopenharmony_ci}
160bf215546Sopenharmony_ci
161bf215546Sopenharmony_cistatic inline void
162bf215546Sopenharmony_ciresource_written(struct etna_context *ctx, struct pipe_resource *prsc)
163bf215546Sopenharmony_ci{
164bf215546Sopenharmony_ci   etna_resource_used(ctx, prsc, ETNA_PENDING_WRITE);
165bf215546Sopenharmony_ci}
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_cienum etna_resource_status
168bf215546Sopenharmony_cietna_resource_status(struct etna_context *ctx, struct etna_resource *res);
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci/* Allocate Tile Status for an etna resource.
171bf215546Sopenharmony_ci * Tile status is a cache of the clear status per tile. This means a smaller
172bf215546Sopenharmony_ci * surface has to be cleared which is faster.
173bf215546Sopenharmony_ci * This is also called "fast clear". */
174bf215546Sopenharmony_cibool
175bf215546Sopenharmony_cietna_screen_resource_alloc_ts(struct pipe_screen *pscreen,
176bf215546Sopenharmony_ci                              struct etna_resource *prsc);
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_cistruct pipe_resource *
179bf215546Sopenharmony_cietna_resource_alloc(struct pipe_screen *pscreen, unsigned layout,
180bf215546Sopenharmony_ci                    uint64_t modifier, const struct pipe_resource *templat);
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_civoid
183bf215546Sopenharmony_cietna_resource_screen_init(struct pipe_screen *pscreen);
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_ci#endif
186