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