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" |
| 13 | #include "GrTexture.h" |
| 14 | #include "GrTextureProvider.h" |
| 15 | |
| 16 | #include "SkUtils.h" |
| 17 | |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 18 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CopySurface, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame^] | 19 | GrContext* context = ctxInfo.grContext(); |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 20 | static const int kW = 10; |
| 21 | static const int kH = 10; |
| 22 | static const size_t kRowBytes = sizeof(uint32_t) * kW; |
| 23 | |
| 24 | GrSurfaceDesc baseDesc; |
| 25 | baseDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 26 | baseDesc.fWidth = kW; |
| 27 | baseDesc.fHeight = kH; |
| 28 | |
| 29 | SkAutoTMalloc<uint32_t> srcPixels(kW * kH); |
| 30 | for (int i = 0; i < kW * kH; ++i) { |
| 31 | srcPixels.get()[i] = i; |
| 32 | } |
| 33 | |
| 34 | SkAutoTMalloc<uint32_t> dstPixels(kW * kH); |
| 35 | for (int i = 0; i < kW * kH; ++i) { |
| 36 | dstPixels.get()[i] = ~i; |
| 37 | } |
| 38 | |
| 39 | static const SkIRect kSrcRects[] { |
| 40 | { 0, 0, kW , kH }, |
| 41 | {-1, -1, kW+1, kH+1}, |
| 42 | { 1, 1, kW-1, kH-1}, |
| 43 | { 5, 5, 6 , 6 }, |
| 44 | }; |
| 45 | |
| 46 | static const SkIPoint kDstPoints[] { |
| 47 | { 0 , 0 }, |
| 48 | { 1 , 1 }, |
| 49 | { kW/2, kH/4}, |
| 50 | { kW-1, kH-1}, |
| 51 | { kW , kH }, |
| 52 | { kW+1, kH+2}, |
| 53 | {-1 , -1 }, |
| 54 | }; |
| 55 | |
| 56 | SkAutoTMalloc<uint32_t> read(kW * kH); |
| 57 | |
| 58 | for (auto sOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) { |
| 59 | for (auto dOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) { |
| 60 | for (auto sFlags: {kRenderTarget_GrSurfaceFlag, kNone_GrSurfaceFlags}) { |
| 61 | for (auto dFlags: {kRenderTarget_GrSurfaceFlag, kNone_GrSurfaceFlags}) { |
| 62 | for (auto srcRect : kSrcRects) { |
| 63 | for (auto dstPoint : kDstPoints) { |
| 64 | GrSurfaceDesc srcDesc = baseDesc; |
| 65 | srcDesc.fOrigin = sOrigin; |
| 66 | srcDesc.fFlags = sFlags; |
| 67 | GrSurfaceDesc dstDesc = baseDesc; |
| 68 | dstDesc.fOrigin = dOrigin; |
| 69 | dstDesc.fFlags = dFlags; |
| 70 | |
| 71 | SkAutoTUnref<GrTexture> src( |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 72 | context->textureProvider()->createTexture(srcDesc, SkBudgeted::kNo, |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 73 | srcPixels.get(), |
| 74 | kRowBytes)); |
| 75 | SkAutoTUnref<GrTexture> dst( |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 76 | context->textureProvider()->createTexture(dstDesc, SkBudgeted::kNo, |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 77 | dstPixels.get(), |
| 78 | kRowBytes)); |
| 79 | if (!src || !dst) { |
| 80 | ERRORF(reporter, |
| 81 | "Could not create surfaces for copy surface test."); |
| 82 | continue; |
| 83 | } |
| 84 | |
| 85 | bool result = context->copySurface(dst, src, srcRect, dstPoint); |
| 86 | |
| 87 | bool expectedResult = true; |
| 88 | SkIPoint dstOffset = { dstPoint.fX - srcRect.fLeft, |
| 89 | dstPoint.fY - srcRect.fTop }; |
| 90 | SkIRect copiedDstRect = SkIRect::MakeXYWH(dstPoint.fX, |
| 91 | dstPoint.fY, |
| 92 | srcRect.width(), |
| 93 | srcRect.height()); |
| 94 | |
| 95 | SkIRect copiedSrcRect; |
| 96 | if (!copiedSrcRect.intersect(srcRect, SkIRect::MakeWH(kW, kH))) { |
| 97 | expectedResult = false; |
| 98 | } else { |
| 99 | // If the src rect was clipped, apply same clipping to each side of |
| 100 | // copied dst rect. |
| 101 | copiedDstRect.fLeft += copiedSrcRect.fLeft - srcRect.fLeft; |
| 102 | copiedDstRect.fTop += copiedSrcRect.fTop - srcRect.fTop; |
| 103 | copiedDstRect.fRight -= copiedSrcRect.fRight - srcRect.fRight; |
| 104 | copiedDstRect.fBottom -= copiedSrcRect.fBottom - srcRect.fBottom; |
| 105 | } |
| 106 | if (copiedDstRect.isEmpty() || |
| 107 | !copiedDstRect.intersect(SkIRect::MakeWH(kW, kH))) { |
| 108 | expectedResult = false; |
| 109 | } |
| 110 | // To make the copied src rect correct we would apply any dst clipping |
| 111 | // back to the src rect, but we don't use it again so don't bother. |
| 112 | if (expectedResult != result) { |
| 113 | ERRORF(reporter, "Expected return value %d from copySurface, got " |
| 114 | "%d.", expectedResult, result); |
| 115 | continue; |
| 116 | } |
| 117 | |
| 118 | if (!expectedResult || !result) { |
| 119 | continue; |
| 120 | } |
| 121 | |
| 122 | sk_memset32(read.get(), 0, kW * kH); |
| 123 | if (!dst->readPixels(0, 0, kW, kH, baseDesc.fConfig, read.get(), |
| 124 | kRowBytes)) { |
| 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 | } |
| 164 | #endif |