add init() to SkTLazy to create a default instance
authormike@reedtribe.org <mike@reedtribe.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Sun, 10 Apr 2011 00:35:29 +0000 (00:35 +0000)
committermike@reedtribe.org <mike@reedtribe.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Sun, 10 Apr 2011 00:35:29 +0000 (00:35 +0000)
use SkLazyPaint in internalDrawBitmap

git-svn-id: http://skia.googlecode.com/svn/trunk@1093 2bbb7eff-a529-9590-31e7-b0007b416f81

include/core/SkTLazy.h
src/core/SkCanvas.cpp

index 94e6415..fecc975 100644 (file)
@@ -48,6 +48,20 @@ public:
     }
 
     /**
+     *  Return a pointer to a default-initialized instance of the class. If a
+     *  previous instance had been initialzied (either from init() or set()) it
+     *  will first be destroyed, so that a freshly initialized instance is
+     *  always returned.
+     */
+    T* init() {
+        if (fPtr) {
+            fPtr->~T();
+        }
+        fPtr = new (fStorage) T;
+        return fPtr;
+    }
+        
+    /**
      *  Copy src into this, and return a pointer to a copy of it. Note this
      *  will always return the same pointer, so if it is called on a lazy that
      *  has already been initialized, then this will copy over the previous
index 07c7dbc..16cfc4b 100644 (file)
@@ -51,6 +51,8 @@
     #define dec_canvas()
 #endif
 
+typedef SkTLazy<SkPaint> SkLazyPaint;
+
 ///////////////////////////////////////////////////////////////////////////////
 // Helpers for computing fast bounds for quickReject tests
 
@@ -305,14 +307,14 @@ public:
     bool next(SkDrawFilter::Type drawType);
     
 private:
-    SkTLazy<SkPaint>    fLazyPaint;
-    SkCanvas*           fCanvas;
-    const SkPaint&      fOrigPaint;
-    SkDrawLooper*       fLooper;
-    SkDrawFilter*       fFilter;
-    const SkPaint*      fPaint;
-    int                 fSaveCount;
-    bool                fDone;
+    SkLazyPaint     fLazyPaint;
+    SkCanvas*       fCanvas;
+    const SkPaint&  fOrigPaint;
+    SkDrawLooper*   fLooper;
+    SkDrawFilter*   fFilter;
+    const SkPaint*  fPaint;
+    int             fSaveCount;
+    bool            fDone;
 };
 
 bool AutoDrawLooper::next(SkDrawFilter::Type drawType) {
@@ -841,12 +843,11 @@ void SkCanvas::internalDrawBitmap(const SkBitmap& bitmap, const SkIRect* srcRect
         return;
     }
 
+    SkLazyPaint lazy;
     if (NULL == paint) {
-        SkPaint tmpPaint;
-        this->commonDrawBitmap(bitmap, srcRect, matrix, tmpPaint);
-    } else {
-        this->commonDrawBitmap(bitmap, srcRect, matrix, *paint);
+        paint = lazy.init();
     }
+    this->commonDrawBitmap(bitmap, srcRect, matrix, *paint);
 }
 
 void SkCanvas::drawDevice(SkDevice* device, int x, int y,
@@ -1380,8 +1381,8 @@ public:
     const SkPaint& paint() const { return *fPaint; }
 
 private:
-    const SkPaint*   fPaint;
-    SkTLazy<SkPaint> fLazy;
+    const SkPaint*  fPaint;
+    SkLazyPaint     fLazy;
 };
 
 void SkCanvas::drawText(const void* text, size_t byteLength,