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" |
| 14 | |
| 15 | void test_read_pixels(skiatest::Reporter* reporter, GrContext* context, |
| 16 | GrSurfaceContext* srcContext, uint32_t expectedPixelValues[], |
| 17 | const char* testName) { |
| 18 | int pixelCnt = srcContext->width() * srcContext->height(); |
| 19 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 20 | memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt); |
| 21 | |
| 22 | SkImageInfo ii = SkImageInfo::Make(srcContext->width(), srcContext->height(), |
| 23 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 24 | bool read = srcContext->readPixels(ii, pixels.get(), 0, 0, 0); |
| 25 | if (!read) { |
| 26 | ERRORF(reporter, "%s: Error reading from texture.", testName); |
| 27 | } |
| 28 | |
| 29 | for (int i = 0; i < pixelCnt; ++i) { |
| 30 | if (pixels.get()[i] != expectedPixelValues[i]) { |
| 31 | ERRORF(reporter, "%s: Error, pixel value %d should be 0x%08x, got 0x%08x.", |
| 32 | testName, i, expectedPixelValues[i], pixels.get()[i]); |
| 33 | break; |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | void test_write_pixels(skiatest::Reporter* reporter, GrContext* context, |
| 39 | GrSurfaceContext* dstContext, bool expectedToWork, |
| 40 | const char* testName) { |
| 41 | int pixelCnt = dstContext->width() * dstContext->height(); |
| 42 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 43 | for (int y = 0; y < dstContext->width(); ++y) { |
| 44 | for (int x = 0; x < dstContext->height(); ++x) { |
| 45 | pixels.get()[y * dstContext->width() + x] = |
| 46 | GrPremulColor(GrColorPackRGBA(x, y, x + y, 2*y)); |
| 47 | } |
| 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 | |
| 60 | if (write && !expectedToWork) { |
| 61 | ERRORF(reporter, "%s: writePixels succeeded when it wasn't supposed to.", testName); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | test_read_pixels(reporter, context, dstContext, pixels.get(), testName); |
| 66 | } |
| 67 | |
| 68 | void test_copy_from_surface(skiatest::Reporter* reporter, GrContext* context, |
| 69 | GrSurfaceProxy* proxy, uint32_t expectedPixelValues[], |
| 70 | bool onlyTestRTConfig, const char* testName) { |
| 71 | GrSurfaceDesc copyDstDesc; |
| 72 | copyDstDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 73 | copyDstDesc.fWidth = proxy->width(); |
| 74 | copyDstDesc.fHeight = proxy->height(); |
| 75 | |
| 76 | for (auto flags : { kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag }) { |
| 77 | if (kNone_GrSurfaceFlags == flags && onlyTestRTConfig) { |
| 78 | continue; |
| 79 | } |
| 80 | |
| 81 | copyDstDesc.fFlags = flags; |
| 82 | |
| 83 | sk_sp<GrSurfaceContext> dstContext(GrSurfaceProxy::TestCopy(context, copyDstDesc, proxy)); |
| 84 | |
| 85 | test_read_pixels(reporter, context, dstContext.get(), expectedPixelValues, testName); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void test_copy_to_surface(skiatest::Reporter* reporter, GrContext* context, |
| 90 | GrSurfaceContext* dstContext, const char* testName) { |
| 91 | |
| 92 | int pixelCnt = dstContext->width() * dstContext->height(); |
| 93 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 94 | for (int y = 0; y < dstContext->width(); ++y) { |
| 95 | for (int x = 0; x < dstContext->height(); ++x) { |
| 96 | pixels.get()[y * dstContext->width() + x] = |
| 97 | GrPremulColor(GrColorPackRGBA(y, x, x * y, 2*y)); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | GrSurfaceDesc copySrcDesc; |
| 102 | copySrcDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 103 | copySrcDesc.fWidth = dstContext->width(); |
| 104 | copySrcDesc.fHeight = dstContext->height(); |
| 105 | |
| 106 | for (auto flags : { kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag }) { |
| 107 | copySrcDesc.fFlags = flags; |
| 108 | |
| 109 | sk_sp<GrSurfaceProxy> src(GrSurfaceProxy::MakeDeferred(*context->caps(), |
| 110 | context->textureProvider(), |
| 111 | copySrcDesc, |
| 112 | SkBudgeted::kYes, pixels.get(), 0)); |
| 113 | dstContext->copy(src.get()); |
| 114 | |
| 115 | test_read_pixels(reporter, context, dstContext, pixels.get(), testName); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | #endif |