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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkImageInfo.h" |
| 9 | #include "include/core/SkPoint.h" |
| 10 | #include "include/core/SkRect.h" |
| 11 | #include "include/core/SkRefCnt.h" |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 12 | #include "include/core/SkTypes.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 13 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/gpu/GrTypes.h" |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 15 | #include "include/private/GrTypesPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/private/SkTemplates.h" |
| 17 | #include "src/core/SkUtils.h" |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrCaps.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrContextPriv.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrImageInfo.h" |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrRenderTargetContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/gpu/GrSurfaceContext.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 23 | #include "src/gpu/GrSurfaceProxy.h" |
| 24 | #include "src/gpu/GrTextureProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/gpu/SkGr.h" |
| 26 | #include "tests/Test.h" |
| 27 | #include "tools/gpu/GrContextFactory.h" |
| 28 | #include "tools/gpu/ProxyUtils.h" |
Ben Wagner | eed6128 | 2018-04-17 14:14:51 -0400 | [diff] [blame] | 29 | |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 30 | #include <initializer_list> |
Ben Wagner | eed6128 | 2018-04-17 14:14:51 -0400 | [diff] [blame] | 31 | #include <utility> |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 32 | |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 33 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CopySurface, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 34 | auto direct = ctxInfo.directContext(); |
| 35 | |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 36 | static const int kW = 10; |
| 37 | static const int kH = 10; |
| 38 | static const size_t kRowBytes = sizeof(uint32_t) * kW; |
| 39 | |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 40 | SkAutoTMalloc<uint32_t> srcPixels(kW * kH); |
| 41 | for (int i = 0; i < kW * kH; ++i) { |
| 42 | srcPixels.get()[i] = i; |
| 43 | } |
| 44 | |
| 45 | SkAutoTMalloc<uint32_t> dstPixels(kW * kH); |
| 46 | for (int i = 0; i < kW * kH; ++i) { |
| 47 | dstPixels.get()[i] = ~i; |
| 48 | } |
| 49 | |
| 50 | static const SkIRect kSrcRects[] { |
| 51 | { 0, 0, kW , kH }, |
| 52 | {-1, -1, kW+1, kH+1}, |
| 53 | { 1, 1, kW-1, kH-1}, |
| 54 | { 5, 5, 6 , 6 }, |
| 55 | }; |
| 56 | |
| 57 | static const SkIPoint kDstPoints[] { |
| 58 | { 0 , 0 }, |
| 59 | { 1 , 1 }, |
| 60 | { kW/2, kH/4}, |
| 61 | { kW-1, kH-1}, |
| 62 | { kW , kH }, |
| 63 | { kW+1, kH+2}, |
| 64 | {-1 , -1 }, |
| 65 | }; |
| 66 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 67 | static const SkImageInfo kImageInfos[] { |
| 68 | SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType), |
| 69 | SkImageInfo::Make(kW, kH, kBGRA_8888_SkColorType, kPremul_SkAlphaType) |
| 70 | }; |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 71 | |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 72 | SkAutoTMalloc<uint32_t> read(kW * kH); |
| 73 | |
| 74 | for (auto sOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) { |
| 75 | for (auto dOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) { |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 76 | for (auto sRenderable : {GrRenderable::kYes, GrRenderable::kNo}) { |
| 77 | for (auto dRenderable : {GrRenderable::kYes, GrRenderable::kNo}) { |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 78 | for (auto srcRect : kSrcRects) { |
| 79 | for (auto dstPoint : kDstPoints) { |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 80 | for (auto ii: kImageInfos) { |
| 81 | auto src = sk_gpu_test::MakeTextureProxyFromData( |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 82 | direct, sRenderable, sOrigin, ii, srcPixels.get(), |
Brian Salomon | 4eda710 | 2019-10-21 15:04:52 -0400 | [diff] [blame] | 83 | kRowBytes); |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 84 | auto dst = sk_gpu_test::MakeTextureProxyFromData( |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 85 | direct, dRenderable, dOrigin, ii, dstPixels.get(), |
Brian Salomon | 4eda710 | 2019-10-21 15:04:52 -0400 | [diff] [blame] | 86 | kRowBytes); |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 87 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 88 | // Should always work if the color type is RGBA, but may not work |
| 89 | // for BGRA |
Timothy Liang | deba212 | 2018-08-07 13:22:03 -0400 | [diff] [blame] | 90 | if (ii.colorType() == kRGBA_8888_SkColorType) { |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 91 | if (!src || !dst) { |
| 92 | ERRORF(reporter, |
| 93 | "Could not create surfaces for copy surface test."); |
| 94 | continue; |
| 95 | } |
| 96 | } else { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 97 | if (!direct->defaultBackendFormat( |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 98 | kBGRA_8888_SkColorType, GrRenderable::kNo).isValid()) { |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 99 | continue; |
| 100 | } |
| 101 | if (!src || !dst) { |
| 102 | ERRORF(reporter, |
| 103 | "Could not create surfaces for copy surface test."); |
| 104 | continue; |
| 105 | } |
| 106 | } |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 107 | |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 108 | GrColorType grColorType = SkColorTypeToGrColorType(ii.colorType()); |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 109 | GrSwizzle dstSwizzle = direct->priv().caps()->getReadSwizzle( |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 110 | dst->backendFormat(), grColorType); |
| 111 | GrSurfaceProxyView dstView(std::move(dst), dOrigin, dstSwizzle); |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 112 | auto dstContext = GrSurfaceContext::Make(direct, |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 113 | std::move(dstView), |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 114 | grColorType, |
| 115 | ii.alphaType(), nullptr); |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 116 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 117 | bool result = false; |
| 118 | if (sOrigin == dOrigin) { |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 119 | result = dstContext->testCopy(src.get(), srcRect, dstPoint); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 120 | } else if (dRenderable == GrRenderable::kYes) { |
| 121 | SkASSERT(dstContext->asRenderTargetContext()); |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 122 | GrSwizzle srcSwizzle = direct->priv().caps()->getReadSwizzle( |
Greg Daniel | 573312e | 2020-02-07 17:22:35 -0500 | [diff] [blame] | 123 | src->backendFormat(), grColorType); |
| 124 | GrSurfaceProxyView view(std::move(src), sOrigin, srcSwizzle); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 125 | result = dstContext->asRenderTargetContext()->blitTexture( |
Greg Daniel | 573312e | 2020-02-07 17:22:35 -0500 | [diff] [blame] | 126 | std::move(view), srcRect, dstPoint); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 127 | } |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 128 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 129 | bool expectedResult = true; |
| 130 | SkIPoint dstOffset = { dstPoint.fX - srcRect.fLeft, |
| 131 | dstPoint.fY - srcRect.fTop }; |
| 132 | SkIRect copiedDstRect = SkIRect::MakeXYWH(dstPoint.fX, |
| 133 | dstPoint.fY, |
| 134 | srcRect.width(), |
| 135 | srcRect.height()); |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 136 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 137 | SkIRect copiedSrcRect; |
| 138 | if (!copiedSrcRect.intersect(srcRect, SkIRect::MakeWH(kW, kH))) { |
| 139 | expectedResult = false; |
| 140 | } else { |
| 141 | // If the src rect was clipped, apply same clipping to each side |
| 142 | // of copied dst rect. |
| 143 | copiedDstRect.fLeft += copiedSrcRect.fLeft - srcRect.fLeft; |
| 144 | copiedDstRect.fTop += copiedSrcRect.fTop - srcRect.fTop; |
| 145 | copiedDstRect.fRight -= copiedSrcRect.fRight - srcRect.fRight; |
| 146 | copiedDstRect.fBottom -= copiedSrcRect.fBottom - |
| 147 | srcRect.fBottom; |
| 148 | } |
| 149 | if (copiedDstRect.isEmpty() || |
| 150 | !copiedDstRect.intersect(SkIRect::MakeWH(kW, kH))) { |
| 151 | expectedResult = false; |
| 152 | } |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 153 | if (sOrigin != dOrigin && dRenderable == GrRenderable::kNo) { |
| 154 | expectedResult = false; |
| 155 | } |
| 156 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 157 | // To make the copied src rect correct we would apply any dst |
| 158 | // clipping back to the src rect, but we don't use it again so |
| 159 | // don't bother. |
| 160 | if (expectedResult != result) { |
| 161 | ERRORF(reporter, "Expected return value %d from copySurface, " |
| 162 | "got %d.", expectedResult, result); |
| 163 | continue; |
| 164 | } |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 165 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 166 | if (!expectedResult || !result) { |
| 167 | continue; |
| 168 | } |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 169 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 170 | sk_memset32(read.get(), 0, kW * kH); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 171 | if (!dstContext->readPixels(ii, read.get(), kRowBytes, {0, 0})) { |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 172 | ERRORF(reporter, "Error calling readPixels"); |
| 173 | continue; |
| 174 | } |
| 175 | |
| 176 | bool abort = false; |
| 177 | // Validate that pixels inside copiedDstRect received the correct |
| 178 | // value from src and that those outside were not modified. |
| 179 | for (int y = 0; y < kH && !abort; ++y) { |
| 180 | for (int x = 0; x < kW; ++x) { |
| 181 | uint32_t r = read.get()[y * kW + x]; |
| 182 | if (copiedDstRect.contains(x, y)) { |
| 183 | int sx = x - dstOffset.fX; |
| 184 | int sy = y - dstOffset.fY; |
| 185 | uint32_t s = srcPixels.get()[sy * kW + sx]; |
| 186 | if (s != r) { |
| 187 | ERRORF(reporter, "Expected dst %d,%d to contain " |
| 188 | "0x%08x copied from src location %d,%d. Got " |
| 189 | "0x%08x", x, y, s, sx, sy, r); |
| 190 | abort = true; |
| 191 | break; |
| 192 | } |
| 193 | } else { |
| 194 | uint32_t d = dstPixels.get()[y * kW + x]; |
| 195 | if (d != r) { |
| 196 | ERRORF(reporter, "Expected dst %d,%d to be " |
| 197 | "unmodified (0x%08x). Got 0x%08x", |
| 198 | x, y, d, r); |
| 199 | abort = true; |
| 200 | break; |
| 201 | } |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |