Return NULL when building empty LayerRasterizer.
authorscroggo <scroggo@google.com>
Tue, 3 Jun 2014 20:12:51 +0000 (13:12 -0700)
committerCommit bot <commit-bot@chromium.org>
Tue, 3 Jun 2014 20:12:51 +0000 (13:12 -0700)
In SkLayerRasterizer::snapshotRasterizer() and ::detachRasterizer(),
if no layers have been added, do not attempt to create an
SkLayerRasterizer. Instead, return NULL.

This fixes an error when running tests on Android.

Update dox to state that NULL may be returned.

Add tests.

R=reed@google.com

Author: scroggo@google.com

Review URL: https://codereview.chromium.org/313653006

include/effects/SkLayerRasterizer.h
src/effects/SkLayerRasterizer.cpp
tests/LayerRasterizerTest.cpp

index fc21a7c..9d4c823 100644 (file)
@@ -39,9 +39,10 @@ public:
 
         /**
           *  Pass queue of layers on to newly created layer rasterizer and return it. The builder
-          *  *cannot* be used any more after calling this function.
+          *  *cannot* be used any more after calling this function. If no layers have been added,
+          *  returns NULL.
           *
-          *  The caller is responsible for calling unref() on the returned object.
+          *  The caller is responsible for calling unref() on the returned object, if non NULL.
           */
         SkLayerRasterizer* detachRasterizer();
 
@@ -51,11 +52,11 @@ public:
           *  *may* be used after calling this function. It will continue to hold any layers
           *  previously added, so consecutive calls to this function will return identical objects,
           *  and objects returned by future calls to this function contain all the layers in
-          *  previously returned objects.
+          *  previously returned objects. If no layers have been added, returns NULL.
           *
           *  Future calls to addLayer will not affect rasterizers previously returned by this call.
           *
-          *  The caller is responsible for calling unref() on the returned object.
+          *  The caller is responsible for calling unref() on the returned object, if non NULL.
           */
         SkLayerRasterizer* snapshotRasterizer() const;
 
index ca611d5..90fd59b 100644 (file)
@@ -213,12 +213,21 @@ void SkLayerRasterizer::Builder::addLayer(const SkPaint& paint, SkScalar dx,
 }
 
 SkLayerRasterizer* SkLayerRasterizer::Builder::detachRasterizer() {
-    SkLayerRasterizer* rasterizer = SkNEW_ARGS(SkLayerRasterizer, (fLayers));
+    SkLayerRasterizer* rasterizer;
+    if (0 == fLayers->count()) {
+        rasterizer = NULL;
+        SkDELETE(fLayers);
+    } else {
+        rasterizer = SkNEW_ARGS(SkLayerRasterizer, (fLayers));
+    }
     fLayers = NULL;
     return rasterizer;
 }
 
 SkLayerRasterizer* SkLayerRasterizer::Builder::snapshotRasterizer() const {
+    if (0 == fLayers->count()) {
+        return NULL;
+    }
     SkDeque* layers = SkNEW_ARGS(SkDeque, (sizeof(SkLayerRasterizer_Rec), fLayers->count()));
     SkDeque::F2BIter                iter(*fLayers);
     const SkLayerRasterizer_Rec*    recOrig;
index 8e6b64c..640c581 100644 (file)
@@ -81,6 +81,7 @@ static bool equals(const SkLayerRasterizer_Rec& rec1, const SkLayerRasterizer_Re
 
 DEF_TEST(LayerRasterizer_copy, reporter) {
     SkLayerRasterizer::Builder builder;
+    REPORTER_ASSERT(reporter, NULL == builder.snapshotRasterizer());
     SkPaint paint;
     // Create a bunch of paints with different flags.
     for (uint32_t flags = 0x01; flags < SkPaint::kAllFlags; flags <<= 1) {
@@ -136,3 +137,8 @@ DEF_TEST(LayerRasterizer_copy, reporter) {
         }
     }
 }
+
+DEF_TEST(LayerRasterizer_detachEmpty, reporter) {
+    SkLayerRasterizer::Builder builder;
+    REPORTER_ASSERT(reporter, NULL == builder.detachRasterizer());
+}