blob: cf3afd7ae0875b8af544027efbb379ff59cecd53 [file] [log] [blame]
Hal Canary6c8422c2020-01-10 15:22:09 -05001// Copyright 2020 Google LLC.
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3#include "tools/fiddle/examples.h"
4REG_FIDDLE(UnpremulGPU, 256, 256, false, 6) {
5void draw(SkCanvas* canvas) {
6 canvas->clear(0xffa020a0);
7 SkImageInfo premulInfo = SkImageInfo::MakeS32(10, 10, kPremul_SkAlphaType);
8 SkBitmap premulBitmap;
9 premulBitmap.allocPixels(premulInfo);
10 for (int y = 0; y < 10; y++) {
11 for (int x = 0; x < 10; x++) {
12 *(premulBitmap.getAddr32(x, y)) = 0x80808080;
13 }
14 }
15 sk_sp<SkImage> premulImage = SkImage::MakeFromBitmap(premulBitmap);
16
17 SkImageInfo unpremulInfo = premulInfo.makeAlphaType(kUnpremul_SkAlphaType);
18 SkBitmap unpremulBitmap;
19 unpremulBitmap.allocPixels(unpremulInfo);
20 for (int y = 0; y < 10; y++) {
21 for (int x = 0; x < 10; x++) {
22 *(unpremulBitmap.getAddr32(x, y)) = 0x80FFFFFF;
23 }
24 }
25 sk_sp<SkImage> unpremulImage = SkImage::MakeFromBitmap(unpremulBitmap);
26
27 SkPaint paint;
28 const SkTileMode tile = SkTileMode::kRepeat;
29 paint.setShader(premulImage->makeShader(tile, tile, nullptr));
30 canvas->drawCircle(10.0f, 10.0f, 10.0f, paint);
31
32 paint.setShader(unpremulImage->makeShader(tile, tile, nullptr));
33 canvas->drawCircle(10.0f, 35.0f, 10.0f, paint);
34}
35} // END FIDDLE