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