• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/core/SkImageInfo.h"
9 #include "include/core/SkPoint.h"
10 #include "include/core/SkRect.h"
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkTypes.h"
13 #include "include/gpu/GrDirectContext.h"
14 #include "include/gpu/GrTypes.h"
15 #include "include/private/GrTypesPriv.h"
16 #include "include/private/SkTemplates.h"
17 #include "src/core/SkOpts.h"
18 #include "src/gpu/GrCaps.h"
19 #include "src/gpu/GrDirectContextPriv.h"
20 #include "src/gpu/GrImageInfo.h"
21 #include "src/gpu/GrSurfaceProxy.h"
22 #include "src/gpu/GrTextureProxy.h"
23 #include "src/gpu/SkGr.h"
24 #include "src/gpu/SurfaceFillContext.h"
25 #include "tests/Test.h"
26 #include "tools/gpu/GrContextFactory.h"
27 #include "tools/gpu/ProxyUtils.h"
28 
29 #include <initializer_list>
30 #include <utility>
31 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CopySurface,reporter,ctxInfo)32 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CopySurface, reporter, ctxInfo) {
33     auto dContext = ctxInfo.directContext();
34 
35     static const int kW = 10;
36     static const int kH = 10;
37     static const size_t kRowBytes = sizeof(uint32_t) * kW;
38 
39     SkAutoTMalloc<uint32_t> srcPixels(kW * kH);
40     for (int i = 0; i < kW * kH; ++i) {
41         srcPixels.get()[i] = i;
42     }
43 
44     SkAutoTMalloc<uint32_t> dstPixels(kW * kH);
45     for (int i = 0; i < kW * kH; ++i) {
46         dstPixels.get()[i] = ~i;
47     }
48 
49     static const SkIRect kSrcRects[] {
50         { 0,  0, kW  , kH  },
51         {-1, -1, kW+1, kH+1},
52         { 1,  1, kW-1, kH-1},
53         { 5,  5, 6   , 6   },
54     };
55 
56     static const SkIPoint kDstPoints[] {
57         { 0   ,  0   },
58         { 1   ,  1   },
59         { kW/2,  kH/4},
60         { kW-1,  kH-1},
61         { kW  ,  kH  },
62         { kW+1,  kH+2},
63         {-1   , -1   },
64     };
65 
66     static const SkImageInfo kImageInfos[] {
67         SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
68         SkImageInfo::Make(kW, kH, kBGRA_8888_SkColorType, kPremul_SkAlphaType)
69     };
70 
71     SkAutoTMalloc<uint32_t> read(kW * kH);
72 
73     for (auto sOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
74         for (auto dOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
75             for (auto sRenderable : {GrRenderable::kYes, GrRenderable::kNo}) {
76                 for (auto dRenderable : {GrRenderable::kYes, GrRenderable::kNo}) {
77                     for (const SkIRect& srcRect : kSrcRects) {
78                         for (const SkIPoint& dstPoint : kDstPoints) {
79                             for (const SkImageInfo& ii: kImageInfos) {
80                                 GrCPixmap srcPM(ii, srcPixels.get(), kRowBytes);
81                                 GrPixmap  dstPM(ii, dstPixels.get(), kRowBytes);
82                                 auto srcView = sk_gpu_test::MakeTextureProxyViewFromData(
83                                         dContext, sRenderable, sOrigin, srcPM);
84                                 auto dstView = sk_gpu_test::MakeTextureProxyViewFromData(
85                                         dContext, dRenderable, dOrigin, dstPM);
86 
87                                 // Should always work if the color type is RGBA, but may not work
88                                 // for BGRA
89                                 if (ii.colorType() == kRGBA_8888_SkColorType) {
90                                     if (!srcView || !dstView) {
91                                         ERRORF(reporter,
92                                                "Could not create surfaces for copy surface test.");
93                                         continue;
94                                     }
95                                 } else {
96                                     if (!dContext->defaultBackendFormat(
97                                             kBGRA_8888_SkColorType, GrRenderable::kNo).isValid()) {
98                                         continue;
99                                     }
100                                     if (!srcView || !dstView) {
101                                         ERRORF(reporter,
102                                                "Could not create surfaces for copy surface test.");
103                                         continue;
104                                     }
105                                 }
106 
107                                 auto dstContext = dContext->priv().makeSC(std::move(dstView),
108                                                                           ii.colorInfo());
109 
110                                 bool result = false;
111                                 if (sOrigin == dOrigin) {
112                                     result = dstContext->testCopy(srcView.refProxy(),
113                                                                   srcRect,
114                                                                   dstPoint);
115                                 } else if (dRenderable == GrRenderable::kYes) {
116                                     SkASSERT(dstContext->asFillContext());
117                                     result = dstContext->asFillContext()->blitTexture(
118                                             std::move(srcView), srcRect, dstPoint);
119                                 }
120 
121                                 bool expectedResult = true;
122                                 SkIPoint dstOffset = { dstPoint.fX - srcRect.fLeft,
123                                                        dstPoint.fY - srcRect.fTop };
124                                 SkIRect copiedDstRect = SkIRect::MakeXYWH(dstPoint.fX,
125                                                                           dstPoint.fY,
126                                                                           srcRect.width(),
127                                                                           srcRect.height());
128 
129                                 SkIRect copiedSrcRect;
130                                 if (!copiedSrcRect.intersect(srcRect, SkIRect::MakeWH(kW, kH))) {
131                                     expectedResult = false;
132                                 } else {
133                                     // If the src rect was clipped, apply same clipping to each side
134                                     // of copied dst rect.
135                                     copiedDstRect.fLeft += copiedSrcRect.fLeft - srcRect.fLeft;
136                                     copiedDstRect.fTop += copiedSrcRect.fTop - srcRect.fTop;
137                                     copiedDstRect.fRight -= copiedSrcRect.fRight - srcRect.fRight;
138                                     copiedDstRect.fBottom -= copiedSrcRect.fBottom -
139                                                              srcRect.fBottom;
140                                 }
141                                 if (copiedDstRect.isEmpty() ||
142                                     !copiedDstRect.intersect(SkIRect::MakeWH(kW, kH))) {
143                                     expectedResult = false;
144                                 }
145                                 if (sOrigin != dOrigin && dRenderable == GrRenderable::kNo) {
146                                     expectedResult = false;
147                                 }
148 
149                                 // To make the copied src rect correct we would apply any dst
150                                 // clipping back to the src rect, but we don't use it again so
151                                 // don't bother.
152                                 if (expectedResult != result) {
153                                     ERRORF(reporter, "Expected return value %d from copySurface, "
154                                            "got %d.", expectedResult, result);
155                                     continue;
156                                 }
157 
158                                 if (!expectedResult || !result) {
159                                     continue;
160                                 }
161 
162                                 sk_memset32(read.get(), 0, kW * kH);
163                                 GrPixmap readPM(ii, read.get(), kRowBytes);
164                                 if (!dstContext->readPixels(dContext, readPM, {0, 0})) {
165                                     ERRORF(reporter, "Error calling readPixels");
166                                     continue;
167                                 }
168 
169                                 bool abort = false;
170                                 // Validate that pixels inside copiedDstRect received the correct
171                                 // value from src and that those outside were not modified.
172                                 for (int y = 0; y < kH && !abort; ++y) {
173                                     for (int x = 0; x < kW; ++x) {
174                                         uint32_t r = read.get()[y * kW + x];
175                                         if (copiedDstRect.contains(x, y)) {
176                                             int sx = x - dstOffset.fX;
177                                             int sy = y - dstOffset.fY;
178                                             uint32_t s = srcPixels.get()[sy * kW + sx];
179                                             if (s != r) {
180                                                 ERRORF(reporter, "Expected dst %d,%d to contain "
181                                                        "0x%08x copied from src location %d,%d. Got "
182                                                        "0x%08x", x, y, s, sx, sy, r);
183                                                 abort = true;
184                                                 break;
185                                             }
186                                         } else {
187                                             uint32_t d = dstPixels.get()[y * kW + x];
188                                             if (d != r) {
189                                                 ERRORF(reporter, "Expected dst %d,%d to be "
190                                                        "unmodified (0x%08x). Got 0x%08x",
191                                                        x, y, d, r);
192                                                 abort = true;
193                                                 break;
194                                             }
195                                         }
196                                     }
197                                 }
198                             }
199                         }
200                     }
201                 }
202             }
203         }
204     }
205 }
206