src/hbcore/image/hbvgimageiconrenderer.cpp
changeset 2 06ff229162e9
parent 1 f7ac710697a9
child 5 627c4a0fd0e7
equal deleted inserted replaced
1:f7ac710697a9 2:06ff229162e9
   105     if (!addedToStates && vgImage != VG_INVALID_HANDLE) {
   105     if (!addedToStates && vgImage != VG_INVALID_HANDLE) {
   106         eglStates->addVGImage(&vgImage);
   106         eglStates->addVGImage(&vgImage);
   107         addedToStates = true;
   107         addedToStates = true;
   108     }
   108     }
   109 
   109 
   110     if (vgImage != VG_INVALID_HANDLE) {
   110     if (vgImage != VG_INVALID_HANDLE) {    
   111 
   111         QPainterPath oldPath;
       
   112         bool clipped = painter->hasClipping();
       
   113         
   112         if (!clipPath.isEmpty()) {
   114         if (!clipPath.isEmpty()) {
       
   115             if (!clipped) {
       
   116                 painter->setClipping(true);
       
   117             }
       
   118             
       
   119             QPainterPath intersect(clipPath);
       
   120             if (clipped) {
       
   121                 oldPath = painter->clipPath();
       
   122                 intersect =  oldPath.intersected(clipPath);
       
   123                 if (intersect.isEmpty()) {
       
   124                     return true;
       
   125                 }
       
   126             }
       
   127         
       
   128             painter->setClipPath(intersect, Qt::ReplaceClip);
   113             painter->beginNativePainting();
   129             painter->beginNativePainting();
   114         }
   130         }
   115 
   131         
   116         VGint imageMode      = vgGeti(VG_IMAGE_MODE);
   132         VGint imageMode      = vgGeti(VG_IMAGE_MODE);
   117         VGint matrixMode     = vgGeti(VG_MATRIX_MODE);
   133         VGint matrixMode     = vgGeti(VG_MATRIX_MODE);
   118         VGPaint oldFillPaint = VG_INVALID_HANDLE;
   134         VGPaint oldFillPaint = VG_INVALID_HANDLE;
   119         VGPaint oldStrkPaint = VG_INVALID_HANDLE;
   135         VGPaint oldStrkPaint = VG_INVALID_HANDLE;
   120         VGint   blendMode    = 0;
   136         VGint   blendMode    = 0;
   121 
   137         
   122         updatePainterTransformation(painter, topLeft);
   138         updatePainterTransformation(painter, topLeft);
   123 
   139         
   124         qreal opacity = painter->opacity();
   140         qreal opacity = painter->opacity();
   125 
   141 
   126         if (opacity != lastOpacity || iconMode == QIcon::Selected) {
   142         if (opacity != lastOpacity || iconMode == QIcon::Selected) {
   127             lastOpacity = opacity;
   143             lastOpacity = opacity;
   128             if (opacityPaint == VG_INVALID_HANDLE) {
   144             if (opacityPaint == VG_INVALID_HANDLE) {
   157             vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_NORMAL);
   173             vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_NORMAL);
   158         } else {
   174         } else {
   159             vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_MULTIPLY);
   175             vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_MULTIPLY);
   160         }
   176         }
   161 
   177 
   162         VGint prevMask = 0;
       
   163         VGPath vgpath = VG_INVALID_HANDLE;
       
   164 
       
   165         if (!clipPath.isEmpty()) {
       
   166 
       
   167             QPaintDevice *pdev = painter->paintEngine()->paintDevice();
       
   168             int width = pdev->width();
       
   169             int height = pdev->height();
       
   170 
       
   171             vgMask(VG_INVALID_HANDLE, VG_CLEAR_MASK, 0, 0, width, height);
       
   172 
       
   173             vgpath = painterPathToVGPath(clipPath);
       
   174             vgRenderToMask(vgpath, VG_FILL_PATH, VG_UNION_MASK);
       
   175 
       
   176             prevMask = vgGeti(VG_MASKING);
       
   177             vgSeti(VG_MASKING, VG_TRUE);
       
   178         }
       
   179 
       
   180         vgDrawImage(vgImage);
   178         vgDrawImage(vgImage);
   181 
       
   182         if (!clipPath.isEmpty()) {
       
   183             vgSeti(VG_MASKING, VG_FALSE);
       
   184         }
       
   185 
   179 
   186         vgSeti(VG_MATRIX_MODE, matrixMode);
   180         vgSeti(VG_MATRIX_MODE, matrixMode);
   187         vgSeti(VG_IMAGE_MODE, imageMode);
   181         vgSeti(VG_IMAGE_MODE, imageMode);
   188 
   182 
   189         if (oldFillPaint) {
   183         if (oldFillPaint) {
   195 
   189 
   196         if (blendMode) {
   190         if (blendMode) {
   197             vgSeti(VG_BLEND_MODE, blendMode);
   191             vgSeti(VG_BLEND_MODE, blendMode);
   198         }
   192         }
   199 
   193 
   200         if (prevMask) {
       
   201             vgSeti(VG_MASKING, prevMask);
       
   202         }
       
   203 
       
   204         if (!clipPath.isEmpty()) {
   194         if (!clipPath.isEmpty()) {
   205             vgDestroyPath(vgpath);
       
   206             vgpath = VG_INVALID_HANDLE;
       
   207             painter->endNativePainting();
   195             painter->endNativePainting();
       
   196             if (!clipped) {
       
   197                 painter->setClipPath(oldPath, Qt::NoClip);
       
   198             } else {
       
   199                 painter->setClipPath(oldPath);
       
   200             }
       
   201             painter->setClipping(clipped);
   208         }
   202         }
   209         return true;
   203         return true;
   210     }
   204     }
   211 
   205 
   212     return false;
   206     return false;
   234     mat[7] = imageTransform.m32();
   228     mat[7] = imageTransform.m32();
   235     mat[8] = imageTransform.m33();
   229     mat[8] = imageTransform.m33();
   236     vgLoadMatrix(mat);
   230     vgLoadMatrix(mat);
   237 }
   231 }
   238 
   232 
   239 VGPath HbVgImageIconRenderer::painterPathToVGPath(const QPainterPath& path)
       
   240 {
       
   241     int count = path.elementCount();
       
   242 
       
   243     // Creating vgpath is needed else clipling is failing,
       
   244     // vgpaintengine also doing the same way,
       
   245     // optimising by storing the path as member was tried but not giving expected output
       
   246     VGPath vgpath = vgCreatePath(VG_PATH_FORMAT_STANDARD,
       
   247                                  VG_PATH_DATATYPE_F,
       
   248                                  1.0f,        // scale
       
   249                                  0.0f,        // bias
       
   250                                  0,   // segmentCapacityHint
       
   251                                  0,   // coordCapacityHint
       
   252                                  VG_PATH_CAPABILITY_APPEND_TO);
       
   253 
       
   254     if (count == 0) {
       
   255         return vgpath;
       
   256     }
       
   257 
       
   258     const QPainterPath::Element *elements = &(path.elementAt(0));
       
   259 
       
   260     // Sizes chosen so that drawRoundedRect() paths fit in these arrays.
       
   261     QVarLengthArray<VGfloat, 48> coords;
       
   262     QVarLengthArray<VGubyte, 20> segments;
       
   263 
       
   264     int curvePos = 0;
       
   265     QPointF temp;
       
   266 
       
   267     // Keep track of the start and end of each sub-path.  QPainterPath
       
   268     // does not have an "implicit close" flag like QVectorPath does.
       
   269     // We therefore have to detect closed paths by looking for a LineTo
       
   270     // element that connects back to the initial MoveTo element.
       
   271     qreal startx = 0.0;
       
   272     qreal starty = 0.0;
       
   273     qreal endx = 0.0;
       
   274     qreal endy = 0.0;
       
   275     bool haveStart = false;
       
   276     bool haveEnd = false;
       
   277 
       
   278     // Convert the members of the element array.
       
   279     for (int i = 0; i < count; ++i) {
       
   280         switch (elements[i].type) {
       
   281 
       
   282         case QPainterPath::MoveToElement: {
       
   283             if (haveStart && haveEnd && startx == endx && starty == endy) {
       
   284                 // Implicitly close the previous sub-path.
       
   285                 segments.append(VG_CLOSE_PATH);
       
   286             }
       
   287             startx = elements[i].x;
       
   288             starty = elements[i].y;
       
   289             coords.append(startx);
       
   290             coords.append(starty);
       
   291             haveStart = true;
       
   292             haveEnd = false;
       
   293             segments.append(VG_MOVE_TO_ABS);
       
   294         }
       
   295         break;
       
   296 
       
   297         case QPainterPath::LineToElement: {
       
   298             endx = elements[i].x;
       
   299             endy = elements[i].y;
       
   300             coords.append(endx);
       
   301             coords.append(endy);
       
   302             haveEnd = true;
       
   303             segments.append(VG_LINE_TO_ABS);
       
   304         }
       
   305         break;
       
   306 
       
   307         case QPainterPath::CurveToElement: {
       
   308             coords.append(elements[i].x);
       
   309             coords.append(elements[i].y);
       
   310             haveEnd = false;
       
   311             curvePos = 2;
       
   312         }
       
   313         break;
       
   314 
       
   315         case QPainterPath::CurveToDataElement: {
       
   316             coords.append(elements[i].x);
       
   317             coords.append(elements[i].y);
       
   318             haveEnd = false;
       
   319             curvePos += 2;
       
   320             if (curvePos == 6) {
       
   321                 curvePos = 0;
       
   322                 segments.append(VG_CUBIC_TO_ABS);
       
   323             }
       
   324         }
       
   325         break;
       
   326 
       
   327         }
       
   328     }
       
   329 
       
   330     if ( haveStart && haveEnd && (startx == endx) && (starty == endy) ) {
       
   331         // Implicitly close the last sub-path.
       
   332         segments.append(VG_CLOSE_PATH);
       
   333     }
       
   334 
       
   335     vgAppendPathData(vgpath, segments.count(),
       
   336                      segments.constData(), coords.constData());
       
   337 
       
   338     return vgpath;
       
   339 }
       
   340