blob: 46abdace8fa523c708f5c282c4fca9679acb9f52 [file] [log] [blame]
Daniel Vetter7520a272016-08-15 16:07:02 +02001/*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#ifndef __DRM_FRAMEBUFFER_H__
24#define __DRM_FRAMEBUFFER_H__
25
26#include <linux/list.h>
27#include <linux/ctype.h>
28#include <drm/drm_modeset.h>
29
30struct drm_framebuffer;
31struct drm_file;
32struct drm_device;
33
34/**
35 * struct drm_framebuffer_funcs - framebuffer hooks
36 */
37struct drm_framebuffer_funcs {
38 /**
39 * @destroy:
40 *
41 * Clean up framebuffer resources, specifically also unreference the
42 * backing storage. The core guarantees to call this function for every
43 * framebuffer successfully created by ->fb_create() in
44 * &drm_mode_config_funcs. Drivers must also call
45 * drm_framebuffer_cleanup() to release DRM core resources for this
46 * framebuffer.
47 */
48 void (*destroy)(struct drm_framebuffer *framebuffer);
49
50 /**
51 * @create_handle:
52 *
53 * Create a buffer handle in the driver-specific buffer manager (either
54 * GEM or TTM) valid for the passed-in struct &drm_file. This is used by
55 * the core to implement the GETFB IOCTL, which returns (for
56 * sufficiently priviledged user) also a native buffer handle. This can
57 * be used for seamless transitions between modesetting clients by
58 * copying the current screen contents to a private buffer and blending
59 * between that and the new contents.
60 *
61 * GEM based drivers should call drm_gem_handle_create() to create the
62 * handle.
63 *
64 * RETURNS:
65 *
66 * 0 on success or a negative error code on failure.
67 */
68 int (*create_handle)(struct drm_framebuffer *fb,
69 struct drm_file *file_priv,
70 unsigned int *handle);
71 /**
72 * @dirty:
73 *
74 * Optional callback for the dirty fb IOCTL.
75 *
76 * Userspace can notify the driver via this callback that an area of the
77 * framebuffer has changed and should be flushed to the display
78 * hardware. This can also be used internally, e.g. by the fbdev
79 * emulation, though that's not the case currently.
80 *
81 * See documentation in drm_mode.h for the struct drm_mode_fb_dirty_cmd
82 * for more information as all the semantics and arguments have a one to
83 * one mapping on this function.
84 *
85 * RETURNS:
86 *
87 * 0 on success or a negative error code on failure.
88 */
89 int (*dirty)(struct drm_framebuffer *framebuffer,
90 struct drm_file *file_priv, unsigned flags,
91 unsigned color, struct drm_clip_rect *clips,
92 unsigned num_clips);
93};
94
95struct drm_framebuffer {
96 struct drm_device *dev;
97 /*
98 * Note that the fb is refcounted for the benefit of driver internals,
99 * for example some hw, disabling a CRTC/plane is asynchronous, and
100 * scanout does not actually complete until the next vblank. So some
101 * cleanup (like releasing the reference(s) on the backing GEM bo(s))
102 * should be deferred. In cases like this, the driver would like to
103 * hold a ref to the fb even though it has already been removed from
104 * userspace perspective.
105 * The refcount is stored inside the mode object.
106 */
107 /*
108 * Place on the dev->mode_config.fb_list, access protected by
109 * dev->mode_config.fb_lock.
110 */
111 struct list_head head;
112 struct drm_mode_object base;
113 const struct drm_framebuffer_funcs *funcs;
114 unsigned int pitches[4];
115 unsigned int offsets[4];
116 uint64_t modifier[4];
117 unsigned int width;
118 unsigned int height;
119 /* depth can be 15 or 16 */
120 unsigned int depth;
121 int bits_per_pixel;
122 int flags;
123 uint32_t pixel_format; /* fourcc format */
124 int hot_x;
125 int hot_y;
126 struct list_head filp_head;
127};
128
129int drm_framebuffer_init(struct drm_device *dev,
130 struct drm_framebuffer *fb,
131 const struct drm_framebuffer_funcs *funcs);
132struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
133 uint32_t id);
134void drm_framebuffer_remove(struct drm_framebuffer *fb);
135void drm_framebuffer_cleanup(struct drm_framebuffer *fb);
136void drm_framebuffer_unregister_private(struct drm_framebuffer *fb);
137
138/**
139 * drm_framebuffer_reference - incr the fb refcnt
140 * @fb: framebuffer
141 *
142 * This functions increments the fb's refcount.
143 */
144static inline void drm_framebuffer_reference(struct drm_framebuffer *fb)
145{
146 drm_mode_object_reference(&fb->base);
147}
148
149/**
150 * drm_framebuffer_unreference - unref a framebuffer
151 * @fb: framebuffer to unref
152 *
153 * This functions decrements the fb's refcount and frees it if it drops to zero.
154 */
155static inline void drm_framebuffer_unreference(struct drm_framebuffer *fb)
156{
157 drm_mode_object_unreference(&fb->base);
158}
159
160/**
161 * drm_framebuffer_read_refcount - read the framebuffer reference count.
162 * @fb: framebuffer
163 *
164 * This functions returns the framebuffer's reference count.
165 */
166static inline uint32_t drm_framebuffer_read_refcount(struct drm_framebuffer *fb)
167{
168 return atomic_read(&fb->base.refcount.refcount);
169}
170#endif