2015-03-20 19:31:52 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-04-30 17:44:26 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkColorSpace.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkFontStyle.h"
|
|
|
|
#include "include/core/SkFontTypes.h"
|
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPixmap.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/Resources.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2015-03-20 19:31:52 +00:00
|
|
|
|
2019-04-30 17:44:26 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <initializer_list>
|
2015-03-20 19:31:52 +00:00
|
|
|
|
|
|
|
static SkBitmap copy_bitmap(const SkBitmap& src, SkColorType colorType) {
|
2017-04-28 15:15:22 +00:00
|
|
|
const SkBitmap* srcPtr = &src;
|
|
|
|
SkBitmap tmp(src);
|
|
|
|
if (kRGB_565_SkColorType == colorType) {
|
|
|
|
tmp.setAlphaType(kOpaque_SkAlphaType);
|
|
|
|
srcPtr = &tmp;
|
|
|
|
}
|
|
|
|
|
2015-03-20 19:31:52 +00:00
|
|
|
SkBitmap copy;
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::copy_to(©, colorType, *srcPtr);
|
2015-03-20 19:31:52 +00:00
|
|
|
copy.setImmutable();
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SCALE 128
|
|
|
|
|
|
|
|
// Make either A8 or gray8 bitmap.
|
|
|
|
static SkBitmap make_bitmap(SkColorType ct) {
|
|
|
|
SkBitmap bm;
|
|
|
|
switch (ct) {
|
|
|
|
case kAlpha_8_SkColorType:
|
|
|
|
bm.allocPixels(SkImageInfo::MakeA8(SCALE, SCALE));
|
|
|
|
break;
|
|
|
|
case kGray_8_SkColorType:
|
|
|
|
bm.allocPixels(
|
|
|
|
SkImageInfo::Make(SCALE, SCALE, ct, kOpaque_SkAlphaType));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SkASSERT(false);
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
uint8_t spectrum[256];
|
|
|
|
for (int y = 0; y < 256; ++y) {
|
|
|
|
spectrum[y] = y;
|
|
|
|
}
|
|
|
|
for (int y = 0; y < 128; ++y) {
|
|
|
|
// Shift over one byte each scanline.
|
|
|
|
memcpy(bm.getAddr8(0, y), &spectrum[y], 128);
|
|
|
|
}
|
|
|
|
bm.setImmutable();
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
2018-12-02 21:04:27 +00:00
|
|
|
static void draw_center_letter(char c, const SkFont& font, SkColor color,
|
|
|
|
SkScalar x, SkScalar y, SkCanvas* canvas) {
|
2015-03-20 19:31:52 +00:00
|
|
|
SkRect bounds;
|
2019-05-07 19:38:46 +00:00
|
|
|
font.measureText(&c, 1, SkTextEncoding::kUTF8, &bounds);
|
|
|
|
canvas->drawSimpleText(&c, 1, SkTextEncoding::kUTF8,
|
2018-12-02 21:04:27 +00:00
|
|
|
x - bounds.centerX(), y - bounds.centerY(),
|
2019-05-02 15:31:28 +00:00
|
|
|
font, SkPaint(SkColor4f::FromColor(color)));
|
2015-03-20 19:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void color_wheel_native(SkCanvas* canvas) {
|
|
|
|
SkAutoCanvasRestore autoCanvasRestore(canvas, true);
|
|
|
|
canvas->translate(0.5f * SCALE, 0.5f * SCALE);
|
2019-05-02 15:31:28 +00:00
|
|
|
canvas->drawCircle(0.0f, 0.0f, SCALE * 0.5f, SkPaint(SkColors::kWhite));
|
2015-03-20 19:31:52 +00:00
|
|
|
|
|
|
|
const double sqrt_3_over_2 = 0.8660254037844387;
|
|
|
|
const SkScalar Z = 0.0f;
|
|
|
|
const SkScalar D = 0.3f * SkIntToScalar(SCALE);
|
|
|
|
const SkScalar X = SkDoubleToScalar(D * sqrt_3_over_2);
|
|
|
|
const SkScalar Y = D * SK_ScalarHalf;
|
2018-12-02 21:04:27 +00:00
|
|
|
|
|
|
|
SkFont font;
|
|
|
|
font.setEdging(SkFont::Edging::kAlias);
|
2019-03-20 16:12:10 +00:00
|
|
|
font.setTypeface(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold()));
|
2018-12-02 21:04:27 +00:00
|
|
|
font.setSize(0.28125f * SCALE);
|
|
|
|
draw_center_letter('K', font, SK_ColorBLACK, Z, Z, canvas);
|
|
|
|
draw_center_letter('R', font, SK_ColorRED, Z, D, canvas);
|
|
|
|
draw_center_letter('G', font, SK_ColorGREEN, -X, -Y, canvas);
|
|
|
|
draw_center_letter('B', font, SK_ColorBLUE, X, -Y, canvas);
|
|
|
|
draw_center_letter('C', font, SK_ColorCYAN, Z, -D, canvas);
|
|
|
|
draw_center_letter('M', font, SK_ColorMAGENTA, X, Y, canvas);
|
|
|
|
draw_center_letter('Y', font, SK_ColorYELLOW, -X, Y, canvas);
|
2015-03-20 19:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
int find(T* array, int N, T item) {
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
if (array[i] == item) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw(SkCanvas* canvas,
|
|
|
|
const SkPaint& p,
|
2019-01-04 20:57:33 +00:00
|
|
|
const SkFont& font,
|
2015-03-20 19:31:52 +00:00
|
|
|
const SkBitmap& src,
|
|
|
|
SkColorType colorType,
|
|
|
|
const char text[]) {
|
|
|
|
SkASSERT(src.colorType() == colorType);
|
|
|
|
canvas->drawBitmap(src, 0.0f, 0.0f);
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 0.0f, 12.0f, font, p);
|
2015-03-20 19:31:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 15:48:38 +00:00
|
|
|
DEF_SIMPLE_GM(all_bitmap_configs, canvas, SCALE, 6 * SCALE) {
|
2015-03-20 19:31:52 +00:00
|
|
|
SkAutoCanvasRestore autoCanvasRestore(canvas, true);
|
2019-05-02 15:31:28 +00:00
|
|
|
SkPaint p(SkColors::kBlack);
|
2015-03-20 19:31:52 +00:00
|
|
|
p.setAntiAlias(true);
|
2019-01-04 20:57:33 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface());
|
2015-03-20 19:31:52 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::draw_checkerboard(canvas, SK_ColorLTGRAY, SK_ColorWHITE, 8);
|
2015-03-20 19:31:52 +00:00
|
|
|
|
|
|
|
SkBitmap bitmap;
|
2017-12-08 15:21:31 +00:00
|
|
|
if (GetResourceAsBitmap("images/color_wheel.png", &bitmap)) {
|
2015-03-20 19:31:52 +00:00
|
|
|
bitmap.setImmutable();
|
2019-01-04 20:57:33 +00:00
|
|
|
draw(canvas, p, font, bitmap, kN32_SkColorType, "Native 32");
|
2015-03-20 19:31:52 +00:00
|
|
|
|
|
|
|
canvas->translate(0.0f, SkIntToScalar(SCALE));
|
|
|
|
SkBitmap copy565 = copy_bitmap(bitmap, kRGB_565_SkColorType);
|
|
|
|
p.setColor(SK_ColorRED);
|
2019-01-04 20:57:33 +00:00
|
|
|
draw(canvas, p, font, copy565, kRGB_565_SkColorType, "RGB 565");
|
2015-03-20 19:31:52 +00:00
|
|
|
p.setColor(SK_ColorBLACK);
|
|
|
|
|
|
|
|
canvas->translate(0.0f, SkIntToScalar(SCALE));
|
|
|
|
SkBitmap copy4444 = copy_bitmap(bitmap, kARGB_4444_SkColorType);
|
2019-01-04 20:57:33 +00:00
|
|
|
draw(canvas, p, font, copy4444, kARGB_4444_SkColorType, "ARGB 4444");
|
2018-09-12 15:48:38 +00:00
|
|
|
|
|
|
|
canvas->translate(0.0f, SkIntToScalar(SCALE));
|
|
|
|
SkBitmap copyF16 = copy_bitmap(bitmap, kRGBA_F16_SkColorType);
|
2019-01-04 20:57:33 +00:00
|
|
|
draw(canvas, p, font, copyF16, kRGBA_F16_SkColorType, "RGBA F16");
|
2018-09-12 15:48:38 +00:00
|
|
|
|
2015-03-20 19:31:52 +00:00
|
|
|
} else {
|
2018-09-12 15:48:38 +00:00
|
|
|
canvas->translate(0.0f, SkIntToScalar(3 * SCALE));
|
2015-03-20 19:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
canvas->translate(0.0f, SkIntToScalar(SCALE));
|
|
|
|
SkBitmap bitmapA8 = make_bitmap(kAlpha_8_SkColorType);
|
2019-01-04 20:57:33 +00:00
|
|
|
draw(canvas, p, font, bitmapA8, kAlpha_8_SkColorType, "Alpha 8");
|
2015-03-20 19:31:52 +00:00
|
|
|
|
|
|
|
p.setColor(SK_ColorRED);
|
|
|
|
canvas->translate(0.0f, SkIntToScalar(SCALE));
|
|
|
|
SkBitmap bitmapG8 = make_bitmap(kGray_8_SkColorType);
|
2019-01-04 20:57:33 +00:00
|
|
|
draw(canvas, p, font, bitmapG8, kGray_8_SkColorType, "Gray 8");
|
2015-03-20 19:31:52 +00:00
|
|
|
}
|
2015-11-06 15:27:23 +00:00
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> make_not_native32_color_wheel() {
|
2015-11-06 15:27:23 +00:00
|
|
|
SkBitmap n32bitmap, notN32bitmap;
|
|
|
|
n32bitmap.allocN32Pixels(SCALE, SCALE);
|
|
|
|
n32bitmap.eraseColor(SK_ColorTRANSPARENT);
|
|
|
|
SkCanvas n32canvas(n32bitmap);
|
|
|
|
color_wheel_native(&n32canvas);
|
|
|
|
#if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
|
|
|
|
const SkColorType ct = kRGBA_8888_SkColorType;
|
|
|
|
#elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
|
|
|
|
const SkColorType ct = kBGRA_8888_SkColorType;
|
|
|
|
#endif
|
|
|
|
static_assert(ct != kN32_SkColorType, "BRGA!=RGBA");
|
2019-03-20 16:12:10 +00:00
|
|
|
SkAssertResult(ToolUtils::copy_to(¬N32bitmap, ct, n32bitmap));
|
2015-11-06 15:27:23 +00:00
|
|
|
SkASSERT(notN32bitmap.colorType() == ct);
|
2016-03-17 17:51:11 +00:00
|
|
|
return SkImage::MakeFromBitmap(notN32bitmap);
|
2015-11-06 15:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_SIMPLE_GM(not_native32_bitmap_config, canvas, SCALE, SCALE) {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> notN32image(make_not_native32_color_wheel());
|
2015-11-06 15:27:23 +00:00
|
|
|
SkASSERT(notN32image);
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::draw_checkerboard(canvas, SK_ColorLTGRAY, SK_ColorWHITE, 8);
|
2016-03-17 17:51:11 +00:00
|
|
|
canvas->drawImage(notN32image.get(), 0.0f, 0.0f);
|
2015-11-06 15:27:23 +00:00
|
|
|
}
|
2016-06-03 16:44:53 +00:00
|
|
|
|
|
|
|
static uint32_t make_pixel(int x, int y, SkAlphaType alphaType) {
|
|
|
|
SkASSERT(x >= 0 && x < SCALE);
|
|
|
|
SkASSERT(y >= 0 && y < SCALE);
|
|
|
|
|
|
|
|
SkScalar R = SCALE / 2.0f;
|
|
|
|
|
|
|
|
uint32_t alpha = 0x00;
|
|
|
|
|
|
|
|
if ((x - R) * (x - R) + (y - R) * (y - R) < R * R) {
|
|
|
|
alpha = 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t component;
|
|
|
|
switch (alphaType) {
|
|
|
|
case kPremul_SkAlphaType:
|
|
|
|
component = alpha;
|
|
|
|
break;
|
|
|
|
case kUnpremul_SkAlphaType:
|
|
|
|
component = 0xFF;
|
|
|
|
break;
|
|
|
|
default:
|
2017-08-16 14:53:04 +00:00
|
|
|
SK_ABORT("Should not get here - invalid alpha type");
|
2016-06-03 16:44:53 +00:00
|
|
|
}
|
|
|
|
return alpha << 24 | component;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void make_color_test_bitmap_variant(
|
|
|
|
SkColorType colorType,
|
|
|
|
SkAlphaType alphaType,
|
2016-06-20 15:25:02 +00:00
|
|
|
sk_sp<SkColorSpace> colorSpace,
|
2016-06-03 16:44:53 +00:00
|
|
|
SkBitmap* bm)
|
|
|
|
{
|
|
|
|
SkASSERT(colorType == kRGBA_8888_SkColorType || colorType == kBGRA_8888_SkColorType);
|
|
|
|
SkASSERT(alphaType == kPremul_SkAlphaType || alphaType == kUnpremul_SkAlphaType);
|
|
|
|
bm->allocPixels(
|
2016-06-20 15:25:02 +00:00
|
|
|
SkImageInfo::Make(SCALE, SCALE, colorType, alphaType, colorSpace));
|
2017-12-15 02:13:47 +00:00
|
|
|
const SkPixmap& pm = bm->pixmap();
|
2017-12-14 20:33:23 +00:00
|
|
|
for (int y = 0; y < pm.height(); y++) {
|
|
|
|
for (int x = 0; x < pm.width(); x++) {
|
2016-06-03 16:44:53 +00:00
|
|
|
*pm.writable_addr32(x, y) = make_pixel(x, y, alphaType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Revert "explictly fail to draw unpremul, as this is buggy, incomplete, tricky, and unused"
This reverts commit 1baaacac74d24604e3dfca9857fbd647df375997.
Reason for revert: Failing the roll: https://storage.googleapis.com/chromium-layout-test-archives/linux_trusty_blink_rel/3868/layout-test-results/results.html
Original change's description:
> explictly fail to draw unpremul, as this is buggy, incomplete, tricky, and unused
>
> BUG=skia:
>
> Change-Id: Ie9da6015cc4955c0f27f6db53fc6ae532e0132f4
> Reviewed-on: https://skia-review.googlesource.com/7304
> Commit-Queue: Mike Reed <reed@google.com>
> Reviewed-by: Matt Sarett <msarett@google.com>
>
TBR=bsalomon@google.com,msarett@google.com,brianosman@google.com,reed@google.com
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=skia:
Change-Id: Ifc808a430b8afe4fef6df3775c2bfdcede1a2dd7
Reviewed-on: https://skia-review.googlesource.com/7329
Commit-Queue: Joe Gregorio <jcgregorio@google.com>
Reviewed-by: Joe Gregorio <jcgregorio@google.com>
2017-01-20 14:21:27 +00:00
|
|
|
DEF_SIMPLE_GM(all_variants_8888, canvas, 4 * SCALE + 30, 2 * SCALE + 10) {
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::draw_checkerboard(canvas, SK_ColorLTGRAY, SK_ColorWHITE, 8);
|
2016-06-03 16:44:53 +00:00
|
|
|
|
2016-06-20 15:25:02 +00:00
|
|
|
sk_sp<SkColorSpace> colorSpaces[] {
|
2017-02-07 18:56:11 +00:00
|
|
|
SkColorSpace::MakeSRGB(),
|
2016-06-20 15:25:02 +00:00
|
|
|
nullptr,
|
|
|
|
};
|
|
|
|
for (auto colorSpace : colorSpaces) {
|
2016-06-03 16:44:53 +00:00
|
|
|
canvas->save();
|
|
|
|
for (auto alphaType : {kPremul_SkAlphaType, kUnpremul_SkAlphaType}) {
|
|
|
|
canvas->save();
|
|
|
|
for (auto colorType : {kRGBA_8888_SkColorType, kBGRA_8888_SkColorType}) {
|
|
|
|
SkBitmap bm;
|
2016-06-20 15:25:02 +00:00
|
|
|
make_color_test_bitmap_variant(colorType, alphaType, colorSpace, &bm);
|
2016-06-03 16:44:53 +00:00
|
|
|
canvas->drawBitmap(bm, 0.0f, 0.0f);
|
|
|
|
canvas->translate(SCALE + 10, 0.0f);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
Revert "explictly fail to draw unpremul, as this is buggy, incomplete, tricky, and unused"
This reverts commit 1baaacac74d24604e3dfca9857fbd647df375997.
Reason for revert: Failing the roll: https://storage.googleapis.com/chromium-layout-test-archives/linux_trusty_blink_rel/3868/layout-test-results/results.html
Original change's description:
> explictly fail to draw unpremul, as this is buggy, incomplete, tricky, and unused
>
> BUG=skia:
>
> Change-Id: Ie9da6015cc4955c0f27f6db53fc6ae532e0132f4
> Reviewed-on: https://skia-review.googlesource.com/7304
> Commit-Queue: Mike Reed <reed@google.com>
> Reviewed-by: Matt Sarett <msarett@google.com>
>
TBR=bsalomon@google.com,msarett@google.com,brianosman@google.com,reed@google.com
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=skia:
Change-Id: Ifc808a430b8afe4fef6df3775c2bfdcede1a2dd7
Reviewed-on: https://skia-review.googlesource.com/7329
Commit-Queue: Joe Gregorio <jcgregorio@google.com>
Reviewed-by: Joe Gregorio <jcgregorio@google.com>
2017-01-20 14:21:27 +00:00
|
|
|
canvas->translate(0.0f, SCALE + 10);
|
2016-06-03 16:44:53 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(2 * (SCALE + 10), 0.0f);
|
|
|
|
}
|
|
|
|
}
|