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