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