blob: 50fc84fda805996960b90c7e1061c0ebf479ecf0 [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
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#if SK_SUPPORT_GPU && SK_ALLOW_STATIC_GLOBAL_INITIALIZERS && defined(SK_VULKAN)
13
14#include "GrContextFactory.h"
Robert Phillipse78b7252017-04-06 07:59:41 -040015#include "GrContextPriv.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050016#include "GrTest.h"
17#include "Test.h"
18#include "vk/GrVkGpu.h"
19
kkinnunen2ae4b2e2016-03-31 08:08:20 -070020using sk_gpu_test::GrContextFactory;
Greg Daniel164a9f02016-02-22 09:56:40 -050021
22void fill_pixel_data(int width, int height, GrColor* data) {
23
24 // build red-green gradient
25 for (int j = 0; j < height; ++j) {
26 for (int i = 0; i < width; ++i) {
27 unsigned int red = (unsigned int)(256.f*(i / (float)width));
28 unsigned int green = (unsigned int)(256.f*(j / (float)height));
29 data[i + j*width] = GrColorPackRGBA(red - (red>>8), green - (green>>8), 0xff, 0xff);
30 }
31 }
32}
33
34bool does_full_buffer_contain_correct_color(GrColor* srcBuffer,
35 GrColor* dstBuffer,
36 GrPixelConfig config,
37 int width,
38 int height) {
39 GrColor* srcPtr = srcBuffer;
40 GrColor* dstPtr = dstBuffer;
41 for (int j = 0; j < height; ++j) {
42 for (int i = 0; i < width; ++i) {
43 if (srcPtr[i] != dstPtr[i]) {
44 return false;
45 }
46 }
47 srcPtr += width;
48 dstPtr += width;
49 }
50 return true;
51}
52
53void basic_texture_test(skiatest::Reporter* reporter, GrContext* context, GrPixelConfig config,
54 bool renderTarget, bool linearTiling) {
55 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->getGpu());
Greg Daniel164a9f02016-02-22 09:56:40 -050056
57 const int kWidth = 16;
58 const int kHeight = 16;
59 SkAutoTMalloc<GrColor> srcBuffer(kWidth*kHeight);
60 SkAutoTMalloc<GrColor> dstBuffer(kWidth*kHeight);
61
62 fill_pixel_data(kWidth, kHeight, srcBuffer.get());
63
64 const GrVkCaps* caps = reinterpret_cast<const GrVkCaps*>(context->caps());
65
66 bool canCreate = true;
67 // the expectation is that the given config is texturable/renderable with optimal tiling
68 // but may not be with linear tiling
69 if (linearTiling) {
egdaniela95d46b2016-08-15 08:06:29 -070070 if (!caps->isConfigTexturableLinearly(config) ||
Greg Daniel164a9f02016-02-22 09:56:40 -050071 (renderTarget && !caps->isConfigRenderableLinearly(config, false))) {
72 canCreate = false;
73 }
74 }
75
76 GrSurfaceDesc surfDesc;
77 surfDesc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
78 if (linearTiling) {
79 surfDesc.fFlags |= kZeroCopy_GrSurfaceFlag;
80 }
81 surfDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
82 surfDesc.fWidth = kWidth;
83 surfDesc.fHeight = kHeight;
84 surfDesc.fConfig = config;
85 surfDesc.fSampleCnt = 0;
Robert Phillipse78b7252017-04-06 07:59:41 -040086 sk_sp<GrTexture> tex0(gpu->createTexture(surfDesc, SkBudgeted::kNo, srcBuffer, 0));
Greg Daniel164a9f02016-02-22 09:56:40 -050087 if (tex0) {
88 REPORTER_ASSERT(reporter, canCreate);
Robert Phillipse78b7252017-04-06 07:59:41 -040089 gpu->readPixels(tex0.get(), 0, 0, kWidth, kHeight, config, dstBuffer, 0);
Greg Daniel164a9f02016-02-22 09:56:40 -050090 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
91 dstBuffer,
92 config,
93 kWidth,
94 kHeight));
95
Robert Phillipse78b7252017-04-06 07:59:41 -040096 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(tex0);
97
98 bool success = context->contextPriv().writeSurfacePixels(proxy.get(), nullptr,
99 2, 10, 10, 2,
100 config, nullptr, srcBuffer, 0);
101 REPORTER_ASSERT(reporter, success);
102
Greg Daniel164a9f02016-02-22 09:56:40 -0500103 memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
Robert Phillipse78b7252017-04-06 07:59:41 -0400104 gpu->readPixels(tex0.get(), 2, 10, 10, 2, config, dstBuffer, 0);
Greg Daniel164a9f02016-02-22 09:56:40 -0500105 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
106 dstBuffer,
107 config,
108 10,
109 2));
Greg Daniel164a9f02016-02-22 09:56:40 -0500110 } else {
111 REPORTER_ASSERT(reporter, !canCreate);
112 }
113
114 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Robert Phillipse78b7252017-04-06 07:59:41 -0400115 sk_sp<GrTexture> tex1(gpu->createTexture(surfDesc, SkBudgeted::kNo, srcBuffer, 0));
Greg Daniel164a9f02016-02-22 09:56:40 -0500116 if (tex1) {
117 REPORTER_ASSERT(reporter, canCreate);
Robert Phillipse78b7252017-04-06 07:59:41 -0400118 gpu->readPixels(tex1.get(), 0, 0, kWidth, kHeight, config, dstBuffer, 0);
Greg Daniel164a9f02016-02-22 09:56:40 -0500119 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
120 dstBuffer,
121 config,
122 kWidth,
123 kHeight));
124
Robert Phillipse78b7252017-04-06 07:59:41 -0400125 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(tex1);
126
127 bool success = context->contextPriv().writeSurfacePixels(proxy.get(), nullptr,
128 5, 4, 4, 5, config, nullptr,
129 srcBuffer, 0);
130 REPORTER_ASSERT(reporter, success);
131
Greg Daniel164a9f02016-02-22 09:56:40 -0500132 memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
Robert Phillipse78b7252017-04-06 07:59:41 -0400133 gpu->readPixels(tex1.get(), 5, 4, 4, 5, config, dstBuffer, 0);
Greg Daniel164a9f02016-02-22 09:56:40 -0500134 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
135 dstBuffer,
136 config,
137 4,
138 5));
139
Greg Daniel164a9f02016-02-22 09:56:40 -0500140 } else {
141 REPORTER_ASSERT(reporter, !canCreate);
142 }
143}
144
bsalomondc0fcd42016-04-11 14:21:33 -0700145DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkUploadPixelsTests, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700146 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, false, false);
147 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, true, false);
148 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, false, true);
149 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, true, true);
150 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, false, false);
151 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, true, false);
152 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, false, true);
153 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, true, true);
Greg Daniel164a9f02016-02-22 09:56:40 -0500154}
155
156#endif