| 1 |
/* |
| 2 |
* Copyright 2014 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkFont.h" |
| 12 |
#include "include/core/SkFontStyle.h" |
| 13 |
#include "include/core/SkImage.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkRefCnt.h" |
| 16 |
#include "include/core/SkScalar.h" |
| 17 |
#include "include/core/SkTypeface.h" |
| 18 |
#include "include/core/SkTypes.h" |
| 19 |
#include "tools/DecodeUtils.h" |
| 20 |
#include "tools/GpuToolUtils.h" |
| 21 |
#include "tools/Resources.h" |
| 22 |
#include "tools/ToolUtils.h" |
| 23 |
#include "tools/fonts/FontToolUtils.h" |
| 24 |
|
| 25 |
static void draw_image(SkCanvas* canvas, const char* resource, int x, int y) { |
| 26 |
sk_sp<SkImage> image(ToolUtils::GetResourceAsImage(resource)); |
| 27 |
if (image) { |
| 28 |
canvas->drawImage(image, SkIntToScalar(x), SkIntToScalar(y)); |
| 29 |
} else { |
| 30 |
SkDebugf("\nCould not decode file '%s'. Did you forget" |
| 31 |
" to set the resourcePath?\n", resource); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/* |
| 36 |
This GM tests whether the image decoders properly decode each color |
| 37 |
channel. Four copies of the same image should appear in the GM, and |
| 38 |
the letter R should be red, B should be blue, G green, C cyan, M |
| 39 |
magenta, Y yellow, and K black. In all but the JPEG version of the |
| 40 |
image, the letters should be on a white disc on a transparent |
| 41 |
background (rendered as a checkerboard). The JPEG image has a grey |
| 42 |
background and compression artifacts. |
| 43 |
*/ |
| 44 |
DEF_SIMPLE_GM(colorwheel, canvas, 384, 256) { |
| 45 |
ToolUtils::draw_checkerboard(canvas); |
| 46 |
draw_image(canvas, "images/color_wheel.png", 0, 0); // top left |
| 47 |
draw_image(canvas, "images/color_wheel.gif", 128, 0); // top middle |
| 48 |
draw_image(canvas, "images/color_wheel.webp", 0, 128); // bottom left |
| 49 |
draw_image(canvas, "images/color_wheel.jpg", 128, 128); // bottom middle |
| 50 |
#if defined(SK_CODEC_DECODES_AVIF) |
| 51 |
draw_image(canvas, "images/color_wheel.avif", 256, 0); // top right |
| 52 |
#endif |
| 53 |
} |
| 54 |
|
| 55 |
DEF_SIMPLE_GM(colorwheelnative, canvas, 128, 28) { |
| 56 |
SkFont font(ToolUtils::CreatePortableTypeface("sans-serif", SkFontStyle::Bold()), 18); |
| 57 |
font.setEdging(SkFont::Edging::kAlias); |
| 58 |
|
| 59 |
canvas->clear(SK_ColorLTGRAY); |
| 60 |
canvas->drawString("R", 8.0f, 20.0f, font, SkPaint(SkColors::kRed)); |
| 61 |
canvas->drawString("G", 24.0f, 20.0f, font, SkPaint(SkColors::kGreen)); |
| 62 |
canvas->drawString("B", 40.0f, 20.0f, font, SkPaint(SkColors::kBlue)); |
| 63 |
canvas->drawString("C", 56.0f, 20.0f, font, SkPaint(SkColors::kCyan)); |
| 64 |
canvas->drawString("M", 72.0f, 20.0f, font, SkPaint(SkColors::kMagenta)); |
| 65 |
canvas->drawString("Y", 88.0f, 20.0f, font, SkPaint(SkColors::kYellow)); |
| 66 |
canvas->drawString("K", 104.0f, 20.0f, font, SkPaint(SkColors::kBlack)); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* This GM tests decoding images with non-default (overridden) alpha types. |
| 71 |
*/ |
| 72 |
DEF_SIMPLE_GM(colorwheel_alphatypes, canvas, 256, 128) { |
| 73 |
canvas->clear(SK_ColorWHITE); |
| 74 |
|
| 75 |
sk_sp<SkData> imgData = GetResourceAsData("images/color_wheel.png"); |
| 76 |
|
| 77 |
auto pmImg = ToolUtils::MakeTextureImage( |
| 78 |
canvas, SkImages::DeferredFromEncodedData(imgData, kPremul_SkAlphaType)); |
| 79 |
auto upmImg = ToolUtils::MakeTextureImage( |
| 80 |
canvas, SkImages::DeferredFromEncodedData(imgData, kUnpremul_SkAlphaType)); |
| 81 |
|
| 82 |
SkSamplingOptions linear{SkFilterMode::kLinear}; |
| 83 |
|
| 84 |
// We draw a tiny (8x8) section of the image that falls right on the edge of transparency, |
| 85 |
// and blow it up so we can really see the impact of filtering in premul or unpremul. |
| 86 |
SkRect srcRect = SkRect::MakeXYWH(12, 102, 8, 8); |
| 87 |
SkRect dstRect = SkRect::MakeLTRB(0, 0, 128, 128); |
| 88 |
|
| 89 |
// First, we draw the normal (premul-then-filter) image, which looks good. The yellow image |
| 90 |
// transitions to the white background like you'd expect. |
| 91 |
canvas->drawImageRect(pmImg, srcRect, dstRect, |
| 92 |
linear, nullptr, SkCanvas::kFast_SrcRectConstraint); |
| 93 |
// Next, we draw the unpremul (filter-then-premul) image, which looks bad. Filtering in unpremul |
| 94 |
// causes the implicit black of the transparent pixels to be bleed into the filtered edge, |
| 95 |
// creating a dark "fringe" at the boundary between the yellow image and white background. |
| 96 |
canvas->drawImageRect(upmImg, srcRect, dstRect.makeOffset(128, 0), |
| 97 |
linear, nullptr, SkCanvas::kFast_SrcRectConstraint); |
| 98 |
} |