OSDN Git Service

ilo: unmap cp bo before destroying it
[android-x86/external-mesa.git] / src / gallium / drivers / ilo / ilo_context.c
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 2012-2013 LunarG, Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the 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  * Authors:
25  *    Chia-I Wu <olv@lunarg.com>
26  */
27
28 #include "util/u_blitter.h"
29 #include "intel_chipset.h"
30
31 #include "ilo_3d.h"
32 #include "ilo_blit.h"
33 #include "ilo_cp.h"
34 #include "ilo_gpgpu.h"
35 #include "ilo_query.h"
36 #include "ilo_resource.h"
37 #include "ilo_screen.h"
38 #include "ilo_shader.h"
39 #include "ilo_state.h"
40 #include "ilo_transfer.h"
41 #include "ilo_video.h"
42 #include "ilo_context.h"
43
44 static void
45 ilo_context_cp_flushed(struct ilo_cp *cp, void *data)
46 {
47    struct ilo_context *ilo = ilo_context(data);
48
49    if (ilo->last_cp_bo)
50       ilo->last_cp_bo->unreference(ilo->last_cp_bo);
51
52    /* remember the just flushed bo, on which fences could wait */
53    ilo->last_cp_bo = cp->bo;
54    ilo->last_cp_bo->reference(ilo->last_cp_bo);
55
56    ilo_3d_cp_flushed(ilo->hw3d);
57 }
58
59 static void
60 ilo_flush(struct pipe_context *pipe,
61           struct pipe_fence_handle **f,
62           unsigned flags)
63 {
64    struct ilo_context *ilo = ilo_context(pipe);
65
66    if (f) {
67       struct ilo_fence *fence;
68
69       fence = CALLOC_STRUCT(ilo_fence);
70       if (fence) {
71          pipe_reference_init(&fence->reference, 1);
72
73          /* reference the batch bo that we want to wait on */
74          if (ilo_cp_empty(ilo->cp))
75             fence->bo = ilo->last_cp_bo;
76          else
77             fence->bo = ilo->cp->bo;
78
79          if (fence->bo)
80             fence->bo->reference(fence->bo);
81       }
82
83       *f = (struct pipe_fence_handle *) fence;
84    }
85
86    ilo_cp_flush(ilo->cp);
87 }
88
89 static void
90 ilo_context_destroy(struct pipe_context *pipe)
91 {
92    struct ilo_context *ilo = ilo_context(pipe);
93
94    ilo_cleanup_states(ilo);
95
96    if (ilo->last_cp_bo)
97       ilo->last_cp_bo->unreference(ilo->last_cp_bo);
98
99    if (ilo->blitter)
100       util_blitter_destroy(ilo->blitter);
101    if (ilo->hw3d)
102       ilo_3d_destroy(ilo->hw3d);
103    if (ilo->shader_cache)
104       ilo_shader_cache_destroy(ilo->shader_cache);
105    if (ilo->cp)
106       ilo_cp_destroy(ilo->cp);
107
108    FREE(ilo);
109 }
110
111 static struct pipe_context *
112 ilo_context_create(struct pipe_screen *screen, void *priv)
113 {
114    struct ilo_screen *is = ilo_screen(screen);
115    struct ilo_context *ilo;
116
117    ilo = CALLOC_STRUCT(ilo_context);
118    if (!ilo)
119       return NULL;
120
121    ilo->winsys = is->winsys;
122    ilo->dev = &is->dev;
123
124    ilo->cp = ilo_cp_create(ilo->winsys, is->dev.has_llc);
125    ilo->shader_cache = ilo_shader_cache_create(ilo->winsys);
126    if (ilo->cp)
127       ilo->hw3d = ilo_3d_create(ilo->cp, ilo->dev);
128
129    if (!ilo->cp || !ilo->shader_cache || !ilo->hw3d) {
130       ilo_context_destroy(&ilo->base);
131       return NULL;
132    }
133
134    ilo_cp_set_flush_callback(ilo->cp,
135          ilo_context_cp_flushed, (void *) ilo);
136
137    ilo->dirty = ILO_DIRTY_ALL;
138
139    ilo->base.screen = screen;
140    ilo->base.priv = priv;
141
142    ilo->base.destroy = ilo_context_destroy;
143    ilo->base.flush = ilo_flush;
144
145    ilo_init_3d_functions(ilo);
146    ilo_init_query_functions(ilo);
147    ilo_init_state_functions(ilo);
148    ilo_init_blit_functions(ilo);
149    ilo_init_transfer_functions(ilo);
150    ilo_init_video_functions(ilo);
151    ilo_init_gpgpu_functions(ilo);
152
153    ilo_init_states(ilo);
154
155    /* this must be called last as u_blitter is a client of the pipe context */
156    ilo->blitter = util_blitter_create(&ilo->base);
157    if (!ilo->blitter) {
158       ilo_context_destroy(&ilo->base);
159       return NULL;
160    }
161
162    return &ilo->base;
163 }
164
165 /**
166  * Initialize context-related functions.
167  */
168 void
169 ilo_init_context_functions(struct ilo_screen *is)
170 {
171    is->base.context_create = ilo_context_create;
172 }