1bf215546Sopenharmony_ci/************************************************************************** 2bf215546Sopenharmony_ci * 3bf215546Sopenharmony_ci * Copyright 2009 VMware, Inc. 4bf215546Sopenharmony_ci * All Rights Reserved. 5bf215546Sopenharmony_ci * 6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the 8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to 11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 12bf215546Sopenharmony_ci * the following conditions: 13bf215546Sopenharmony_ci * 14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the 15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions 16bf215546Sopenharmony_ci * of the Software. 17bf215546Sopenharmony_ci * 18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25bf215546Sopenharmony_ci * 26bf215546Sopenharmony_ci **************************************************************************/ 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_ci/** 30bf215546Sopenharmony_ci * Scene queue. We'll use two queues. One contains "full" scenes which 31bf215546Sopenharmony_ci * are produced by the "setup" code. The other contains "empty" scenes 32bf215546Sopenharmony_ci * which are produced by the "rast" code when it finishes rendering a scene. 33bf215546Sopenharmony_ci */ 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_ci#include "os/os_thread.h" 36bf215546Sopenharmony_ci#include "util/u_memory.h" 37bf215546Sopenharmony_ci#include "lp_scene_queue.h" 38bf215546Sopenharmony_ci#include "util/u_math.h" 39bf215546Sopenharmony_ci#include "lp_setup_context.h" 40bf215546Sopenharmony_ci 41bf215546Sopenharmony_ci 42bf215546Sopenharmony_ci#define SCENE_QUEUE_SIZE MAX_SCENES 43bf215546Sopenharmony_ci 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_ci/** 47bf215546Sopenharmony_ci * A queue of scenes 48bf215546Sopenharmony_ci */ 49bf215546Sopenharmony_cistruct lp_scene_queue 50bf215546Sopenharmony_ci{ 51bf215546Sopenharmony_ci struct lp_scene *scenes[SCENE_QUEUE_SIZE]; 52bf215546Sopenharmony_ci 53bf215546Sopenharmony_ci mtx_t mutex; 54bf215546Sopenharmony_ci cnd_t change; 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_ci /* These values wrap around, so that head == tail means empty. When used 57bf215546Sopenharmony_ci * to index the array, we use them modulo the queue size. This scheme 58bf215546Sopenharmony_ci * works because the queue size is a power of two. 59bf215546Sopenharmony_ci */ 60bf215546Sopenharmony_ci unsigned head; 61bf215546Sopenharmony_ci unsigned tail; 62bf215546Sopenharmony_ci}; 63bf215546Sopenharmony_ci 64bf215546Sopenharmony_ci 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_ci/** Allocate a new scene queue */ 67bf215546Sopenharmony_cistruct lp_scene_queue * 68bf215546Sopenharmony_cilp_scene_queue_create(void) 69bf215546Sopenharmony_ci{ 70bf215546Sopenharmony_ci /* Circular queue behavior depends on size being a power of two. */ 71bf215546Sopenharmony_ci STATIC_ASSERT(SCENE_QUEUE_SIZE > 0); 72bf215546Sopenharmony_ci STATIC_ASSERT((SCENE_QUEUE_SIZE & (SCENE_QUEUE_SIZE - 1)) == 0); 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_ci struct lp_scene_queue *queue = CALLOC_STRUCT(lp_scene_queue); 75bf215546Sopenharmony_ci 76bf215546Sopenharmony_ci if (!queue) 77bf215546Sopenharmony_ci return NULL; 78bf215546Sopenharmony_ci 79bf215546Sopenharmony_ci (void) mtx_init(&queue->mutex, mtx_plain); 80bf215546Sopenharmony_ci cnd_init(&queue->change); 81bf215546Sopenharmony_ci 82bf215546Sopenharmony_ci return queue; 83bf215546Sopenharmony_ci} 84bf215546Sopenharmony_ci 85bf215546Sopenharmony_ci 86bf215546Sopenharmony_ci/** Delete a scene queue */ 87bf215546Sopenharmony_civoid 88bf215546Sopenharmony_cilp_scene_queue_destroy(struct lp_scene_queue *queue) 89bf215546Sopenharmony_ci{ 90bf215546Sopenharmony_ci cnd_destroy(&queue->change); 91bf215546Sopenharmony_ci mtx_destroy(&queue->mutex); 92bf215546Sopenharmony_ci FREE(queue); 93bf215546Sopenharmony_ci} 94bf215546Sopenharmony_ci 95bf215546Sopenharmony_ci 96bf215546Sopenharmony_ci/** Remove first lp_scene from head of queue */ 97bf215546Sopenharmony_cistruct lp_scene * 98bf215546Sopenharmony_cilp_scene_dequeue(struct lp_scene_queue *queue, boolean wait) 99bf215546Sopenharmony_ci{ 100bf215546Sopenharmony_ci mtx_lock(&queue->mutex); 101bf215546Sopenharmony_ci 102bf215546Sopenharmony_ci if (wait) { 103bf215546Sopenharmony_ci /* Wait for queue to be not empty. */ 104bf215546Sopenharmony_ci while (queue->head == queue->tail) 105bf215546Sopenharmony_ci cnd_wait(&queue->change, &queue->mutex); 106bf215546Sopenharmony_ci } else { 107bf215546Sopenharmony_ci if (queue->head == queue->tail) { 108bf215546Sopenharmony_ci mtx_unlock(&queue->mutex); 109bf215546Sopenharmony_ci return NULL; 110bf215546Sopenharmony_ci } 111bf215546Sopenharmony_ci } 112bf215546Sopenharmony_ci 113bf215546Sopenharmony_ci struct lp_scene *scene = queue->scenes[queue->head++ % SCENE_QUEUE_SIZE]; 114bf215546Sopenharmony_ci 115bf215546Sopenharmony_ci cnd_signal(&queue->change); 116bf215546Sopenharmony_ci mtx_unlock(&queue->mutex); 117bf215546Sopenharmony_ci 118bf215546Sopenharmony_ci return scene; 119bf215546Sopenharmony_ci} 120bf215546Sopenharmony_ci 121bf215546Sopenharmony_ci 122bf215546Sopenharmony_ci/** Add an lp_scene to tail of queue */ 123bf215546Sopenharmony_civoid 124bf215546Sopenharmony_cilp_scene_enqueue(struct lp_scene_queue *queue, struct lp_scene *scene) 125bf215546Sopenharmony_ci{ 126bf215546Sopenharmony_ci mtx_lock(&queue->mutex); 127bf215546Sopenharmony_ci 128bf215546Sopenharmony_ci /* Wait for free space. */ 129bf215546Sopenharmony_ci while (queue->tail - queue->head >= SCENE_QUEUE_SIZE) 130bf215546Sopenharmony_ci cnd_wait(&queue->change, &queue->mutex); 131bf215546Sopenharmony_ci 132bf215546Sopenharmony_ci queue->scenes[queue->tail++ % SCENE_QUEUE_SIZE] = scene; 133bf215546Sopenharmony_ci 134bf215546Sopenharmony_ci cnd_signal(&queue->change); 135bf215546Sopenharmony_ci mtx_unlock(&queue->mutex); 136bf215546Sopenharmony_ci} 137