Bug 1385159 - Use AutoArray to prevent buffer allocation in DrawTargetSkia::DrawGlyphs. draft
authorcku <cku@mozilla.com>
Fri, 28 Jul 2017 16:24:20 +0800
changeset 617263 5809e499797365044d28555eb33e55035eb1dcd6
parent 617262 a7270c7288a4e3534b4c76978040bf0fd4ca1a4f
child 639770 a2f1adfe1b647bb1fa33ab6fd2a94537cedbbed0
push id71004
push usercku@mozilla.com
push dateFri, 28 Jul 2017 08:31:32 +0000
bugs1385159
milestone56.0a1
Bug 1385159 - Use AutoArray to prevent buffer allocation in DrawTargetSkia::DrawGlyphs. Replace std::vector by AutoArray to prevent heap allocation. I set default size of these array by 512, since in practice, most strings are shorter than 512. MozReview-Commit-ID: 4xVOaEHkJVi
gfx/2d/DrawTargetSkia.cpp
--- a/gfx/2d/DrawTargetSkia.cpp
+++ b/gfx/2d/DrawTargetSkia.cpp
@@ -1438,28 +1438,24 @@ DrawTargetSkia::DrawGlyphs(ScaledFont* a
     break;
   }
 #endif
   default:
     break;
   }
 
   paint.mPaint.setSubpixelText(useSubpixelText);
-  AutoTArray<uint16_t, 10> indices;
-  AutoTArray<SkPoint, 10> offsets;
-  if (!indices.SetLength(aBuffer.mNumGlyphs, fallible) ||
-      !offsets.SetLength(aBuffer.mNumGlyphs, fallible)) {
-    // Run out of memory.
-    return;
-  }
+  AutoTArray<uint16_t, 512> indices;
+  AutoTArray<SkPoint, 512> offsets;
 
   for (unsigned int i = 0; i < aBuffer.mNumGlyphs; i++) {
-    indices[i] = aBuffer.mGlyphs[i].mIndex;
-    offsets[i].fX = SkFloatToScalar(aBuffer.mGlyphs[i].mPosition.x);
-    offsets[i].fY = SkFloatToScalar(aBuffer.mGlyphs[i].mPosition.y);
+    indices.AppendElement(aBuffer.mGlyphs[i].mIndex);
+    offsets.AppendElement(
+      SkPoint::Make(SkFloatToScalar(aBuffer.mGlyphs[i].mPosition.x),
+                    SkFloatToScalar(aBuffer.mGlyphs[i].mPosition.y)));
   }
 
   mCanvas->drawPosText(indices.Elements(), aBuffer.mNumGlyphs*2, offsets.Elements(), paint.mPaint);
 }
 
 void
 DrawTargetSkia::FillGlyphs(ScaledFont* aFont,
                            const GlyphBuffer& aBuffer,