Home | History | Annotate | Download | only in svga
      1 
      2 /**********************************************************
      3  * Copyright 2008-2009 VMware, Inc.  All rights reserved.
      4  *
      5  * Permission is hereby granted, free of charge, to any person
      6  * obtaining a copy of this software and associated documentation
      7  * files (the "Software"), to deal in the Software without
      8  * restriction, including without limitation the rights to use, copy,
      9  * modify, merge, publish, distribute, sublicense, and/or sell copies
     10  * of the Software, and to permit persons to whom the Software is
     11  * furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be
     14  * included in all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
     20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
     21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
     22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     23  * SOFTWARE.
     24  *
     25  **********************************************************/
     26 
     27 #ifndef SVGA_SCREEN_CACHE_H_
     28 #define SVGA_SCREEN_CACHE_H_
     29 
     30 
     31 #include "svga_types.h"
     32 #include "svga_reg.h"
     33 #include "svga3d_reg.h"
     34 
     35 #include "os/os_thread.h"
     36 
     37 #include "util/list.h"
     38 
     39 
     40 /* Guess the storage size of cached surfaces and try and keep it under
     41  * this amount:
     42  */
     43 #define SVGA_HOST_SURFACE_CACHE_BYTES (16 * 1024 * 1024)
     44 
     45 /* Maximum number of discrete surfaces in the cache:
     46  */
     47 #define SVGA_HOST_SURFACE_CACHE_SIZE 1024
     48 
     49 /* Number of hash buckets:
     50  */
     51 #define SVGA_HOST_SURFACE_CACHE_BUCKETS 256
     52 
     53 
     54 struct svga_winsys_surface;
     55 struct svga_screen;
     56 
     57 /**
     58  * Same as svga_winsys_screen::surface_create.
     59  */
     60 struct svga_host_surface_cache_key
     61 {
     62    SVGA3dSurfaceFlags flags;
     63    SVGA3dSurfaceFormat format;
     64    SVGA3dSize size;
     65    uint32_t numFaces:3;
     66    uint32_t arraySize:16;
     67    uint32_t numMipLevels:6;
     68    uint32_t cachable:1;         /* False if this is a shared surface */
     69    uint32_t sampleCount:5;
     70    uint32_t scanout:1;
     71 };
     72 
     73 
     74 struct svga_host_surface_cache_entry
     75 {
     76    /**
     77     * Head for the LRU list, svga_host_surface_cache::unused, and
     78     * svga_host_surface_cache::empty
     79     */
     80    struct list_head head;
     81 
     82    /** Head for the bucket lists. */
     83    struct list_head bucket_head;
     84 
     85    struct svga_host_surface_cache_key key;
     86    struct svga_winsys_surface *handle;
     87 
     88    struct pipe_fence_handle *fence;
     89 };
     90 
     91 
     92 /**
     93  * Cache of the host surfaces.
     94  *
     95  * A cache entry can be in the following stages:
     96  * 1. empty (entry->handle = NULL)
     97  * 2. holding a buffer in a validate list
     98  * 3. holding a buffer in an invalidate list
     99  * 4. holding a flushed buffer (not in any validate list) with an active fence
    100  * 5. holding a flushed buffer with an expired fence
    101  *
    102  * An entry progresses from 1 -> 2 -> 3 -> 4 -> 5. When we need an entry to put a
    103  * buffer into we preferentially take from 1, or from the least recently used
    104  * buffer from 4/5.
    105  */
    106 struct svga_host_surface_cache
    107 {
    108    pipe_mutex mutex;
    109 
    110    /* Unused buffers are put in buckets to speed up lookups */
    111    struct list_head bucket[SVGA_HOST_SURFACE_CACHE_BUCKETS];
    112 
    113    /* Entries with unused buffers, ordered from most to least recently used
    114     * (3 and 4) */
    115    struct list_head unused;
    116 
    117    /* Entries with buffers still in validate list (2) */
    118    struct list_head validated;
    119 
    120    /* Entries with buffers still in invalidate list (3) */
    121    struct list_head invalidated;
    122 
    123    /** Empty entries (1) */
    124    struct list_head empty;
    125 
    126    /** The actual storage for the entries */
    127    struct svga_host_surface_cache_entry entries[SVGA_HOST_SURFACE_CACHE_SIZE];
    128 
    129    /** Sum of sizes of all surfaces (in bytes) */
    130    unsigned total_size;
    131 };
    132 
    133 
    134 void
    135 svga_screen_cache_cleanup(struct svga_screen *svgascreen);
    136 
    137 void
    138 svga_screen_cache_flush(struct svga_screen *svgascreen,
    139                         struct pipe_fence_handle *fence);
    140 
    141 enum pipe_error
    142 svga_screen_cache_init(struct svga_screen *svgascreen);
    143 
    144 
    145 struct svga_winsys_surface *
    146 svga_screen_surface_create(struct svga_screen *svgascreen,
    147                            unsigned bind_flags, enum pipe_resource_usage usage,
    148                            boolean *validated,
    149                            struct svga_host_surface_cache_key *key);
    150 
    151 void
    152 svga_screen_surface_destroy(struct svga_screen *svgascreen,
    153                             const struct svga_host_surface_cache_key *key,
    154                             struct svga_winsys_surface **handle);
    155 
    156 void
    157 svga_screen_cache_dump(const struct svga_screen *svgascreen);
    158 
    159 
    160 #endif /* SVGA_SCREEN_CACHE_H_ */
    161