Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 1 | |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2016 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include <initializer_list> |
| 10 | #include "Test.h" |
| 11 | |
| 12 | #if SK_SUPPORT_GPU |
| 13 | #include "GrContext.h" |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 14 | #include "GrContextPriv.h" |
Robert Phillips | 009e9af | 2017-06-15 14:01:04 -0400 | [diff] [blame] | 15 | #include "GrTextureProxy.h" |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 16 | #include "ProxyUtils.h" |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 17 | |
| 18 | #include "SkUtils.h" |
| 19 | |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 20 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CopySurface, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 21 | GrContext* context = ctxInfo.grContext(); |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 22 | static const int kW = 10; |
| 23 | static const int kH = 10; |
| 24 | static const size_t kRowBytes = sizeof(uint32_t) * kW; |
| 25 | |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 26 | SkAutoTMalloc<uint32_t> srcPixels(kW * kH); |
| 27 | for (int i = 0; i < kW * kH; ++i) { |
| 28 | srcPixels.get()[i] = i; |
| 29 | } |
| 30 | |
| 31 | SkAutoTMalloc<uint32_t> dstPixels(kW * kH); |
| 32 | for (int i = 0; i < kW * kH; ++i) { |
| 33 | dstPixels.get()[i] = ~i; |
| 34 | } |
| 35 | |
| 36 | static const SkIRect kSrcRects[] { |
| 37 | { 0, 0, kW , kH }, |
| 38 | {-1, -1, kW+1, kH+1}, |
| 39 | { 1, 1, kW-1, kH-1}, |
| 40 | { 5, 5, 6 , 6 }, |
| 41 | }; |
| 42 | |
| 43 | static const SkIPoint kDstPoints[] { |
| 44 | { 0 , 0 }, |
| 45 | { 1 , 1 }, |
| 46 | { kW/2, kH/4}, |
| 47 | { kW-1, kH-1}, |
| 48 | { kW , kH }, |
| 49 | { kW+1, kH+2}, |
| 50 | {-1 , -1 }, |
| 51 | }; |
| 52 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 53 | const SkImageInfo ii = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 54 | |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 55 | SkAutoTMalloc<uint32_t> read(kW * kH); |
| 56 | |
| 57 | for (auto sOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) { |
| 58 | for (auto dOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) { |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 59 | for (auto sRT : {true, false}) { |
| 60 | for (auto dRT : {true, false}) { |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 61 | for (auto srcRect : kSrcRects) { |
| 62 | for (auto dstPoint : kDstPoints) { |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 63 | auto src = sk_gpu_test::MakeTextureProxyFromData( |
| 64 | context, sRT, kW, kH, ii.colorType(), sOrigin, srcPixels.get(), |
| 65 | kRowBytes); |
| 66 | auto dst = sk_gpu_test::MakeTextureProxyFromData( |
| 67 | context, dRT, kW, kH, ii.colorType(), dOrigin, dstPixels.get(), |
| 68 | kRowBytes); |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 69 | if (!src || !dst) { |
| 70 | ERRORF(reporter, |
| 71 | "Could not create surfaces for copy surface test."); |
| 72 | continue; |
| 73 | } |
| 74 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 75 | sk_sp<GrSurfaceContext> dstContext = |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 76 | context->contextPriv().makeWrappedSurfaceContext(std::move(dst)); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 77 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 78 | bool result = dstContext->copy(src.get(), srcRect, dstPoint); |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 79 | |
| 80 | bool expectedResult = true; |
| 81 | SkIPoint dstOffset = { dstPoint.fX - srcRect.fLeft, |
| 82 | dstPoint.fY - srcRect.fTop }; |
| 83 | SkIRect copiedDstRect = SkIRect::MakeXYWH(dstPoint.fX, |
| 84 | dstPoint.fY, |
| 85 | srcRect.width(), |
| 86 | srcRect.height()); |
| 87 | |
| 88 | SkIRect copiedSrcRect; |
| 89 | if (!copiedSrcRect.intersect(srcRect, SkIRect::MakeWH(kW, kH))) { |
| 90 | expectedResult = false; |
| 91 | } else { |
| 92 | // If the src rect was clipped, apply same clipping to each side of |
| 93 | // copied dst rect. |
| 94 | copiedDstRect.fLeft += copiedSrcRect.fLeft - srcRect.fLeft; |
| 95 | copiedDstRect.fTop += copiedSrcRect.fTop - srcRect.fTop; |
| 96 | copiedDstRect.fRight -= copiedSrcRect.fRight - srcRect.fRight; |
| 97 | copiedDstRect.fBottom -= copiedSrcRect.fBottom - srcRect.fBottom; |
| 98 | } |
| 99 | if (copiedDstRect.isEmpty() || |
| 100 | !copiedDstRect.intersect(SkIRect::MakeWH(kW, kH))) { |
| 101 | expectedResult = false; |
| 102 | } |
| 103 | // To make the copied src rect correct we would apply any dst clipping |
| 104 | // back to the src rect, but we don't use it again so don't bother. |
| 105 | if (expectedResult != result) { |
| 106 | ERRORF(reporter, "Expected return value %d from copySurface, got " |
| 107 | "%d.", expectedResult, result); |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | if (!expectedResult || !result) { |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | sk_memset32(read.get(), 0, kW * kH); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 116 | if (!dstContext->readPixels(ii, read.get(), kRowBytes, 0, 0)) { |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 117 | ERRORF(reporter, "Error calling readPixels"); |
| 118 | continue; |
| 119 | } |
| 120 | |
| 121 | bool abort = false; |
| 122 | // Validate that pixels inside copiedDstRect received the correct value |
| 123 | // from src and that those outside were not modified. |
| 124 | for (int y = 0; y < kH && !abort; ++y) { |
| 125 | for (int x = 0; x < kW; ++x) { |
| 126 | uint32_t r = read.get()[y * kW + x]; |
| 127 | if (copiedDstRect.contains(x, y)) { |
| 128 | int sx = x - dstOffset.fX; |
| 129 | int sy = y - dstOffset.fY; |
| 130 | uint32_t s = srcPixels.get()[sy * kW + sx]; |
| 131 | if (s != r) { |
| 132 | ERRORF(reporter, "Expected dst %d,%d to contain " |
| 133 | "0x%08x copied from src location %d,%d. Got " |
| 134 | "0x%08x", x, y, s, sx, sy, r); |
| 135 | abort = true; |
| 136 | break; |
| 137 | } |
| 138 | } else { |
| 139 | uint32_t d = dstPixels.get()[y * kW + x]; |
| 140 | if (d != r) { |
| 141 | ERRORF(reporter, "Expected dst %d,%d to be unmodified (" |
| 142 | "0x%08x). Got 0x%08x", x, y, d, r); |
| 143 | abort = true; |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | #endif |