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 "util/u_cpu_detect.h"
25 #include "util/u_dl.h"
26 #include "swr_public.h"
27 #include "swr_screen.h"
28 
29 #include <stdio.h>
30 
31 // Helper function to resolve the backend filename based on architecture
32 static bool
swr_initialize_screen_interface(struct swr_screen * screen,const char arch[])33 swr_initialize_screen_interface(struct swr_screen *screen, const char arch[])
34 {
35 #ifdef HAVE_SWR_BUILTIN
36    screen->pLibrary = NULL;
37    screen->pfnSwrGetInterface = SwrGetInterface;
38    fprintf(stderr, "(using: builtin).\n");
39 #else
40    char filename[256] = { 0 };
41    sprintf(filename, "%sswr%s%s", UTIL_DL_PREFIX, arch, UTIL_DL_EXT);
42 
43    screen->pLibrary = util_dl_open(filename);
44    if (!screen->pLibrary) {
45       fprintf(stderr, "(skipping: %s).\n", util_dl_error());
46       return false;
47    }
48 
49    util_dl_proc pApiProc = util_dl_get_proc_address(screen->pLibrary,
50       "SwrGetInterface");
51    if (!pApiProc) {
52       fprintf(stderr, "(skipping: %s).\n", util_dl_error());
53       util_dl_close(screen->pLibrary);
54       screen->pLibrary = NULL;
55       return false;
56    }
57 
58    screen->pfnSwrGetInterface = (PFNSwrGetInterface)pApiProc;
59    fprintf(stderr, "(using: %s).\n", filename);
60 #endif
61    return true;
62 }
63 
64 
65 struct pipe_screen *
swr_create_screen(struct sw_winsys * winsys)66 swr_create_screen(struct sw_winsys *winsys)
67 {
68    struct pipe_screen *p_screen = swr_create_screen_internal(winsys);
69    if (!p_screen) {
70       return NULL;
71    }
72 
73    struct swr_screen *screen = swr_screen(p_screen);
74    screen->is_knl = false;
75 
76    util_cpu_detect();
77 
78    if (util_cpu_caps.has_avx512f && util_cpu_caps.has_avx512er) {
79       fprintf(stderr, "SWR detected KNL instruction support ");
80 #ifndef HAVE_SWR_KNL
81       fprintf(stderr, "(skipping: not built).\n");
82 #else
83       if (swr_initialize_screen_interface(screen, "KNL")) {
84          screen->is_knl = true;
85          return p_screen;
86       }
87 #endif
88    }
89 
90    if (util_cpu_caps.has_avx512f && util_cpu_caps.has_avx512bw) {
91       fprintf(stderr, "SWR detected SKX instruction support ");
92 #ifndef HAVE_SWR_SKX
93       fprintf(stderr, "(skipping not built).\n");
94 #else
95       if (swr_initialize_screen_interface(screen, "SKX"))
96          return p_screen;
97 #endif
98    }
99 
100    if (util_cpu_caps.has_avx2) {
101       fprintf(stderr, "SWR detected AVX2 instruction support ");
102 #ifndef HAVE_SWR_AVX2
103       fprintf(stderr, "(skipping not built).\n");
104 #else
105       if (swr_initialize_screen_interface(screen, "AVX2"))
106          return p_screen;
107 #endif
108    }
109 
110    if (util_cpu_caps.has_avx) {
111       fprintf(stderr, "SWR detected AVX instruction support ");
112 #ifndef HAVE_SWR_AVX
113       fprintf(stderr, "(skipping not built).\n");
114 #else
115       if (swr_initialize_screen_interface(screen, "AVX"))
116          return p_screen;
117 #endif
118    }
119 
120    fprintf(stderr, "SWR could not initialize a supported CPU architecture.\n");
121    swr_destroy_screen_internal(&screen);
122 
123    return NULL;
124 }
125 
126 
127 #ifdef _WIN32
128 // swap function called from libl_gdi.c
129 
130 void
swr_gdi_swap(struct pipe_screen * screen,struct pipe_resource * res,void * hDC)131 swr_gdi_swap(struct pipe_screen *screen,
132              struct pipe_resource *res,
133              void *hDC)
134 {
135    screen->flush_frontbuffer(screen,
136                              res,
137                              0, 0,
138                              hDC,
139                              NULL);
140 }
141 
142 #endif /* _WIN32 */
143