skia2/gm/unpremul.cpp
Mike Reed 607a382298 drawBitmap is deprecated, use drawImage
Change-Id: Ib66517fe26036704ccb8328ef92fa0f5240c79f4
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/358222
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Reed <reed@google.com>
2021-01-25 01:44:55 +00:00

78 lines
2.1 KiB
C++

/*
* Copyright 2018 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm/gm.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkBlendMode.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkPaint.h"
#include <string.h>
DEF_SIMPLE_GM(unpremul, canvas, 200, 200) {
const SkColor color = 0xbf400000;
auto grade = [&](int x, int y){
SkBitmap bm;
bm.allocPixels(SkImageInfo::Make(1,1,
kBGRA_8888_SkColorType,
kUnpremul_SkAlphaType,
SkColorSpace::MakeSRGB()));
if (!canvas->readPixels(bm, x,y)) {
// Picture-backed canvases, that sort of thing. Just assume they're good.
MarkGMGood(canvas, 140,40);
return;
}
SkColor pixel;
memcpy(&pixel, bm.getAddr(0,0), sizeof(pixel));
auto close = [](int x, int y) {
return x-y < 2
&& y-x < 2;
};
if (close(SkColorGetR(pixel), SkColorGetR(color)) &&
close(SkColorGetG(pixel), SkColorGetG(color)) &&
close(SkColorGetB(pixel), SkColorGetB(color)) &&
close(SkColorGetA(pixel), SkColorGetA(color))) {
MarkGMGood(canvas, 140,40);
} else {
MarkGMBad(canvas, 140,40);
}
};
{
SkPaint paint;
paint.setBlendMode(SkBlendMode::kSrc);
paint.setColor(color);
canvas->drawRect({0,0,100,100}, paint);
grade(50,50);
}
canvas->translate(0,100);
{
SkPaint paint;
paint.setBlendMode(SkBlendMode::kSrc);
SkBitmap bm;
bm.allocPixels(SkImageInfo::Make(100,100, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType));
bm.eraseColor(color);
canvas->drawImage(bm.asImage(), 0,0, SkSamplingOptions(), &paint);
grade(50,150);
}
}