Change SkCanvasState to use inheritance.

Cherry-pick from master.

The base class, SkCanvasState, now holds the version, width, and
height. These fields will always be a necessary part of the class.
(Also add in some padding.)
The other fields, which may change, have been moved into the
subclass, SkCanvasState_v1. If/when the version changes, it will
correspond to a new subclass.
In SkCanvasStateUtils::CreateFromCanvasState, check the version on
the base class, then do a static_cast to the version corresponding
to SkCanvasState::version.

Remove CANVAS_STATE_VERSION, which is redundant with the version
specified by the subclass.

Use unambiguous type for rowBytes.

Build Android with SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG. This allows us
to run the full suite of CanvasState tests. It is also representative
of what will be used on Android by WebView.

Fix CanvasStateTest where it was broken inside ifdef'ed out code.

Use SkCanvas::getBaseLayerSize() instead of the deprecated
SkCanvas::getDeviceSize().

Update the comments in the header to be more clear. In particular,
an SkCanvasState can only be used to pass an SkCanvas' state to a
future version of Skia (or the same); not an older version.

NOTREECHECKS=true

BUG=b/15693384
R=reed@google.com, djsollen@google.com, mtklein@google.com

Author: scroggo@google.com

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

Review URL: https://codereview.chromium.org/396763002
diff --git a/include/utils/SkCanvasStateUtils.h b/include/utils/SkCanvasStateUtils.h
index f8266c9..6ea7b10 100644
--- a/include/utils/SkCanvasStateUtils.h
+++ b/include/utils/SkCanvasStateUtils.h
@@ -13,9 +13,9 @@
 class SkCanvasState;
 
 /**
- * A set of functions that are useful for copying an SkCanvas across a library
- * boundary where the Skia libraries on either side of the boundary may not be
- * version identical.  The expected usage is outline below...
+ * A set of functions that are useful for copying the state of an SkCanvas
+ * across a library boundary where the Skia library on the other side of the
+ * boundary may be newer. The expected usage is outline below...
  *
  *                          Lib Boundary
  * CaptureCanvasState(...)      |||
@@ -30,8 +30,8 @@
 namespace SkCanvasStateUtils {
     /**
      * Captures the current state of the canvas into an opaque ptr that is safe
-     * to pass between different instances of Skia (which may or may not be the
-     * same version). The function will return NULL in the event that one of the
+     * to pass to a different instance of Skia (which may be the same version,
+     * or may be newer). The function will return NULL in the event that one of the
      * following conditions are true.
      *  1) the canvas device type is not supported (currently only raster is supported)
      *  2) the canvas clip type is not supported (currently only non-AA clips are supported)
@@ -56,7 +56,7 @@
      *  1) the captured state is in an unrecognized format
      *  2) the captured canvas device type is not supported
      *
-     * @param canvas The canvas you wish to capture the current state of.
+     * @param state Opaque object created by CaptureCanvasState.
      * @return NULL or an SkCanvas* whose devices and matrix/clip state are
      *         identical to the captured canvas. The caller is responsible for
      *         calling unref on the SkCanvas.
@@ -66,7 +66,8 @@
     /**
      * Free the memory associated with the captured canvas state.  The state
      * should not be released until all SkCanvas objects created using that
-     * state have been dereferenced.
+     * state have been dereferenced. Must be called from the same library
+     * instance that created the state via CaptureCanvasState.
      *
      * @param state The captured state you wish to dispose of.
      */
diff --git a/src/utils/SkCanvasStateUtils.cpp b/src/utils/SkCanvasStateUtils.cpp
index eaee61b..e286c91 100644
--- a/src/utils/SkCanvasStateUtils.cpp
+++ b/src/utils/SkCanvasStateUtils.cpp
@@ -13,14 +13,13 @@
 #include "SkErrorInternals.h"
 #include "SkWriter32.h"
 
-#define CANVAS_STATE_VERSION 1
 /*
  * WARNING: The structs below are part of a stable ABI and as such we explicitly
  * use unambigious primitives (e.g. int32_t instead of an enum).
  *
- * ANY CHANGES TO THE STRUCTS BELOW THAT IMPACT THE ABI SHOULD RESULT IN AN
- * UPDATE OF THE CANVAS_STATE_VERSION. SUCH CHANGES SHOULD ONLY BE MADE IF
- * ABSOLUTELY NECESSARY!
+ * ANY CHANGES TO THE STRUCTS BELOW THAT IMPACT THE ABI SHOULD RESULT IN A NEW
+ * NEW SUBCLASS OF SkCanvasState. SUCH CHANGES SHOULD ONLY BE MADE IF ABSOLUTELY
+ * NECESSARY!
  */
 enum RasterConfigs {
   kUnknown_RasterConfig   = 0,
@@ -48,7 +47,8 @@
     ClipRect* clipRects;
 };
 
-// NOTE: If you add more members, bump CanvasState::version.
+// NOTE: If you add more members, create a new subclass of SkCanvasState with a
+// new CanvasState::version.
 struct SkCanvasLayerState {
     CanvasBackend type;
     int32_t x, y;
@@ -60,7 +60,7 @@
     union {
         struct {
             RasterConfig config; // pixel format: a value from RasterConfigs.
-            size_t rowBytes;     // Number of bytes from start of one line to next.
+            uint64_t rowBytes;   // Number of bytes from start of one line to next.
             void* pixels;        // The pixels, all (height * rowBytes) of them.
         } raster;
         struct {
@@ -71,20 +71,41 @@
 
 class SkCanvasState {
 public:
-    SkCanvasState(SkCanvas* canvas) {
+    SkCanvasState(int32_t version, SkCanvas* canvas) {
         SkASSERT(canvas);
-        version = CANVAS_STATE_VERSION;
-        width = canvas->getDeviceSize().width();
-        height = canvas->getDeviceSize().height();
-        layerCount = 0;
-        layers = NULL;
-        originalCanvas = SkRef(canvas);
+        this->version = version;
+        width = canvas->getBaseLayerSize().width();
+        height = canvas->getBaseLayerSize().height();
 
-        mcState.clipRectCount = 0;
-        mcState.clipRects = NULL;
     }
 
-    ~SkCanvasState() {
+    /**
+     * The version this struct was built with.  This field must always appear
+     * first in the struct so that when the versions don't match (and the
+     * remaining contents and size are potentially different) we can still
+     * compare the version numbers.
+     */
+    int32_t version;
+    int32_t width;
+    int32_t height;
+    int32_t alignmentPadding;
+};
+
+class SkCanvasState_v1 : public SkCanvasState {
+public:
+    static const int32_t kVersion = 1;
+
+    SkCanvasState_v1(SkCanvas* canvas)
+    : INHERITED(kVersion, canvas)
+    {
+        layerCount = 0;
+        layers = NULL;
+        mcState.clipRectCount = 0;
+        mcState.clipRects = NULL;
+        originalCanvas = SkRef(canvas);
+    }
+
+    ~SkCanvasState_v1() {
         // loop through the layers and free the data allocated to the clipRects
         for (int i = 0; i < layerCount; ++i) {
             sk_free(layers[i].mcState.clipRects);
@@ -98,24 +119,13 @@
         originalCanvas->unref();
     }
 
-    /**
-     * The version this struct was built with.  This field must always appear
-     * first in the struct so that when the versions don't match (and the
-     * remaining contents and size are potentially different) we can still
-     * compare the version numbers.
-     */
-    int32_t version;
-
-    int32_t width;
-    int32_t height;
-
     SkMCState mcState;
 
     int32_t layerCount;
     SkCanvasLayerState* layers;
-
 private:
     SkCanvas* originalCanvas;
+    typedef SkCanvasState INHERITED;
 };
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -191,7 +201,7 @@
         return NULL;
     }
 
-    SkAutoTDelete<SkCanvasState> canvasState(SkNEW_ARGS(SkCanvasState, (canvas)));
+    SkAutoTDelete<SkCanvasState_v1> canvasState(SkNEW_ARGS(SkCanvasState_v1, (canvas)));
 
     // decompose the total matrix and clip
     setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(),
@@ -247,7 +257,7 @@
 
     // for now, just ignore any client supplied DrawFilter.
     if (canvas->getDrawFilter()) {
-//        SkDEBUGF(("CaptureCanvasState will ignore the canvases draw filter.\n"));
+//        SkDEBUGF(("CaptureCanvasState will ignore the canvas's draw filter.\n"));
     }
 
     return canvasState.detach();
@@ -291,7 +301,7 @@
 
     bitmap.installPixels(SkImageInfo::Make(layerState.width, layerState.height,
                                            colorType, kPremul_SkAlphaType),
-                         layerState.raster.pixels, layerState.raster.rowBytes);
+                         layerState.raster.pixels, (size_t) layerState.raster.rowBytes);
 
     SkASSERT(!bitmap.empty());
     SkASSERT(!bitmap.isNull());
@@ -306,30 +316,28 @@
 
 SkCanvas* SkCanvasStateUtils::CreateFromCanvasState(const SkCanvasState* state) {
     SkASSERT(state);
+    // Currently there is only one possible version.
+    SkASSERT(SkCanvasState_v1::kVersion == state->version);
 
-    // check that the versions match
-    if (CANVAS_STATE_VERSION != state->version) {
-        SkDebugf("CreateFromCanvasState version does not match the one use to create the input");
-        return NULL;
-    }
+    const SkCanvasState_v1* state_v1 = static_cast<const SkCanvasState_v1*>(state);
 
-    if (state->layerCount < 1) {
+    if (state_v1->layerCount < 1) {
         return NULL;
     }
 
     SkAutoTUnref<SkCanvasStack> canvas(SkNEW_ARGS(SkCanvasStack, (state->width, state->height)));
 
     // setup the matrix and clip on the n-way canvas
-    setup_canvas_from_MC_state(state->mcState, canvas);
+    setup_canvas_from_MC_state(state_v1->mcState, canvas);
 
     // Iterate over the layers and add them to the n-way canvas
-    for (int i = state->layerCount - 1; i >= 0; --i) {
-        SkAutoTUnref<SkCanvas> canvasLayer(create_canvas_from_canvas_layer(state->layers[i]));
+    for (int i = state_v1->layerCount - 1; i >= 0; --i) {
+        SkAutoTUnref<SkCanvas> canvasLayer(create_canvas_from_canvas_layer(state_v1->layers[i]));
         if (!canvasLayer.get()) {
             return NULL;
         }
-        canvas->pushCanvas(canvasLayer.get(), SkIPoint::Make(state->layers[i].x,
-                                                             state->layers[i].y));
+        canvas->pushCanvas(canvasLayer.get(), SkIPoint::Make(state_v1->layers[i].x,
+                                                             state_v1->layers[i].y));
     }
 
     return canvas.detach();
@@ -338,5 +346,9 @@
 ////////////////////////////////////////////////////////////////////////////////
 
 void SkCanvasStateUtils::ReleaseCanvasState(SkCanvasState* state) {
-    SkDELETE(state);
+    SkASSERT(!state || SkCanvasState_v1::kVersion == state->version);
+    // Upcast to the correct version of SkCanvasState. This avoids having a virtual destructor on
+    // SkCanvasState. That would be strange since SkCanvasState has no other virtual functions, and
+    // instead uses the field "version" to determine how to behave.
+    SkDELETE(static_cast<SkCanvasState_v1*>(state));
 }