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