1/**************************************************************************
2 *
3 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 * Allocate buffers from two alternative buffer providers.
31 *
32 * \author Jose Fonseca <jrfonseca@tungstengraphics.com>
33 */
34
35
36#include "pipe/p_compiler.h"
37#include "util/u_debug.h"
38#include "util/u_memory.h"
39
40#include "pb_buffer.h"
41#include "pb_bufmgr.h"
42
43
44struct pb_alt_manager
45{
46   struct pb_manager base;
47
48   struct pb_manager *provider1;
49   struct pb_manager *provider2;
50};
51
52
53static INLINE struct pb_alt_manager *
54pb_alt_manager(struct pb_manager *mgr)
55{
56   assert(mgr);
57   return (struct pb_alt_manager *)mgr;
58}
59
60
61static struct pb_buffer *
62pb_alt_manager_create_buffer(struct pb_manager *_mgr,
63                             pb_size size,
64                             const struct pb_desc *desc)
65{
66   struct pb_alt_manager *mgr = pb_alt_manager(_mgr);
67   struct pb_buffer *buf;
68
69   buf = mgr->provider1->create_buffer(mgr->provider1, size, desc);
70   if(buf)
71      return buf;
72
73   buf = mgr->provider2->create_buffer(mgr->provider2, size, desc);
74   return buf;
75}
76
77
78static void
79pb_alt_manager_flush(struct pb_manager *_mgr)
80{
81   struct pb_alt_manager *mgr = pb_alt_manager(_mgr);
82
83   assert(mgr->provider1->flush);
84   if(mgr->provider1->flush)
85      mgr->provider1->flush(mgr->provider1);
86
87   assert(mgr->provider2->flush);
88   if(mgr->provider2->flush)
89      mgr->provider2->flush(mgr->provider2);
90}
91
92
93static void
94pb_alt_manager_destroy(struct pb_manager *mgr)
95{
96   FREE(mgr);
97}
98
99
100struct pb_manager *
101pb_alt_manager_create(struct pb_manager *provider1,
102                      struct pb_manager *provider2)
103{
104   struct pb_alt_manager *mgr;
105
106   if(!provider1 || !provider2)
107      return NULL;
108
109   mgr = CALLOC_STRUCT(pb_alt_manager);
110   if (!mgr)
111      return NULL;
112
113   mgr->base.destroy = pb_alt_manager_destroy;
114   mgr->base.create_buffer = pb_alt_manager_create_buffer;
115   mgr->base.flush = pb_alt_manager_flush;
116   mgr->provider1 = provider1;
117   mgr->provider2 = provider2;
118
119   return &mgr->base;
120}
121