blob: efb6b2d43d9bbfb79f7491bc2de15897bf51ee52 [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"
Robert Phillipse2f7d182016-12-15 09:23:05 -050013#include "GrContextPriv.h"
14#include "GrSurfaceContext.h"
15#include "GrSurfaceProxy.h"
bsalomonb8fea972016-02-16 07:34:17 -080016#include "GrTexture.h"
17#include "GrTextureProvider.h"
18
19#include "SkUtils.h"
20
bsalomon68d91342016-04-12 09:59:58 -070021DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CopySurface, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070022 GrContext* context = ctxInfo.grContext();
bsalomonb8fea972016-02-16 07:34:17 -080023 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
59 SkAutoTMalloc<uint32_t> read(kW * kH);
60
61 for (auto sOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
62 for (auto dOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
63 for (auto sFlags: {kRenderTarget_GrSurfaceFlag, kNone_GrSurfaceFlags}) {
64 for (auto dFlags: {kRenderTarget_GrSurfaceFlag, kNone_GrSurfaceFlags}) {
65 for (auto srcRect : kSrcRects) {
66 for (auto dstPoint : kDstPoints) {
67 GrSurfaceDesc srcDesc = baseDesc;
68 srcDesc.fOrigin = sOrigin;
69 srcDesc.fFlags = sFlags;
70 GrSurfaceDesc dstDesc = baseDesc;
71 dstDesc.fOrigin = dOrigin;
72 dstDesc.fFlags = dFlags;
73
Robert Phillipse2f7d182016-12-15 09:23:05 -050074 sk_sp<GrSurfaceProxy> src(GrSurfaceProxy::MakeDeferred(
75 *context->caps(),
76 context->textureProvider(),
77 srcDesc, SkBudgeted::kNo,
78 srcPixels.get(),
79 kRowBytes));
80
81 sk_sp<GrSurfaceProxy> dst(GrSurfaceProxy::MakeDeferred(
82 *context->caps(),
83 context->textureProvider(),
84 dstDesc, SkBudgeted::kNo,
85 dstPixels.get(),
86 kRowBytes));
bsalomonb8fea972016-02-16 07:34:17 -080087 if (!src || !dst) {
88 ERRORF(reporter,
89 "Could not create surfaces for copy surface test.");
90 continue;
91 }
92
Robert Phillipse2f7d182016-12-15 09:23:05 -050093 sk_sp<GrSurfaceContext> sContext =
Robert Phillips2c862492017-01-18 10:08:39 -050094 context->contextPriv().makeWrappedSurfaceContext(dst, nullptr);
Robert Phillipse2f7d182016-12-15 09:23:05 -050095
96 bool result = sContext->copy(src.get(), srcRect, dstPoint);
bsalomonb8fea972016-02-16 07:34:17 -080097
98 bool expectedResult = true;
99 SkIPoint dstOffset = { dstPoint.fX - srcRect.fLeft,
100 dstPoint.fY - srcRect.fTop };
101 SkIRect copiedDstRect = SkIRect::MakeXYWH(dstPoint.fX,
102 dstPoint.fY,
103 srcRect.width(),
104 srcRect.height());
105
106 SkIRect copiedSrcRect;
107 if (!copiedSrcRect.intersect(srcRect, SkIRect::MakeWH(kW, kH))) {
108 expectedResult = false;
109 } else {
110 // If the src rect was clipped, apply same clipping to each side of
111 // copied dst rect.
112 copiedDstRect.fLeft += copiedSrcRect.fLeft - srcRect.fLeft;
113 copiedDstRect.fTop += copiedSrcRect.fTop - srcRect.fTop;
114 copiedDstRect.fRight -= copiedSrcRect.fRight - srcRect.fRight;
115 copiedDstRect.fBottom -= copiedSrcRect.fBottom - srcRect.fBottom;
116 }
117 if (copiedDstRect.isEmpty() ||
118 !copiedDstRect.intersect(SkIRect::MakeWH(kW, kH))) {
119 expectedResult = false;
120 }
121 // To make the copied src rect correct we would apply any dst clipping
122 // back to the src rect, but we don't use it again so don't bother.
123 if (expectedResult != result) {
124 ERRORF(reporter, "Expected return value %d from copySurface, got "
125 "%d.", expectedResult, result);
126 continue;
127 }
128
129 if (!expectedResult || !result) {
130 continue;
131 }
132
Robert Phillipse2f7d182016-12-15 09:23:05 -0500133 GrSurface* dstSurf = dst->instantiate(context->textureProvider());
134
bsalomonb8fea972016-02-16 07:34:17 -0800135 sk_memset32(read.get(), 0, kW * kH);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500136 if (!dstSurf->readPixels(0, 0, kW, kH, baseDesc.fConfig, read.get(),
137 kRowBytes)) {
bsalomonb8fea972016-02-16 07:34:17 -0800138 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