1 /**************************************************************************
2  *
3  * Copyright (C) 2018 Collabora Ltd
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR 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
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  **************************************************************************/
24 
25 #include <string.h>
26 #include <stdlib.h>
27 #include "vrend_tweaks.h"
28 #include "vrend_debug.h"
29 #include "virgl_protocol.h"
30 
get_tf3_samples_passed_factor(struct vrend_context_tweaks * ctx,void * params)31 inline static void get_tf3_samples_passed_factor(struct vrend_context_tweaks *ctx, void *params)
32 {
33    *(uint32_t *)params =  ctx->tf3_samples_passed_factor;
34 }
35 
vrend_get_tweak_is_active_with_params(struct vrend_context_tweaks * ctx,enum vrend_tweak_type t,void * params)36 bool vrend_get_tweak_is_active_with_params(struct vrend_context_tweaks *ctx, enum vrend_tweak_type t, void *params)
37 {
38    if (!(ctx->active_tweaks & (1u << t)))
39       return false;
40 
41    switch (t) {
42    case virgl_tweak_gles_tf3_samples_passes_multiplier:
43       get_tf3_samples_passed_factor(ctx, params); break;
44    default:
45       ;
46    }
47 
48    return true;
49 }
50 
vrend_get_tweak_is_active(struct vrend_context_tweaks * ctx,enum vrend_tweak_type t)51 bool vrend_get_tweak_is_active(struct vrend_context_tweaks *ctx, enum vrend_tweak_type t)
52 {
53    return (ctx->active_tweaks & (1u << t)) ? true : false;
54 }
55 
56 const char *tweak_debug_table[] = {
57    [virgl_tweak_gles_brga_emulate] =
58    "GLES: Skip linearization in blits to BGRA_UNORM surfaces",
59 
60    [virgl_tweak_gles_brga_apply_dest_swizzle] =
61    "GLES: Apply dest swizzle when a BGRA surface is emulated by an RGBA surface",
62 
63    [virgl_tweak_gles_tf3_samples_passes_multiplier] =
64     "GLES: Value to return when emulating GL_SAMPLES_PASSES by using GL_ANY_SAMPLES_PASSES",
65 
66    [virgl_tweak_undefined] = "Undefined tweak"
67 };
68 
set_tweak_and_params(struct vrend_context_tweaks * ctx,enum vrend_tweak_type t,uint32_t value)69 static void set_tweak_and_params(struct vrend_context_tweaks *ctx,
70                                  enum vrend_tweak_type t, uint32_t value)
71 {
72    ctx->active_tweaks |= 1u << t;
73 
74    switch (t) {
75    case virgl_tweak_gles_tf3_samples_passes_multiplier:
76       ctx->tf3_samples_passed_factor = value;
77       break;
78    default:
79       ;
80    }
81 }
82 
set_tweak_and_params_from_string(struct vrend_context_tweaks * ctx,enum vrend_tweak_type t,const char * value)83 static void set_tweak_and_params_from_string(struct vrend_context_tweaks *ctx,
84                                              enum vrend_tweak_type t, const char *value)
85 {
86    ctx->active_tweaks |= 1u << t;
87 
88    switch (t) {
89    case virgl_tweak_gles_tf3_samples_passes_multiplier:
90       ctx->tf3_samples_passed_factor = value ? atoi(value) : 2048;
91       break;
92    default:
93       ;
94    }
95 }
96 
97 /* we expect a string like tweak1:value,tweak2:value */
vrend_set_active_tweaks(struct vrend_context_tweaks * ctx,uint32_t tweak_id,uint32_t value)98 void vrend_set_active_tweaks(struct vrend_context_tweaks *ctx, uint32_t tweak_id, uint32_t value)
99 {
100    if (tweak_id < virgl_tweak_undefined) {
101       VREND_DEBUG(dbg_tweak, NULL, "Apply tweak '%s' = %u\n", tweak_debug_table[tweak_id], value);
102       set_tweak_and_params(ctx, tweak_id, value);
103    } else {
104       VREND_DEBUG(dbg_tweak, NULL, "Unknown tweak %d = %d sent\n", tweak_id, value);
105    }
106 }
107 
108 struct {
109    enum vrend_tweak_type flag;
110    const char *name;
111    const char *descr;
112 } tweak_table [] = {
113    { virgl_tweak_gles_brga_emulate, "emu-bgra",
114      "Emulate BGRA_UNORM and BGRA_SRB by using swizzled RGBA formats" },
115 
116    { virgl_tweak_gles_brga_apply_dest_swizzle, "bgra-dest-swz",
117      "Apply the destination swizzle of emulated BGRA surfaces in blits"},
118 
119    { virgl_tweak_gles_tf3_samples_passes_multiplier, "samples-passed",
120      "Return this value when GL_SAMPLES_PASSED is emulated by GL_ANY_SAMPLES_PASSED"},
121 
122    { virgl_tweak_undefined, NULL, NULL}
123 };
124 
125 
vrend_set_tweak_from_env(struct vrend_context_tweaks * ctx)126 void vrend_set_tweak_from_env(struct vrend_context_tweaks *ctx)
127 {
128    char *tweaks = getenv("VREND_TWEAK");
129    if (tweaks) {
130       VREND_DEBUG(dbg_tweak, NULL, "ENV tweaks %s\n", tweaks);
131 
132       char *saveptr;
133       char *tweak_descr_copy = strdup(tweaks);
134 
135       char *tweak = strtok_r(tweak_descr_copy, ":", &saveptr);
136       while (tweak) {
137          char *tweak_param = strtok_r(NULL, ",", &saveptr);
138 
139          for (int i = 0; i < virgl_tweak_undefined; ++i) {
140             if (!strcmp(tweak, tweak_table[i].name)) {
141                VREND_DEBUG(dbg_tweak, NULL, "Apply tweak %s=%s\n", tweak, tweak_param);
142                set_tweak_and_params_from_string(ctx, tweak_table[i].flag, tweak_param);
143             }
144          }
145          tweak = strtok_r(NULL, ":", &saveptr);
146       }
147       free(tweak_descr_copy);
148    }
149 }
150