Chia-I Wu | f9911eb | 2014-08-06 13:50:31 +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 "mem.h" |
| 28 | |
| 29 | XGL_RESULT intel_mem_alloc(struct intel_dev *dev, |
| 30 | const XGL_MEMORY_ALLOC_INFO *info, |
| 31 | struct intel_mem **mem_ret) |
| 32 | { |
| 33 | struct intel_mem *mem; |
| 34 | |
| 35 | if (4096 % info->alignment) |
| 36 | return XGL_ERROR_INVALID_ALIGNMENT; |
| 37 | if (info->heapCount != 1 || info->heaps[0] != 0) |
| 38 | return XGL_ERROR_INVALID_POINTER; |
| 39 | |
| 40 | mem = icd_alloc(sizeof(*mem), 0, XGL_SYSTEM_ALLOC_API_OBJECT); |
| 41 | if (!mem) |
| 42 | return XGL_ERROR_OUT_OF_MEMORY; |
| 43 | |
| 44 | mem->base.dispatch = dev->base.dispatch; |
| 45 | if (dev->base.dbg) { |
| 46 | mem->base.dbg = |
| 47 | intel_base_dbg_create(XGL_DBG_OBJECT_GPU_MEMORY, NULL, 0); |
| 48 | if (!mem->base.dbg) { |
| 49 | intel_mem_free(mem); |
| 50 | return XGL_ERROR_OUT_OF_MEMORY; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | mem->bo = intel_winsys_alloc_buffer(dev->winsys, |
| 55 | "xgl-gpu-memory", info->allocationSize, 0); |
| 56 | if (!mem->bo) { |
| 57 | intel_mem_free(mem); |
| 58 | return XGL_ERROR_UNKNOWN; |
| 59 | } |
| 60 | |
| 61 | return XGL_SUCCESS; |
| 62 | } |
| 63 | |
| 64 | void intel_mem_free(struct intel_mem *mem) |
| 65 | { |
| 66 | if (mem->bo) |
| 67 | intel_bo_unreference(mem->bo); |
| 68 | |
| 69 | if (mem->base.dbg) |
| 70 | intel_base_dbg_destroy(mem->base.dbg); |
| 71 | |
| 72 | icd_free(mem); |
| 73 | } |
| 74 | |
| 75 | XGL_RESULT intel_mem_set_priority(struct intel_mem *mem, |
| 76 | XGL_MEMORY_PRIORITY priority) |
| 77 | { |
| 78 | /* pin the bo when XGL_MEMORY_PRIORITY_VERY_HIGH? */ |
| 79 | return XGL_SUCCESS; |
| 80 | } |
| 81 | |
| 82 | void *intel_mem_map(struct intel_mem *mem, XGL_FLAGS flags) |
| 83 | { |
| 84 | return intel_bo_map_unsynchronized(mem->bo); |
| 85 | } |
| 86 | |
| 87 | void intel_mem_unmap(struct intel_mem *mem) |
| 88 | { |
| 89 | intel_bo_unmap(mem->bo); |
| 90 | } |
| 91 | |
| 92 | XGL_RESULT XGLAPI intelAllocMemory( |
| 93 | XGL_DEVICE device, |
| 94 | const XGL_MEMORY_ALLOC_INFO* pAllocInfo, |
| 95 | XGL_GPU_MEMORY* pMem) |
| 96 | { |
| 97 | struct intel_dev *dev = intel_dev(device); |
| 98 | |
| 99 | return intel_mem_alloc(dev, pAllocInfo, (struct intel_mem **) pMem); |
| 100 | } |
| 101 | |
| 102 | XGL_RESULT XGLAPI intelFreeMemory( |
| 103 | XGL_GPU_MEMORY mem_) |
| 104 | { |
| 105 | struct intel_mem *mem = intel_mem(mem_); |
| 106 | |
| 107 | intel_mem_free(mem); |
| 108 | |
| 109 | return XGL_SUCCESS; |
| 110 | } |
| 111 | |
| 112 | XGL_RESULT XGLAPI intelSetMemoryPriority( |
| 113 | XGL_GPU_MEMORY mem_, |
| 114 | XGL_MEMORY_PRIORITY priority) |
| 115 | { |
| 116 | struct intel_mem *mem = intel_mem(mem_); |
| 117 | |
| 118 | return intel_mem_set_priority(mem, priority); |
| 119 | } |
| 120 | |
| 121 | XGL_RESULT XGLAPI intelMapMemory( |
| 122 | XGL_GPU_MEMORY mem_, |
| 123 | XGL_FLAGS flags, |
| 124 | XGL_VOID** ppData) |
| 125 | { |
| 126 | struct intel_mem *mem = intel_mem(mem_); |
| 127 | void *ptr = intel_mem_map(mem, flags); |
| 128 | |
| 129 | *ppData = ptr; |
| 130 | |
| 131 | return (ptr) ? XGL_SUCCESS : XGL_ERROR_UNKNOWN; |
| 132 | } |
| 133 | |
| 134 | XGL_RESULT XGLAPI intelUnmapMemory( |
| 135 | XGL_GPU_MEMORY mem_) |
| 136 | { |
| 137 | struct intel_mem *mem = intel_mem(mem_); |
| 138 | |
| 139 | intel_mem_unmap(mem); |
| 140 | |
| 141 | return XGL_SUCCESS; |
| 142 | } |