mesa: Change "BRIAN PAUL" to "THE AUTHORS" in license text.
[mesa.git] / src / mesa / drivers / dri / swrast / swrast.c
index 75d25253eb4a869a67bc4cfdb074e6ec1a6bd6c0..ba809aace639b75ae5a3d77dea6ac5a38feb6747 100644 (file)
@@ -14,7 +14,7 @@
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
- * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
  * The back-buffer is allocated by the driver and is private.
  */
 
+#include "main/api_exec.h"
 #include "main/context.h"
 #include "main/extensions.h"
 #include "main/formats.h"
 #include "main/framebuffer.h"
 #include "main/imports.h"
 #include "main/renderbuffer.h"
+#include "main/version.h"
+#include "main/vtxfmt.h"
 #include "swrast/swrast.h"
+#include "swrast/s_renderbuffer.h"
 #include "swrast_setup/swrast_setup.h"
 #include "tnl/tnl.h"
 #include "tnl/t_context.h"
@@ -90,10 +94,10 @@ static void swrastSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
     else
        texFormat = MESA_FORMAT_ARGB8888;
 
-    _mesa_init_teximage_fields(&dri_ctx->Base, target, texImage,
+    _mesa_init_teximage_fields(&dri_ctx->Base, texImage,
                               w, h, 1, 0, internalFormat, texFormat);
 
-    sPriv->swrast_loader->getImage(dPriv, x, y, w, h, (char *)swImage->Data,
+    sPriv->swrast_loader->getImage(dPriv, x, y, w, h, (char *)swImage->Buffer,
                                   dPriv->loaderPrivate);
 
     _mesa_unlock_texture(&dri_ctx->Base, texObj);
@@ -124,8 +128,7 @@ swrastFillInModes(__DRIscreen *psp,
     __DRIconfig **configs;
     unsigned depth_buffer_factor;
     unsigned back_buffer_factor;
-    GLenum fb_format;
-    GLenum fb_type;
+    gl_format format;
 
     /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
      * support pageflipping at all.
@@ -160,21 +163,14 @@ swrastFillInModes(__DRIscreen *psp,
     back_buffer_factor = 2;
 
     switch (pixel_bits) {
-    case 8:
-       fb_format = GL_RGB;
-       fb_type = GL_UNSIGNED_BYTE_2_3_3_REV;
-       break;
     case 16:
-       fb_format = GL_RGB;
-       fb_type = GL_UNSIGNED_SHORT_5_6_5;
+       format = MESA_FORMAT_RGB565;
        break;
     case 24:
-       fb_format = GL_BGR;
-       fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
+        format = MESA_FORMAT_XRGB8888;
        break;
     case 32:
-       fb_format = GL_BGRA;
-       fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
+       format = MESA_FORMAT_ARGB8888;
        break;
     default:
        fprintf(stderr, "[%s:%u] bad depth %d\n", __func__, __LINE__,
@@ -182,7 +178,7 @@ swrastFillInModes(__DRIscreen *psp,
        return NULL;
     }
 
-    configs = driCreateConfigs(fb_format, fb_type,
+    configs = driCreateConfigs(format,
                               depth_bits_array, stencil_bits_array,
                               depth_buffer_factor, back_buffer_modes,
                               back_buffer_factor, msaa_samples_array, 1,
@@ -199,18 +195,16 @@ swrastFillInModes(__DRIscreen *psp,
 static const __DRIconfig **
 dri_init_screen(__DRIscreen * psp)
 {
-    __DRIconfig **configs8, **configs16, **configs24, **configs32;
+    __DRIconfig **configs16, **configs24, **configs32;
 
     TRACE;
 
     psp->extensions = dri_screen_extensions;
 
-    configs8  = swrastFillInModes(psp,  8,  8, 0, 1);
     configs16 = swrastFillInModes(psp, 16, 16, 0, 1);
     configs24 = swrastFillInModes(psp, 24, 24, 8, 1);
     configs32 = swrastFillInModes(psp, 32, 24, 8, 1);
 
-    configs16 = driConcatConfigs(configs8, configs16);
     configs24 = driConcatConfigs(configs16, configs24);
     configs32 = driConcatConfigs(configs24, configs32);
 
@@ -260,12 +254,14 @@ choose_pixel_format(const struct gl_config *v)
 }
 
 static void
-swrast_delete_renderbuffer(struct gl_renderbuffer *rb)
+swrast_delete_renderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb)
 {
+    struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
+
     TRACE;
 
-    free(rb->Data);
-    free(rb);
+    free(xrb->Base.Buffer);
+    _mesa_delete_renderbuffer(ctx, rb);
 }
 
 /* see bytes_per_line in libGL */
@@ -281,17 +277,16 @@ static GLboolean
 swrast_alloc_front_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
                           GLenum internalFormat, GLuint width, GLuint height)
 {
-    struct swrast_renderbuffer *xrb = swrast_renderbuffer(rb);
+    struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
 
     TRACE;
 
     (void) ctx;
     (void) internalFormat;
 
-    rb->Data = NULL;
+    xrb->Base.Buffer = NULL;
     rb->Width = width;
     rb->Height = height;
-
     xrb->pitch = bytes_per_line(width * xrb->bpp, 32);
 
     return GL_TRUE;
@@ -301,23 +296,25 @@ static GLboolean
 swrast_alloc_back_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
                          GLenum internalFormat, GLuint width, GLuint height)
 {
-    struct swrast_renderbuffer *xrb = swrast_renderbuffer(rb);
+    struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
 
     TRACE;
 
-    free(rb->Data);
+    free(xrb->Base.Buffer);
 
     swrast_alloc_front_storage(ctx, rb, internalFormat, width, height);
 
-    rb->Data = malloc(height * xrb->pitch);
+    xrb->Base.Buffer = malloc(height * xrb->pitch);
 
     return GL_TRUE;
 }
 
-static struct swrast_renderbuffer *
-swrast_new_renderbuffer(const struct gl_config *visual, GLboolean front)
+static struct dri_swrast_renderbuffer *
+swrast_new_renderbuffer(const struct gl_config *visual, __DRIdrawable *dPriv,
+                       GLboolean front)
 {
-    struct swrast_renderbuffer *xrb = calloc(1, sizeof *xrb);
+    struct dri_swrast_renderbuffer *xrb = calloc(1, sizeof *xrb);
+    struct gl_renderbuffer *rb;
     GLuint pixel_format;
 
     TRACE;
@@ -325,56 +322,126 @@ swrast_new_renderbuffer(const struct gl_config *visual, GLboolean front)
     if (!xrb)
        return NULL;
 
-    _mesa_init_renderbuffer(&xrb->Base, 0);
+    rb = &xrb->Base.Base;
+
+    _mesa_init_renderbuffer(rb, 0);
 
     pixel_format = choose_pixel_format(visual);
 
-    xrb->Base.Delete = swrast_delete_renderbuffer;
+    xrb->dPriv = dPriv;
+    xrb->Base.Base.Delete = swrast_delete_renderbuffer;
     if (front) {
-       xrb->Base.AllocStorage = swrast_alloc_front_storage;
-       swrast_set_span_funcs_front(xrb, pixel_format);
+        rb->AllocStorage = swrast_alloc_front_storage;
     }
     else {
-       xrb->Base.AllocStorage = swrast_alloc_back_storage;
-       swrast_set_span_funcs_back(xrb, pixel_format);
+       rb->AllocStorage = swrast_alloc_back_storage;
     }
 
     switch (pixel_format) {
     case PF_A8R8G8B8:
-       xrb->Base.Format = MESA_FORMAT_ARGB8888;
-       xrb->Base.InternalFormat = GL_RGBA;
-       xrb->Base._BaseFormat = GL_RGBA;
-       xrb->Base.DataType = GL_UNSIGNED_BYTE;
+       rb->Format = MESA_FORMAT_ARGB8888;
+       rb->InternalFormat = GL_RGBA;
+       rb->_BaseFormat = GL_RGBA;
        xrb->bpp = 32;
        break;
     case PF_X8R8G8B8:
-       xrb->Base.Format = MESA_FORMAT_ARGB8888; /* XXX */
-       xrb->Base.InternalFormat = GL_RGB;
-       xrb->Base._BaseFormat = GL_RGB;
-       xrb->Base.DataType = GL_UNSIGNED_BYTE;
+       rb->Format = MESA_FORMAT_ARGB8888; /* XXX */
+       rb->InternalFormat = GL_RGB;
+       rb->_BaseFormat = GL_RGB;
        xrb->bpp = 32;
        break;
     case PF_R5G6B5:
-       xrb->Base.Format = MESA_FORMAT_RGB565;
-       xrb->Base.InternalFormat = GL_RGB;
-       xrb->Base._BaseFormat = GL_RGB;
-       xrb->Base.DataType = GL_UNSIGNED_BYTE;
+       rb->Format = MESA_FORMAT_RGB565;
+       rb->InternalFormat = GL_RGB;
+       rb->_BaseFormat = GL_RGB;
        xrb->bpp = 16;
        break;
     case PF_R3G3B2:
-       xrb->Base.Format = MESA_FORMAT_RGB332;
-       xrb->Base.InternalFormat = GL_RGB;
-       xrb->Base._BaseFormat = GL_RGB;
-       xrb->Base.DataType = GL_UNSIGNED_BYTE;
+       rb->Format = MESA_FORMAT_RGB332;
+       rb->InternalFormat = GL_RGB;
+       rb->_BaseFormat = GL_RGB;
        xrb->bpp = 8;
        break;
     default:
+       free(xrb);
        return NULL;
     }
 
     return xrb;
 }
 
+static void
+swrast_map_renderbuffer(struct gl_context *ctx,
+                       struct gl_renderbuffer *rb,
+                       GLuint x, GLuint y, GLuint w, GLuint h,
+                       GLbitfield mode,
+                       GLubyte **out_map,
+                       GLint *out_stride)
+{
+   struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
+   GLubyte *map = xrb->Base.Buffer;
+   int cpp = _mesa_get_format_bytes(rb->Format);
+   int stride = rb->Width * cpp;
+
+   if (rb->AllocStorage == swrast_alloc_front_storage) {
+      __DRIdrawable *dPriv = xrb->dPriv;
+      __DRIscreen *sPriv = dPriv->driScreenPriv;
+
+      xrb->map_mode = mode;
+      xrb->map_x = x;
+      xrb->map_y = y;
+      xrb->map_w = w;
+      xrb->map_h = h;
+
+      stride = w * cpp;
+      xrb->Base.Buffer = malloc(h * stride);
+
+      sPriv->swrast_loader->getImage(dPriv, x, y, w, h,
+                                    (char *) xrb->Base.Buffer,
+                                    dPriv->loaderPrivate);
+
+      *out_map = xrb->Base.Buffer;
+      *out_stride = stride;
+      return;
+   }
+
+   ASSERT(xrb->Base.Buffer);
+
+   if (rb->AllocStorage == swrast_alloc_back_storage) {
+      map += (rb->Height - 1) * stride;
+      stride = -stride;
+   }
+
+   map += (GLsizei)y * stride;
+   map += (GLsizei)x * cpp;
+
+   *out_map = map;
+   *out_stride = stride;
+}
+
+static void
+swrast_unmap_renderbuffer(struct gl_context *ctx,
+                         struct gl_renderbuffer *rb)
+{
+   struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
+
+   if (rb->AllocStorage == swrast_alloc_front_storage) {
+      __DRIdrawable *dPriv = xrb->dPriv;
+      __DRIscreen *sPriv = dPriv->driScreenPriv;
+
+      if (xrb->map_mode & GL_MAP_WRITE_BIT) {
+        sPriv->swrast_loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_DRAW,
+                                       xrb->map_x, xrb->map_y,
+                                       xrb->map_w, xrb->map_h,
+                                       (char *) xrb->Base.Buffer,
+                                       dPriv->loaderPrivate);
+      }
+
+      free(xrb->Base.Buffer);
+      xrb->Base.Buffer = NULL;
+   }
+}
+
 static GLboolean
 dri_create_buffer(__DRIscreen * sPriv,
                  __DRIdrawable * dPriv,
@@ -382,7 +449,7 @@ dri_create_buffer(__DRIscreen * sPriv,
 {
     struct dri_drawable *drawable = NULL;
     struct gl_framebuffer *fb;
-    struct swrast_renderbuffer *frontrb, *backrb;
+    struct dri_swrast_renderbuffer *frontrb, *backrb;
 
     TRACE;
 
@@ -396,7 +463,7 @@ dri_create_buffer(__DRIscreen * sPriv,
     dPriv->driverPrivate = drawable;
     drawable->dPriv = dPriv;
 
-    drawable->row = malloc(MAX_WIDTH * 4);
+    drawable->row = malloc(SWRAST_MAX_WIDTH * 4);
     if (drawable->row == NULL)
        goto drawable_fail;
 
@@ -406,23 +473,23 @@ dri_create_buffer(__DRIscreen * sPriv,
     _mesa_initialize_window_framebuffer(fb, visual);
 
     /* add front renderbuffer */
-    frontrb = swrast_new_renderbuffer(visual, GL_TRUE);
-    _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontrb->Base);
+    frontrb = swrast_new_renderbuffer(visual, dPriv, GL_TRUE);
+    _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontrb->Base.Base);
 
     /* add back renderbuffer */
     if (visual->doubleBufferMode) {
-       backrb = swrast_new_renderbuffer(visual, GL_FALSE);
-       _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backrb->Base);
+       backrb = swrast_new_renderbuffer(visual, dPriv, GL_FALSE);
+       _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backrb->Base.Base);
     }
 
     /* add software renderbuffers */
-    _mesa_add_soft_renderbuffers(fb,
-                                GL_FALSE, /* color */
-                                visual->haveDepthBuffer,
-                                visual->haveStencilBuffer,
-                                visual->haveAccumBuffer,
-                                GL_FALSE, /* alpha */
-                                GL_FALSE /* aux bufs */);
+    _swrast_add_soft_renderbuffers(fb,
+                                   GL_FALSE, /* color */
+                                   visual->haveDepthBuffer,
+                                   visual->haveStencilBuffer,
+                                   visual->haveAccumBuffer,
+                                   GL_FALSE, /* alpha */
+                                   GL_FALSE /* aux bufs */);
 
     return GL_TRUE;
 
@@ -431,7 +498,7 @@ drawable_fail:
     if (drawable)
        free(drawable->row);
 
-    FREE(drawable);
+    free(drawable);
 
     return GL_FALSE;
 }
@@ -463,16 +530,16 @@ dri_swap_buffers(__DRIdrawable * dPriv)
 
     struct dri_drawable *drawable = dri_drawable(dPriv);
     struct gl_framebuffer *fb;
-    struct swrast_renderbuffer *frontrb, *backrb;
+    struct dri_swrast_renderbuffer *frontrb, *backrb;
 
     TRACE;
 
     fb = &drawable->Base;
 
     frontrb =
-       swrast_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
+       dri_swrast_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
     backrb =
-       swrast_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
+       dri_swrast_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
 
     /* check for signle-buffered */
     if (backrb == NULL)
@@ -486,9 +553,9 @@ dri_swap_buffers(__DRIdrawable * dPriv)
 
     sPriv->swrast_loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
                                   0, 0,
-                                  frontrb->Base.Width,
-                                  frontrb->Base.Height,
-                                  backrb->Base.Data,
+                                  frontrb->Base.Base.Width,
+                                  frontrb->Base.Base.Height,
+                                  (char *) backrb->Base.Buffer,
                                   dPriv->loaderPrivate);
 }
 
@@ -559,13 +626,14 @@ viewport(struct gl_context *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
 }
 
 static gl_format swrastChooseTextureFormat(struct gl_context * ctx,
+                                           GLenum target,
                                           GLint internalFormat,
                                           GLenum format,
                                           GLenum type)
 {
     if (internalFormat == GL_RGB)
        return MESA_FORMAT_XRGB8888;
-    return _mesa_choose_tex_format(ctx, internalFormat, format, type);
+    return _mesa_choose_tex_format(ctx, target, internalFormat, format, type);
 }
 
 static void
@@ -576,18 +644,15 @@ swrast_init_driver_functions(struct dd_function_table *driver)
     driver->GetBufferSize = NULL;
     driver->Viewport = viewport;
     driver->ChooseTextureFormat = swrastChooseTextureFormat;
+    driver->MapRenderbuffer = swrast_map_renderbuffer;
+    driver->UnmapRenderbuffer = swrast_unmap_renderbuffer;
 }
 
 static const char *es2_extensions[] = {
    /* Used by mesa internally (cf all_mesa_extensions in ../common/utils.c) */
-   "GL_ARB_transpose_matrix",
-   "GL_ARB_window_pos",
    "GL_EXT_blend_func_separate",
-   "GL_EXT_compiled_vertex_array",
    "GL_EXT_framebuffer_blit",
-   "GL_IBM_multimode_draw_arrays",
    "GL_MESA_window_pos",
-   "GL_NV_vertex_program",
 
    /* Required by GLES2 */
    "GL_ARB_fragment_program",
@@ -625,7 +690,12 @@ InitExtensionsES2(struct gl_context *ctx)
 static GLboolean
 dri_create_context(gl_api api,
                   const struct gl_config * visual,
-                  __DRIcontext * cPriv, void *sharedContextPrivate)
+                  __DRIcontext * cPriv,
+                  unsigned major_version,
+                  unsigned minor_version,
+                  uint32_t flags,
+                  unsigned *error,
+                  void *sharedContextPrivate)
 {
     struct dri_context *ctx = NULL;
     struct dri_context *share = (struct dri_context *)sharedContextPrivate;
@@ -635,9 +705,31 @@ dri_create_context(gl_api api,
 
     TRACE;
 
+    /* Flag filtering is handled in dri2CreateContextAttribs.
+     */
+    (void) flags;
+
+    switch (api) {
+    case API_OPENGL_COMPAT:
+        if (major_version > 2
+           || (major_version == 2 && minor_version > 1)) {
+            *error = __DRI_CTX_ERROR_BAD_VERSION;
+            return GL_FALSE;
+        }
+        break;
+    case API_OPENGLES:
+    case API_OPENGLES2:
+        break;
+    case API_OPENGL_CORE:
+        *error = __DRI_CTX_ERROR_BAD_API;
+        return GL_FALSE;
+    }
+
     ctx = CALLOC_STRUCT(dri_context);
-    if (ctx == NULL)
+    if (ctx == NULL) {
+       *error = __DRI_CTX_ERROR_NO_MEMORY;
        goto context_fail;
+    }
 
     cPriv->driverPrivate = ctx;
     ctx->cPriv = cPriv;
@@ -653,7 +745,8 @@ dri_create_context(gl_api api,
     mesaCtx = &ctx->Base;
 
     /* basic context setup */
-    if (!_mesa_initialize_context(mesaCtx, api, visual, sharedCtx, &functions, (void *) cPriv)) {
+    if (!_mesa_initialize_context(mesaCtx, api, visual, sharedCtx, &functions)) {
+       *error = __DRI_CTX_ERROR_NO_MEMORY;
        goto context_fail;
     }
 
@@ -677,7 +770,9 @@ dri_create_context(gl_api api,
     _mesa_enable_sw_extensions(mesaCtx);
 
     switch (api) {
-    case API_OPENGL:
+    case API_OPENGL_CORE:
+        /* XXX fix me, fall-through for now */
+    case API_OPENGL_COMPAT:
         _mesa_enable_1_3_extensions(mesaCtx);
         _mesa_enable_1_4_extensions(mesaCtx);
         _mesa_enable_1_5_extensions(mesaCtx);
@@ -695,11 +790,17 @@ dri_create_context(gl_api api,
         break;
     }
 
+    _mesa_compute_version(mesaCtx);
+
+    _mesa_initialize_dispatch_tables(mesaCtx);
+    _mesa_initialize_vbo_vtxfmt(mesaCtx);
+
+    *error = __DRI_CTX_ERROR_SUCCESS;
     return GL_TRUE;
 
 context_fail:
 
-    FREE(ctx);
+    free(ctx);
 
     return GL_FALSE;
 }