1 // Copyright 2019 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"
4 // HASH=dd81527bbdf5eaae7dd21ac04ab84f9e
5 REG_FIDDLE(Color_Type_RGBA_F16, 256, 96, false, 0) {
6 union FloatUIntUnion {
7 uint32_t fUInt;
8 float fFloat;
9 };
FloatToHalf(float f)10 uint16_t FloatToHalf(float f) {
11 static const FloatUIntUnion magic = { 15 << 23 };
12 static const uint32_t round_mask = ~0xfffu;
13 FloatUIntUnion floatUnion;
14 floatUnion.fFloat = f;
15 uint32_t sign = floatUnion.fUInt & 0x80000000u;
16 floatUnion.fUInt ^= sign;
17 floatUnion.fUInt &= round_mask;
18 floatUnion.fFloat *= magic.fFloat;
19 floatUnion.fUInt -= round_mask;
20 return (floatUnion.fUInt >> 13) | (sign >> 16);
21 }
22
draw(SkCanvas * canvas)23 void draw(SkCanvas* canvas) {
24 canvas->scale(16, 16);
25 SkBitmap bitmap;
26 SkImageInfo imageInfo = SkImageInfo::Make(2, 2, kRGBA_F16_SkColorType, kPremul_SkAlphaType);
27 bitmap.allocPixels(imageInfo);
28 SkCanvas offscreen(bitmap);
29 offscreen.clear(SK_ColorGREEN);
30 canvas->drawBitmap(bitmap, 0, 0);
31 auto H = [](float c) -> uint16_t {
32 return FloatToHalf(c);
33 };
34 // R G B A
35 uint16_t red_f16[][4] = { { H(1.0), H(0.0), H(0.0), H(1.0) },
36 { H(.75), H(0.0), H(0.0), H(1.0) },
37 { H(.50), H(0.0), H(0.0), H(1.0) },
38 { H(.25), H(0.0), H(0.0), H(1.0) } };
39 uint16_t blue_f16[][4] = { { H(0.0), H(0.0), H(1.0), H(1.0) },
40 { H(0.0), H(0.0), H(.75), H(1.0) },
41 { H(0.0), H(0.0), H(.50), H(1.0) },
42 { H(0.0), H(0.0), H(.25), H(1.0) } };
43 SkPixmap redPixmap(imageInfo, red_f16, imageInfo.minRowBytes());
44 if (bitmap.writePixels(redPixmap, 0, 0)) {
45 canvas->drawBitmap(bitmap, 2, 2);
46 }
47 SkPixmap bluePixmap(imageInfo, blue_f16, imageInfo.minRowBytes());
48 if (bitmap.writePixels(bluePixmap, 0, 0)) {
49 canvas->drawBitmap(bitmap, 4, 4);
50 }
51 }
52 } // END FIDDLE
53