• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
8 #include "gm/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkColorSpace.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkPaint.h"
16 
17 #include <string.h>
18 
19 DEF_SIMPLE_GM(unpremul, canvas, 200, 200) {
20     const SkColor color = 0xbf400000;
21 
__anonfd3df41a0102(int x, int y)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 }
77