1/*
2 * Copyright © 2010 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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24/**
25 * @file brw_object_purgeable.c
26 *
27 * The driver implementation of the GL_APPLE_object_purgeable extension.
28 */
29
30#include "main/imports.h"
31#include "main/mtypes.h"
32#include "main/macros.h"
33#include "main/bufferobj.h"
34
35#include "brw_context.h"
36#include "intel_buffer_objects.h"
37#include "intel_fbo.h"
38#include "intel_mipmap_tree.h"
39
40static GLenum
41intel_buffer_purgeable(struct brw_bo *buffer)
42{
43   int retained = 0;
44
45   if (buffer != NULL)
46      retained = brw_bo_madvise(buffer, I915_MADV_DONTNEED);
47
48   return retained ? GL_VOLATILE_APPLE : GL_RELEASED_APPLE;
49}
50
51static GLenum
52intel_buffer_object_purgeable(struct gl_context * ctx,
53                              struct gl_buffer_object *obj,
54                              GLenum option)
55{
56   struct intel_buffer_object *intel_obj = intel_buffer_object(obj);
57
58   if (intel_obj->buffer != NULL)
59      return intel_buffer_purgeable(intel_obj->buffer);
60
61   if (option == GL_RELEASED_APPLE) {
62      return GL_RELEASED_APPLE;
63   } else {
64      /* XXX Create the buffer and madvise(MADV_DONTNEED)? */
65      return intel_buffer_purgeable(intel_obj->buffer);
66   }
67}
68
69static GLenum
70intel_texture_object_purgeable(struct gl_context * ctx,
71                               struct gl_texture_object *obj,
72                               GLenum option)
73{
74   struct intel_texture_object *intel;
75
76   (void) ctx;
77   (void) option;
78
79   intel = intel_texture_object(obj);
80   if (intel->mt == NULL || intel->mt->bo == NULL)
81      return GL_RELEASED_APPLE;
82
83   return intel_buffer_purgeable(intel->mt->bo);
84}
85
86static GLenum
87intel_render_object_purgeable(struct gl_context * ctx,
88                              struct gl_renderbuffer *obj,
89                              GLenum option)
90{
91   struct intel_renderbuffer *intel;
92
93   (void) ctx;
94   (void) option;
95
96   intel = intel_renderbuffer(obj);
97   if (intel->mt == NULL)
98      return GL_RELEASED_APPLE;
99
100   return intel_buffer_purgeable(intel->mt->bo);
101}
102
103static int
104intel_bo_unpurgeable(struct brw_bo *buffer)
105{
106   int retained;
107
108   retained = 0;
109   if (buffer != NULL)
110      retained = brw_bo_madvise(buffer, I915_MADV_WILLNEED);
111
112   return retained;
113}
114
115static GLenum
116intel_buffer_object_unpurgeable(struct gl_context * ctx,
117                                struct gl_buffer_object *obj,
118                                GLenum option)
119{
120   struct intel_buffer_object *intel = intel_buffer_object(obj);
121
122   (void) ctx;
123
124   if (!intel->buffer)
125      return GL_UNDEFINED_APPLE;
126
127   if (option == GL_UNDEFINED_APPLE || !intel_bo_unpurgeable(intel->buffer)) {
128      brw_bo_unreference(intel->buffer);
129      intel->buffer = NULL;
130      return GL_UNDEFINED_APPLE;
131   }
132
133   return GL_RETAINED_APPLE;
134}
135
136static GLenum
137intel_texture_object_unpurgeable(struct gl_context * ctx,
138                                 struct gl_texture_object *obj,
139                                 GLenum option)
140{
141   struct intel_texture_object *intel;
142
143   (void) ctx;
144
145   intel = intel_texture_object(obj);
146   if (intel->mt == NULL || intel->mt->bo == NULL)
147      return GL_UNDEFINED_APPLE;
148
149   if (option == GL_UNDEFINED_APPLE || !intel_bo_unpurgeable(intel->mt->bo)) {
150      intel_miptree_release(&intel->mt);
151      return GL_UNDEFINED_APPLE;
152   }
153
154   return GL_RETAINED_APPLE;
155}
156
157static GLenum
158intel_render_object_unpurgeable(struct gl_context * ctx,
159                                struct gl_renderbuffer *obj,
160                                GLenum option)
161{
162   struct intel_renderbuffer *intel;
163
164   (void) ctx;
165
166   intel = intel_renderbuffer(obj);
167   if (intel->mt == NULL)
168      return GL_UNDEFINED_APPLE;
169
170   if (option == GL_UNDEFINED_APPLE || !intel_bo_unpurgeable(intel->mt->bo)) {
171      intel_miptree_release(&intel->mt);
172      return GL_UNDEFINED_APPLE;
173   }
174
175   return GL_RETAINED_APPLE;
176}
177
178void
179brw_init_object_purgeable_functions(struct dd_function_table *functions)
180{
181   functions->BufferObjectPurgeable = intel_buffer_object_purgeable;
182   functions->TextureObjectPurgeable = intel_texture_object_purgeable;
183   functions->RenderObjectPurgeable = intel_render_object_purgeable;
184
185   functions->BufferObjectUnpurgeable = intel_buffer_object_unpurgeable;
186   functions->TextureObjectUnpurgeable = intel_texture_object_unpurgeable;
187   functions->RenderObjectUnpurgeable = intel_render_object_unpurgeable;
188}
189