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/GrSurfaceProxy.h"
14 #include "src/gpu/SkGr.h"
15 #include "tests/Test.h"
16 #include "tests/TestUtils.h"
17 #include "tools/gpu/GrContextFactory.h"
18 #include "tools/gpu/ProxyUtils.h"
19
20 using sk_gpu_test::GrContextFactory;
21
basic_texture_test(skiatest::Reporter * reporter,GrContext * context,SkColorType ct,GrRenderable renderable)22 void basic_texture_test(skiatest::Reporter* reporter, GrContext* context, SkColorType ct,
23 GrRenderable renderable) {
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, renderable, kWidth, kHeight, ct,
32 kPremul_SkAlphaType,
33 kTopLeft_GrSurfaceOrigin, srcBuffer, 0);
34 REPORTER_ASSERT(reporter, proxy);
35 if (proxy) {
36 sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext(
37 proxy, SkColorTypeToGrColorType(ct), kPremul_SkAlphaType);
38
39 SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
40
41 bool result = sContext->readPixels(dstInfo, dstBuffer, 0, {0, 0});
42 REPORTER_ASSERT(reporter, result);
43 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
44 dstBuffer,
45 kWidth,
46 kHeight));
47
48 dstInfo = SkImageInfo::Make(10, 2, ct, kPremul_SkAlphaType);
49 result = sContext->writePixels(dstInfo, srcBuffer, 0, {2, 10});
50 REPORTER_ASSERT(reporter, result);
51
52 memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
53
54 result = sContext->readPixels(dstInfo, dstBuffer, 0, {2, 10});
55 REPORTER_ASSERT(reporter, result);
56
57 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
58 dstBuffer,
59 10,
60 2));
61 }
62
63 proxy = sk_gpu_test::MakeTextureProxyFromData(context, renderable, kWidth, kHeight, ct,
64 kPremul_SkAlphaType, kBottomLeft_GrSurfaceOrigin,
65 srcBuffer, 0);
66 REPORTER_ASSERT(reporter, proxy);
67 if (proxy) {
68 sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext(
69 proxy, SkColorTypeToGrColorType(ct), kPremul_SkAlphaType);
70
71 SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
72
73 bool result = sContext->readPixels(dstInfo, dstBuffer, 0, {0, 0});
74 REPORTER_ASSERT(reporter, result);
75 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
76 dstBuffer,
77 kWidth,
78 kHeight));
79
80 dstInfo = SkImageInfo::Make(4, 5, ct, kPremul_SkAlphaType);
81 result = sContext->writePixels(dstInfo, srcBuffer, 0, {5, 4});
82 REPORTER_ASSERT(reporter, result);
83
84 memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
85
86 result = sContext->readPixels(dstInfo, dstBuffer, 0, {5, 4});
87 REPORTER_ASSERT(reporter, result);
88
89 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
90 dstBuffer,
91 4,
92 5));
93
94 }
95 }
96
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrUploadPixelsTests,reporter,ctxInfo)97 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrUploadPixelsTests, reporter, ctxInfo) {
98 // RGBA
99 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, GrRenderable::kNo);
100 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, GrRenderable::kYes);
101
102 // BGRA
103 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, GrRenderable::kNo);
104 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, GrRenderable::kYes);
105 }
106