diff options
author | humper@google.com <humper@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-04-08 21:44:11 +0000 |
---|---|---|
committer | humper@google.com <humper@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-04-08 21:44:11 +0000 |
commit | 75e3ca127cd14fffc9c8df7ea03d6529fb001831 (patch) | |
tree | ef98cdcf55fb22388bf648e90f305ac19b9535d0 /src/core | |
parent | 496120185c7baed4edf1c110f4f5b43f29bab1c0 (diff) |
Error checking / reporting API
Review URL: https://codereview.chromium.org/13699004
git-svn-id: http://skia.googlecode.com/svn/trunk@8566 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/SkError.cpp | 143 | ||||
-rw-r--r-- | src/core/SkErrorInternals.h | 27 | ||||
-rw-r--r-- | src/core/SkPath.cpp | 11 |
3 files changed, 180 insertions, 1 deletions
diff --git a/src/core/SkError.cpp b/src/core/SkError.cpp new file mode 100644 index 0000000000..3555c6961c --- /dev/null +++ b/src/core/SkError.cpp @@ -0,0 +1,143 @@ + +/* + * Copyright 2013 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkTLS.h" +#include "SkTypes.h" +#include "SkError.h" +#include "SkErrorInternals.h" + +#include <stdio.h> +#include <stdarg.h> + +namespace { + void *CreateThreadError() { + return SkNEW_ARGS(SkError, (kNoError_SkError)); + } + void DeleteThreadError(void* v) { + SkDELETE(reinterpret_cast<SkError*>(v)); + } + #define THREAD_ERROR \ + (*reinterpret_cast<SkError*>(SkTLS::Get(CreateThreadError, DeleteThreadError))) + + void *CreateThreadErrorCallback() { + return SkNEW_ARGS(SkErrorCallbackFunction, (SkErrorInternals::DefaultErrorCallback)); + } + void DeleteThreadErrorCallback(void* v) { + SkDELETE(reinterpret_cast<SkErrorCallbackFunction *>(v)); + } + + #define THREAD_ERROR_CALLBACK \ + *(reinterpret_cast<SkErrorCallbackFunction *>(SkTLS::Get(CreateThreadErrorCallback, \ + DeleteThreadErrorCallback))) + + void *CreateThreadErrorContext() { + return SkNEW_ARGS(void **, (NULL)); + } + void DeleteThreadErrorContext(void* v) { + SkDELETE(reinterpret_cast<void **>(v)); + } + #define THREAD_ERROR_CONTEXT \ + (*reinterpret_cast<void **>(SkTLS::Get(CreateThreadErrorContext, DeleteThreadErrorContext))) + + #define ERROR_STRING_LENGTH 2048 + + void *CreateThreadErrorString() { + return SkNEW_ARRAY(char, (ERROR_STRING_LENGTH)); + } + void DeleteThreadErrorString(void* v) { + SkDELETE_ARRAY(reinterpret_cast<char *>(v)); + } + #define THREAD_ERROR_STRING \ + (reinterpret_cast<char *>(SkTLS::Get(CreateThreadErrorString, DeleteThreadErrorString))) +} + +SkError SkGetLastError() { + return SkErrorInternals::GetLastError(); +} +void SkClearLastError() { + SkErrorInternals::ClearError(); +} +void SkSetErrorCallback(SkErrorCallbackFunction cb, void *context) { + SkErrorInternals::SetErrorCallback(cb, context); +} +const char *SkGetLastErrorString() { + return SkErrorInternals::GetLastErrorString(); +} + +// ------------ Private Error functions --------- + +void SkErrorInternals::SetErrorCallback(SkErrorCallbackFunction cb, void *context) { + if (cb == NULL) { + THREAD_ERROR_CALLBACK = SkErrorInternals::DefaultErrorCallback; + } else { + THREAD_ERROR_CALLBACK = cb; + } + THREAD_ERROR_CONTEXT = context; +} + +void SkErrorInternals::DefaultErrorCallback(SkError code, void *context) { + SkDebugf("Skia Error: %s\n", SkGetLastErrorString()); +} + +void SkErrorInternals::ClearError() { + SkErrorInternals::SetError( kNoError_SkError, "All is well" ); +} + +SkError SkErrorInternals::GetLastError() { + return THREAD_ERROR; +} + +const char *SkErrorInternals::GetLastErrorString() { + return THREAD_ERROR_STRING; +} + +void SkErrorInternals::SetError(SkError code, const char *fmt, ...) { + THREAD_ERROR = code; + va_list args; + + char *str = THREAD_ERROR_STRING; + const char *error_name = NULL; + switch( code ) { + case kNoError_SkError: + error_name = "No Error"; + break; + case kInvalidArgument_SkError: + error_name = "Invalid Argument"; + break; + case kInvalidOperation_SkError: + error_name = "Invalid Operation"; + break; + case kInvalidHandle_SkError: + error_name = "Invalid Handle"; + break; + case kInvalidPaint_SkError: + error_name = "Invalid Paint"; + break; + case kOutOfMemory_SkError: + error_name = "Out Of Memory"; + break; + case kParseError_SkError: + error_name = "Parse Error"; + break; + default: + error_name = "Unknown error"; + break; + } + + sprintf( str, "%s: ", error_name ); + int string_left = ERROR_STRING_LENGTH - strlen( str ); + str += strlen(str); + + va_start( args, fmt ); + vsnprintf( str, string_left, fmt, args ); + va_end( args ); + SkErrorCallbackFunction fn = THREAD_ERROR_CALLBACK; + if (fn && code != kNoError_SkError) { + fn(code, THREAD_ERROR_CONTEXT); + } +} diff --git a/src/core/SkErrorInternals.h b/src/core/SkErrorInternals.h new file mode 100644 index 0000000000..778d539866 --- /dev/null +++ b/src/core/SkErrorInternals.h @@ -0,0 +1,27 @@ + +/* + * Copyright 2013 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkErrorInternals_DEFINED +#define SkErrorInternals_DEFINED + +#include "SkError.h" + +class SkErrorInternals { + +public: + static void ClearError(); + static void SetError(SkError code, const char *fmt, ...); + static SkError GetLastError(); + static const char *GetLastErrorString(); + static void SetErrorCallback(SkErrorCallbackFunction cb, void *context); + static void DefaultErrorCallback(SkError code, void *context); +}; + + + +#endif /* SkErrorInternals_DEFINED */ diff --git a/src/core/SkPath.cpp b/src/core/SkPath.cpp index 006fcf85f2..e029d438fd 100644 --- a/src/core/SkPath.cpp +++ b/src/core/SkPath.cpp @@ -7,9 +7,10 @@ */ -#include "SkPath.h" #include "SkBuffer.h" +#include "SkErrorInternals.h" #include "SkMath.h" +#include "SkPath.h" #include "SkPathRef.h" #include "SkRRect.h" #include "SkThread.h" @@ -1034,6 +1035,14 @@ bool SkPath::hasOnlyMoveTos() const { void SkPath::addRoundRect(const SkRect& rect, SkScalar rx, SkScalar ry, Direction dir) { assert_known_direction(dir); + + if (rx < 0 || ry < 0) { + SkErrorInternals::SetError( kInvalidArgument_SkError, + "I got %f and %f as radii to SkPath::AddRoundRect, " + "but negative radii are not allowed.", + SkScalarToDouble(rx), SkScalarToDouble(ry) ); + return; + } SkScalar w = rect.width(); SkScalar halfW = SkScalarHalf(w); |