Remove uses of SkTAddOffset and other private Skia functions

Bug: skbug.com/13983
Change-Id: Ic99990667de25aa0a2e84f76ff0145d158dbef49
diff --git a/libs/hwui/RecordingCanvas.cpp b/libs/hwui/RecordingCanvas.cpp
index cb385d4..c5580b3 100644
--- a/libs/hwui/RecordingCanvas.cpp
+++ b/libs/hwui/RecordingCanvas.cpp
@@ -21,6 +21,7 @@
 #include <hwui/Paint.h>
 
 #include <experimental/type_traits>
+#include <log/log.h>
 #include <utility>
 
 #include "SkAndroidFrameworkUtils.h"
@@ -65,16 +66,24 @@
 
 template <typename S, typename... Rest>
 static void copy_v(void* dst, const S* src, int n, Rest&&... rest) {
-    SkASSERTF(((uintptr_t)dst & (alignof(S) - 1)) == 0,
-              "Expected %p to be aligned for at least %zu bytes.", dst, alignof(S));
-    sk_careful_memcpy(dst, src, n * sizeof(S));
-    copy_v(SkTAddOffset<void>(dst, n * sizeof(S)), std::forward<Rest>(rest)...);
+    LOG_FATAL_IF(((uintptr_t)dst & (alignof(S) - 1)) != 0,
+                 "Expected %p to be aligned for at least %zu bytes.",
+                 dst, alignof(S));
+    // If n is 0, there is nothing to copy into dst from src.
+    if (n > 0) {
+        memcpy(dst, src, n * sizeof(S));
+        dst = reinterpret_cast<void*>(
+                reinterpret_cast<uint8_t*>(dst) + n * sizeof(S));
+    }
+    // Repeat for the next items, if any
+    copy_v(dst, std::forward<Rest>(rest)...);
 }
 
 // Helper for getting back at arrays which have been copy_v'd together after an Op.
 template <typename D, typename T>
 static const D* pod(const T* op, size_t offset = 0) {
-    return SkTAddOffset<const D>(op + 1, offset);
+    return reinterpret_cast<const D*>(
+                reinterpret_cast<const uint8_t*>(op + 1) + offset);
 }
 
 namespace {
@@ -612,7 +621,7 @@
 template <typename T, typename... Args>
 void* DisplayListData::push(size_t pod, Args&&... args) {
     size_t skip = SkAlignPtr(sizeof(T) + pod);
-    SkASSERT(skip < (1 << 24));
+    LOG_FATAL_IF(skip >= (1 << 24));
     if (fUsed + skip > fReserved) {
         static_assert(is_power_of_two(SKLITEDL_PAGE),
                       "This math needs updating for non-pow2.");
@@ -621,7 +630,7 @@
         fBytes.realloc(fReserved);
         LOG_ALWAYS_FATAL_IF(fBytes.get() == nullptr, "realloc(%zd) failed", fReserved);
     }
-    SkASSERT(fUsed + skip <= fReserved);
+    LOG_FATAL_IF((fUsed + skip) > fReserved);
     auto op = (T*)(fBytes.get() + fUsed);
     fUsed += skip;
     new (op) T{std::forward<Args>(args)...};
@@ -751,7 +760,7 @@
     int fs = lattice.fRectTypes ? (xs + 1) * (ys + 1) : 0;
     size_t bytes = (xs + ys) * sizeof(int) + fs * sizeof(SkCanvas::Lattice::RectType) +
                    fs * sizeof(SkColor);
-    SkASSERT(lattice.fBounds);
+    LOG_FATAL_IF(!lattice.fBounds);
     void* pod = this->push<DrawImageLattice>(bytes, std::move(image), xs, ys, fs, *lattice.fBounds,
                                              dst, filter, paint, palette);
     copy_v(pod, lattice.fXDivs, xs, lattice.fYDivs, ys, lattice.fColors, fs, lattice.fRectTypes,
diff --git a/libs/hwui/jni/Graphics.cpp b/libs/hwui/jni/Graphics.cpp
index ae2e974..f5cd793 100644
--- a/libs/hwui/jni/Graphics.cpp
+++ b/libs/hwui/jni/Graphics.cpp
@@ -8,7 +8,6 @@
 #include <nativehelper/JNIHelp.h>
 #include "GraphicsJNI.h"
 
-#include "include/private/SkTemplates.h" // SkTAddOffset
 #include "SkBitmap.h"
 #include "SkCanvas.h"
 #include "SkColorSpace.h"
@@ -21,6 +20,7 @@
 #include "SkTypes.h"
 #include <cutils/ashmem.h>
 #include <hwui/Canvas.h>
+#include <log/log.h>
 
 using namespace android;
 
@@ -490,7 +490,7 @@
 
 void GraphicsJNI::set_metrics(JNIEnv* env, jobject metrics, const SkFontMetrics& skmetrics) {
     if (metrics == nullptr) return;
-    SkASSERT(env->IsInstanceOf(metrics, gFontMetrics_class));
+    LOG_FATAL_IF(!env->IsInstanceOf(metrics, gFontMetrics_class));
     env->SetFloatField(metrics, gFontMetrics_top, SkScalarToFloat(skmetrics.fTop));
     env->SetFloatField(metrics, gFontMetrics_ascent, SkScalarToFloat(skmetrics.fAscent));
     env->SetFloatField(metrics, gFontMetrics_descent, SkScalarToFloat(skmetrics.fDescent));
@@ -504,7 +504,7 @@
     int leading = SkScalarRoundToInt(skmetrics.fLeading);
 
     if (metrics) {
-        SkASSERT(env->IsInstanceOf(metrics, gFontMetricsInt_class));
+        LOG_FATAL_IF(!env->IsInstanceOf(metrics, gFontMetricsInt_class));
         env->SetIntField(metrics, gFontMetricsInt_top, SkScalarFloorToInt(skmetrics.fTop));
         env->SetIntField(metrics, gFontMetricsInt_ascent, ascent);
         env->SetIntField(metrics, gFontMetricsInt_descent, descent);
@@ -713,7 +713,9 @@
                 mSkiaBitmap->info().height());
         for (int y = 0; y < rowsToCopy; y++) {
             memcpy(dst, mSkiaBitmap->getAddr(0, y), bytesToCopy);
-            dst = SkTAddOffset<void>(dst, dstRowBytes);
+            // Cast to bytes in order to apply the dstRowBytes offset correctly.
+            dst = reinterpret_cast<void*>(
+                    reinterpret_cast<uint8_t*>(dst) + dstRowBytes);
         }
         recycledPixels->notifyPixelsChanged();
         recycledPixels->unref();
diff --git a/libs/hwui/jni/Mesh.h b/libs/hwui/jni/Mesh.h
index 7a73f2d..61c2260 100644
--- a/libs/hwui/jni/Mesh.h
+++ b/libs/hwui/jni/Mesh.h
@@ -20,6 +20,7 @@
 #include <SkMesh.h>
 #include <jni.h>
 
+#include <log/log.h>
 #include <utility>
 
 #include "graphics_jni_helpers.h"
@@ -186,23 +187,24 @@
         std::enable_if_t<std::is_trivially_copyable<T>::value, MeshUniform> operator=(
                 const T& val) {
             if (!fVar) {
-                SkDEBUGFAIL("Assigning to missing variable");
+                LOG_FATAL("Assigning to missing variable");
             } else if (sizeof(val) != fVar->sizeInBytes()) {
-                SkDEBUGFAIL("Incorrect value size");
+                LOG_FATAL("Incorrect value size");
             } else {
-                memcpy(SkTAddOffset<void>(fOwner->writableUniformData(), fVar->offset), &val,
-                       szeof(val));
+                void* dst = reinterpret_cast<void*>(
+                    reinterpret_cast<uint8_t*>(fOwner->writableUniformData()) + fVar->offset);
+                memcpy(dst, &val, sizeof(val));
             }
         }
 
         MeshUniform& operator=(const SkMatrix& val) {
             if (!fVar) {
-                SkDEBUGFAIL("Assigning to missing variable");
+                LOG_FATAL("Assigning to missing variable");
             } else if (fVar->sizeInBytes() != 9 * sizeof(float)) {
-                SkDEBUGFAIL("Incorrect value size");
+                LOG_FATAL("Incorrect value size");
             } else {
-                float* data =
-                        SkTAddOffset<float>(fOwner->writableUniformData(), (ptrdiff_t)fVar->offset);
+                float* data = reinterpret_cast<float*>(
+                    reinterpret_cast<uint8_t*>(fOwner->writableUniformData()) + fVar->offset);
                 data[0] = val.get(0);
                 data[1] = val.get(3);
                 data[2] = val.get(6);
@@ -220,14 +222,15 @@
         bool set(const T val[], const int count) {
             static_assert(std::is_trivially_copyable<T>::value, "Value must be trivial copyable");
             if (!fVar) {
-                SkDEBUGFAIL("Assigning to missing variable");
+                LOG_FATAL("Assigning to missing variable");
                 return false;
             } else if (sizeof(T) * count != fVar->sizeInBytes()) {
-                SkDEBUGFAIL("Incorrect value size");
+                LOG_FATAL("Incorrect value size");
                 return false;
             } else {
-                memcpy(SkTAddOffset<void>(fOwner->writableUniformData(), fVar->offset), val,
-                       sizeof(T) * count);
+                void* dst = reinterpret_cast<void*>(
+                    reinterpret_cast<uint8_t*>(fOwner->writableUniformData()) + fVar->offset);
+                memcpy(dst, val, sizeof(T) * count);
             }
             return true;
         }