• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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 // This is a GPU-backend specific test. It relies on static intializers to work
9 
10 #include "include/core/SkTypes.h"
11 
12 #include "src/gpu/GrContextPriv.h"
13 #include "src/gpu/GrImageInfo.h"
14 #include "src/gpu/GrSurfaceProxy.h"
15 #include "src/gpu/SkGr.h"
16 #include "tests/Test.h"
17 #include "tests/TestUtils.h"
18 #include "tools/gpu/GrContextFactory.h"
19 #include "tools/gpu/ProxyUtils.h"
20 
21 using sk_gpu_test::GrContextFactory;
22 
basic_texture_test(skiatest::Reporter * reporter,GrContext * context,SkColorType ct,GrRenderable renderable)23 void basic_texture_test(skiatest::Reporter* reporter, GrContext* context, SkColorType ct,
24                         GrRenderable renderable) {
25     const int kWidth = 16;
26     const int kHeight = 16;
27     SkAutoTMalloc<GrColor> srcBuffer(kWidth*kHeight);
28     SkAutoTMalloc<GrColor> dstBuffer(kWidth*kHeight);
29 
30     FillPixelData(kWidth, kHeight, srcBuffer.get());
31 
32     auto grCT = SkColorTypeToGrColorType(ct);
33     auto proxy = sk_gpu_test::MakeTextureProxyFromData(
34             context, renderable, kTopLeft_GrSurfaceOrigin,
35             {grCT, kPremul_SkAlphaType, nullptr, kWidth, kHeight}, srcBuffer, 0);
36     REPORTER_ASSERT(reporter, proxy);
37     if (proxy) {
38         GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(), grCT);
39         GrSurfaceProxyView view(proxy, kTopLeft_GrSurfaceOrigin, swizzle);
40         auto sContext = GrSurfaceContext::Make(context, std::move(view), grCT, kPremul_SkAlphaType,
41                                                nullptr);
42 
43         SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
44 
45         bool result = sContext->readPixels(dstInfo, dstBuffer, 0, {0, 0});
46         REPORTER_ASSERT(reporter, result);
47         REPORTER_ASSERT(reporter,
48                         DoesFullBufferContainCorrectColor(srcBuffer, dstBuffer, kWidth, kHeight));
49 
50         dstInfo = SkImageInfo::Make(10, 2, ct, kPremul_SkAlphaType);
51         result = sContext->writePixels(dstInfo, srcBuffer, 0, {2, 10});
52         REPORTER_ASSERT(reporter, result);
53 
54         memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
55 
56         result = sContext->readPixels(dstInfo, dstBuffer, 0, {2, 10});
57         REPORTER_ASSERT(reporter, result);
58 
59         REPORTER_ASSERT(reporter, DoesFullBufferContainCorrectColor(srcBuffer, dstBuffer, 10, 2));
60     }
61 
62     proxy = sk_gpu_test::MakeTextureProxyFromData(
63             context, renderable, kBottomLeft_GrSurfaceOrigin,
64             {grCT, kPremul_SkAlphaType, nullptr, kWidth, kHeight}, srcBuffer, 0);
65     REPORTER_ASSERT(reporter, proxy);
66     if (proxy) {
67         GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(), grCT);
68         GrSurfaceProxyView view(proxy, kBottomLeft_GrSurfaceOrigin, swizzle);
69         auto sContext = GrSurfaceContext::Make(context, std::move(view), grCT, kPremul_SkAlphaType,
70                                                nullptr);
71 
72         SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
73 
74         bool result = sContext->readPixels(dstInfo, dstBuffer, 0, {0, 0});
75         REPORTER_ASSERT(reporter, result);
76         REPORTER_ASSERT(reporter,
77                         DoesFullBufferContainCorrectColor(srcBuffer, dstBuffer, kWidth, kHeight));
78 
79         dstInfo = SkImageInfo::Make(4, 5, ct, kPremul_SkAlphaType);
80         result = sContext->writePixels(dstInfo, srcBuffer, 0, {5, 4});
81         REPORTER_ASSERT(reporter, result);
82 
83         memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
84 
85         result = sContext->readPixels(dstInfo, dstBuffer, 0, {5, 4});
86         REPORTER_ASSERT(reporter, result);
87 
88         REPORTER_ASSERT(reporter, DoesFullBufferContainCorrectColor(srcBuffer, dstBuffer, 4, 5));
89 
90     }
91 }
92 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrUploadPixelsTests,reporter,ctxInfo)93 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrUploadPixelsTests, reporter, ctxInfo) {
94     // RGBA
95     basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, GrRenderable::kNo);
96     basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, GrRenderable::kYes);
97 
98     // BGRA
99     basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, GrRenderable::kNo);
100     basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, GrRenderable::kYes);
101 }
102