TestUtils.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright 2017 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. #include "tests/TestUtils.h"
  8. #include "include/encode/SkPngEncoder.h"
  9. #include "include/utils/SkBase64.h"
  10. #include "src/core/SkUtils.h"
  11. #include "src/gpu/GrContextPriv.h"
  12. #include "src/gpu/GrDrawingManager.h"
  13. #include "src/gpu/GrGpu.h"
  14. #include "src/gpu/GrSurfaceContext.h"
  15. #include "src/gpu/GrSurfaceProxy.h"
  16. #include "src/gpu/GrTextureContext.h"
  17. #include "src/gpu/GrTextureProxy.h"
  18. #include "src/gpu/SkGr.h"
  19. void test_read_pixels(skiatest::Reporter* reporter,
  20. GrSurfaceContext* srcContext, uint32_t expectedPixelValues[],
  21. const char* testName) {
  22. int pixelCnt = srcContext->width() * srcContext->height();
  23. SkAutoTMalloc<uint32_t> pixels(pixelCnt);
  24. memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
  25. SkImageInfo ii = SkImageInfo::Make(srcContext->width(), srcContext->height(),
  26. kRGBA_8888_SkColorType, kPremul_SkAlphaType);
  27. bool read = srcContext->readPixels(ii, pixels.get(), 0, {0, 0});
  28. if (!read) {
  29. ERRORF(reporter, "%s: Error reading from texture.", testName);
  30. }
  31. for (int i = 0; i < pixelCnt; ++i) {
  32. if (pixels.get()[i] != expectedPixelValues[i]) {
  33. ERRORF(reporter, "%s: Error, pixel value %d should be 0x%08x, got 0x%08x.",
  34. testName, i, expectedPixelValues[i], pixels.get()[i]);
  35. break;
  36. }
  37. }
  38. }
  39. void test_write_pixels(skiatest::Reporter* reporter,
  40. GrSurfaceContext* dstContext, bool expectedToWork,
  41. const char* testName) {
  42. int pixelCnt = dstContext->width() * dstContext->height();
  43. SkAutoTMalloc<uint32_t> pixels(pixelCnt);
  44. for (int y = 0; y < dstContext->width(); ++y) {
  45. for (int x = 0; x < dstContext->height(); ++x) {
  46. pixels.get()[y * dstContext->width() + x] =
  47. SkColorToPremulGrColor(SkColorSetARGB(2*y, x, y, x + y));
  48. }
  49. }
  50. SkImageInfo ii = SkImageInfo::Make(dstContext->width(), dstContext->height(),
  51. kRGBA_8888_SkColorType, kPremul_SkAlphaType);
  52. bool write = dstContext->writePixels(ii, pixels.get(), 0, {0, 0});
  53. if (!write) {
  54. if (expectedToWork) {
  55. ERRORF(reporter, "%s: Error writing to texture.", testName);
  56. }
  57. return;
  58. }
  59. if (write && !expectedToWork) {
  60. ERRORF(reporter, "%s: writePixels succeeded when it wasn't supposed to.", testName);
  61. return;
  62. }
  63. test_read_pixels(reporter, dstContext, pixels.get(), testName);
  64. }
  65. void test_copy_from_surface(skiatest::Reporter* reporter, GrContext* context, GrSurfaceProxy* proxy,
  66. GrColorType colorType, uint32_t expectedPixelValues[],
  67. const char* testName) {
  68. sk_sp<GrTextureProxy> dstProxy = GrSurfaceProxy::Copy(context, proxy, GrMipMapped::kNo,
  69. SkBackingFit::kExact, SkBudgeted::kYes);
  70. SkASSERT(dstProxy);
  71. sk_sp<GrSurfaceContext> dstContext = context->priv().makeWrappedSurfaceContext(
  72. std::move(dstProxy), colorType, kPremul_SkAlphaType);
  73. SkASSERT(dstContext.get());
  74. test_read_pixels(reporter, dstContext.get(), expectedPixelValues, testName);
  75. }
  76. void fill_pixel_data(int width, int height, GrColor* data) {
  77. for (int j = 0; j < height; ++j) {
  78. for (int i = 0; i < width; ++i) {
  79. unsigned int red = (unsigned int)(256.f * (i / (float)width));
  80. unsigned int green = (unsigned int)(256.f * (j / (float)height));
  81. data[i + j * width] = GrColorPackRGBA(red - (red >> 8), green - (green >> 8),
  82. 0xff, 0xff);
  83. }
  84. }
  85. }
  86. bool create_backend_texture(GrContext* context, GrBackendTexture* backendTex,
  87. const SkImageInfo& ii, const SkColor4f& color,
  88. GrMipMapped mipMapped, GrRenderable renderable) {
  89. // TODO: use the color-init version of createBackendTexture once Metal supports it.
  90. #if 0
  91. *backendTex = context->createBackendTexture(ii.width(), ii.height(), ii.colorType(),
  92. color, mipMapped, renderable);
  93. #else
  94. SkBitmap bm;
  95. bm.allocPixels(ii);
  96. sk_memset32(bm.getAddr32(0, 0), color.toSkColor(), ii.width() * ii.height());
  97. SkASSERT(GrMipMapped::kNo == mipMapped);
  98. *backendTex = context->priv().createBackendTexture(&bm.pixmap(), 1, renderable,
  99. GrProtected::kNo);
  100. #endif
  101. return backendTex->isValid();
  102. }
  103. void delete_backend_texture(GrContext* context, const GrBackendTexture& backendTex) {
  104. GrFlushInfo flushInfo;
  105. flushInfo.fFlags = kSyncCpu_GrFlushFlag;
  106. context->flush(flushInfo);
  107. context->deleteBackendTexture(backendTex);
  108. }
  109. bool does_full_buffer_contain_correct_color(const GrColor* srcBuffer,
  110. const GrColor* dstBuffer,
  111. int width,
  112. int height) {
  113. const GrColor* srcPtr = srcBuffer;
  114. const GrColor* dstPtr = dstBuffer;
  115. for (int j = 0; j < height; ++j) {
  116. for (int i = 0; i < width; ++i) {
  117. if (srcPtr[i] != dstPtr[i]) {
  118. return false;
  119. }
  120. }
  121. srcPtr += width;
  122. dstPtr += width;
  123. }
  124. return true;
  125. }
  126. bool bitmap_to_base64_data_uri(const SkBitmap& bitmap, SkString* dst) {
  127. SkPixmap pm;
  128. if (!bitmap.peekPixels(&pm)) {
  129. dst->set("peekPixels failed");
  130. return false;
  131. }
  132. // We're going to embed this PNG in a data URI, so make it as small as possible
  133. SkPngEncoder::Options options;
  134. options.fFilterFlags = SkPngEncoder::FilterFlag::kAll;
  135. options.fZLibLevel = 9;
  136. SkDynamicMemoryWStream wStream;
  137. if (!SkPngEncoder::Encode(&wStream, pm, options)) {
  138. dst->set("SkPngEncoder::Encode failed");
  139. return false;
  140. }
  141. sk_sp<SkData> pngData = wStream.detachAsData();
  142. size_t len = SkBase64::Encode(pngData->data(), pngData->size(), nullptr);
  143. // The PNG can be almost arbitrarily large. We don't want to fill our logs with enormous URLs.
  144. // Infra says these can be pretty big, as long as we're only outputting them on failure.
  145. static const size_t kMaxBase64Length = 1024 * 1024;
  146. if (len > kMaxBase64Length) {
  147. dst->printf("Encoded image too large (%u bytes)", static_cast<uint32_t>(len));
  148. return false;
  149. }
  150. dst->resize(len);
  151. SkBase64::Encode(pngData->data(), pngData->size(), dst->writable_str());
  152. dst->prepend("data:image/png;base64,");
  153. return true;
  154. }
  155. bool compare_pixels(const GrPixelInfo& infoA, const char* a, size_t rowBytesA,
  156. const GrPixelInfo& infoB, const char* b, size_t rowBytesB,
  157. const float tolRGBA[4], std::function<ComparePixmapsErrorReporter>& error) {
  158. if (infoA.width() != infoB.width() || infoA.height() != infoB.height()) {
  159. static constexpr float kDummyDiffs[4] = {};
  160. error(-1, -1, kDummyDiffs);
  161. return false;
  162. }
  163. SkAlphaType floatAlphaType = infoA.alphaType();
  164. // If one is premul and the other is unpremul we do the comparison in premul space.
  165. if ((infoA.alphaType() == kPremul_SkAlphaType ||
  166. infoB.alphaType() == kPremul_SkAlphaType) &&
  167. (infoA.alphaType() == kUnpremul_SkAlphaType ||
  168. infoB.alphaType() == kUnpremul_SkAlphaType)) {
  169. floatAlphaType = kPremul_SkAlphaType;
  170. }
  171. sk_sp<SkColorSpace> floatCS;
  172. if (SkColorSpace::Equals(infoA.colorSpace(), infoB.colorSpace())) {
  173. floatCS = infoA.refColorSpace();
  174. } else {
  175. floatCS = SkColorSpace::MakeSRGBLinear();
  176. }
  177. GrPixelInfo floatInfo(GrColorType::kRGBA_F32, floatAlphaType, std::move(floatCS),
  178. infoA.width(), infoA.height());
  179. size_t floatBpp = GrColorTypeBytesPerPixel(GrColorType::kRGBA_F32);
  180. size_t floatRowBytes = floatBpp * infoA.width();
  181. std::unique_ptr<char[]> floatA(new char[floatRowBytes * infoA.height()]);
  182. std::unique_ptr<char[]> floatB(new char[floatRowBytes * infoA.height()]);
  183. SkAssertResult(GrConvertPixels(floatInfo, floatA.get(), floatRowBytes, infoA, a, rowBytesA));
  184. SkAssertResult(GrConvertPixels(floatInfo, floatB.get(), floatRowBytes, infoB, b, rowBytesB));
  185. auto at = [floatBpp, floatRowBytes](const char* floatBuffer, int x, int y) {
  186. return reinterpret_cast<const float*>(floatBuffer + y * floatRowBytes + x * floatBpp);
  187. };
  188. for (int y = 0; y < infoA.height(); ++y) {
  189. for (int x = 0; x < infoA.width(); ++x) {
  190. const float* rgbaA = at(floatA.get(), x, y);
  191. const float* rgbaB = at(floatB.get(), x, y);
  192. float diffs[4];
  193. bool bad = false;
  194. for (int i = 0; i < 4; ++i) {
  195. diffs[i] = rgbaB[i] - rgbaA[i];
  196. if (std::abs(diffs[i]) > std::abs(tolRGBA[i])) {
  197. bad = true;
  198. }
  199. }
  200. if (bad) {
  201. error(x, y, diffs);
  202. return false;
  203. }
  204. }
  205. }
  206. return true;
  207. }
  208. bool compare_pixels(const SkPixmap& a, const SkPixmap& b, const float tolRGBA[4],
  209. std::function<ComparePixmapsErrorReporter>& error) {
  210. return compare_pixels(a.info(), static_cast<const char*>(a.addr()), a.rowBytes(),
  211. b.info(), static_cast<const char*>(b.addr()), b.rowBytes(),
  212. tolRGBA, error);
  213. }
  214. #include "src/utils/SkCharToGlyphCache.h"
  215. static SkGlyphID hash_to_glyph(uint32_t value) {
  216. return SkToU16(((value >> 16) ^ value) & 0xFFFF);
  217. }
  218. namespace {
  219. class UnicharGen {
  220. SkUnichar fU;
  221. const int fStep;
  222. public:
  223. UnicharGen(int step) : fU(0), fStep(step) {}
  224. SkUnichar next() {
  225. fU += fStep;
  226. return fU;
  227. }
  228. };
  229. }
  230. DEF_TEST(chartoglyph_cache, reporter) {
  231. SkCharToGlyphCache cache;
  232. const int step = 3;
  233. UnicharGen gen(step);
  234. for (int i = 0; i < 500; ++i) {
  235. SkUnichar c = gen.next();
  236. SkGlyphID glyph = hash_to_glyph(c);
  237. int index = cache.findGlyphIndex(c);
  238. if (index >= 0) {
  239. index = cache.findGlyphIndex(c);
  240. }
  241. REPORTER_ASSERT(reporter, index < 0);
  242. cache.insertCharAndGlyph(~index, c, glyph);
  243. UnicharGen gen2(step);
  244. for (int j = 0; j <= i; ++j) {
  245. c = gen2.next();
  246. glyph = hash_to_glyph(c);
  247. index = cache.findGlyphIndex(c);
  248. if ((unsigned)index != glyph) {
  249. index = cache.findGlyphIndex(c);
  250. }
  251. REPORTER_ASSERT(reporter, (unsigned)index == glyph);
  252. }
  253. }
  254. }