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