blob: e47846c98bb4ef1161260a410556c03d4cc09067 [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.
Chia-I Wu44e42362014-09-02 08:32:09 +080023 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
Chia-I Wubdf4c562014-08-07 06:36:33 +080026 */
27
28#include "kmd/winsys.h"
Chia-I Wu34f45182014-08-19 14:02:59 +080029#include "cmd.h"
Chia-I Wubdf4c562014-08-07 06:36:33 +080030#include "dev.h"
31#include "fence.h"
32
Chia-I Wu26f0bd02014-08-07 10:38:40 +080033static void fence_destroy(struct intel_obj *obj)
Chia-I Wubdf4c562014-08-07 06:36:33 +080034{
35 struct intel_fence *fence = intel_fence_from_obj(obj);
36
37 intel_fence_destroy(fence);
38}
39
40XGL_RESULT intel_fence_create(struct intel_dev *dev,
41 const XGL_FENCE_CREATE_INFO *info,
42 struct intel_fence **fence_ret)
43{
44 struct intel_fence *fence;
45
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -060046 fence = (struct intel_fence *) intel_base_create(dev, sizeof(*fence),
Chia-I Wubbf2c932014-08-07 12:20:08 +080047 dev->base.dbg, XGL_DBG_OBJECT_FENCE, info, 0);
Chia-I Wubdf4c562014-08-07 06:36:33 +080048 if (!fence)
49 return XGL_ERROR_OUT_OF_MEMORY;
50
Chia-I Wu26f0bd02014-08-07 10:38:40 +080051 fence->obj.destroy = fence_destroy;
Chia-I Wubdf4c562014-08-07 06:36:33 +080052
Chia-I Wubdf4c562014-08-07 06:36:33 +080053 *fence_ret = fence;
54
55 return XGL_SUCCESS;
56}
57
58void intel_fence_destroy(struct intel_fence *fence)
59{
Chia-I Wubbf2c932014-08-07 12:20:08 +080060 intel_base_destroy(&fence->obj.base);
Chia-I Wubdf4c562014-08-07 06:36:33 +080061}
62
63XGL_RESULT intel_fence_get_status(struct intel_fence *fence)
64{
Chia-I Wue24c3292014-08-21 14:05:23 +080065 struct intel_bo *bo = (fence->cmd) ?
66 intel_cmd_get_batch(fence->cmd, NULL) : NULL;
67
68 if (!bo)
Chia-I Wubdf4c562014-08-07 06:36:33 +080069 return XGL_ERROR_UNAVAILABLE;
70
Chia-I Wue24c3292014-08-21 14:05:23 +080071 return (intel_bo_is_busy(bo)) ? XGL_NOT_READY : XGL_SUCCESS;
Chia-I Wubdf4c562014-08-07 06:36:33 +080072}
73
74XGL_RESULT intel_fence_wait(struct intel_fence *fence, int64_t timeout_ns)
75{
Chia-I Wue24c3292014-08-21 14:05:23 +080076 struct intel_bo *bo = (fence->cmd) ?
77 intel_cmd_get_batch(fence->cmd, NULL) : NULL;
Chia-I Wubdf4c562014-08-07 06:36:33 +080078 int err;
79
Chia-I Wue24c3292014-08-21 14:05:23 +080080 if (!bo)
Chia-I Wubdf4c562014-08-07 06:36:33 +080081 return XGL_ERROR_UNAVAILABLE;
82
Chia-I Wue24c3292014-08-21 14:05:23 +080083 err = intel_bo_wait(bo, timeout_ns);
Chia-I Wubdf4c562014-08-07 06:36:33 +080084
85 return (err) ? XGL_NOT_READY : XGL_SUCCESS;
86}
87
88XGL_RESULT XGLAPI intelCreateFence(
89 XGL_DEVICE device,
90 const XGL_FENCE_CREATE_INFO* pCreateInfo,
91 XGL_FENCE* pFence)
92{
93 struct intel_dev *dev = intel_dev(device);
94
95 return intel_fence_create(dev, pCreateInfo,
96 (struct intel_fence **) pFence);
97}
98
99XGL_RESULT XGLAPI intelGetFenceStatus(
100 XGL_FENCE fence_)
101{
102 struct intel_fence *fence = intel_fence(fence_);
103
104 return intel_fence_get_status(fence);
105}
106
107XGL_RESULT XGLAPI intelWaitForFences(
108 XGL_DEVICE device,
109 XGL_UINT fenceCount,
110 const XGL_FENCE* pFences,
111 XGL_BOOL waitAll,
112 XGL_UINT64 timeout)
113{
114 XGL_RESULT ret = XGL_SUCCESS;
115 XGL_UINT i;
116
117 for (i = 0; i < fenceCount; i++) {
118 struct intel_fence *fence = intel_fence(pFences[i]);
119 int64_t ns;
120 XGL_RESULT r;
121
122 if (timeout <= (XGL_UINT64) (INT64_MAX / 1000 / 1000 / 1000))
123 ns = timeout * 1000 * 1000 * 1000;
124 else
125 ns = -1;
126
127 r = intel_fence_wait(fence, ns);
128
129 if (!waitAll && r == XGL_SUCCESS)
130 return XGL_SUCCESS;
131
132 if (r != XGL_SUCCESS)
133 ret = r;
134 }
135
136 return ret;
137}