Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 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 | |
| 8 | #include "TestUtils.h" |
| 9 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 10 | #include "GrProxyProvider.h" |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 11 | #include "GrSurfaceContext.h" |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 12 | #include "GrSurfaceContextPriv.h" |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 13 | #include "GrSurfaceProxy.h" |
Mike Reed | 84dd857 | 2017-03-08 22:21:00 -0500 | [diff] [blame] | 14 | #include "GrTextureProxy.h" |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 15 | #include "ProxyUtils.h" |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 16 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 17 | void test_read_pixels(skiatest::Reporter* reporter, |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 18 | GrSurfaceContext* srcContext, uint32_t expectedPixelValues[], |
| 19 | const char* testName) { |
| 20 | int pixelCnt = srcContext->width() * srcContext->height(); |
| 21 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 22 | memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt); |
| 23 | |
| 24 | SkImageInfo ii = SkImageInfo::Make(srcContext->width(), srcContext->height(), |
| 25 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 26 | bool read = srcContext->readPixels(ii, pixels.get(), 0, 0, 0); |
| 27 | if (!read) { |
| 28 | ERRORF(reporter, "%s: Error reading from texture.", testName); |
| 29 | } |
| 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 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 40 | void test_write_pixels(skiatest::Reporter* reporter, |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 41 | GrSurfaceContext* dstContext, bool expectedToWork, |
| 42 | const char* testName) { |
| 43 | int pixelCnt = dstContext->width() * dstContext->height(); |
| 44 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 45 | for (int y = 0; y < dstContext->width(); ++y) { |
| 46 | for (int x = 0; x < dstContext->height(); ++x) { |
| 47 | pixels.get()[y * dstContext->width() + x] = |
| 48 | GrPremulColor(GrColorPackRGBA(x, y, x + y, 2*y)); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | SkImageInfo ii = SkImageInfo::Make(dstContext->width(), dstContext->height(), |
| 53 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 54 | bool write = dstContext->writePixels(ii, pixels.get(), 0, 0, 0); |
| 55 | if (!write) { |
| 56 | if (expectedToWork) { |
| 57 | ERRORF(reporter, "%s: Error writing to texture.", testName); |
| 58 | } |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | if (write && !expectedToWork) { |
| 63 | ERRORF(reporter, "%s: writePixels succeeded when it wasn't supposed to.", testName); |
| 64 | return; |
| 65 | } |
| 66 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 67 | test_read_pixels(reporter, dstContext, pixels.get(), testName); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void test_copy_from_surface(skiatest::Reporter* reporter, GrContext* context, |
| 71 | GrSurfaceProxy* proxy, uint32_t expectedPixelValues[], |
| 72 | bool onlyTestRTConfig, const char* testName) { |
| 73 | GrSurfaceDesc copyDstDesc; |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 74 | copyDstDesc.fWidth = proxy->width(); |
| 75 | copyDstDesc.fHeight = proxy->height(); |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 76 | copyDstDesc.fConfig = kRGBA_8888_GrPixelConfig; |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 77 | |
| 78 | for (auto flags : { kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag }) { |
| 79 | if (kNone_GrSurfaceFlags == flags && onlyTestRTConfig) { |
| 80 | continue; |
| 81 | } |
| 82 | |
| 83 | copyDstDesc.fFlags = flags; |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 84 | auto origin = (kNone_GrSurfaceFlags == flags) ? kTopLeft_GrSurfaceOrigin |
| 85 | : kBottomLeft_GrSurfaceOrigin; |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 86 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 87 | sk_sp<GrSurfaceContext> dstContext( |
| 88 | GrSurfaceProxy::TestCopy(context, copyDstDesc, origin, proxy)); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 89 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 90 | test_read_pixels(reporter, dstContext.get(), expectedPixelValues, testName); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 94 | void test_copy_to_surface(skiatest::Reporter* reporter, GrProxyProvider* proxyProvider, |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 95 | GrSurfaceContext* dstContext, const char* testName) { |
| 96 | |
| 97 | int pixelCnt = dstContext->width() * dstContext->height(); |
| 98 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 99 | for (int y = 0; y < dstContext->width(); ++y) { |
| 100 | for (int x = 0; x < dstContext->height(); ++x) { |
| 101 | pixels.get()[y * dstContext->width() + x] = |
| 102 | GrPremulColor(GrColorPackRGBA(y, x, x * y, 2*y)); |
| 103 | } |
| 104 | } |
| 105 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 106 | for (auto isRT : {false, true}) { |
| 107 | for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) { |
| 108 | auto src = sk_gpu_test::MakeTextureProxyFromData( |
| 109 | dstContext->surfPriv().getContext(), isRT, dstContext->width(), |
| 110 | dstContext->height(), GrColorType::kRGBA_8888, origin, pixels.get(), 0); |
| 111 | dstContext->copy(src.get()); |
| 112 | test_read_pixels(reporter, dstContext, pixels.get(), testName); |
| 113 | } |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 114 | } |
| 115 | } |