1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2014 Broadcom
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, sublicense,
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 next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * 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 NONINFRINGEMENT.  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 DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include <xf86drm.h>
25bf215546Sopenharmony_ci#include <err.h>
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "pipe/p_defines.h"
28bf215546Sopenharmony_ci#include "util/ralloc.h"
29bf215546Sopenharmony_ci#include "util/u_inlines.h"
30bf215546Sopenharmony_ci#include "util/u_memory.h"
31bf215546Sopenharmony_ci#include "util/u_blitter.h"
32bf215546Sopenharmony_ci#include "util/u_upload_mgr.h"
33bf215546Sopenharmony_ci#include "pipe/p_screen.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#include "vc4_screen.h"
36bf215546Sopenharmony_ci#include "vc4_context.h"
37bf215546Sopenharmony_ci#include "vc4_resource.h"
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_civoid
40bf215546Sopenharmony_civc4_flush(struct pipe_context *pctx)
41bf215546Sopenharmony_ci{
42bf215546Sopenharmony_ci        struct vc4_context *vc4 = vc4_context(pctx);
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci        hash_table_foreach(vc4->jobs, entry) {
45bf215546Sopenharmony_ci                struct vc4_job *job = entry->data;
46bf215546Sopenharmony_ci                vc4_job_submit(vc4, job);
47bf215546Sopenharmony_ci        }
48bf215546Sopenharmony_ci}
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_cistatic void
51bf215546Sopenharmony_civc4_pipe_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
52bf215546Sopenharmony_ci               unsigned flags)
53bf215546Sopenharmony_ci{
54bf215546Sopenharmony_ci        struct vc4_context *vc4 = vc4_context(pctx);
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci        vc4_flush(pctx);
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci        if (fence) {
59bf215546Sopenharmony_ci                struct pipe_screen *screen = pctx->screen;
60bf215546Sopenharmony_ci                int fd = -1;
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci                if (flags & PIPE_FLUSH_FENCE_FD) {
63bf215546Sopenharmony_ci                        /* The vc4_fence takes ownership of the returned fd. */
64bf215546Sopenharmony_ci                        drmSyncobjExportSyncFile(vc4->fd, vc4->job_syncobj,
65bf215546Sopenharmony_ci                                                 &fd);
66bf215546Sopenharmony_ci                }
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci                struct vc4_fence *f = vc4_fence_create(vc4->screen,
69bf215546Sopenharmony_ci                                                       vc4->last_emit_seqno,
70bf215546Sopenharmony_ci                                                       fd);
71bf215546Sopenharmony_ci                screen->fence_reference(screen, fence, NULL);
72bf215546Sopenharmony_ci                *fence = (struct pipe_fence_handle *)f;
73bf215546Sopenharmony_ci        }
74bf215546Sopenharmony_ci}
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci/* We can't flush the texture cache within rendering a tile, so we have to
77bf215546Sopenharmony_ci * flush all rendering to the kernel so that the next job reading from the
78bf215546Sopenharmony_ci * tile gets a flushed cache.
79bf215546Sopenharmony_ci */
80bf215546Sopenharmony_cistatic void
81bf215546Sopenharmony_civc4_texture_barrier(struct pipe_context *pctx, unsigned flags)
82bf215546Sopenharmony_ci{
83bf215546Sopenharmony_ci        vc4_flush(pctx);
84bf215546Sopenharmony_ci}
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_cistatic void
87bf215546Sopenharmony_civc4_set_debug_callback(struct pipe_context *pctx,
88bf215546Sopenharmony_ci                       const struct util_debug_callback *cb)
89bf215546Sopenharmony_ci{
90bf215546Sopenharmony_ci        struct vc4_context *vc4 = vc4_context(pctx);
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci        if (cb)
93bf215546Sopenharmony_ci                vc4->debug = *cb;
94bf215546Sopenharmony_ci        else
95bf215546Sopenharmony_ci                memset(&vc4->debug, 0, sizeof(vc4->debug));
96bf215546Sopenharmony_ci}
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_cistatic void
99bf215546Sopenharmony_civc4_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
100bf215546Sopenharmony_ci{
101bf215546Sopenharmony_ci        struct vc4_context *vc4 = vc4_context(pctx);
102bf215546Sopenharmony_ci        struct vc4_resource *rsc = vc4_resource(prsc);
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci        rsc->initialized_buffers = 0;
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci        struct hash_entry *entry = _mesa_hash_table_search(vc4->write_jobs,
107bf215546Sopenharmony_ci                                                           prsc);
108bf215546Sopenharmony_ci        if (!entry)
109bf215546Sopenharmony_ci                return;
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci        struct vc4_job *job = entry->data;
112bf215546Sopenharmony_ci        if (job->key.zsbuf && job->key.zsbuf->texture == prsc)
113bf215546Sopenharmony_ci                job->resolve &= ~(PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL);
114bf215546Sopenharmony_ci}
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_cistatic void
117bf215546Sopenharmony_civc4_context_destroy(struct pipe_context *pctx)
118bf215546Sopenharmony_ci{
119bf215546Sopenharmony_ci        struct vc4_context *vc4 = vc4_context(pctx);
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci        vc4_flush(pctx);
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci        if (vc4->blitter)
124bf215546Sopenharmony_ci                util_blitter_destroy(vc4->blitter);
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci        if (vc4->uploader)
127bf215546Sopenharmony_ci                u_upload_destroy(vc4->uploader);
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci        slab_destroy_child(&vc4->transfer_pool);
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci        pipe_surface_reference(&vc4->framebuffer.cbufs[0], NULL);
132bf215546Sopenharmony_ci        pipe_surface_reference(&vc4->framebuffer.zsbuf, NULL);
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci        if (vc4->yuv_linear_blit_vs)
135bf215546Sopenharmony_ci                pctx->delete_vs_state(pctx, vc4->yuv_linear_blit_vs);
136bf215546Sopenharmony_ci        if (vc4->yuv_linear_blit_fs_8bit)
137bf215546Sopenharmony_ci                pctx->delete_fs_state(pctx, vc4->yuv_linear_blit_fs_8bit);
138bf215546Sopenharmony_ci        if (vc4->yuv_linear_blit_fs_16bit)
139bf215546Sopenharmony_ci                pctx->delete_fs_state(pctx, vc4->yuv_linear_blit_fs_16bit);
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci        vc4_program_fini(pctx);
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci        if (vc4->screen->has_syncobj) {
144bf215546Sopenharmony_ci                drmSyncobjDestroy(vc4->fd, vc4->job_syncobj);
145bf215546Sopenharmony_ci                drmSyncobjDestroy(vc4->fd, vc4->in_syncobj);
146bf215546Sopenharmony_ci        }
147bf215546Sopenharmony_ci        if (vc4->in_fence_fd >= 0)
148bf215546Sopenharmony_ci                close(vc4->in_fence_fd);
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_ci        ralloc_free(vc4);
151bf215546Sopenharmony_ci}
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_cistruct pipe_context *
154bf215546Sopenharmony_civc4_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
155bf215546Sopenharmony_ci{
156bf215546Sopenharmony_ci        struct vc4_screen *screen = vc4_screen(pscreen);
157bf215546Sopenharmony_ci        struct vc4_context *vc4;
158bf215546Sopenharmony_ci        int err;
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ci        /* Prevent dumping of the shaders built during context setup. */
161bf215546Sopenharmony_ci        uint32_t saved_shaderdb_flag = vc4_debug & VC4_DEBUG_SHADERDB;
162bf215546Sopenharmony_ci        vc4_debug &= ~VC4_DEBUG_SHADERDB;
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_ci        vc4 = rzalloc(NULL, struct vc4_context);
165bf215546Sopenharmony_ci        if (!vc4)
166bf215546Sopenharmony_ci                return NULL;
167bf215546Sopenharmony_ci        struct pipe_context *pctx = &vc4->base;
168bf215546Sopenharmony_ci
169bf215546Sopenharmony_ci        vc4->screen = screen;
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci        pctx->screen = pscreen;
172bf215546Sopenharmony_ci        pctx->priv = priv;
173bf215546Sopenharmony_ci        pctx->destroy = vc4_context_destroy;
174bf215546Sopenharmony_ci        pctx->flush = vc4_pipe_flush;
175bf215546Sopenharmony_ci        pctx->set_debug_callback = vc4_set_debug_callback;
176bf215546Sopenharmony_ci        pctx->invalidate_resource = vc4_invalidate_resource;
177bf215546Sopenharmony_ci        pctx->texture_barrier = vc4_texture_barrier;
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ci        vc4_draw_init(pctx);
180bf215546Sopenharmony_ci        vc4_state_init(pctx);
181bf215546Sopenharmony_ci        vc4_program_init(pctx);
182bf215546Sopenharmony_ci        vc4_query_init(pctx);
183bf215546Sopenharmony_ci        vc4_resource_context_init(pctx);
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_ci        vc4->fd = screen->fd;
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci        err = vc4_job_init(vc4);
188bf215546Sopenharmony_ci        if (err)
189bf215546Sopenharmony_ci                goto fail;
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_ci        err = vc4_fence_context_init(vc4);
192bf215546Sopenharmony_ci        if (err)
193bf215546Sopenharmony_ci                goto fail;
194bf215546Sopenharmony_ci
195bf215546Sopenharmony_ci        slab_create_child(&vc4->transfer_pool, &screen->transfer_pool);
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_ci	vc4->uploader = u_upload_create_default(&vc4->base);
198bf215546Sopenharmony_ci	vc4->base.stream_uploader = vc4->uploader;
199bf215546Sopenharmony_ci	vc4->base.const_uploader = vc4->uploader;
200bf215546Sopenharmony_ci
201bf215546Sopenharmony_ci	vc4->blitter = util_blitter_create(pctx);
202bf215546Sopenharmony_ci        if (!vc4->blitter)
203bf215546Sopenharmony_ci                goto fail;
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_ci        vc4_debug |= saved_shaderdb_flag;
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_ci        vc4->sample_mask = (1 << VC4_MAX_SAMPLES) - 1;
208bf215546Sopenharmony_ci
209bf215546Sopenharmony_ci        return &vc4->base;
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_cifail:
212bf215546Sopenharmony_ci        pctx->destroy(pctx);
213bf215546Sopenharmony_ci        return NULL;
214bf215546Sopenharmony_ci}
215