1/************************************************************************** 2 * 3 * Copyright 2007-2008 VMware, Inc. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28/** 29 * \file 30 * Buffer cache. 31 * 32 * \author Jose Fonseca <jfonseca-at-vmware-dot-com> 33 * \author Thomas Hellström <thellstom-at-vmware-dot-com> 34 */ 35 36 37#include "pipe/p_compiler.h" 38#include "util/u_debug.h" 39#include "os/os_thread.h" 40#include "util/u_memory.h" 41#include "util/list.h" 42#include "util/u_time.h" 43 44#include "pb_buffer.h" 45#include "pb_bufmgr.h" 46#include "pb_cache.h" 47 48 49/** 50 * Wrapper around a pipe buffer which adds delayed destruction. 51 */ 52struct pb_cache_buffer 53{ 54 struct pb_buffer base; 55 struct pb_buffer *buffer; 56 struct pb_cache_manager *mgr; 57 struct pb_cache_entry cache_entry; 58}; 59 60 61struct pb_cache_manager 62{ 63 struct pb_manager base; 64 struct pb_manager *provider; 65 struct pb_cache cache; 66}; 67 68 69static inline struct pb_cache_buffer * 70pb_cache_buffer(struct pb_buffer *buf) 71{ 72 assert(buf); 73 return (struct pb_cache_buffer *)buf; 74} 75 76 77static inline struct pb_cache_manager * 78pb_cache_manager(struct pb_manager *mgr) 79{ 80 assert(mgr); 81 return (struct pb_cache_manager *)mgr; 82} 83 84 85void 86pb_cache_manager_remove_buffer(struct pb_buffer *pb_buf) 87{ 88 struct pb_cache_buffer *buf = pb_cache_buffer(pb_buf); 89 90 /* the buffer won't be added if mgr is NULL */ 91 buf->mgr = NULL; 92} 93 94/** 95 * Actually destroy the buffer. 96 */ 97static void 98_pb_cache_buffer_destroy(struct pb_buffer *pb_buf) 99{ 100 struct pb_cache_buffer *buf = pb_cache_buffer(pb_buf); 101 102 assert(!pipe_is_referenced(&buf->base.reference)); 103 pb_reference(&buf->buffer, NULL); 104 FREE(buf); 105} 106 107 108static void 109pb_cache_buffer_destroy(struct pb_buffer *_buf) 110{ 111 struct pb_cache_buffer *buf = pb_cache_buffer(_buf); 112 struct pb_cache_manager *mgr = buf->mgr; 113 114 if (!mgr) { 115 pb_reference(&buf->buffer, NULL); 116 FREE(buf); 117 return; 118 } 119 120 pb_cache_add_buffer(&buf->cache_entry); 121} 122 123 124static void * 125pb_cache_buffer_map(struct pb_buffer *_buf, 126 unsigned flags, void *flush_ctx) 127{ 128 struct pb_cache_buffer *buf = pb_cache_buffer(_buf); 129 return pb_map(buf->buffer, flags, flush_ctx); 130} 131 132 133static void 134pb_cache_buffer_unmap(struct pb_buffer *_buf) 135{ 136 struct pb_cache_buffer *buf = pb_cache_buffer(_buf); 137 pb_unmap(buf->buffer); 138} 139 140 141static enum pipe_error 142pb_cache_buffer_validate(struct pb_buffer *_buf, 143 struct pb_validate *vl, 144 unsigned flags) 145{ 146 struct pb_cache_buffer *buf = pb_cache_buffer(_buf); 147 return pb_validate(buf->buffer, vl, flags); 148} 149 150 151static void 152pb_cache_buffer_fence(struct pb_buffer *_buf, 153 struct pipe_fence_handle *fence) 154{ 155 struct pb_cache_buffer *buf = pb_cache_buffer(_buf); 156 pb_fence(buf->buffer, fence); 157} 158 159 160static void 161pb_cache_buffer_get_base_buffer(struct pb_buffer *_buf, 162 struct pb_buffer **base_buf, 163 pb_size *offset) 164{ 165 struct pb_cache_buffer *buf = pb_cache_buffer(_buf); 166 pb_get_base_buffer(buf->buffer, base_buf, offset); 167} 168 169 170const struct pb_vtbl 171pb_cache_buffer_vtbl = { 172 pb_cache_buffer_destroy, 173 pb_cache_buffer_map, 174 pb_cache_buffer_unmap, 175 pb_cache_buffer_validate, 176 pb_cache_buffer_fence, 177 pb_cache_buffer_get_base_buffer 178}; 179 180 181static bool 182pb_cache_can_reclaim_buffer(struct pb_buffer *_buf) 183{ 184 struct pb_cache_buffer *buf = pb_cache_buffer(_buf); 185 186 if (buf->mgr->provider->is_buffer_busy) { 187 if (buf->mgr->provider->is_buffer_busy(buf->mgr->provider, buf->buffer)) 188 return false; 189 } else { 190 void *ptr = pb_map(buf->buffer, PB_USAGE_DONTBLOCK, NULL); 191 192 if (!ptr) 193 return false; 194 195 pb_unmap(buf->buffer); 196 } 197 198 return true; 199} 200 201 202static struct pb_buffer * 203pb_cache_manager_create_buffer(struct pb_manager *_mgr, 204 pb_size size, 205 const struct pb_desc *desc) 206{ 207 struct pb_cache_manager *mgr = pb_cache_manager(_mgr); 208 struct pb_cache_buffer *buf; 209 210 /* get a buffer from the cache */ 211 buf = (struct pb_cache_buffer *) 212 pb_cache_reclaim_buffer(&mgr->cache, size, desc->alignment, 213 desc->usage, 0); 214 if (buf) 215 return &buf->base; 216 217 /* create a new one */ 218 buf = CALLOC_STRUCT(pb_cache_buffer); 219 if (!buf) 220 return NULL; 221 222 buf->buffer = mgr->provider->create_buffer(mgr->provider, size, desc); 223 224 /* Empty the cache and try again. */ 225 if (!buf->buffer) { 226 pb_cache_release_all_buffers(&mgr->cache); 227 buf->buffer = mgr->provider->create_buffer(mgr->provider, size, desc); 228 } 229 230 if(!buf->buffer) { 231 FREE(buf); 232 return NULL; 233 } 234 235 assert(pipe_is_referenced(&buf->buffer->reference)); 236 assert(pb_check_alignment(desc->alignment, buf->buffer->alignment)); 237 assert(buf->buffer->size >= size); 238 239 pipe_reference_init(&buf->base.reference, 1); 240 buf->base.alignment = buf->buffer->alignment; 241 buf->base.usage = buf->buffer->usage; 242 buf->base.size = buf->buffer->size; 243 244 buf->base.vtbl = &pb_cache_buffer_vtbl; 245 buf->mgr = mgr; 246 pb_cache_init_entry(&mgr->cache, &buf->cache_entry, &buf->base, 0); 247 248 return &buf->base; 249} 250 251 252static void 253pb_cache_manager_flush(struct pb_manager *_mgr) 254{ 255 struct pb_cache_manager *mgr = pb_cache_manager(_mgr); 256 257 pb_cache_release_all_buffers(&mgr->cache); 258 259 assert(mgr->provider->flush); 260 if(mgr->provider->flush) 261 mgr->provider->flush(mgr->provider); 262} 263 264 265static void 266pb_cache_manager_destroy(struct pb_manager *_mgr) 267{ 268 struct pb_cache_manager *mgr = pb_cache_manager(_mgr); 269 270 pb_cache_deinit(&mgr->cache); 271 FREE(mgr); 272} 273 274/** 275 * Create a caching buffer manager 276 * 277 * @param provider The buffer manager to which cache miss buffer requests 278 * should be redirected. 279 * @param usecs Unused buffers may be released from the cache after this 280 * time 281 * @param size_factor Declare buffers that are size_factor times bigger than 282 * the requested size as cache hits. 283 * @param bypass_usage Bitmask. If (requested usage & bypass_usage) != 0, 284 * buffer allocation requests are redirected to the provider. 285 * @param maximum_cache_size Maximum size of all unused buffers the cache can 286 * hold. 287 */ 288struct pb_manager * 289pb_cache_manager_create(struct pb_manager *provider, 290 unsigned usecs, 291 float size_factor, 292 unsigned bypass_usage, 293 uint64_t maximum_cache_size) 294{ 295 struct pb_cache_manager *mgr; 296 297 if (!provider) 298 return NULL; 299 300 mgr = CALLOC_STRUCT(pb_cache_manager); 301 if (!mgr) 302 return NULL; 303 304 mgr->base.destroy = pb_cache_manager_destroy; 305 mgr->base.create_buffer = pb_cache_manager_create_buffer; 306 mgr->base.flush = pb_cache_manager_flush; 307 mgr->provider = provider; 308 pb_cache_init(&mgr->cache, usecs, size_factor, bypass_usage, 309 maximum_cache_size, 310 _pb_cache_buffer_destroy, 311 pb_cache_can_reclaim_buffer); 312 return &mgr->base; 313} 314