1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2016 Christian Gmeiner <christian.gmeiner@gmail.com>
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Christian Gmeiner <christian.gmeiner@gmail.com>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#ifndef RENDERONLY_H
28bf215546Sopenharmony_ci#define RENDERONLY_H
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include <stdint.h>
31bf215546Sopenharmony_ci#include "frontend/drm_driver.h"
32bf215546Sopenharmony_ci#include "pipe/p_state.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_cistruct renderonly_scanout {
35bf215546Sopenharmony_ci   uint32_t handle;
36bf215546Sopenharmony_ci   uint32_t stride;
37bf215546Sopenharmony_ci};
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_cistruct renderonly {
40bf215546Sopenharmony_ci   /**
41bf215546Sopenharmony_ci    * Create a renderonly_scanout object for scanout resource.
42bf215546Sopenharmony_ci    *
43bf215546Sopenharmony_ci    * This function creates a renderonly_scanout object based on the provided
44bf215546Sopenharmony_ci    * resource. The library is designed that the driver specific pipe_resource
45bf215546Sopenharmony_ci    * struct holds a pointer to a renderonly_scanout struct.
46bf215546Sopenharmony_ci    *
47bf215546Sopenharmony_ci    * struct driver_resource {
48bf215546Sopenharmony_ci    *    struct pipe_resource base;
49bf215546Sopenharmony_ci    *    struct renderonly_scanout *scanout;
50bf215546Sopenharmony_ci    *   ...
51bf215546Sopenharmony_ci    * };
52bf215546Sopenharmony_ci    *
53bf215546Sopenharmony_ci    * The renderonly_scanout object exits for two reasons:
54bf215546Sopenharmony_ci    * - Do any special treatment for a scanout resource like importing the GPU
55bf215546Sopenharmony_ci    *   resource into the scanout hw.
56bf215546Sopenharmony_ci    * - Make it easier for a gallium driver to detect if anything special needs
57bf215546Sopenharmony_ci    *   to be done in flush_resource(..) like a resolve to linear.
58bf215546Sopenharmony_ci    *
59bf215546Sopenharmony_ci    * When the screen has renderonly enabled, drivers need to follow these
60bf215546Sopenharmony_ci    * rules:
61bf215546Sopenharmony_ci    * - Create the scanout resource in resource_create and
62bf215546Sopenharmony_ci    *   resource_create_with_modifiers if PIPE_BIND_SCANOUT is set. Drivers
63bf215546Sopenharmony_ci    *   can fail if the scanout resource couldn't be created.
64bf215546Sopenharmony_ci    * - Try to import the scanout resource in resource_from_handle with
65bf215546Sopenharmony_ci    *   renderonly_create_gpu_import_for_resource. Drivers MUST NOT fail if
66bf215546Sopenharmony_ci    *   the scanout resource couldn't be created.
67bf215546Sopenharmony_ci    * - In a resource_get_handle call for WINSYS_HANDLE_TYPE_KMS, use
68bf215546Sopenharmony_ci    *   renderonly_get_handle with the scanout resource, even if the scanout
69bf215546Sopenharmony_ci    *   resource is NULL. Drivers MUST NOT return their own resource here,
70bf215546Sopenharmony_ci    *   because the GEM handle will not be valid for the caller's DRM FD.
71bf215546Sopenharmony_ci    * - Implement resource_get_params for at least PIPE_RESOURCE_PARAM_STRIDE,
72bf215546Sopenharmony_ci    *   PIPE_RESOURCE_PARAM_OFFSET and PIPE_RESOURCE_PARAM_MODIFIER.
73bf215546Sopenharmony_ci    */
74bf215546Sopenharmony_ci   struct renderonly_scanout *(*create_for_resource)(struct pipe_resource *rsc,
75bf215546Sopenharmony_ci                                                     struct renderonly *ro,
76bf215546Sopenharmony_ci                                                     struct winsys_handle *out_handle);
77bf215546Sopenharmony_ci   void (*destroy)(struct renderonly *ro);
78bf215546Sopenharmony_ci   int kms_fd;
79bf215546Sopenharmony_ci   int gpu_fd;
80bf215546Sopenharmony_ci};
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_cistatic inline struct renderonly_scanout *
83bf215546Sopenharmony_cirenderonly_scanout_for_resource(struct pipe_resource *rsc,
84bf215546Sopenharmony_ci                                struct renderonly *ro,
85bf215546Sopenharmony_ci                                struct winsys_handle *out_handle)
86bf215546Sopenharmony_ci{
87bf215546Sopenharmony_ci   return ro->create_for_resource(rsc, ro, out_handle);
88bf215546Sopenharmony_ci}
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_civoid
91bf215546Sopenharmony_cirenderonly_scanout_destroy(struct renderonly_scanout *scanout,
92bf215546Sopenharmony_ci			   struct renderonly *ro);
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_cistatic inline boolean
95bf215546Sopenharmony_cirenderonly_get_handle(struct renderonly_scanout *scanout,
96bf215546Sopenharmony_ci      struct winsys_handle *handle)
97bf215546Sopenharmony_ci{
98bf215546Sopenharmony_ci   if (!scanout)
99bf215546Sopenharmony_ci      return FALSE;
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   assert(handle->type == WINSYS_HANDLE_TYPE_KMS);
102bf215546Sopenharmony_ci   handle->handle = scanout->handle;
103bf215546Sopenharmony_ci   handle->stride = scanout->stride;
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   return TRUE;
106bf215546Sopenharmony_ci}
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci/**
109bf215546Sopenharmony_ci * Create a dumb buffer object for a resource at scanout hw.
110bf215546Sopenharmony_ci */
111bf215546Sopenharmony_cistruct renderonly_scanout *
112bf215546Sopenharmony_cirenderonly_create_kms_dumb_buffer_for_resource(struct pipe_resource *rsc,
113bf215546Sopenharmony_ci                                               struct renderonly *ro,
114bf215546Sopenharmony_ci                                               struct winsys_handle *out_handle);
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci/**
117bf215546Sopenharmony_ci * Import GPU resource into scanout hw.
118bf215546Sopenharmony_ci */
119bf215546Sopenharmony_cistruct renderonly_scanout *
120bf215546Sopenharmony_cirenderonly_create_gpu_import_for_resource(struct pipe_resource *rsc,
121bf215546Sopenharmony_ci                                          struct renderonly *ro,
122bf215546Sopenharmony_ci                                          struct winsys_handle *out_handle);
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci#endif /* RENDERONLY_H_ */
125