panfrost: Cleanup leak todos
[mesa.git] / src / gallium / drivers / panfrost / pan_job.c
1 /*
2 * Copyright (C) 2019 Alyssa Rosenzweig
3 * Copyright (C) 2014-2017 Broadcom
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 */
25
26 #include "pan_context.h"
27 #include "util/hash_table.h"
28 #include "util/ralloc.h"
29
30 struct panfrost_job *
31 panfrost_create_job(struct panfrost_context *ctx)
32 {
33 struct panfrost_job *job = rzalloc(NULL, struct panfrost_job);
34
35 job->ctx = ctx;
36
37 job->bos = _mesa_set_create(job,
38 _mesa_hash_pointer,
39 _mesa_key_pointer_equal);
40
41 return job;
42 }
43
44 void
45 panfrost_free_job(struct panfrost_context *ctx, struct panfrost_job *job)
46 {
47 if (!job)
48 return;
49
50 set_foreach(job->bos, entry) {
51 struct panfrost_bo *bo = (struct panfrost_bo *)entry->key;
52 panfrost_bo_unreference(ctx->base.screen, bo);
53 }
54
55 _mesa_hash_table_remove_key(ctx->jobs, &job->key);
56
57 if (ctx->job == job)
58 ctx->job = NULL;
59
60 ralloc_free(job);
61 }
62
63 struct panfrost_job *
64 panfrost_get_job(struct panfrost_context *ctx,
65 struct pipe_surface **cbufs, struct pipe_surface *zsbuf)
66 {
67 /* Lookup the job first */
68
69 struct panfrost_job_key key = {
70 .cbufs = {
71 cbufs[0],
72 cbufs[1],
73 cbufs[2],
74 cbufs[3],
75 },
76 .zsbuf = zsbuf
77 };
78
79 struct hash_entry *entry = _mesa_hash_table_search(ctx->jobs, &key);
80
81 if (entry)
82 return entry->data;
83
84 /* Otherwise, let's create a job */
85
86 struct panfrost_job *job = panfrost_create_job(ctx);
87
88 /* Save the created job */
89
90 memcpy(&job->key, &key, sizeof(key));
91 _mesa_hash_table_insert(ctx->jobs, &job->key, job);
92
93 return job;
94 }
95
96 /* Get the job corresponding to the FBO we're currently rendering into */
97
98 struct panfrost_job *
99 panfrost_get_job_for_fbo(struct panfrost_context *ctx)
100 {
101 /* If we already began rendering, use that */
102
103 if (ctx->job)
104 return ctx->job;
105
106 /* If not, look up the job */
107
108 struct pipe_surface **cbufs = ctx->pipe_framebuffer.cbufs;
109 struct pipe_surface *zsbuf = ctx->pipe_framebuffer.zsbuf;
110 struct panfrost_job *job = panfrost_get_job(ctx, cbufs, zsbuf);
111
112 return job;
113 }
114
115 void
116 panfrost_job_add_bo(struct panfrost_job *job, struct panfrost_bo *bo)
117 {
118 if (!bo)
119 return;
120
121 if (_mesa_set_search(job->bos, bo))
122 return;
123
124 panfrost_bo_reference(bo);
125 _mesa_set_add(job->bos, bo);
126 }
127
128 void
129 panfrost_flush_jobs_writing_resource(struct panfrost_context *panfrost,
130 struct pipe_resource *prsc)
131 {
132 #if 0
133 struct hash_entry *entry = _mesa_hash_table_search(panfrost->write_jobs,
134 prsc);
135 if (entry) {
136 struct panfrost_job *job = entry->data;
137 panfrost_job_submit(panfrost, job);
138 }
139 #endif
140 /* TODO stub */
141 }
142
143 void
144 panfrost_flush_jobs_reading_resource(struct panfrost_context *panfrost,
145 struct pipe_resource *prsc)
146 {
147 struct panfrost_resource *rsc = pan_resource(prsc);
148
149 panfrost_flush_jobs_writing_resource(panfrost, prsc);
150
151 hash_table_foreach(panfrost->jobs, entry) {
152 struct panfrost_job *job = entry->data;
153
154 if (_mesa_set_search(job->bos, rsc->bo)) {
155 printf("TODO: submit job for flush\n");
156 //panfrost_job_submit(panfrost, job);
157 continue;
158 }
159 }
160 }
161
162 static bool
163 panfrost_job_compare(const void *a, const void *b)
164 {
165 return memcmp(a, b, sizeof(struct panfrost_job_key)) == 0;
166 }
167
168 static uint32_t
169 panfrost_job_hash(const void *key)
170 {
171 return _mesa_hash_data(key, sizeof(struct panfrost_job_key));
172 }
173
174 void
175 panfrost_job_init(struct panfrost_context *ctx)
176 {
177 /* TODO: Don't leak */
178 ctx->jobs = _mesa_hash_table_create(NULL,
179 panfrost_job_hash,
180 panfrost_job_compare);
181
182 ctx->write_jobs = _mesa_hash_table_create(NULL,
183 _mesa_hash_pointer,
184 _mesa_key_pointer_equal);
185
186 }