1/*
2 * Copyright � 2006 Intel Corporation
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 *    Eric Anholt <anholt@FreeBSD.org>
25 *
26 */
27
28#ifdef HAVE_CONFIG_H
29#include <xorg-config.h>
30#endif
31
32#include <string.h>
33
34#include "exa_priv.h"
35
36#include "xf86str.h"
37#include "xf86.h"
38
39typedef struct _ExaXorgScreenPrivRec {
40    CloseScreenProcPtr 		 SavedCloseScreen;
41    EnableDisableFBAccessProcPtr SavedEnableDisableFBAccess;
42    OptionInfoPtr		 options;
43} ExaXorgScreenPrivRec, *ExaXorgScreenPrivPtr;
44
45static DevPrivateKeyRec exaXorgScreenPrivateKeyRec;
46#define exaXorgScreenPrivateKey (&exaXorgScreenPrivateKeyRec)
47
48typedef enum {
49    EXAOPT_MIGRATION_HEURISTIC,
50    EXAOPT_NO_COMPOSITE,
51    EXAOPT_NO_UTS,
52    EXAOPT_NO_DFS,
53    EXAOPT_OPTIMIZE_MIGRATION
54} EXAOpts;
55
56static const OptionInfoRec EXAOptions[] = {
57    { EXAOPT_MIGRATION_HEURISTIC,	"MigrationHeuristic",
58				OPTV_ANYSTR,	{0}, FALSE },
59    { EXAOPT_NO_COMPOSITE,		"EXANoComposite",
60				OPTV_BOOLEAN,	{0}, FALSE },
61    { EXAOPT_NO_UTS,			"EXANoUploadToScreen",
62				OPTV_BOOLEAN,	{0}, FALSE },
63    { EXAOPT_NO_DFS,			"EXANoDownloadFromScreen",
64				OPTV_BOOLEAN,	{0}, FALSE },
65    { EXAOPT_OPTIMIZE_MIGRATION,	"EXAOptimizeMigration",
66				OPTV_BOOLEAN,	{0}, FALSE },
67    { -1,				NULL,
68				OPTV_NONE,	{0}, FALSE }
69};
70
71static Bool
72exaXorgCloseScreen (int i, ScreenPtr pScreen)
73{
74    ScrnInfoPtr pScrn = XF86SCRNINFO(pScreen);
75    ExaXorgScreenPrivPtr pScreenPriv = (ExaXorgScreenPrivPtr)
76	dixLookupPrivate(&pScreen->devPrivates, exaXorgScreenPrivateKey);
77
78    pScreen->CloseScreen = pScreenPriv->SavedCloseScreen;
79
80    pScrn->EnableDisableFBAccess = pScreenPriv->SavedEnableDisableFBAccess;
81
82    free(pScreenPriv->options);
83    free(pScreenPriv);
84
85    return pScreen->CloseScreen (i, pScreen);
86}
87
88static void
89exaXorgEnableDisableFBAccess (int index, Bool enable)
90{
91    ScreenPtr pScreen = screenInfo.screens[index];
92    ExaXorgScreenPrivPtr pScreenPriv = (ExaXorgScreenPrivPtr)
93	dixLookupPrivate(&pScreen->devPrivates, exaXorgScreenPrivateKey);
94
95    if (!enable)
96	exaEnableDisableFBAccess (index, enable);
97
98    if (pScreenPriv->SavedEnableDisableFBAccess)
99       pScreenPriv->SavedEnableDisableFBAccess (index, enable);
100
101    if (enable)
102	exaEnableDisableFBAccess (index, enable);
103}
104
105/**
106 * This will be called during exaDriverInit, giving us the chance to set options
107 * and hook in our EnableDisableFBAccess.
108 */
109void
110exaDDXDriverInit(ScreenPtr pScreen)
111{
112    ExaScreenPriv(pScreen);
113    /* Do NOT use XF86SCRNINFO macro here!! */
114    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
115    ExaXorgScreenPrivPtr pScreenPriv;
116
117    if (!dixRegisterPrivateKey(&exaXorgScreenPrivateKeyRec, PRIVATE_SCREEN, 0))
118	return;
119
120    pScreenPriv = calloc(1, sizeof(ExaXorgScreenPrivRec));
121    if (pScreenPriv == NULL)
122	return;
123
124    pScreenPriv->options = xnfalloc (sizeof(EXAOptions));
125    memcpy(pScreenPriv->options, EXAOptions, sizeof(EXAOptions));
126    xf86ProcessOptions (pScrn->scrnIndex, pScrn->options, pScreenPriv->options);
127
128    if (pExaScr->info->flags & EXA_OFFSCREEN_PIXMAPS) {
129	if (!(pExaScr->info->flags & EXA_HANDLES_PIXMAPS) &&
130	    pExaScr->info->offScreenBase < pExaScr->info->memorySize) {
131	    char *heuristicName;
132
133	    heuristicName = xf86GetOptValString (pScreenPriv->options,
134						 EXAOPT_MIGRATION_HEURISTIC);
135	    if (heuristicName != NULL) {
136		if (strcmp(heuristicName, "greedy") == 0)
137		    pExaScr->migration = ExaMigrationGreedy;
138		else if (strcmp(heuristicName, "always") == 0)
139		    pExaScr->migration = ExaMigrationAlways;
140		else if (strcmp(heuristicName, "smart") == 0)
141		    pExaScr->migration = ExaMigrationSmart;
142		else {
143		    xf86DrvMsg (pScreen->myNum, X_WARNING,
144				"EXA: unknown migration heuristic %s\n",
145				heuristicName);
146		}
147	    }
148	}
149
150	pExaScr->optimize_migration =
151	    xf86ReturnOptValBool(pScreenPriv->options,
152				 EXAOPT_OPTIMIZE_MIGRATION,
153				 TRUE);
154    }
155
156    if (xf86ReturnOptValBool(pScreenPriv->options,
157                             EXAOPT_NO_COMPOSITE, FALSE)) {
158	xf86DrvMsg(pScreen->myNum, X_CONFIG,
159		   "EXA: Disabling Composite operation "
160		   "(RENDER acceleration)\n");
161	pExaScr->info->CheckComposite = NULL;
162	pExaScr->info->PrepareComposite = NULL;
163    }
164
165    if (xf86ReturnOptValBool(pScreenPriv->options, EXAOPT_NO_UTS, FALSE)) {
166	xf86DrvMsg(pScreen->myNum, X_CONFIG,
167		   "EXA: Disabling UploadToScreen\n");
168	pExaScr->info->UploadToScreen = NULL;
169    }
170
171    if (xf86ReturnOptValBool(pScreenPriv->options, EXAOPT_NO_DFS, FALSE)) {
172	xf86DrvMsg(pScreen->myNum, X_CONFIG,
173		   "EXA: Disabling DownloadFromScreen\n");
174	pExaScr->info->DownloadFromScreen = NULL;
175    }
176
177    dixSetPrivate(&pScreen->devPrivates, exaXorgScreenPrivateKey, pScreenPriv);
178
179    pScreenPriv->SavedEnableDisableFBAccess = pScrn->EnableDisableFBAccess;
180    pScrn->EnableDisableFBAccess = exaXorgEnableDisableFBAccess;
181
182    pScreenPriv->SavedCloseScreen = pScreen->CloseScreen;
183    pScreen->CloseScreen = exaXorgCloseScreen;
184
185}
186
187static XF86ModuleVersionInfo exaVersRec =
188{
189	"exa",
190	MODULEVENDORSTRING,
191	MODINFOSTRING1,
192	MODINFOSTRING2,
193	XORG_VERSION_CURRENT,
194	EXA_VERSION_MAJOR, EXA_VERSION_MINOR, EXA_VERSION_RELEASE,
195	ABI_CLASS_VIDEODRV,		/* requires the video driver ABI */
196	ABI_VIDEODRV_VERSION,
197	MOD_CLASS_NONE,
198	{0,0,0,0}
199};
200
201_X_EXPORT XF86ModuleData exaModuleData = { &exaVersRec, NULL, NULL };
202