+/*
+ * In some circumstances (such as running google-chrome) the state
+ * tracker may try to delete a resource view from a context different
+ * than when it was created. We don't want to do that.
+ *
+ * In that situation, st_texture_release_all_sampler_views() calls this
+ * function to transfer the sampler view reference to this context (expected
+ * to be the context which created the view.)
+ */
+void
+st_save_zombie_sampler_view(struct st_context *st,
+ struct pipe_sampler_view *view)
+{
+ struct st_zombie_sampler_view_node *entry;
+
+ assert(view->context == st->pipe);
+
+ entry = MALLOC_STRUCT(st_zombie_sampler_view_node);
+ if (!entry)
+ return;
+
+ entry->view = view;
+
+ /* We need a mutex since this function may be called from one thread
+ * while free_zombie_resource_views() is called from another.
+ */
+ simple_mtx_lock(&st->zombie_sampler_views.mutex);
+ LIST_ADDTAIL(&entry->node, &st->zombie_sampler_views.list.node);
+ simple_mtx_unlock(&st->zombie_sampler_views.mutex);
+}
+
+
+/*
+ * Since OpenGL shaders may be shared among contexts, we can wind up
+ * with variants of a shader created with different contexts.
+ * When we go to destroy a gallium shader, we want to free it with the
+ * same context that it was created with, unless the driver reports
+ * PIPE_CAP_SHAREABLE_SHADERS = TRUE.
+ */
+void
+st_save_zombie_shader(struct st_context *st,
+ enum pipe_shader_type type,
+ struct pipe_shader_state *shader)
+{
+ struct st_zombie_shader_node *entry;
+
+ /* we shouldn't be here if the driver supports shareable shaders */
+ assert(!st->has_shareable_shaders);
+
+ entry = MALLOC_STRUCT(st_zombie_shader_node);
+ if (!entry)
+ return;
+
+ entry->shader = shader;
+ entry->type = type;
+
+ /* We need a mutex since this function may be called from one thread
+ * while free_zombie_shaders() is called from another.
+ */
+ simple_mtx_lock(&st->zombie_shaders.mutex);
+ LIST_ADDTAIL(&entry->node, &st->zombie_shaders.list.node);
+ simple_mtx_unlock(&st->zombie_shaders.mutex);
+}
+
+
+/*
+ * Free any zombie sampler views that may be attached to this context.
+ */
+static void
+free_zombie_sampler_views(struct st_context *st)
+{
+ struct st_zombie_sampler_view_node *entry, *next;
+
+ if (LIST_IS_EMPTY(&st->zombie_sampler_views.list.node)) {
+ return;
+ }
+
+ simple_mtx_lock(&st->zombie_sampler_views.mutex);
+
+ LIST_FOR_EACH_ENTRY_SAFE(entry, next,
+ &st->zombie_sampler_views.list.node, node) {
+ LIST_DEL(&entry->node); // remove this entry from the list
+
+ assert(entry->view->context == st->pipe);
+ pipe_sampler_view_reference(&entry->view, NULL);
+
+ free(entry);
+ }
+
+ assert(LIST_IS_EMPTY(&st->zombie_sampler_views.list.node));
+
+ simple_mtx_unlock(&st->zombie_sampler_views.mutex);
+}
+
+
+/*
+ * Free any zombie shaders that may be attached to this context.
+ */
+static void
+free_zombie_shaders(struct st_context *st)
+{
+ struct st_zombie_shader_node *entry, *next;
+
+ if (LIST_IS_EMPTY(&st->zombie_shaders.list.node)) {
+ return;
+ }
+
+ simple_mtx_lock(&st->zombie_shaders.mutex);
+
+ LIST_FOR_EACH_ENTRY_SAFE(entry, next,
+ &st->zombie_shaders.list.node, node) {
+ LIST_DEL(&entry->node); // remove this entry from the list
+
+ switch (entry->type) {
+ case PIPE_SHADER_VERTEX:
+ cso_delete_vertex_shader(st->cso_context, entry->shader);
+ break;
+ case PIPE_SHADER_FRAGMENT:
+ cso_delete_fragment_shader(st->cso_context, entry->shader);
+ break;
+ case PIPE_SHADER_GEOMETRY:
+ cso_delete_geometry_shader(st->cso_context, entry->shader);
+ break;
+ case PIPE_SHADER_TESS_CTRL:
+ cso_delete_tessctrl_shader(st->cso_context, entry->shader);
+ break;
+ case PIPE_SHADER_TESS_EVAL:
+ cso_delete_tesseval_shader(st->cso_context, entry->shader);
+ break;
+ case PIPE_SHADER_COMPUTE:
+ cso_delete_compute_shader(st->cso_context, entry->shader);
+ break;
+ default:
+ unreachable("invalid shader type in free_zombie_shaders()");
+ }
+ free(entry);
+ }
+
+ assert(LIST_IS_EMPTY(&st->zombie_shaders.list.node));
+
+ simple_mtx_unlock(&st->zombie_shaders.mutex);
+}
+
+
+/*
+ * This function is called periodically to free any zombie objects
+ * which are attached to this context.
+ */
+void
+st_context_free_zombie_objects(struct st_context *st)
+{
+ free_zombie_sampler_views(st);
+ free_zombie_shaders(st);
+}
+
+