+/**
+ * Flush context if necessary.
+ *
+ * Returns FALSE if it would have block, but do_not_block was set, TRUE
+ * otherwise.
+ *
+ * TODO: move this logic to an auxiliary library?
+ */
+boolean
+llvmpipe_flush_resource(struct pipe_context *pipe,
+ struct pipe_resource *resource,
+ unsigned level,
+ int layer,
+ unsigned flush_flags,
+ boolean read_only,
+ boolean cpu_access,
+ boolean do_not_block,
+ const char *reason)
+{
+ unsigned referenced;
+
+ referenced = pipe->is_resource_referenced(pipe, resource, level, layer);
+
+ if ((referenced & PIPE_REFERENCED_FOR_WRITE) ||
+ ((referenced & PIPE_REFERENCED_FOR_READ) && !read_only)) {
+
+ if (cpu_access) {
+ /*
+ * Flush and wait.
+ */
+ if (do_not_block)
+ return FALSE;
+
+ llvmpipe_finish(pipe, reason);
+ } else {
+ /*
+ * Just flush.
+ */
+
+ llvmpipe_flush(pipe, flush_flags, NULL, reason);
+ }
+ }
+
+ return TRUE;
+}