expand internal subclasses into separate files
authorreed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 30 Jul 2012 18:20:12 +0000 (18:20 +0000)
committerreed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 30 Jul 2012 18:20:12 +0000 (18:20 +0000)
git-svn-id: http://skia.googlecode.com/svn/trunk@4836 2bbb7eff-a529-9590-31e7-b0007b416f81

gm/image.cpp
src/image/SkImage.cpp
src/image/SkImagePriv.h
src/image/SkImage_Base.h [new file with mode: 0644]
src/image/SkImage_Picture.cpp [new file with mode: 0644]
src/image/SkImage_Raster.cpp [new file with mode: 0644]

index 0d6443e275369c7d2222daca10158c8d63c5c9b6..8947f0a4b7ab74330ca0ff37e17475996721d4ad 100644 (file)
@@ -47,6 +47,7 @@ static void test_surface(SkCanvas* canvas, SkSurface* surf) {
 
 class ImageGM : public skiagm::GM {
     void*   fBuffer;
+    size_t  fBufferSize;
     SkSize  fSize;
     enum {
         W = 64,
@@ -55,7 +56,8 @@ class ImageGM : public skiagm::GM {
     };
 public:
     ImageGM() {
-        fBuffer = sk_malloc_throw(RB * H);
+        fBufferSize = RB * H;
+        fBuffer = sk_malloc_throw(fBufferSize);
         fSize.set(SkIntToScalar(W), SkIntToScalar(H));
     }
     
@@ -74,6 +76,9 @@ protected:
     }
     
     virtual void onDraw(SkCanvas* canvas) {
+        // since we draw into this directly, we need to start fresh
+        sk_bzero(fBuffer, fBufferSize);
+
         SkImage::Info info;
 
         info.fWidth = W;
index 9cf9267618d92e624d9d585313d7d9d58f3d0c68..8faa91f00ab0fbcbabab0afdc363f6c3229cf56b 100644 (file)
-#include "SkImage.h"
+#include "SkImage_Base.h"
 #include "SkImagePriv.h"
 #include "SkBitmap.h"
 #include "SkCanvas.h"
 
 ///////////////////////////////////////////////////////////////////////////////
 
-class SkImage_Base : public SkImage {
-public:
-    SkImage_Base(int width, int height) : INHERITED(width, height) {}
-
-    virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) = 0;
-
-private:    
-    typedef SkImage INHERITED;
-};
-
 static SkImage_Base* asIB(SkImage* image) {
     return static_cast<SkImage_Base*>(image);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
 
-
-class SkImage_Raster : public SkImage_Base {
-public:
-    static bool ValidArgs(const Info& info, SkColorSpace* cs, size_t rowBytes) {
-        const int maxDimension = SK_MaxS32 >> 2;
-        const size_t kMaxPixelByteSize = SK_MaxS32;
-
-        if (info.fWidth < 0 || info.fHeight < 0) {
-            return false;
-        }
-        if (info.fWidth > maxDimension || info.fHeight > maxDimension) {
-            return false;
-        }
-        if ((unsigned)info.fColorType > (unsigned)kLastEnum_ColorType) {
-            return false;
-        }
-        if ((unsigned)info.fAlphaType > (unsigned)kLastEnum_AlphaType) {
-            return false;
-        }
-
-        bool isOpaque;
-        if (SkImageInfoToBitmapConfig(info, &isOpaque) == SkBitmap::kNo_Config) {
-            return false;
-        }
-            
-        // TODO: check colorspace
-        
-        if (rowBytes < SkImageMinRowBytes(info)) {
-            return false;
-        }
-        
-        int64_t size = (int64_t)info.fHeight * rowBytes;
-        if (size > kMaxPixelByteSize) {
-            return false;
-        }
-        return true;
-    }
-
-    static SkImage* NewEmpty();
-
-    SkImage_Raster(const SkImage::Info&, SkColorSpace*, SkData*, size_t rb);
-    virtual ~SkImage_Raster();
-
-    virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRIDE;
-
-    // exposed for SkSurface_Raster via SkNewImageFromPixelRef
-    SkImage_Raster(const SkImage::Info&, SkPixelRef*, size_t rowBytes);
-
-private:
-    SkImage_Raster() : INHERITED(0, 0) {}
-
-    SkBitmap    fBitmap;
-
-    typedef SkImage_Base INHERITED;
-};
-
-SkImage* SkNewImageFromPixelRef(const SkImage::Info& info, SkPixelRef* pr,
-                                size_t rowBytes) {
-    return SkNEW_ARGS(SkImage_Raster, (info, pr, rowBytes));
-}
-
-///////////////////////////////////////////////////////////////////////////////
-
-#include "SkData.h"
-#include "SkDataPixelRef.h"
-
-SkImage* SkImage_Raster::NewEmpty() {
-    // Returns lazily created singleton
-    static SkImage* gEmpty;
-    if (NULL == gEmpty) {
-        gEmpty = SkNEW(SkImage_Raster);
-    }
-    gEmpty->ref();
-    return gEmpty;
-}
-
-SkImage_Raster::SkImage_Raster(const Info& info, SkColorSpace* cs,
-                               SkData* data, size_t rowBytes)
-: INHERITED(info.fWidth, info.fHeight) {
-    bool isOpaque;
-    SkBitmap::Config config = SkImageInfoToBitmapConfig(info, &isOpaque);
-    
-    fBitmap.setConfig(config, info.fWidth, info.fHeight, rowBytes);
-    fBitmap.setPixelRef(SkNEW_ARGS(SkDataPixelRef, (data)))->unref();
-    fBitmap.setIsOpaque(isOpaque);
-    fBitmap.setImmutable();
-}
-
-SkImage_Raster::SkImage_Raster(const Info& info, SkPixelRef* pr, size_t rowBytes)
-        : INHERITED(info.fWidth, info.fHeight) {
-    SkASSERT(pr->isImmutable());
-
-    bool isOpaque;
-    SkBitmap::Config config = SkImageInfoToBitmapConfig(info, &isOpaque);
-
-    fBitmap.setConfig(config, info.fWidth, info.fHeight, rowBytes);
-    fBitmap.setPixelRef(pr);
-    fBitmap.setIsOpaque(isOpaque);
-    fBitmap.setImmutable();
-}
-
-SkImage_Raster::~SkImage_Raster() {}
-
-void SkImage_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) {
-    canvas->drawBitmap(fBitmap, x, y, paint);
-}
-
-///////////////////////////////////////////////////////////////////////////////
-
-#include "SkPicture.h"
-
-class SkImage_Picture : public SkImage_Base {
-public:
-    SkImage_Picture(SkPicture*);
-    virtual ~SkImage_Picture();
-
-    virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRIDE;
-
-private:
-    SkPicture*  fPicture;
-
-    typedef SkImage_Base INHERITED;
-};
-
-SkImage_Picture::SkImage_Picture(SkPicture* pict) : INHERITED(pict->width(), pict->height()) {
-    pict->endRecording();
-    pict->ref();
-    fPicture = pict;
-}
-
-SkImage_Picture::~SkImage_Picture() {
-    fPicture->unref();
-}
-
-void SkImage_Picture::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
-                             const SkPaint* paint) {
-    SkImagePrivDrawPicture(canvas, fPicture, x, y, paint);
-}
-
-SkImage* SkNewImageFromPicture(SkPicture* pict) {
-    return SkNEW_ARGS(SkImage_Picture, (pict));
-}
-
-///////////////////////////////////////////////////////////////////////////////
-
-SkImage* SkImage::NewRasterCopy(const SkImage::Info& info, SkColorSpace* cs,
-                                const void* pixels, size_t rowBytes) {
-    if (!SkImage_Raster::ValidArgs(info, cs, rowBytes)) {
-        return NULL;
-    }
-    if (0 == info.fWidth && 0 == info.fHeight) {
-        return SkImage_Raster::NewEmpty();
-    }
-    // check this after empty-check
-    if (NULL == pixels) {
-        return NULL;
-    }
-    
-    // Here we actually make a copy of the caller's pixel data
-    SkAutoDataUnref data(SkData::NewWithCopy(pixels, info.fHeight * rowBytes));
-    return SkNEW_ARGS(SkImage_Raster, (info, cs, data, rowBytes));
-}
-
-
-SkImage* SkImage::NewRasterData(const SkImage::Info& info, SkColorSpace* cs,
-                                SkData* pixelData, size_t rowBytes) {
-    if (!SkImage_Raster::ValidArgs(info, cs, rowBytes)) {
-        return NULL;
-    }
-    if (0 == info.fWidth && 0 == info.fHeight) {
-        return SkImage_Raster::NewEmpty();
-    }
-    // check this after empty-check
-    if (NULL == pixelData) {
-        return NULL;
-    }
-    
-    // did they give us enough data?
-    size_t size = info.fHeight * rowBytes;
-    if (pixelData->size() < size) {
-        return NULL;
-    }
-    
-    SkAutoDataUnref data(pixelData);
-    return SkNEW_ARGS(SkImage_Raster, (info, cs, data, rowBytes));
-}
-
-///////////////////////////////////////////////////////////////////////////////
-
-#include "SkCanvas.h"
-
 uint32_t SkImage::NextUniqueID() {
     static int32_t gUniqueID;
 
index 525ca420392481d656ba76280da69d17cf06aaf3..f69eaac4d4475808fa8edcb02152b1998f146544 100644 (file)
@@ -1,3 +1,9 @@
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
 
 #ifndef SkImagePriv_DEFINED
 #define SkImagePriv_DEFINED
diff --git a/src/image/SkImage_Base.h b/src/image/SkImage_Base.h
new file mode 100644 (file)
index 0000000..25498ec
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkImage_Base_DEFINED
+#define SkImage_Base_DEFINED
+
+#include "SkImage.h"
+
+class SkImage_Base : public SkImage {
+public:
+    SkImage_Base(int width, int height) : INHERITED(width, height) {}
+
+    virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) = 0;
+
+private:    
+    typedef SkImage INHERITED;
+};
+
+#endif
+
diff --git a/src/image/SkImage_Picture.cpp b/src/image/SkImage_Picture.cpp
new file mode 100644 (file)
index 0000000..c93c06f
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkImage_Base.h"
+#include "SkImagePriv.h"
+#include "SkPicture.h"
+
+class SkImage_Picture : public SkImage_Base {
+public:
+    SkImage_Picture(SkPicture*);
+    virtual ~SkImage_Picture();
+
+    virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRIDE;
+
+private:
+    SkPicture*  fPicture;
+
+    typedef SkImage_Base INHERITED;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+SkImage_Picture::SkImage_Picture(SkPicture* pict) : INHERITED(pict->width(), pict->height()) {
+    pict->endRecording();
+    pict->ref();
+    fPicture = pict;
+}
+
+SkImage_Picture::~SkImage_Picture() {
+    fPicture->unref();
+}
+
+void SkImage_Picture::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
+                             const SkPaint* paint) {
+    SkImagePrivDrawPicture(canvas, fPicture, x, y, paint);
+}
+
+SkImage* SkNewImageFromPicture(SkPicture* pict) {
+    return SkNEW_ARGS(SkImage_Picture, (pict));
+}
+
diff --git a/src/image/SkImage_Raster.cpp b/src/image/SkImage_Raster.cpp
new file mode 100644 (file)
index 0000000..2e6915e
--- /dev/null
@@ -0,0 +1,161 @@
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkImage_Base.h"
+#include "SkImagePriv.h"
+#include "SkBitmap.h"
+#include "SkCanvas.h"
+#include "SkData.h"
+#include "SkDataPixelRef.h"
+
+class SkImage_Raster : public SkImage_Base {
+public:
+    static bool ValidArgs(const Info& info, SkColorSpace* cs, size_t rowBytes) {
+        const int maxDimension = SK_MaxS32 >> 2;
+        const size_t kMaxPixelByteSize = SK_MaxS32;
+
+        if (info.fWidth < 0 || info.fHeight < 0) {
+            return false;
+        }
+        if (info.fWidth > maxDimension || info.fHeight > maxDimension) {
+            return false;
+        }
+        if ((unsigned)info.fColorType > (unsigned)kLastEnum_ColorType) {
+            return false;
+        }
+        if ((unsigned)info.fAlphaType > (unsigned)kLastEnum_AlphaType) {
+            return false;
+        }
+
+        bool isOpaque;
+        if (SkImageInfoToBitmapConfig(info, &isOpaque) == SkBitmap::kNo_Config) {
+            return false;
+        }
+            
+        // TODO: check colorspace
+        
+        if (rowBytes < SkImageMinRowBytes(info)) {
+            return false;
+        }
+        
+        int64_t size = (int64_t)info.fHeight * rowBytes;
+        if (size > kMaxPixelByteSize) {
+            return false;
+        }
+        return true;
+    }
+
+    static SkImage* NewEmpty();
+
+    SkImage_Raster(const SkImage::Info&, SkColorSpace*, SkData*, size_t rb);
+    virtual ~SkImage_Raster();
+
+    virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRIDE;
+
+    // exposed for SkSurface_Raster via SkNewImageFromPixelRef
+    SkImage_Raster(const SkImage::Info&, SkPixelRef*, size_t rowBytes);
+
+private:
+    SkImage_Raster() : INHERITED(0, 0) {}
+
+    SkBitmap    fBitmap;
+
+    typedef SkImage_Base INHERITED;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+SkImage* SkImage_Raster::NewEmpty() {
+    // Returns lazily created singleton
+    static SkImage* gEmpty;
+    if (NULL == gEmpty) {
+        gEmpty = SkNEW(SkImage_Raster);
+    }
+    gEmpty->ref();
+    return gEmpty;
+}
+
+SkImage_Raster::SkImage_Raster(const Info& info, SkColorSpace* cs,
+                               SkData* data, size_t rowBytes)
+: INHERITED(info.fWidth, info.fHeight) {
+    bool isOpaque;
+    SkBitmap::Config config = SkImageInfoToBitmapConfig(info, &isOpaque);
+    
+    fBitmap.setConfig(config, info.fWidth, info.fHeight, rowBytes);
+    fBitmap.setPixelRef(SkNEW_ARGS(SkDataPixelRef, (data)))->unref();
+    fBitmap.setIsOpaque(isOpaque);
+    fBitmap.setImmutable();
+}
+
+SkImage_Raster::SkImage_Raster(const Info& info, SkPixelRef* pr, size_t rowBytes)
+: INHERITED(info.fWidth, info.fHeight) {
+    SkASSERT(pr->isImmutable());
+    
+    bool isOpaque;
+    SkBitmap::Config config = SkImageInfoToBitmapConfig(info, &isOpaque);
+    
+    fBitmap.setConfig(config, info.fWidth, info.fHeight, rowBytes);
+    fBitmap.setPixelRef(pr);
+    fBitmap.setIsOpaque(isOpaque);
+    fBitmap.setImmutable();
+}
+
+SkImage_Raster::~SkImage_Raster() {}
+
+void SkImage_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) {
+    canvas->drawBitmap(fBitmap, x, y, paint);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+SkImage* SkImage::NewRasterCopy(const SkImage::Info& info, SkColorSpace* cs,
+                                const void* pixels, size_t rowBytes) {
+    if (!SkImage_Raster::ValidArgs(info, cs, rowBytes)) {
+        return NULL;
+    }
+    if (0 == info.fWidth && 0 == info.fHeight) {
+        return SkImage_Raster::NewEmpty();
+    }
+    // check this after empty-check
+    if (NULL == pixels) {
+        return NULL;
+    }
+    
+    // Here we actually make a copy of the caller's pixel data
+    SkAutoDataUnref data(SkData::NewWithCopy(pixels, info.fHeight * rowBytes));
+    return SkNEW_ARGS(SkImage_Raster, (info, cs, data, rowBytes));
+}
+
+
+SkImage* SkImage::NewRasterData(const SkImage::Info& info, SkColorSpace* cs,
+                                SkData* pixelData, size_t rowBytes) {
+    if (!SkImage_Raster::ValidArgs(info, cs, rowBytes)) {
+        return NULL;
+    }
+    if (0 == info.fWidth && 0 == info.fHeight) {
+        return SkImage_Raster::NewEmpty();
+    }
+    // check this after empty-check
+    if (NULL == pixelData) {
+        return NULL;
+    }
+    
+    // did they give us enough data?
+    size_t size = info.fHeight * rowBytes;
+    if (pixelData->size() < size) {
+        return NULL;
+    }
+    
+    SkAutoDataUnref data(pixelData);
+    return SkNEW_ARGS(SkImage_Raster, (info, cs, data, rowBytes));
+}
+
+SkImage* SkNewImageFromPixelRef(const SkImage::Info& info, SkPixelRef* pr,
+                                size_t rowBytes) {
+    return SkNEW_ARGS(SkImage_Raster, (info, pr, rowBytes));
+}
+