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