From 30b83d45a3af444e6868c638d86442c2ea3e20c6 Mon Sep 17 00:00:00 2001 From: halcanary Date: Sun, 26 Oct 2014 05:23:53 -0700 Subject: add gm: colorwheel BUG=skia:3061 Review URL: https://codereview.chromium.org/680533002 --- gm/colorwheel.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ gm/gm.h | 14 +++++++++++++ gyp/gmslides.gypi | 1 + tools/Resources.cpp | 13 +++++++++++- tools/Resources.h | 4 ++++ 5 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 gm/colorwheel.cpp diff --git a/gm/colorwheel.cpp b/gm/colorwheel.cpp new file mode 100644 index 0000000000..93c59388d9 --- /dev/null +++ b/gm/colorwheel.cpp @@ -0,0 +1,57 @@ +/* + * Copyright 2014 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "Resources.h" +#include "SkData.h" +#include "SkDecodingImageGenerator.h" +#include "gm.h" + +static void checkerboard( + SkCanvas* canvas, int w, int h, int size, SkColor c1, SkColor c2) { + SkAutoCanvasRestore autoCanvasRestore(canvas, true); + canvas->clipRect(SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h))); + canvas->drawColor(c1); + SkPaint paint; + paint.setColor(c2); + SkScalar s = SkIntToScalar(size); + for (int y = 0; y < h; y += size) { + SkScalar ty = SkIntToScalar(y); + bool oddRow = (y % (2 * size)) != 0; + for (int x = oddRow ? size : 0; x < w; x += (2 * size)) { + SkScalar tx = SkIntToScalar(x); + canvas->drawRect(SkRect::MakeXYWH(tx, ty, s, s), paint); + } + } +} + +static void draw_bitmap(SkCanvas* canvas, const char* resource, int x, int y) { + SkBitmap bitmap; + if (GetResourceAsBitmap(resource, &bitmap)) { + canvas->drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y)); + } else { + SkDebugf("\nCould not decode file '%s'. Did you forget" + " to set the resourcePath?\n", resource); + } +} + +/* + This GM tests whether the image decoders properly decode each color + channel. Four copies of the same image should appear in the GM, and + the letter R should be red, B should be blue, G green, C cyan, M + magenta, Y yellow, and K black. In all but the JPEG version of the + image, the letters should be on a white disc on a transparent + background (rendered as a checkerboard). The JPEG image has a grey + background and compression artifacts. + */ +DEF_SIMPLE_GM(colorwheel, canvas, 256, 256) { + canvas->clear(SK_ColorWHITE); + checkerboard(canvas, 256, 556, 8, 0xFF999999, 0xFF666666); + draw_bitmap(canvas, "color_wheel.png", 0, 0); // top left + draw_bitmap(canvas, "color_wheel.gif", 128, 0); // top right + draw_bitmap(canvas, "color_wheel.webp", 0, 128); // bottom left + draw_bitmap(canvas, "color_wheel.jpg", 128, 128); // bottom right +} diff --git a/gm/gm.h b/gm/gm.h index e48f772f39..9ba9f452a7 100644 --- a/gm/gm.h +++ b/gm/gm.h @@ -24,6 +24,20 @@ static skiagm::GM* SK_MACRO_APPEND_LINE(F_)(void*) { code; } \ static skiagm::GMRegistry SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_)); +// See colorwheel.cpp for example usage. +#define DEF_SIMPLE_GM(NAME, CANVAS, W, H) \ + class SK_MACRO_CONCAT(NAME, _GM) : public skiagm::GM { \ + virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE; \ + virtual SkISize onISize() SK_OVERRIDE { \ + return SkISize::Make((W), (H)); \ + } \ + virtual SkString onShortName() SK_OVERRIDE { \ + return SkString(#NAME); \ + } \ + }; \ + DEF_GM( return SkNEW(SK_MACRO_CONCAT(NAME, _GM)); ) \ + void SK_MACRO_CONCAT(NAME, _GM)::onDraw(SkCanvas* CANVAS) + namespace skiagm { class GM { diff --git a/gyp/gmslides.gypi b/gyp/gmslides.gypi index 716b26320e..261300ebc2 100644 --- a/gyp/gmslides.gypi +++ b/gyp/gmslides.gypi @@ -49,6 +49,7 @@ '../gm/colorfilters.cpp', '../gm/colormatrix.cpp', '../gm/colortype.cpp', + '../gm/colorwheel.cpp', '../gm/complexclip.cpp', '../gm/complexclip2.cpp', '../gm/composeshader.cpp', diff --git a/tools/Resources.cpp b/tools/Resources.cpp index 606c5c4968..50c08922fa 100644 --- a/tools/Resources.cpp +++ b/tools/Resources.cpp @@ -6,8 +6,10 @@ */ #include "Resources.h" - +#include "SkBitmap.h" #include "SkCommandLineFlags.h" +#include "SkData.h" +#include "SkDecodingImageGenerator.h" #include "SkOSFile.h" DEFINE_string2(resourcePath, i, "resources", "Directory with test resources: images, fonts, etc."); @@ -19,3 +21,12 @@ SkString GetResourcePath(const char* resource) { void SetResourcePath(const char* resource) { FLAGS_resourcePath.set(0, resource); } + +bool GetResourceAsBitmap(const char* resource, SkBitmap* dst) { + SkString resourcePath = GetResourcePath(resource); + SkAutoTUnref resourceData( + SkData::NewFromFileName(resourcePath.c_str())); + return resourceData && SkInstallDiscardablePixelRef( + SkDecodingImageGenerator::Create( + resourceData, SkDecodingImageGenerator::Options()), dst); +} diff --git a/tools/Resources.h b/tools/Resources.h index 485a112231..99de77a8b7 100644 --- a/tools/Resources.h +++ b/tools/Resources.h @@ -10,7 +10,11 @@ #include "SkString.h" +class SkBitmap; + SkString GetResourcePath(const char* resource = ""); void SetResourcePath(const char* ); +bool GetResourceAsBitmap(const char* resource, SkBitmap* dst); + #endif // Resources_DEFINED -- cgit v1.2.3