1/*
2 * Copyright 2012  Advanced Micro Devices, Inc.
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 shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23#ifndef RADEON_BO_HELPER_H
24#define RADEON_BO_HELPER_H 1
25
26#ifdef USE_GLAMOR
27#include <gbm.h>
28#endif
29
30#define RADEON_BO_FLAGS_GBM	0x1
31
32struct radeon_buffer {
33	union {
34#ifdef USE_GLAMOR
35		struct gbm_bo *gbm;
36#endif
37		struct radeon_bo *radeon;
38	} bo;
39	uint32_t ref_count;
40    uint32_t flags;
41};
42
43extern struct radeon_buffer *
44radeon_alloc_pixmap_bo(ScrnInfoPtr pScrn, int width, int height, int depth,
45		       int usage_hint, int bitsPerPixel, int *new_pitch,
46		       struct radeon_surface *new_surface, uint32_t *new_tiling);
47
48extern void
49radeon_finish(ScrnInfoPtr scrn, struct radeon_buffer *bo);
50
51extern void
52radeon_pixmap_clear(PixmapPtr pixmap);
53
54extern uint32_t
55radeon_get_pixmap_tiling_flags(PixmapPtr pPix);
56
57extern Bool
58radeon_share_pixmap_backing(struct radeon_bo *bo, void **handle_p);
59
60extern Bool
61radeon_set_shared_pixmap_backing(PixmapPtr ppix, void *fd_handle,
62				 struct radeon_surface *surface);
63
64/**
65 * get_drawable_pixmap() returns the backing pixmap for a given drawable.
66 *
67 * @param drawable the drawable being requested.
68 *
69 * This function returns the backing pixmap for a drawable, whether it is a
70 * redirected window, unredirected window, or already a pixmap.
71 */
72static inline PixmapPtr get_drawable_pixmap(DrawablePtr drawable)
73{
74    if (drawable->type == DRAWABLE_PIXMAP)
75	return (PixmapPtr)drawable;
76    else
77	return drawable->pScreen->GetWindowPixmap((WindowPtr)drawable);
78}
79
80static inline void
81radeon_buffer_ref(struct radeon_buffer *buffer)
82{
83    buffer->ref_count++;
84}
85
86static inline void
87radeon_buffer_unref(struct radeon_buffer **buffer)
88{
89    struct radeon_buffer *buf = *buffer;
90
91    if (!buf)
92	return;
93
94    if (buf->ref_count > 1) {
95	buf->ref_count--;
96	return;
97    }
98
99#ifdef USE_GLAMOR
100    if (buf->flags & RADEON_BO_FLAGS_GBM) {
101	gbm_bo_destroy(buf->bo.gbm);
102    } else
103#endif
104    {
105	radeon_bo_unmap(buf->bo.radeon);
106	radeon_bo_unref(buf->bo.radeon);
107    }
108
109    free(buf);
110    *buffer = NULL;
111}
112
113#endif /* RADEON_BO_HELPER_H */
114