blob: 25ddae5ef7cc79b95c56032bae83c4e19e6cce37 [file] [log] [blame]
Mike Kleinc9eace82018-10-31 10:49:38 -04001/*
2 * Copyright 2018 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkBlendMode.h"
11#include "include/core/SkCanvas.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkColor.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkColorSpace.h"
14#include "include/core/SkImageInfo.h"
15#include "include/core/SkPaint.h"
16
17#include <string.h>
Mike Kleinc9eace82018-10-31 10:49:38 -040018
19DEF_SIMPLE_GM(unpremul, canvas, 200, 200) {
20 const SkColor color = 0xbf400000;
21
22 auto grade = [&](int x, int y){
23 SkBitmap bm;
24 bm.allocPixels(SkImageInfo::Make(1,1,
25 kBGRA_8888_SkColorType,
26 kUnpremul_SkAlphaType,
27 SkColorSpace::MakeSRGB()));
28 if (!canvas->readPixels(bm, x,y)) {
29 // Picture-backed canvases, that sort of thing. Just assume they're good.
30 MarkGMGood(canvas, 140,40);
31 return;
32 }
33
34 SkColor pixel;
35 memcpy(&pixel, bm.getAddr(0,0), sizeof(pixel));
36
37 auto close = [](int x, int y) {
38 return x-y < 2
39 && y-x < 2;
40 };
41
42 if (close(SkColorGetR(pixel), SkColorGetR(color)) &&
43 close(SkColorGetG(pixel), SkColorGetG(color)) &&
44 close(SkColorGetB(pixel), SkColorGetB(color)) &&
45 close(SkColorGetA(pixel), SkColorGetA(color))) {
46
47 MarkGMGood(canvas, 140,40);
48 } else {
49 MarkGMBad(canvas, 140,40);
50 }
51 };
52
53 {
54 SkPaint paint;
55 paint.setBlendMode(SkBlendMode::kSrc);
56 paint.setColor(color);
57
58 canvas->drawRect({0,0,100,100}, paint);
59 grade(50,50);
60 }
61
62 canvas->translate(0,100);
63
64 {
65 SkPaint paint;
66 paint.setBlendMode(SkBlendMode::kSrc);
67
68 SkBitmap bm;
69 bm.allocPixels(SkImageInfo::Make(100,100, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType));
70 bm.eraseColor(color);
71
72 canvas->drawBitmap(bm, 0,0, &paint);
73 grade(50,150);
74 }
75
76}