xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/renderonly/renderonly.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (C) 2016 Christian Gmeiner <[email protected]>
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 FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  * Authors:
24  *    Christian Gmeiner <[email protected]>
25  */
26 
27 #ifndef RENDERONLY_H
28 #define RENDERONLY_H
29 
30 #include <stdint.h>
31 #include "frontend/drm_driver.h"
32 #include "pipe/p_state.h"
33 #include "util/simple_mtx.h"
34 #include "util/sparse_array.h"
35 
36 struct renderonly_scanout {
37    uint32_t handle;
38    uint32_t stride;
39    int32_t refcnt;
40 };
41 
42 struct renderonly {
43    /**
44     * Create a renderonly_scanout object for scanout resource.
45     *
46     * This function creates a renderonly_scanout object based on the provided
47     * resource. The library is designed that the driver specific pipe_resource
48     * struct holds a pointer to a renderonly_scanout struct.
49     *
50     * struct driver_resource {
51     *    struct pipe_resource base;
52     *    struct renderonly_scanout *scanout;
53     *   ...
54     * };
55     *
56     * The renderonly_scanout object exits for two reasons:
57     * - Do any special treatment for a scanout resource like importing the GPU
58     *   resource into the scanout hw.
59     * - Make it easier for a gallium driver to detect if anything special needs
60     *   to be done in flush_resource(..) like a resolve to linear.
61     *
62     * When the screen has renderonly enabled, drivers need to follow these
63     * rules:
64     * - Create the scanout resource in resource_create and
65     *   resource_create_with_modifiers if PIPE_BIND_SCANOUT is set. Drivers
66     *   can fail if the scanout resource couldn't be created.
67     * - Try to import the scanout resource in resource_from_handle with
68     *   renderonly_create_gpu_import_for_resource. Drivers MUST NOT fail if
69     *   the scanout resource couldn't be created.
70     * - In a resource_get_handle call for WINSYS_HANDLE_TYPE_KMS, use
71     *   renderonly_get_handle with the scanout resource, even if the scanout
72     *   resource is NULL. Drivers MUST NOT return their own resource here,
73     *   because the GEM handle will not be valid for the caller's DRM FD.
74     * - Implement resource_get_params for at least PIPE_RESOURCE_PARAM_STRIDE,
75     *   PIPE_RESOURCE_PARAM_OFFSET and PIPE_RESOURCE_PARAM_MODIFIER.
76     */
77    struct renderonly_scanout *(*create_for_resource)(struct pipe_resource *rsc,
78                                                      struct renderonly *ro,
79                                                      struct winsys_handle *out_handle);
80    void (*destroy)(struct renderonly *ro);
81    int kms_fd;
82    int gpu_fd;
83 
84    simple_mtx_t bo_map_lock;
85    struct util_sparse_array bo_map;
86 };
87 
88 static inline struct renderonly_scanout *
renderonly_scanout_for_resource(struct pipe_resource * rsc,struct renderonly * ro,struct winsys_handle * out_handle)89 renderonly_scanout_for_resource(struct pipe_resource *rsc,
90                                 struct renderonly *ro,
91                                 struct winsys_handle *out_handle)
92 {
93    return ro->create_for_resource(rsc, ro, out_handle);
94 }
95 
96 void
97 renderonly_scanout_destroy(struct renderonly_scanout *scanout,
98 			   struct renderonly *ro);
99 
100 static inline bool
renderonly_get_handle(struct renderonly_scanout * scanout,struct winsys_handle * handle)101 renderonly_get_handle(struct renderonly_scanout *scanout,
102       struct winsys_handle *handle)
103 {
104    if (!scanout)
105       return false;
106 
107    assert(handle->type == WINSYS_HANDLE_TYPE_KMS);
108    handle->handle = scanout->handle;
109    handle->stride = scanout->stride;
110 
111    return true;
112 }
113 
114 /**
115  * Create a dumb buffer object for a resource at scanout hw.
116  */
117 struct renderonly_scanout *
118 renderonly_create_kms_dumb_buffer_for_resource(struct pipe_resource *rsc,
119                                                struct renderonly *ro,
120                                                struct winsys_handle *out_handle);
121 
122 /**
123  * Import GPU resource into scanout hw.
124  */
125 struct renderonly_scanout *
126 renderonly_create_gpu_import_for_resource(struct pipe_resource *rsc,
127                                           struct renderonly *ro,
128                                           struct winsys_handle *out_handle);
129 
130 #endif /* RENDERONLY_H_ */
131