+ /* Determine if copy should be bottom-to-top or top-to-bottom */
+ if (!overlapping && srcY < destY) {
+ /* top-down max-to-min */
+ sy = srcY + height - 1;
+ dy = destY + height - 1;
+ stepy = -1;
+ }
+ else {
+ /* bottom-up min-to-max */
+ sy = srcY;
+ dy = destY;
+ stepy = 1;
+ }
+
+ if (overlapping) {
+ GLint ssy = sy;
+
+ if (stencilMask != 0x0) {
+ tempStencilImage
+ = (GLstencil *) _mesa_malloc(width * height * sizeof(GLstencil));
+ if (!tempStencilImage) {
+ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
+ return;
+ }
+
+ /* get copy of stencil pixels */
+ stencilPtr = tempStencilImage;
+ for (j = 0; j < height; j++, ssy += stepy) {
+ _swrast_read_stencil_span(ctx, stencilReadRb,
+ width, srcX, ssy, stencilPtr);
+ stencilPtr += width;
+ }
+ stencilPtr = tempStencilImage;
+ }
+
+ if (ctx->Depth.Mask) {
+ tempDepthImage
+ = (GLfloat *) _mesa_malloc(width * height * sizeof(GLfloat));
+ if (!tempDepthImage) {
+ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
+ _mesa_free(tempStencilImage);
+ return;
+ }
+
+ /* get copy of depth pixels */
+ depthPtr = tempDepthImage;
+ for (j = 0; j < height; j++, ssy += stepy) {
+ _swrast_read_depth_span_float(ctx, depthReadRb,
+ width, srcX, ssy, depthPtr);
+ depthPtr += width;
+ }
+ depthPtr = tempDepthImage;
+ }
+ }
+
+ for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
+ if (stencilMask != 0x0) {
+ GLstencil stencil[MAX_WIDTH];
+
+ /* Get stencil values */
+ if (overlapping) {
+ _mesa_memcpy(stencil, stencilPtr, width * sizeof(GLstencil));
+ stencilPtr += width;
+ }
+ else {
+ _swrast_read_stencil_span(ctx, stencilReadRb,
+ width, srcX, sy, stencil);
+ }
+
+ _mesa_apply_stencil_transfer_ops(ctx, width, stencil);
+
+ /* Write values */
+ if (zoom) {
+ _swrast_write_zoomed_stencil_span(ctx, destX, destY, width,
+ destX, dy, stencil);
+ }
+ else {
+ _swrast_write_stencil_span( ctx, width, destX, dy, stencil );
+ }
+ }
+
+ if (ctx->Depth.Mask) {
+ GLfloat depth[MAX_WIDTH];
+ GLuint zVals32[MAX_WIDTH];
+ GLushort zVals16[MAX_WIDTH];
+ GLvoid *zVals;
+ GLuint zBytes;
+
+ /* get depth values */
+ if (overlapping) {
+ _mesa_memcpy(depth, depthPtr, width * sizeof(GLfloat));
+ depthPtr += width;
+ }
+ else {
+ _swrast_read_depth_span_float(ctx, depthReadRb,
+ width, srcX, sy, depth);
+ }
+
+ /* scale & bias */
+ if (scaleOrBias) {
+ _mesa_scale_and_bias_depth(ctx, width, depth);
+ }
+ /* convert to integer Z values */
+ if (depthDrawRb->DataType == GL_UNSIGNED_SHORT) {
+ GLint k;
+ for (k = 0; k < width; k++)
+ zVals16[k] = (GLushort) (depth[k] * depthScale);
+ zVals = zVals16;
+ zBytes = 2;
+ }
+ else {
+ GLint k;
+ for (k = 0; k < width; k++)
+ zVals32[k] = (GLuint) (depth[k] * depthScale);
+ zVals = zVals32;
+ zBytes = 4;
+ }
+
+ /* Write values */
+ if (zoom) {
+ _swrast_write_zoomed_z_span(ctx, destX, destY, width,
+ destX, dy, zVals);
+ }
+ else {
+ _swrast_put_row(ctx, depthDrawRb, width, destX, dy, zVals, zBytes);
+ }
+ }
+ }
+
+ if (tempStencilImage)
+ _mesa_free(tempStencilImage);
+
+ if (tempDepthImage)
+ _mesa_free(tempDepthImage);
+}
+
+
+
+/**
+ * Try to do a fast copy pixels.
+ */
+static GLboolean
+fast_copy_pixels(GLcontext *ctx,
+ GLint srcX, GLint srcY, GLsizei width, GLsizei height,
+ GLint dstX, GLint dstY, GLenum type)
+{
+ struct gl_framebuffer *srcFb = ctx->ReadBuffer;
+ struct gl_framebuffer *dstFb = ctx->DrawBuffer;
+ struct gl_renderbuffer *srcRb, *dstRb;
+ GLint row, yStep;
+
+ if (SWRAST_CONTEXT(ctx)->_RasterMask != 0x0 ||
+ ctx->Pixel.ZoomX != 1.0F ||
+ ctx->Pixel.ZoomY != 1.0F ||
+ ctx->_ImageTransferState) {
+ /* can't handle these */
+ return GL_FALSE;
+ }
+
+ if (type == GL_COLOR) {
+ if (dstFb->_NumColorDrawBuffers != 1)
+ return GL_FALSE;
+ srcRb = srcFb->_ColorReadBuffer;
+ dstRb = dstFb->_ColorDrawBuffers[0];
+ }
+ else if (type == GL_STENCIL) {
+ srcRb = srcFb->_StencilBuffer;
+ dstRb = dstFb->_StencilBuffer;
+ }
+ else if (type == GL_DEPTH) {
+ srcRb = srcFb->_DepthBuffer;
+ dstRb = dstFb->_DepthBuffer;
+ }
+ else {
+ ASSERT(type == GL_DEPTH_STENCIL_EXT);
+ /* XXX correct? */
+ srcRb = srcFb->Attachment[BUFFER_DEPTH].Renderbuffer;
+ dstRb = dstFb->Attachment[BUFFER_DEPTH].Renderbuffer;
+ }
+
+ /* src and dst renderbuffers must be same format and type */
+ if (!srcRb || !dstRb ||
+ srcRb->DataType != dstRb->DataType ||
+ srcRb->_BaseFormat != dstRb->_BaseFormat) {
+ return GL_FALSE;
+ }
+
+ /* clipping not supported */
+ if (srcX < 0 || srcX + width > (GLint) srcFb->Width ||
+ srcY < 0 || srcY + height > (GLint) srcFb->Height ||
+ dstX < dstFb->_Xmin || dstX + width > dstFb->_Xmax ||
+ dstY < dstFb->_Ymin || dstY + height > dstFb->_Ymax) {
+ return GL_FALSE;
+ }
+
+ /* overlapping src/dst doesn't matter, just determine Y direction */
+ if (srcY < dstY) {
+ /* top-down max-to-min */
+ srcY = srcY + height - 1;
+ dstY = dstY + height - 1;
+ yStep = -1;
+ }
+ else {
+ /* bottom-up min-to-max */
+ yStep = 1;
+ }
+
+ for (row = 0; row < height; row++) {
+ GLuint temp[MAX_WIDTH][4];
+ srcRb->GetRow(ctx, srcRb, width, srcX, srcY, temp);
+ dstRb->PutRow(ctx, dstRb, width, dstX, dstY, temp, NULL);
+ srcY += yStep;
+ dstY += yStep;
+ }
+
+ return GL_TRUE;
+}
+
+
+/**
+ * Do software-based glCopyPixels.
+ * By time we get here, all parameters will have been error-checked.
+ */