1/****************************************************************************
2 * Copyright (C) 2016 Intel Corporation.   All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 ***************************************************************************/
23
24#include "memory/InitMemory.h"
25#include "util/u_cpu_detect.h"
26#include "util/u_dl.h"
27#include "swr_public.h"
28#include "swr_screen.h"
29
30#include <stdio.h>
31
32// Helper function to resolve the backend filename based on architecture
33static bool
34swr_initialize_screen_interface(struct swr_screen *screen, const char arch[])
35{
36#ifdef HAVE_SWR_BUILTIN
37   screen->pLibrary = NULL;
38   screen->pfnSwrGetInterface = SwrGetInterface;
39   InitTilesTable();
40   fprintf(stderr, "(using: builtin).\n");
41#else
42   char filename[256] = { 0 };
43   sprintf(filename, "%sswr%s%s", UTIL_DL_PREFIX, arch, UTIL_DL_EXT);
44
45   screen->pLibrary = util_dl_open(filename);
46   if (!screen->pLibrary) {
47      fprintf(stderr, "(skipping: %s).\n", util_dl_error());
48      return false;
49   }
50
51   util_dl_proc pApiProc = util_dl_get_proc_address(screen->pLibrary,
52      "SwrGetInterface");
53   util_dl_proc pInitFunc = util_dl_get_proc_address(screen->pLibrary,
54      "InitTilesTable");
55   if (!pApiProc || !pInitFunc) {
56      fprintf(stderr, "(skipping: %s).\n", util_dl_error());
57      util_dl_close(screen->pLibrary);
58      screen->pLibrary = NULL;
59      return false;
60   }
61
62   screen->pfnSwrGetInterface = (PFNSwrGetInterface)pApiProc;
63   pInitFunc();
64
65   fprintf(stderr, "(using: %s).\n", filename);
66#endif
67   return true;
68}
69
70
71struct pipe_screen *
72swr_create_screen(struct sw_winsys *winsys)
73{
74   struct pipe_screen *p_screen = swr_create_screen_internal(winsys);
75   if (!p_screen) {
76      return NULL;
77   }
78
79   struct swr_screen *screen = swr_screen(p_screen);
80   screen->is_knl = false;
81
82   util_cpu_detect();
83
84   if (util_cpu_caps.has_avx512f && util_cpu_caps.has_avx512er) {
85      fprintf(stderr, "SWR detected KNL instruction support ");
86#ifndef HAVE_SWR_KNL
87      fprintf(stderr, "(skipping: not built).\n");
88#else
89      if (swr_initialize_screen_interface(screen, "KNL")) {
90         screen->is_knl = true;
91         return p_screen;
92      }
93#endif
94   }
95
96   if (util_cpu_caps.has_avx512f && util_cpu_caps.has_avx512bw) {
97      fprintf(stderr, "SWR detected SKX instruction support ");
98#ifndef HAVE_SWR_SKX
99      fprintf(stderr, "(skipping not built).\n");
100#else
101      if (swr_initialize_screen_interface(screen, "SKX"))
102         return p_screen;
103#endif
104   }
105
106   if (util_cpu_caps.has_avx2) {
107      fprintf(stderr, "SWR detected AVX2 instruction support ");
108#ifndef HAVE_SWR_AVX2
109      fprintf(stderr, "(skipping not built).\n");
110#else
111      if (swr_initialize_screen_interface(screen, "AVX2"))
112         return p_screen;
113#endif
114   }
115
116   if (util_cpu_caps.has_avx) {
117      fprintf(stderr, "SWR detected AVX instruction support ");
118#ifndef HAVE_SWR_AVX
119      fprintf(stderr, "(skipping not built).\n");
120#else
121      if (swr_initialize_screen_interface(screen, "AVX"))
122         return p_screen;
123#endif
124   }
125
126   fprintf(stderr, "SWR could not initialize a supported CPU architecture.\n");
127   swr_destroy_screen_internal(&screen);
128
129   return NULL;
130}
131
132
133#ifdef _WIN32
134// swap function called from libl_gdi.c
135
136void
137swr_gdi_swap(struct pipe_screen *screen,
138             struct pipe_resource *res,
139             void *hDC)
140{
141   screen->flush_frontbuffer(screen,
142                             res,
143                             0, 0,
144                             hDC,
145                             NULL);
146}
147
148#endif /* _WIN32 */
149