intel: implement query functions
diff --git a/icd/intel/CMakeLists.txt b/icd/intel/CMakeLists.txt
index 222815a..0377a37 100644
--- a/icd/intel/CMakeLists.txt
+++ b/icd/intel/CMakeLists.txt
@@ -25,6 +25,7 @@
     gpu.c
     mem.c
     obj.c
+    query.c
     queue.c
     kmd/winsys_drm.c
     )
diff --git a/icd/intel/query.c b/icd/intel/query.c
new file mode 100644
index 0000000..df35e30
--- /dev/null
+++ b/icd/intel/query.c
@@ -0,0 +1,177 @@
+/*
+ * XGL
+ *
+ * Copyright (C) 2014 LunarG, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "dev.h"
+#include "mem.h"
+#include "query.h"
+
+static void query_destroy(struct intel_obj *obj)
+{
+    struct intel_query *query = intel_query_from_obj(obj);
+
+    intel_query_destroy(query);
+}
+
+static XGL_RESULT query_get_info(struct intel_base *base, int type,
+                                 XGL_SIZE *size, XGL_VOID *data)
+{
+    struct intel_query *query = intel_query_from_base(base);
+    XGL_RESULT ret = XGL_SUCCESS;
+
+    switch (type) {
+    case XGL_INFO_TYPE_MEMORY_REQUIREMENTS:
+        {
+            XGL_MEMORY_REQUIREMENTS *mem_req = data;
+
+            mem_req->size = query->slot_stride * query->slot_count;
+            mem_req->alignment = 64;
+            mem_req->heapCount = 1;
+            mem_req->heaps[0] = 0;
+
+            *size = sizeof(*mem_req);
+        }
+        break;
+    default:
+        ret = intel_base_get_info(base, type, size, data);
+        break;
+    }
+
+    return ret;
+}
+
+XGL_RESULT intel_query_create(struct intel_dev *dev,
+                              const XGL_QUERY_POOL_CREATE_INFO *info,
+                              struct intel_query **query_ret)
+{
+    struct intel_query *query;
+
+    query = (struct intel_query *) intel_base_create(sizeof(*query),
+            dev->base.dbg, XGL_DBG_OBJECT_QUERY_POOL, info, 0);
+    if (!query)
+        return XGL_ERROR_OUT_OF_MEMORY;
+
+    query->type = info->queryType;
+    query->slot_count = info->slots;
+
+    switch (info->queryType) {
+    case XGL_QUERY_OCCLUSION:
+        query->slot_stride = align(sizeof(uint32_t) * 2, 64);
+        break;
+    case XGL_QUERY_PIPELINE_STATISTICS:
+        query->slot_stride =
+            align(sizeof(XGL_PIPELINE_STATISTICS_DATA) * 2, 64);
+        break;
+    default:
+        break;
+    }
+
+    if (!query->slot_stride) {
+        intel_query_destroy(query);
+        return XGL_ERROR_INVALID_VALUE;
+    }
+
+    query->obj.base.get_info = query_get_info;
+    query->obj.destroy = query_destroy;
+
+    *query_ret = query;
+
+    return XGL_SUCCESS;
+}
+
+void intel_query_destroy(struct intel_query *query)
+{
+    intel_base_destroy(&query->obj.base);
+}
+
+XGL_RESULT intel_query_get_results(struct intel_query *query,
+                                   XGL_UINT slot_start, XGL_UINT slot_count,
+                                   void *results)
+{
+    const uint8_t *ptr;
+    XGL_UINT i;
+
+    if (!query->obj.mem)
+        return XGL_ERROR_MEMORY_NOT_BOUND;
+
+    if (intel_mem_is_busy(query->obj.mem))
+        return XGL_NOT_READY;
+
+    ptr = (const uint8_t *) intel_mem_map_sync(query->obj.mem, false);
+    if (!ptr)
+        return XGL_ERROR_MEMORY_MAP_FAILED;
+
+    ptr += query->obj.offset + query->slot_stride * slot_start;
+
+    switch (query->type) {
+    case XGL_QUERY_OCCLUSION:
+        for (i = 0; i < slot_count; i++) {
+            const uint32_t *pair = (const uint32_t *) ptr;
+
+            ((uint64_t *) results)[i] = pair[1] - pair[0];
+            ptr += query->slot_stride;
+        }
+        break;
+    case XGL_QUERY_PIPELINE_STATISTICS:
+        for (i = 0; i < slot_count; i++) {
+            const XGL_UINT count =
+                sizeof(XGL_PIPELINE_STATISTICS_DATA) / sizeof(uint64_t);
+            const uint64_t *before = (const uint64_t *) ptr;
+            const uint64_t *after = (const uint64_t *) ptr + count;
+            uint64_t *dst = (uint64_t *)
+                ((XGL_PIPELINE_STATISTICS_DATA *) results + i);
+            XGL_UINT j;
+
+            for (j = 0; j < count; j++)
+                dst[j] = after[j] - before[j];
+
+            ptr += query->slot_stride;
+        }
+        break;
+    default:
+        assert(0);
+        break;
+    }
+
+    intel_mem_unmap(query->obj.mem);
+
+    return XGL_SUCCESS;
+}
+
+XGL_RESULT XGLAPI intelCreateQueryPool(
+    XGL_DEVICE                                  device,
+    const XGL_QUERY_POOL_CREATE_INFO*           pCreateInfo,
+    XGL_QUERY_POOL*                             pQueryPool)
+{
+    return XGL_ERROR_UNAVAILABLE;
+}
+
+XGL_RESULT XGLAPI intelGetQueryPoolResults(
+    XGL_QUERY_POOL                              queryPool,
+    XGL_UINT                                    startQuery,
+    XGL_UINT                                    queryCount,
+    XGL_SIZE*                                   pDataSize,
+    XGL_VOID*                                   pData)
+{
+    return XGL_ERROR_UNAVAILABLE;
+}
diff --git a/icd/intel/query.h b/icd/intel/query.h
new file mode 100644
index 0000000..41e21d3
--- /dev/null
+++ b/icd/intel/query.h
@@ -0,0 +1,75 @@
+/*
+ * XGL
+ *
+ * Copyright (C) 2014 LunarG, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef QUERY_H
+#define QUERY_H
+
+#include "intel.h"
+#include "obj.h"
+
+struct intel_query {
+    struct intel_obj obj;
+
+    XGL_QUERY_TYPE type;
+    XGL_UINT slot_stride;
+    XGL_UINT slot_count;
+};
+
+static inline struct intel_query *intel_query(XGL_QUERY_POOL pool)
+{
+    return (struct intel_query *) pool;
+}
+
+static inline struct intel_query *intel_query_from_base(struct intel_base *base)
+{
+    return (struct intel_query *) base;
+}
+
+static inline struct intel_query *intel_query_from_obj(struct intel_obj *obj)
+{
+    return intel_query_from_base(&obj->base);
+}
+
+XGL_RESULT intel_query_create(struct intel_dev *dev,
+                              const XGL_QUERY_POOL_CREATE_INFO *info,
+                              struct intel_query **query_ret);
+void intel_query_destroy(struct intel_query *query);
+
+XGL_RESULT intel_query_get_results(struct intel_query *query,
+                                   XGL_UINT slot_start, XGL_UINT slot_count,
+                                   void *results);
+
+XGL_RESULT XGLAPI intelCreateQueryPool(
+    XGL_DEVICE                                  device,
+    const XGL_QUERY_POOL_CREATE_INFO*           pCreateInfo,
+    XGL_QUERY_POOL*                             pQueryPool);
+
+XGL_RESULT XGLAPI intelGetQueryPoolResults(
+    XGL_QUERY_POOL                              queryPool,
+    XGL_UINT                                    startQuery,
+    XGL_UINT                                    queryCount,
+    XGL_SIZE*                                   pDataSize,
+    XGL_VOID*                                   pData);
+
+#endif /* QUERY_H */