~gnome3-team/mutter/trunk

« back to all changes in this revision

Viewing changes to cogl/cogl/cogl-pipeline-hash-table.c

  • Committer: Rui Matos
  • Date: 2016-04-27 16:36:25 UTC
  • mfrom: (0.87.3184)
  • Revision ID: git-v1:3fcbe1d3ec5c9208dde080f7e9dac24e4c379bc0
Merge cogl's cogl-1.22 branch into mutter

https://bugzilla.gnome.org/show_bug.cgi?id=760439

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Cogl
 
3
 *
 
4
 * A Low Level GPU Graphics and Utilities API
 
5
 *
 
6
 * Copyright (C) 2013 Intel Corporation.
 
7
 *
 
8
 * Permission is hereby granted, free of charge, to any person
 
9
 * obtaining a copy of this software and associated documentation
 
10
 * files (the "Software"), to deal in the Software without
 
11
 * restriction, including without limitation the rights to use, copy,
 
12
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 
13
 * of the Software, and to permit persons to whom the Software is
 
14
 * furnished to do so, subject to the following conditions:
 
15
 *
 
16
 * The above copyright notice and this permission notice shall be
 
17
 * included in all copies or substantial portions of the Software.
 
18
 *
 
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
20
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
21
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
22
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 
23
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 
24
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
25
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 
26
 * SOFTWARE.
 
27
 *
 
28
 *
 
29
 * Authors:
 
30
 *   Neil Roberts <neil@linux.intel.com>
 
31
 *   Robert Bragg <robert@linux.intel.com>
 
32
 */
 
33
 
 
34
#ifdef HAVE_CONFIG_H
 
35
#include "config.h"
 
36
#endif
 
37
 
 
38
#include "cogl-context-private.h"
 
39
#include "cogl-pipeline-private.h"
 
40
#include "cogl-pipeline-hash-table.h"
 
41
#include "cogl-pipeline-cache.h"
 
42
 
 
43
typedef struct
 
44
{
 
45
  CoglPipelineCacheEntry parent;
 
46
 
 
47
  /* Calculating the hash is a little bit expensive for pipelines so
 
48
   * we don't want to do it repeatedly for entries that are already in
 
49
   * the hash table. Instead we cache the value here and calculate it
 
50
   * outside of the GHashTable. */
 
51
  unsigned int hash_value;
 
52
 
 
53
  /* GHashTable annoyingly doesn't let us pass a user data pointer to
 
54
   * the hash and equal functions so to work around it we have to
 
55
   * store the pointer in every hash table entry. We will use this
 
56
   * entry as both the key and the value */
 
57
  CoglPipelineHashTable *hash;
 
58
 
 
59
  /* The number of unique pipelines that had been created when this
 
60
   * pipeline was last accessed */
 
61
  int age;
 
62
} CoglPipelineHashTableEntry;
 
63
 
 
64
static void
 
65
value_destroy_cb (void *value)
 
66
{
 
67
  CoglPipelineHashTableEntry *entry = value;
 
68
 
 
69
  cogl_object_unref (entry->parent.pipeline);
 
70
 
 
71
  g_slice_free (CoglPipelineHashTableEntry, entry);
 
72
}
 
73
 
 
74
static unsigned int
 
75
entry_hash (const void *data)
 
76
{
 
77
  const CoglPipelineHashTableEntry *entry = data;
 
78
 
 
79
  return entry->hash_value;
 
80
}
 
81
 
 
82
static CoglBool
 
83
entry_equal (const void *a,
 
84
             const void *b)
 
85
{
 
86
  const CoglPipelineHashTableEntry *entry_a = a;
 
87
  const CoglPipelineHashTableEntry *entry_b = b;
 
88
  const CoglPipelineHashTable *hash = entry_a->hash;
 
89
 
 
90
  return _cogl_pipeline_equal (entry_a->parent.pipeline,
 
91
                               entry_b->parent.pipeline,
 
92
                               hash->main_state,
 
93
                               hash->layer_state,
 
94
                               0);
 
95
}
 
96
 
 
97
void
 
98
_cogl_pipeline_hash_table_init (CoglPipelineHashTable *hash,
 
99
                                unsigned int main_state,
 
100
                                unsigned int layer_state,
 
101
                                const char *debug_string)
 
102
{
 
103
  hash->n_unique_pipelines = 0;
 
104
  hash->debug_string = debug_string;
 
105
  hash->main_state = main_state;
 
106
  hash->layer_state = layer_state;
 
107
  /* We'll only start pruning once we get to 16 unique pipelines */
 
108
  hash->expected_min_size = 8;
 
109
  hash->table = g_hash_table_new_full (entry_hash,
 
110
                                       entry_equal,
 
111
                                       NULL, /* key destroy */
 
112
                                       value_destroy_cb);
 
113
}
 
114
 
 
115
void
 
116
_cogl_pipeline_hash_table_destroy (CoglPipelineHashTable *hash)
 
117
{
 
118
  g_hash_table_destroy (hash->table);
 
119
}
 
120
 
 
121
static void
 
122
collect_prunable_entries_cb (void *key,
 
123
                             void *value,
 
124
                             void *user_data)
 
125
{
 
126
  GQueue *entries = user_data;
 
127
  CoglPipelineCacheEntry *entry = value;
 
128
 
 
129
  if (entry->usage_count == 0)
 
130
    g_queue_push_tail (entries, entry);
 
131
}
 
132
 
 
133
static int
 
134
compare_pipeline_age_cb (const void *a,
 
135
                         const void *b)
 
136
{
 
137
  const CoglPipelineHashTableEntry *ae = a;
 
138
  const CoglPipelineHashTableEntry *be = b;
 
139
 
 
140
  return be->age - ae->age;
 
141
}
 
142
 
 
143
static void
 
144
prune_old_pipelines (CoglPipelineHashTable *hash)
 
145
{
 
146
  GQueue entries;
 
147
  GList *l;
 
148
  int i;
 
149
 
 
150
  /* Collect all of the prunable entries into a GQueue */
 
151
  g_queue_init (&entries);
 
152
  g_hash_table_foreach (hash->table,
 
153
                        collect_prunable_entries_cb,
 
154
                        &entries);
 
155
 
 
156
  /* Sort the entries by increasing order of age */
 
157
  entries.head = g_list_sort (entries.head, compare_pipeline_age_cb);
 
158
 
 
159
  /* The +1 is to include the pipeline that we're about to add */
 
160
  hash->expected_min_size = (g_hash_table_size (hash->table) -
 
161
                             entries.length +
 
162
                             1);
 
163
 
 
164
  /* Remove oldest half of the prunable pipelines. We still want to
 
165
   * keep some of the prunable entries that are recently used because
 
166
   * it's not unlikely that the application will recreate the same
 
167
   * pipeline */
 
168
  for (l = entries.head, i = 0; i < entries.length / 2; l = l->next, i++)
 
169
    {
 
170
      CoglPipelineCacheEntry *entry = l->data;
 
171
 
 
172
      g_hash_table_remove (hash->table, entry);
 
173
    }
 
174
 
 
175
  g_list_free (entries.head);
 
176
}
 
177
 
 
178
CoglPipelineCacheEntry *
 
179
_cogl_pipeline_hash_table_get (CoglPipelineHashTable *hash,
 
180
                               CoglPipeline *key_pipeline)
 
181
{
 
182
  CoglPipelineHashTableEntry dummy_entry;
 
183
  CoglPipelineHashTableEntry *entry;
 
184
  unsigned int copy_state;
 
185
 
 
186
  dummy_entry.parent.pipeline = key_pipeline;
 
187
  dummy_entry.hash = hash;
 
188
  dummy_entry.hash_value = _cogl_pipeline_hash (key_pipeline,
 
189
                                                hash->main_state,
 
190
                                                hash->layer_state,
 
191
                                                0);
 
192
  entry = g_hash_table_lookup (hash->table, &dummy_entry);
 
193
 
 
194
  if (entry)
 
195
    {
 
196
      entry->age = hash->n_unique_pipelines;
 
197
      return &entry->parent;
 
198
    }
 
199
 
 
200
  if (hash->n_unique_pipelines == 50)
 
201
    g_warning ("Over 50 separate %s have been generated which is very "
 
202
               "unusual, so something is probably wrong!\n",
 
203
               hash->debug_string);
 
204
 
 
205
  /* If we are going to have more than twice the expected minimum
 
206
   * number of pipelines in the hash then we'll try pruning and update
 
207
   * the minimum */
 
208
  if (g_hash_table_size (hash->table) >= hash->expected_min_size * 2)
 
209
    prune_old_pipelines (hash);
 
210
 
 
211
  entry = g_slice_new (CoglPipelineHashTableEntry);
 
212
  entry->parent.usage_count = 0;
 
213
  entry->hash = hash;
 
214
  entry->hash_value = dummy_entry.hash_value;
 
215
  entry->age = hash->n_unique_pipelines;
 
216
 
 
217
  copy_state = hash->main_state;
 
218
  if (hash->layer_state)
 
219
    copy_state |= COGL_PIPELINE_STATE_LAYERS;
 
220
 
 
221
  /* Create a new pipeline that is a child of the root pipeline
 
222
   * instead of a normal copy so that the template pipeline won't hold
 
223
   * a reference to the original pipeline */
 
224
  entry->parent.pipeline = _cogl_pipeline_deep_copy (key_pipeline,
 
225
                                                     copy_state,
 
226
                                                     hash->layer_state);
 
227
 
 
228
  g_hash_table_insert (hash->table, entry, entry);
 
229
 
 
230
  hash->n_unique_pipelines++;
 
231
 
 
232
  return &entry->parent;
 
233
}