• 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 "SkTypes.h"
11 
12 #include "GrContextFactory.h"
13 #include "GrContextPriv.h"
14 #include "GrSurfaceProxy.h"
15 #include "ProxyUtils.h"
16 #include "SkGr.h"
17 #include "Test.h"
18 #include "TestUtils.h"
19 
20 using sk_gpu_test::GrContextFactory;
21 
basic_texture_test(skiatest::Reporter * reporter,GrContext * context,SkColorType ct,bool renderTarget)22 void basic_texture_test(skiatest::Reporter* reporter, GrContext* context, SkColorType ct,
23                         bool renderTarget) {
24     const int kWidth = 16;
25     const int kHeight = 16;
26     SkAutoTMalloc<GrColor> srcBuffer(kWidth*kHeight);
27     SkAutoTMalloc<GrColor> dstBuffer(kWidth*kHeight);
28 
29     fill_pixel_data(kWidth, kHeight, srcBuffer.get());
30 
31     auto proxy = sk_gpu_test::MakeTextureProxyFromData(context, renderTarget, kWidth, kHeight, ct,
32                                                        kTopLeft_GrSurfaceOrigin, srcBuffer, 0);
33     REPORTER_ASSERT(reporter, proxy);
34     if (proxy) {
35         sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext(proxy);
36 
37         SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
38 
39         bool result = sContext->readPixels(dstInfo, dstBuffer, 0, 0, 0);
40         REPORTER_ASSERT(reporter, result);
41         REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
42                                                                          dstBuffer,
43                                                                          kWidth,
44                                                                          kHeight));
45 
46         dstInfo = SkImageInfo::Make(10, 2, ct, kPremul_SkAlphaType);
47         result = sContext->writePixels(dstInfo, srcBuffer, 0, 2, 10);
48         REPORTER_ASSERT(reporter, result);
49 
50         memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
51 
52         result = sContext->readPixels(dstInfo, dstBuffer, 0, 2, 10);
53         REPORTER_ASSERT(reporter, result);
54 
55         REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
56                                                                          dstBuffer,
57                                                                          10,
58                                                                          2));
59     }
60 
61     proxy = sk_gpu_test::MakeTextureProxyFromData(context, renderTarget, kWidth, kHeight, ct,
62                                                   kBottomLeft_GrSurfaceOrigin, srcBuffer, 0);
63     REPORTER_ASSERT(reporter, proxy);
64     if (proxy) {
65         sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext(proxy);
66 
67         SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
68 
69         bool result = sContext->readPixels(dstInfo, dstBuffer, 0, 0, 0);
70         REPORTER_ASSERT(reporter, result);
71         REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
72                                                                          dstBuffer,
73                                                                          kWidth,
74                                                                          kHeight));
75 
76         dstInfo = SkImageInfo::Make(4, 5, ct, kPremul_SkAlphaType);
77         result = sContext->writePixels(dstInfo, srcBuffer, 0, 5, 4);
78         REPORTER_ASSERT(reporter, result);
79 
80         memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
81 
82         result = sContext->readPixels(dstInfo, dstBuffer, 0, 5, 4);
83         REPORTER_ASSERT(reporter, result);
84 
85         REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
86                                                                          dstBuffer,
87                                                                          4,
88                                                                          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, false);
96     basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, true);
97 
98     // BGRA
99     basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, false);
100     basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, true);
101 }
102