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