i915: Fork the shared code from i965.
[mesa.git] / src / mesa / drivers / dri / intel / intel_buffers.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "intel_context.h"
29 #include "intel_buffers.h"
30 #include "intel_fbo.h"
31 #include "intel_mipmap_tree.h"
32
33 #include "main/fbobject.h"
34 #include "main/framebuffer.h"
35 #include "main/renderbuffer.h"
36
37 /**
38 * Return pointer to current color reading region, or NULL.
39 */
40 struct intel_region *
41 intel_readbuf_region(struct intel_context *intel)
42 {
43 struct intel_renderbuffer *irb
44 = intel_renderbuffer(intel->ctx.ReadBuffer->_ColorReadBuffer);
45 if (irb && irb->mt)
46 return irb->mt->region;
47 else
48 return NULL;
49 }
50
51 /**
52 * Check if we're about to draw into the front color buffer.
53 * If so, set the intel->front_buffer_dirty field to true.
54 */
55 void
56 intel_check_front_buffer_rendering(struct intel_context *intel)
57 {
58 const struct gl_framebuffer *fb = intel->ctx.DrawBuffer;
59 if (_mesa_is_winsys_fbo(fb)) {
60 /* drawing to window system buffer */
61 if (fb->_NumColorDrawBuffers > 0) {
62 if (fb->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT) {
63 intel->front_buffer_dirty = true;
64 }
65 }
66 }
67 }
68
69 static void
70 intelDrawBuffer(struct gl_context * ctx, GLenum mode)
71 {
72 if (ctx->DrawBuffer && _mesa_is_winsys_fbo(ctx->DrawBuffer)) {
73 struct intel_context *const intel = intel_context(ctx);
74 const bool was_front_buffer_rendering =
75 intel->is_front_buffer_rendering;
76
77 intel->is_front_buffer_rendering = (mode == GL_FRONT_LEFT)
78 || (mode == GL_FRONT) || (mode == GL_FRONT_AND_BACK);
79
80 /* If we weren't front-buffer rendering before but we are now,
81 * invalidate our DRI drawable so we'll ask for new buffers
82 * (including the fake front) before we start rendering again.
83 */
84 if (!was_front_buffer_rendering && intel->is_front_buffer_rendering)
85 dri2InvalidateDrawable(intel->driContext->driDrawablePriv);
86 }
87
88 intel_draw_buffer(ctx);
89 }
90
91
92 static void
93 intelReadBuffer(struct gl_context * ctx, GLenum mode)
94 {
95 if (ctx->DrawBuffer && _mesa_is_winsys_fbo(ctx->DrawBuffer)) {
96 struct intel_context *const intel = intel_context(ctx);
97 const bool was_front_buffer_reading =
98 intel->is_front_buffer_reading;
99
100 intel->is_front_buffer_reading = (mode == GL_FRONT_LEFT)
101 || (mode == GL_FRONT);
102
103 /* If we weren't front-buffer reading before but we are now,
104 * invalidate our DRI drawable so we'll ask for new buffers
105 * (including the fake front) before we start reading again.
106 */
107 if (!was_front_buffer_reading && intel->is_front_buffer_reading)
108 dri2InvalidateDrawable(intel->driContext->driReadablePriv);
109 }
110 }
111
112
113 void
114 intelInitBufferFuncs(struct dd_function_table *functions)
115 {
116 functions->DrawBuffer = intelDrawBuffer;
117 functions->ReadBuffer = intelReadBuffer;
118 }