From 44af23156fac578543e6d17c67d739e75e51c972 Mon Sep 17 00:00:00 2001
From: Carolina Gomes <ext-carolina.s.gomes@nomovok.com>
Date: Mon, 14 Jun 2010 13:23:48 +0200
Subject: Fixed error reporting in grayraster and reduced default pool size.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Task-number: QTBUG-10471

Merge-request: 642
Reviewed-by: Samuel Rødal <samuel.rodal@nokia.com>
---
 src/gui/painting/qgrayraster.c           | 35 +++++++++++++++++++-------------
 src/gui/painting/qgrayraster_p.h         |  4 ++++
 src/gui/painting/qpaintengine_raster.cpp | 12 +++++++----
 src/gui/painting/qrasterdefs_p.h         |  6 ++----
 4 files changed, 35 insertions(+), 22 deletions(-)

diff --git a/src/gui/painting/qgrayraster.c b/src/gui/painting/qgrayraster.c
index ff2469c..5e7c67a 100644
--- a/src/gui/painting/qgrayraster.c
+++ b/src/gui/painting/qgrayraster.c
@@ -156,6 +156,7 @@
 #define ErrRaster_Invalid_Outline   -1
 #define ErrRaster_Invalid_Argument  -3
 #define ErrRaster_Memory_Overflow   -4
+#define ErrRaster_OutOfMemory       -6
 
 #define QT_FT_BEGIN_HEADER
 #define QT_FT_END_HEADER
@@ -222,7 +223,6 @@
 #define DOWNSCALE( x )  ( (x) << ( 6 - PIXEL_BITS ) )
 #endif
 
-
   /*************************************************************************/
   /*                                                                       */
   /*   TYPE DEFINITIONS                                                    */
@@ -1757,8 +1757,7 @@
 #ifdef DEBUG_GRAYS
           fprintf( stderr, "Rotten glyph!\n" );
 #endif
-          /* == Raster_Err_OutOfMemory in qblackraster.c */
-          return -6;
+          return ErrRaster_OutOfMemory;
         }
 
         if ( bottom-top >= ras.band_size )
@@ -1784,7 +1783,7 @@
 
 
   static int
-  gray_raster_render( PRaster                  raster,
+  gray_raster_render( QT_FT_Raster                  raster,
                       const QT_FT_Raster_Params*  params )
   {
     const QT_FT_Outline*  outline    = (const QT_FT_Outline*)params->source;
@@ -1795,6 +1794,12 @@
     if ( !raster || !raster->buffer || !raster->buffer_size )
       return ErrRaster_Invalid_Argument;
 
+    // If raster object and raster buffer are allocated, but
+    // raster size isn't of the minimum size, indicate out of
+    // memory.
+    if (raster && raster->buffer && raster->buffer_size < MINIMUM_POOL_SIZE )
+      return ErrRaster_OutOfMemory;
+
     /* return immediately if the outline is empty */
     if ( outline->n_points == 0 || outline->n_contours <= 0 )
       return 0;
@@ -1874,19 +1879,15 @@
   /****                         a static object.                  *****/
 
   static int
-  gray_raster_new( void *  memory,
-                   QT_FT_Raster*  araster )
+  gray_raster_new( QT_FT_Raster*  araster )
   {
-    if (memory)
-      fprintf(stderr, "gray_raster_new(), memory ignored");
-    memory = malloc(sizeof(TRaster));
-    if (!memory) {
+    *araster = malloc(sizeof(TRaster));
+    if (!*araster) {
         *araster = 0;
         return ErrRaster_Memory_Overflow;
     }
-    QT_FT_MEM_ZERO(memory, sizeof(TRaster));
+    QT_FT_MEM_ZERO(*araster, sizeof(TRaster));
 
-    *araster = (QT_FT_Raster) memory;
     return 0;
   }
 
@@ -1905,10 +1906,9 @@
   {
     PRaster  rast = (PRaster)raster;
 
-
     if ( raster )
     {
-      if ( pool_base && pool_size >= (long)sizeof ( TWorker ) + 2048 )
+      if ( pool_base && ( pool_size >= MINIMUM_POOL_SIZE ) )
       {
         PWorker  worker = (PWorker)pool_base;
 
@@ -1923,6 +1923,13 @@
         rast->band_size   = (int)( rast->buffer_size /
                                      ( sizeof ( TCell ) * 8 ) );
       }
+      else if ( pool_base)
+      { // Case when there is a raster pool allocated, but it
+        // doesn't have the minimum size (and so memory will be reallocated)
+          rast->buffer = pool_base;
+          rast->worker = NULL;
+          rast->buffer_size = pool_size;
+      }
       else
       {
         rast->buffer      = NULL;
diff --git a/src/gui/painting/qgrayraster_p.h b/src/gui/painting/qgrayraster_p.h
index 4463fc9..ad595b8 100644
--- a/src/gui/painting/qgrayraster_p.h
+++ b/src/gui/painting/qgrayraster_p.h
@@ -89,6 +89,10 @@
 #define QT_FT_EXPORT_VAR( x )  extern  x
 #endif
 
+/* Minimum buffer size for raster object, that accounts
+   for TWorker and TCell sizes.*/
+#define MINIMUM_POOL_SIZE 4096
+
   QT_FT_EXPORT_VAR( const QT_FT_Raster_Funcs )  qt_ft_grays_raster;
 
 
diff --git a/src/gui/painting/qpaintengine_raster.cpp b/src/gui/painting/qpaintengine_raster.cpp
index 08e14fb..9f1128b 100644
--- a/src/gui/painting/qpaintengine_raster.cpp
+++ b/src/gui/painting/qpaintengine_raster.cpp
@@ -345,7 +345,7 @@ void QRasterPaintEngine::init()
     // The antialiasing raster.
     d->grayRaster.reset(new QT_FT_Raster);
     Q_CHECK_PTR(d->grayRaster.data());
-    if (qt_ft_grays_raster.raster_new(0, d->grayRaster.data()))
+    if (qt_ft_grays_raster.raster_new(d->grayRaster.data()))
         QT_THROW(std::bad_alloc()); // an error creating the raster is caused by a bad malloc
 
 
@@ -4185,7 +4185,11 @@ void QRasterPaintEnginePrivate::rasterize(QT_FT_Outline *outline,
         return;
     }
 
-    const int rasterPoolInitialSize = 8192;
+    // Initial size for raster pool is MINIMUM_POOL_SIZE so as to
+    // minimize memory reallocations. However if initial size for
+    // raster pool is changed for lower value, reallocations will
+    // occur normally.
+    const int rasterPoolInitialSize = MINIMUM_POOL_SIZE;
     int rasterPoolSize = rasterPoolInitialSize;
     unsigned char *rasterPoolBase;
 #if defined(Q_WS_WIN64)
@@ -4229,7 +4233,7 @@ void QRasterPaintEnginePrivate::rasterize(QT_FT_Outline *outline,
         error = qt_ft_grays_raster.raster_render(*grayRaster.data(), &rasterParams);
 
         // Out of memory, reallocate some more and try again...
-        if (error == -6) { // -6 is Result_err_OutOfMemory
+        if (error == -6) { // ErrRaster_OutOfMemory from qgrayraster.c
             int new_size = rasterPoolSize * 2;
             if (new_size > 1024 * 1024) {
                 qWarning("QPainter: Rasterization of primitive failed");
@@ -4255,7 +4259,7 @@ void QRasterPaintEnginePrivate::rasterize(QT_FT_Outline *outline,
             Q_CHECK_PTR(rasterPoolBase); // note: we just freed the old rasterPoolBase. I hope it's not fatal.
 
             qt_ft_grays_raster.raster_done(*grayRaster.data());
-            qt_ft_grays_raster.raster_new(0, grayRaster.data());
+            qt_ft_grays_raster.raster_new(grayRaster.data());
             qt_ft_grays_raster.raster_reset(*grayRaster.data(), rasterPoolBase, rasterPoolSize);
         } else {
             done = true;
diff --git a/src/gui/painting/qrasterdefs_p.h b/src/gui/painting/qrasterdefs_p.h
index c33fa57..19a0b16 100644
--- a/src/gui/painting/qrasterdefs_p.h
+++ b/src/gui/painting/qrasterdefs_p.h
@@ -81,7 +81,6 @@
 
 QT_FT_BEGIN_HEADER
 
-
   /*************************************************************************/
   /*                                                                       */
   /* <Section>                                                             */
@@ -837,7 +836,7 @@ QT_FT_BEGIN_HEADER
   /*    A handle (pointer) to a raster object.  Each object can be used    */
   /*    independently to convert an outline into a bitmap or pixmap.       */
   /*                                                                       */
-  typedef struct QT_FT_RasterRec_*  QT_FT_Raster;
+  typedef struct TRaster_ *QT_FT_Raster;
 
 
   /*************************************************************************/
@@ -1118,8 +1117,7 @@ QT_FT_BEGIN_HEADER
   /*    ignored by a given raster implementation.                          */
   /*                                                                       */
   typedef int
-  (*QT_FT_Raster_NewFunc)( void*       memory,
-                        QT_FT_Raster*  raster );
+  (*QT_FT_Raster_NewFunc)( QT_FT_Raster*  raster );
 
 #define  QT_FT_Raster_New_Func    QT_FT_Raster_NewFunc
 
-- 
cgit v0.12