blob: 842f1348648ec495d6e9e90dd20c1acc519424e0 [file] [log] [blame]
Chia-I Wubdf4c562014-08-07 06:36:33 +08001/*
2 * XGL
3 *
4 * Copyright (C) 2014 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
25#include "kmd/winsys.h"
26#include "dev.h"
27#include "fence.h"
28
Chia-I Wu26f0bd02014-08-07 10:38:40 +080029static void fence_destroy(struct intel_obj *obj)
Chia-I Wubdf4c562014-08-07 06:36:33 +080030{
31 struct intel_fence *fence = intel_fence_from_obj(obj);
32
33 intel_fence_destroy(fence);
34}
35
36XGL_RESULT intel_fence_create(struct intel_dev *dev,
37 const XGL_FENCE_CREATE_INFO *info,
38 struct intel_fence **fence_ret)
39{
40 struct intel_fence *fence;
41
42 fence = icd_alloc(sizeof(*fence), 0, XGL_SYSTEM_ALLOC_API_OBJECT);
43 if (!fence)
44 return XGL_ERROR_OUT_OF_MEMORY;
45
46 memset(fence, 0, sizeof(*fence));
47
Chia-I Wu26f0bd02014-08-07 10:38:40 +080048 fence->obj.destroy = fence_destroy;
Chia-I Wubdf4c562014-08-07 06:36:33 +080049
50 fence->obj.base.dispatch = dev->base.dispatch;
51 if (dev->base.dbg) {
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +080052 fence->obj.base.dbg =
53 intel_base_dbg_create(XGL_DBG_OBJECT_FENCE, info, 0);
Chia-I Wubdf4c562014-08-07 06:36:33 +080054 if (!fence->obj.base.dbg) {
55 icd_free(fence);
56 return XGL_ERROR_OUT_OF_MEMORY;
57 }
58 }
Chia-I Wu26f0bd02014-08-07 10:38:40 +080059 fence->obj.base.get_info = intel_base_get_info;
Chia-I Wubdf4c562014-08-07 06:36:33 +080060
61 *fence_ret = fence;
62
63 return XGL_SUCCESS;
64}
65
66void intel_fence_destroy(struct intel_fence *fence)
67{
68 if (fence->submitted_bo)
69 intel_bo_unreference(fence->submitted_bo);
70
71 if (fence->obj.base.dbg)
72 intel_base_dbg_destroy(fence->obj.base.dbg);
73
74 icd_free(fence);
75}
76
77XGL_RESULT intel_fence_get_status(struct intel_fence *fence)
78{
79 if (!fence->submitted_bo)
80 return XGL_ERROR_UNAVAILABLE;
81
82 return (intel_bo_is_busy(fence->submitted_bo)) ?
83 XGL_NOT_READY : XGL_SUCCESS;
84}
85
86XGL_RESULT intel_fence_wait(struct intel_fence *fence, int64_t timeout_ns)
87{
88 int err;
89
90 if (!fence->submitted_bo)
91 return XGL_ERROR_UNAVAILABLE;
92
93 err = intel_bo_wait(fence->submitted_bo, timeout_ns);
94
95 return (err) ? XGL_NOT_READY : XGL_SUCCESS;
96}
97
98XGL_RESULT XGLAPI intelCreateFence(
99 XGL_DEVICE device,
100 const XGL_FENCE_CREATE_INFO* pCreateInfo,
101 XGL_FENCE* pFence)
102{
103 struct intel_dev *dev = intel_dev(device);
104
105 return intel_fence_create(dev, pCreateInfo,
106 (struct intel_fence **) pFence);
107}
108
109XGL_RESULT XGLAPI intelGetFenceStatus(
110 XGL_FENCE fence_)
111{
112 struct intel_fence *fence = intel_fence(fence_);
113
114 return intel_fence_get_status(fence);
115}
116
117XGL_RESULT XGLAPI intelWaitForFences(
118 XGL_DEVICE device,
119 XGL_UINT fenceCount,
120 const XGL_FENCE* pFences,
121 XGL_BOOL waitAll,
122 XGL_UINT64 timeout)
123{
124 XGL_RESULT ret = XGL_SUCCESS;
125 XGL_UINT i;
126
127 for (i = 0; i < fenceCount; i++) {
128 struct intel_fence *fence = intel_fence(pFences[i]);
129 int64_t ns;
130 XGL_RESULT r;
131
132 if (timeout <= (XGL_UINT64) (INT64_MAX / 1000 / 1000 / 1000))
133 ns = timeout * 1000 * 1000 * 1000;
134 else
135 ns = -1;
136
137 r = intel_fence_wait(fence, ns);
138
139 if (!waitAll && r == XGL_SUCCESS)
140 return XGL_SUCCESS;
141
142 if (r != XGL_SUCCESS)
143 ret = r;
144 }
145
146 return ret;
147}