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