blob: 828eff073e186214114dfac73729c305ef61b5c8 [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"
Brian Osman32342f02017-03-04 08:12:46 -050014#include "GrResourceProvider.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050015#include "GrSurfaceContext.h"
16#include "GrSurfaceProxy.h"
bsalomonb8fea972016-02-16 07:34:17 -080017#include "GrTexture.h"
bsalomonb8fea972016-02-16 07:34:17 -080018
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
Robert Phillipsd46697a2017-01-25 12:10:37 -050059 const SkImageInfo ii = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
60
bsalomonb8fea972016-02-16 07:34:17 -080061 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 Phillips2f493142017-03-02 18:18:38 -050076 sk_sp<GrTextureProxy> src(GrSurfaceProxy::MakeDeferred(
Robert Phillipse2f7d182016-12-15 09:23:05 -050077 *context->caps(),
Brian Osman32342f02017-03-04 08:12:46 -050078 context->resourceProvider(),
Robert Phillipse2f7d182016-12-15 09:23:05 -050079 srcDesc, SkBudgeted::kNo,
80 srcPixels.get(),
81 kRowBytes));
82
Robert Phillips2f493142017-03-02 18:18:38 -050083 sk_sp<GrTextureProxy> dst(GrSurfaceProxy::MakeDeferred(
Robert Phillipse2f7d182016-12-15 09:23:05 -050084 *context->caps(),
Brian Osman32342f02017-03-04 08:12:46 -050085 context->resourceProvider(),
Robert Phillipse2f7d182016-12-15 09:23:05 -050086 dstDesc, SkBudgeted::kNo,
87 dstPixels.get(),
88 kRowBytes));
bsalomonb8fea972016-02-16 07:34:17 -080089 if (!src || !dst) {
90 ERRORF(reporter,
91 "Could not create surfaces for copy surface test.");
92 continue;
93 }
94
Robert Phillipsd46697a2017-01-25 12:10:37 -050095 sk_sp<GrSurfaceContext> dstContext =
96 context->contextPriv().makeWrappedSurfaceContext(std::move(dst),
97 nullptr);
Robert Phillipse2f7d182016-12-15 09:23:05 -050098
Robert Phillipsd46697a2017-01-25 12:10:37 -050099 bool result = dstContext->copy(src.get(), srcRect, dstPoint);
bsalomonb8fea972016-02-16 07:34:17 -0800100
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 Phillipsd46697a2017-01-25 12:10:37 -0500137 if (!dstContext->readPixels(ii, read.get(), kRowBytes, 0, 0)) {
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