blob: 053b456f57ae02e556348358eab2ddca6c35c35a [file] [log] [blame]
bsalomonb8fea972016-02-16 07:34:17 -08001/*
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
bsalomon68d91342016-04-12 09:59:58 -070018DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CopySurface, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070019 GrContext* context = ctxInfo.grContext();
bsalomonb8fea972016-02-16 07:34:17 -080020 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
Robert Phillipsd316e772016-12-14 12:04:46 +000071 sk_sp<GrTexture> src(
72 context->textureProvider()->createTexture(srcDesc, SkBudgeted::kNo,
73 srcPixels.get(),
74 kRowBytes));
75 sk_sp<GrTexture> dst(
76 context->textureProvider()->createTexture(dstDesc, SkBudgeted::kNo,
77 dstPixels.get(),
78 kRowBytes));
bsalomonb8fea972016-02-16 07:34:17 -080079 if (!src || !dst) {
80 ERRORF(reporter,
81 "Could not create surfaces for copy surface test.");
82 continue;
83 }
84
Robert Phillipsd316e772016-12-14 12:04:46 +000085 bool result
86 = context->copySurface(dst.get(), src.get(), srcRect, dstPoint);
bsalomonb8fea972016-02-16 07:34:17 -080087
88 bool expectedResult = true;
89 SkIPoint dstOffset = { dstPoint.fX - srcRect.fLeft,
90 dstPoint.fY - srcRect.fTop };
91 SkIRect copiedDstRect = SkIRect::MakeXYWH(dstPoint.fX,
92 dstPoint.fY,
93 srcRect.width(),
94 srcRect.height());
95
96 SkIRect copiedSrcRect;
97 if (!copiedSrcRect.intersect(srcRect, SkIRect::MakeWH(kW, kH))) {
98 expectedResult = false;
99 } else {
100 // If the src rect was clipped, apply same clipping to each side of
101 // copied dst rect.
102 copiedDstRect.fLeft += copiedSrcRect.fLeft - srcRect.fLeft;
103 copiedDstRect.fTop += copiedSrcRect.fTop - srcRect.fTop;
104 copiedDstRect.fRight -= copiedSrcRect.fRight - srcRect.fRight;
105 copiedDstRect.fBottom -= copiedSrcRect.fBottom - srcRect.fBottom;
106 }
107 if (copiedDstRect.isEmpty() ||
108 !copiedDstRect.intersect(SkIRect::MakeWH(kW, kH))) {
109 expectedResult = false;
110 }
111 // To make the copied src rect correct we would apply any dst clipping
112 // back to the src rect, but we don't use it again so don't bother.
113 if (expectedResult != result) {
114 ERRORF(reporter, "Expected return value %d from copySurface, got "
115 "%d.", expectedResult, result);
116 continue;
117 }
118
119 if (!expectedResult || !result) {
120 continue;
121 }
122
123 sk_memset32(read.get(), 0, kW * kH);
Robert Phillipsd316e772016-12-14 12:04:46 +0000124 if (!dst->readPixels(0, 0, kW, kH, baseDesc.fConfig, read.get(),
125 kRowBytes)) {
bsalomonb8fea972016-02-16 07:34:17 -0800126 ERRORF(reporter, "Error calling readPixels");
127 continue;
128 }
129
130 bool abort = false;
131 // Validate that pixels inside copiedDstRect received the correct value
132 // from src and that those outside were not modified.
133 for (int y = 0; y < kH && !abort; ++y) {
134 for (int x = 0; x < kW; ++x) {
135 uint32_t r = read.get()[y * kW + x];
136 if (copiedDstRect.contains(x, y)) {
137 int sx = x - dstOffset.fX;
138 int sy = y - dstOffset.fY;
139 uint32_t s = srcPixels.get()[sy * kW + sx];
140 if (s != r) {
141 ERRORF(reporter, "Expected dst %d,%d to contain "
142 "0x%08x copied from src location %d,%d. Got "
143 "0x%08x", x, y, s, sx, sy, r);
144 abort = true;
145 break;
146 }
147 } else {
148 uint32_t d = dstPixels.get()[y * kW + x];
149 if (d != r) {
150 ERRORF(reporter, "Expected dst %d,%d to be unmodified ("
151 "0x%08x). Got 0x%08x", x, y, d, r);
152 abort = true;
153 break;
154 }
155 }
156 }
157 }
158 }
159 }
160 }
161 }
162 }
163 }
164}
165#endif