Chia-I Wu | bdf4c56 | 2014-08-07 06:36:33 +0800 | [diff] [blame^] | 1 | /* |
| 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 | |
| 29 | static void fence_destroy_callback(struct intel_obj *obj) |
| 30 | { |
| 31 | struct intel_fence *fence = intel_fence_from_obj(obj); |
| 32 | |
| 33 | intel_fence_destroy(fence); |
| 34 | } |
| 35 | |
| 36 | XGL_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 | |
| 48 | fence->obj.destroy = fence_destroy_callback; |
| 49 | |
| 50 | fence->obj.base.dispatch = dev->base.dispatch; |
| 51 | if (dev->base.dbg) { |
| 52 | fence->obj.base.dbg = |
| 53 | intel_base_dbg_create(XGL_DBG_OBJECT_FENCE, info, sizeof(*info)); |
| 54 | if (!fence->obj.base.dbg) { |
| 55 | icd_free(fence); |
| 56 | return XGL_ERROR_OUT_OF_MEMORY; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | *fence_ret = fence; |
| 61 | |
| 62 | return XGL_SUCCESS; |
| 63 | } |
| 64 | |
| 65 | void intel_fence_destroy(struct intel_fence *fence) |
| 66 | { |
| 67 | if (fence->submitted_bo) |
| 68 | intel_bo_unreference(fence->submitted_bo); |
| 69 | |
| 70 | if (fence->obj.base.dbg) |
| 71 | intel_base_dbg_destroy(fence->obj.base.dbg); |
| 72 | |
| 73 | icd_free(fence); |
| 74 | } |
| 75 | |
| 76 | XGL_RESULT intel_fence_get_status(struct intel_fence *fence) |
| 77 | { |
| 78 | if (!fence->submitted_bo) |
| 79 | return XGL_ERROR_UNAVAILABLE; |
| 80 | |
| 81 | return (intel_bo_is_busy(fence->submitted_bo)) ? |
| 82 | XGL_NOT_READY : XGL_SUCCESS; |
| 83 | } |
| 84 | |
| 85 | XGL_RESULT intel_fence_wait(struct intel_fence *fence, int64_t timeout_ns) |
| 86 | { |
| 87 | int err; |
| 88 | |
| 89 | if (!fence->submitted_bo) |
| 90 | return XGL_ERROR_UNAVAILABLE; |
| 91 | |
| 92 | err = intel_bo_wait(fence->submitted_bo, timeout_ns); |
| 93 | |
| 94 | return (err) ? XGL_NOT_READY : XGL_SUCCESS; |
| 95 | } |
| 96 | |
| 97 | XGL_RESULT XGLAPI intelCreateFence( |
| 98 | XGL_DEVICE device, |
| 99 | const XGL_FENCE_CREATE_INFO* pCreateInfo, |
| 100 | XGL_FENCE* pFence) |
| 101 | { |
| 102 | struct intel_dev *dev = intel_dev(device); |
| 103 | |
| 104 | return intel_fence_create(dev, pCreateInfo, |
| 105 | (struct intel_fence **) pFence); |
| 106 | } |
| 107 | |
| 108 | XGL_RESULT XGLAPI intelGetFenceStatus( |
| 109 | XGL_FENCE fence_) |
| 110 | { |
| 111 | struct intel_fence *fence = intel_fence(fence_); |
| 112 | |
| 113 | return intel_fence_get_status(fence); |
| 114 | } |
| 115 | |
| 116 | XGL_RESULT XGLAPI intelWaitForFences( |
| 117 | XGL_DEVICE device, |
| 118 | XGL_UINT fenceCount, |
| 119 | const XGL_FENCE* pFences, |
| 120 | XGL_BOOL waitAll, |
| 121 | XGL_UINT64 timeout) |
| 122 | { |
| 123 | XGL_RESULT ret = XGL_SUCCESS; |
| 124 | XGL_UINT i; |
| 125 | |
| 126 | for (i = 0; i < fenceCount; i++) { |
| 127 | struct intel_fence *fence = intel_fence(pFences[i]); |
| 128 | int64_t ns; |
| 129 | XGL_RESULT r; |
| 130 | |
| 131 | if (timeout <= (XGL_UINT64) (INT64_MAX / 1000 / 1000 / 1000)) |
| 132 | ns = timeout * 1000 * 1000 * 1000; |
| 133 | else |
| 134 | ns = -1; |
| 135 | |
| 136 | r = intel_fence_wait(fence, ns); |
| 137 | |
| 138 | if (!waitAll && r == XGL_SUCCESS) |
| 139 | return XGL_SUCCESS; |
| 140 | |
| 141 | if (r != XGL_SUCCESS) |
| 142 | ret = r; |
| 143 | } |
| 144 | |
| 145 | return ret; |
| 146 | } |