r300g: align the height of NPOT textures to POT
[profile/ivi/mesa.git] / src / gallium / drivers / i915 / i915_buffer.c
1 /**************************************************************************
2  *
3  * Copyright © 2009 Jakob Bornecrantz
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 #include "util/u_inlines.h"
27 #include "util/u_memory.h"
28 #include "i915_screen.h"
29 #include "i915_buffer.h"
30
31 struct i915_winsys_buffer;
32
33 struct i915_buffer
34 {
35    struct pipe_buffer base;
36
37    struct i915_winsys_buffer *ibuf; /** hw buffer */
38
39    void *data; /**< user and malloc data */
40    boolean own; /**< we own the data incase of malloc */
41 };
42
43 static INLINE struct i915_buffer *
44 i915_buffer(struct pipe_buffer *buffer)
45 {
46    return (struct i915_buffer *)buffer;
47 }
48
49 static struct pipe_buffer *
50 i915_buffer_create(struct pipe_screen *screen,
51                    unsigned alignment,
52                    unsigned usage,
53                    unsigned size)
54 {
55    struct i915_buffer *buf = CALLOC_STRUCT(i915_buffer);
56
57    if (!buf)
58       return NULL;
59
60    pipe_reference_init(&buf->base.reference, 1);
61    buf->base.alignment = alignment;
62    buf->base.screen = screen;
63    buf->base.usage = usage;
64    buf->base.size = size;
65    buf->data = MALLOC(size);
66    buf->own = TRUE;
67
68    if (!buf->data)
69       goto err;
70
71    return &buf->base;
72
73 err:
74    FREE(buf);
75    return NULL;
76 }
77
78 static struct pipe_buffer *
79 i915_user_buffer_create(struct pipe_screen *screen,
80                         void *ptr,
81                         unsigned bytes)
82 {
83    struct i915_buffer *buf = CALLOC_STRUCT(i915_buffer);
84
85    if (!buf)
86       return NULL;
87
88    pipe_reference_init(&buf->base.reference, 1);
89    buf->base.alignment = 0;
90    buf->base.screen = screen;
91    buf->base.usage = 0;
92    buf->base.size = bytes;
93    buf->data = ptr;
94    buf->own = FALSE;
95
96    return &buf->base;
97 }
98
99 static void *
100 i915_buffer_map(struct pipe_screen *screen,
101                 struct pipe_buffer *buffer,
102                 unsigned usage)
103 {
104    struct i915_buffer *buf = i915_buffer(buffer);
105    assert(!buf->ibuf);
106    return buf->data;
107 }
108
109 static void
110 i915_buffer_unmap(struct pipe_screen *screen,
111                   struct pipe_buffer *buffer)
112 {
113    struct i915_buffer *buf = i915_buffer(buffer);
114    assert(!buf->ibuf);
115    (void) buf;
116 }
117
118 static void
119 i915_buffer_destroy(struct pipe_buffer *buffer)
120 {
121    struct i915_buffer *buf = i915_buffer(buffer);
122    assert(!buf->ibuf);
123
124    if (buf->own)
125       FREE(buf->data);
126    FREE(buf);
127 }
128
129 void i915_init_screen_buffer_functions(struct i915_screen *screen)
130 {
131    screen->base.buffer_create = i915_buffer_create;
132    screen->base.user_buffer_create = i915_user_buffer_create;
133    screen->base.buffer_map = i915_buffer_map;
134    screen->base.buffer_map_range = NULL;
135    screen->base.buffer_flush_mapped_range = NULL;
136    screen->base.buffer_unmap = i915_buffer_unmap;
137    screen->base.buffer_destroy = i915_buffer_destroy;
138 }