1/**************************************************************************
2 *
3 * Copyright 2006 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 portionsalloc
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 "main/accum.h"
29#include "main/enums.h"
30#include "main/state.h"
31#include "main/bufferobj.h"
32#include "main/context.h"
33#include "swrast/swrast.h"
34
35#include "intel_context.h"
36#include "intel_pixel.h"
37#include "intel_regions.h"
38
39#define FILE_DEBUG_FLAG DEBUG_PIXEL
40
41static GLenum
42effective_func(GLenum func, bool src_alpha_is_one)
43{
44   if (src_alpha_is_one) {
45      if (func == GL_SRC_ALPHA)
46	 return GL_ONE;
47      if (func == GL_ONE_MINUS_SRC_ALPHA)
48	 return GL_ZERO;
49   }
50
51   return func;
52}
53
54/**
55 * Check if any fragment operations are in effect which might effect
56 * glDraw/CopyPixels.
57 */
58bool
59intel_check_blit_fragment_ops(struct gl_context * ctx, bool src_alpha_is_one)
60{
61   if (ctx->NewState)
62      _mesa_update_state(ctx);
63
64   if (ctx->FragmentProgram._Enabled) {
65      DBG("fallback due to fragment program\n");
66      return false;
67   }
68
69   if (ctx->Color.BlendEnabled &&
70       (effective_func(ctx->Color.Blend[0].SrcRGB, src_alpha_is_one) != GL_ONE ||
71	effective_func(ctx->Color.Blend[0].DstRGB, src_alpha_is_one) != GL_ZERO ||
72	ctx->Color.Blend[0].EquationRGB != GL_FUNC_ADD ||
73	effective_func(ctx->Color.Blend[0].SrcA, src_alpha_is_one) != GL_ONE ||
74	effective_func(ctx->Color.Blend[0].DstA, src_alpha_is_one) != GL_ZERO ||
75	ctx->Color.Blend[0].EquationA != GL_FUNC_ADD)) {
76      DBG("fallback due to blend\n");
77      return false;
78   }
79
80   if (ctx->Texture._EnabledUnits) {
81      DBG("fallback due to texturing\n");
82      return false;
83   }
84
85   if (!(ctx->Color.ColorMask[0][0] &&
86	 ctx->Color.ColorMask[0][1] &&
87	 ctx->Color.ColorMask[0][2] &&
88	 ctx->Color.ColorMask[0][3])) {
89      DBG("fallback due to color masking\n");
90      return false;
91   }
92
93   if (ctx->Color.AlphaEnabled) {
94      DBG("fallback due to alpha\n");
95      return false;
96   }
97
98   if (ctx->Depth.Test) {
99      DBG("fallback due to depth test\n");
100      return false;
101   }
102
103   if (ctx->Fog.Enabled) {
104      DBG("fallback due to fog\n");
105      return false;
106   }
107
108   if (ctx->_ImageTransferState) {
109      DBG("fallback due to image transfer\n");
110      return false;
111   }
112
113   if (ctx->Stencil._Enabled) {
114      DBG("fallback due to image stencil\n");
115      return false;
116   }
117
118   if (ctx->RenderMode != GL_RENDER) {
119      DBG("fallback due to render mode\n");
120      return false;
121   }
122
123   return true;
124}
125
126/* The intel_region struct doesn't really do enough to capture the
127 * format of the pixels in the region.  For now this code assumes that
128 * the region is a display surface and hence is either ARGB8888 or
129 * RGB565.
130 * XXX FBO: If we'd pass in the intel_renderbuffer instead of region, we'd
131 * know the buffer's pixel format.
132 *
133 * \param format  as given to glDraw/ReadPixels
134 * \param type  as given to glDraw/ReadPixels
135 */
136bool
137intel_check_blit_format(struct intel_region * region,
138                        GLenum format, GLenum type)
139{
140   if (region->cpp == 4 &&
141       (type == GL_UNSIGNED_INT_8_8_8_8_REV ||
142        type == GL_UNSIGNED_BYTE) && format == GL_BGRA) {
143      return true;
144   }
145
146   if (region->cpp == 2 &&
147       type == GL_UNSIGNED_SHORT_5_6_5_REV && format == GL_BGR) {
148      return true;
149   }
150
151   DBG("%s: bad format for blit (cpp %d, type %s format %s)\n",
152       __FUNCTION__, region->cpp,
153       _mesa_lookup_enum_by_nr(type), _mesa_lookup_enum_by_nr(format));
154
155   return false;
156}
157
158void
159intelInitPixelFuncs(struct dd_function_table *functions)
160{
161   functions->Accum = _mesa_accum;
162   functions->Bitmap = intelBitmap;
163   functions->CopyPixels = intelCopyPixels;
164   functions->DrawPixels = intelDrawPixels;
165   functions->ReadPixels = intelReadPixels;
166}
167
168