herb | cc49e59 | 2016-05-17 09:57:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 <string> |
| 9 | #include <tuple> |
| 10 | #include <vector> |
| 11 | #include "Resources.h" |
| 12 | #include "SkCpu.h" |
| 13 | #include "SkImage.h" |
| 14 | #include "SkImage_Base.h" |
| 15 | #include "SkOpts.h" |
herb | 2edf0c6 | 2016-07-12 15:00:46 -0700 | [diff] [blame] | 16 | #include "SkPM4fPriv.h" |
herb | cc49e59 | 2016-05-17 09:57:34 -0700 | [diff] [blame] | 17 | #include "SkNx.h" |
| 18 | #include "Test.h" |
herb | cc49e59 | 2016-05-17 09:57:34 -0700 | [diff] [blame] | 19 | |
| 20 | typedef void (*Blender)(uint32_t* dst, const uint32_t* const srcStart, int ndst, const int nsrc); |
| 21 | |
mtklein | 0c90247 | 2016-07-20 18:10:07 -0700 | [diff] [blame] | 22 | static inline void srcover_srgb_srgb_1(uint32_t* dst, uint32_t src) { |
| 23 | auto d = Sk4f_fromS32(*dst), |
| 24 | s = Sk4f_fromS32( src); |
| 25 | *dst = Sk4f_toS32(s + d * (1.0f - s[3])); |
| 26 | } |
| 27 | |
herb | 2edf0c6 | 2016-07-12 15:00:46 -0700 | [diff] [blame] | 28 | static void brute_force_srcover_srgb_srgb( |
| 29 | uint32_t* dst, const uint32_t* const src, int ndst, const int nsrc) { |
| 30 | while (ndst > 0) { |
| 31 | int n = SkTMin(ndst, nsrc); |
| 32 | |
| 33 | for (int i = 0; i < n; i++) { |
mtklein | 0c90247 | 2016-07-20 18:10:07 -0700 | [diff] [blame] | 34 | srcover_srgb_srgb_1(dst++, src[i]); |
herb | 2edf0c6 | 2016-07-12 15:00:46 -0700 | [diff] [blame] | 35 | } |
| 36 | ndst -= n; |
| 37 | } |
herb | cc49e59 | 2016-05-17 09:57:34 -0700 | [diff] [blame] | 38 | } |
| 39 | |
mtklein | 0358a6a | 2016-07-13 08:02:20 -0700 | [diff] [blame] | 40 | static SkString mismatch_message(std::string resourceName, int x, int y, |
herb | cc49e59 | 2016-05-17 09:57:34 -0700 | [diff] [blame] | 41 | uint32_t src, uint32_t good, uint32_t bad) { |
| 42 | return SkStringPrintf( |
mtklein | 0358a6a | 2016-07-13 08:02:20 -0700 | [diff] [blame] | 43 | "%s - missmatch at %d, %d src: %08x good: %08x bad: %08x", |
| 44 | resourceName.c_str(), x, y, src, good, bad); |
herb | cc49e59 | 2016-05-17 09:57:34 -0700 | [diff] [blame] | 45 | } |
| 46 | |
mtklein | 0358a6a | 2016-07-13 08:02:20 -0700 | [diff] [blame] | 47 | static void test_blender(std::string resourceName, skiatest::Reporter* reporter) { |
herb | cc49e59 | 2016-05-17 09:57:34 -0700 | [diff] [blame] | 48 | std::string fileName = resourceName + ".png"; |
| 49 | sk_sp<SkImage> image = GetResourceAsImage(fileName.c_str()); |
herb | cc49e59 | 2016-05-17 09:57:34 -0700 | [diff] [blame] | 50 | if (image == nullptr) { |
bsalomon | 037655f | 2016-05-17 18:36:23 -0700 | [diff] [blame] | 51 | ERRORF(reporter, "image is NULL"); |
| 52 | return; |
herb | cc49e59 | 2016-05-17 09:57:34 -0700 | [diff] [blame] | 53 | } |
| 54 | SkBitmap bm; |
| 55 | if (!as_IB(image)->getROPixels(&bm)) { |
bsalomon | 037655f | 2016-05-17 18:36:23 -0700 | [diff] [blame] | 56 | ERRORF(reporter, "Could not read resource"); |
| 57 | return; |
herb | cc49e59 | 2016-05-17 09:57:34 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | SkPixmap pixmap; |
| 61 | bm.peekPixels(&pixmap); |
| 62 | SkASSERTF(pixmap.colorType() == kN32_SkColorType, "colorType: %d", pixmap.colorType()); |
| 63 | SkASSERT(pixmap.alphaType() != kUnpremul_SkAlphaType); |
| 64 | const uint32_t* src = pixmap.addr32(); |
| 65 | const int width = pixmap.rowBytesAsPixels(); |
| 66 | SkASSERT(width > 0); |
| 67 | SkASSERT(width < 4000); |
| 68 | SkAutoTArray<uint32_t> correctDst(width); |
| 69 | SkAutoTArray<uint32_t> testDst(width); |
| 70 | |
| 71 | for (int y = 0; y < pixmap.height(); y++) { |
mtklein | 0c90247 | 2016-07-20 18:10:07 -0700 | [diff] [blame] | 72 | // TODO: zero is not the most interesting dst to test srcover... |
herb | 2edf0c6 | 2016-07-12 15:00:46 -0700 | [diff] [blame] | 73 | sk_bzero(correctDst.get(), width * sizeof(uint32_t)); |
| 74 | sk_bzero(testDst.get(), width * sizeof(uint32_t)); |
| 75 | brute_force_srcover_srgb_srgb(correctDst.get(), src, width, width); |
mtklein | 0358a6a | 2016-07-13 08:02:20 -0700 | [diff] [blame] | 76 | SkOpts:: srcover_srgb_srgb( testDst.get(), src, width, width); |
herb | cc49e59 | 2016-05-17 09:57:34 -0700 | [diff] [blame] | 77 | for (int x = 0; x < width; x++) { |
| 78 | REPORTER_ASSERT_MESSAGE( |
| 79 | reporter, correctDst[x] == testDst[x], |
mtklein | 0358a6a | 2016-07-13 08:02:20 -0700 | [diff] [blame] | 80 | mismatch_message(resourceName, x, y, src[x], correctDst[x], testDst[x])); |
herb | cc49e59 | 2016-05-17 09:57:34 -0700 | [diff] [blame] | 81 | if (correctDst[x] != testDst[x]) break; |
| 82 | } |
| 83 | src += width; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | DEF_TEST(SkBlend_optsCheck, reporter) { |
herb | cc49e59 | 2016-05-17 09:57:34 -0700 | [diff] [blame] | 88 | std::vector<std::string> testResources = { |
| 89 | "yellow_rose", "baby_tux", "plane", "mandrill_512", "iconstrip" |
| 90 | }; |
| 91 | |
mtklein | 0358a6a | 2016-07-13 08:02:20 -0700 | [diff] [blame] | 92 | for (auto& resourceName : testResources) { |
| 93 | test_blender(resourceName, reporter); |
herb | cc49e59 | 2016-05-17 09:57:34 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
herb | cc49e59 | 2016-05-17 09:57:34 -0700 | [diff] [blame] | 97 | DEF_TEST(SkBlend_optsSqrtCheck, reporter) { |
| 98 | for (int c = 0; c < 256; c++) { |
| 99 | Sk4f i{(float)c}; |
| 100 | Sk4f ii = i * i; |
| 101 | Sk4f s = ii.sqrt() + 0.5f; |
| 102 | Sk4f sf = s.floor(); |
| 103 | REPORTER_ASSERT_MESSAGE( |
| 104 | reporter, i[0] == sf[0], SkStringPrintf("i: %f, s: %f", i[0], sf[0])); |
| 105 | } |
| 106 | } |