blob: 417457e1a267f34ca282738f580adf65aa6fd73c [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"
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040016#include "GrSurfaceProxy.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050017#include "GrTest.h"
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040018#include "SkGr.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050019#include "Test.h"
20#include "vk/GrVkGpu.h"
21
kkinnunen2ae4b2e2016-03-31 08:08:20 -070022using sk_gpu_test::GrContextFactory;
Greg Daniel164a9f02016-02-22 09:56:40 -050023
24void fill_pixel_data(int width, int height, GrColor* data) {
25
26 // build red-green gradient
27 for (int j = 0; j < height; ++j) {
28 for (int i = 0; i < width; ++i) {
29 unsigned int red = (unsigned int)(256.f*(i / (float)width));
30 unsigned int green = (unsigned int)(256.f*(j / (float)height));
31 data[i + j*width] = GrColorPackRGBA(red - (red>>8), green - (green>>8), 0xff, 0xff);
32 }
33 }
34}
35
36bool does_full_buffer_contain_correct_color(GrColor* srcBuffer,
37 GrColor* dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -050038 int width,
39 int height) {
40 GrColor* srcPtr = srcBuffer;
41 GrColor* dstPtr = dstBuffer;
42 for (int j = 0; j < height; ++j) {
43 for (int i = 0; i < width; ++i) {
44 if (srcPtr[i] != dstPtr[i]) {
45 return false;
46 }
47 }
48 srcPtr += width;
49 dstPtr += width;
50 }
51 return true;
52}
53
54void basic_texture_test(skiatest::Reporter* reporter, GrContext* context, GrPixelConfig config,
55 bool renderTarget, bool linearTiling) {
Greg Daniel164a9f02016-02-22 09:56:40 -050056 const int kWidth = 16;
57 const int kHeight = 16;
58 SkAutoTMalloc<GrColor> srcBuffer(kWidth*kHeight);
59 SkAutoTMalloc<GrColor> dstBuffer(kWidth*kHeight);
60
61 fill_pixel_data(kWidth, kHeight, srcBuffer.get());
62
63 const GrVkCaps* caps = reinterpret_cast<const GrVkCaps*>(context->caps());
64
65 bool canCreate = true;
66 // the expectation is that the given config is texturable/renderable with optimal tiling
67 // but may not be with linear tiling
68 if (linearTiling) {
egdaniela95d46b2016-08-15 08:06:29 -070069 if (!caps->isConfigTexturableLinearly(config) ||
Greg Daniel164a9f02016-02-22 09:56:40 -050070 (renderTarget && !caps->isConfigRenderableLinearly(config, false))) {
71 canCreate = false;
72 }
73 }
74
75 GrSurfaceDesc surfDesc;
76 surfDesc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
77 if (linearTiling) {
78 surfDesc.fFlags |= kZeroCopy_GrSurfaceFlag;
79 }
80 surfDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
81 surfDesc.fWidth = kWidth;
82 surfDesc.fHeight = kHeight;
83 surfDesc.fConfig = config;
84 surfDesc.fSampleCnt = 0;
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040085
86 SkColorType ct;
87 SkAssertResult(GrPixelConfigToColorType(config, &ct));
88
89 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
90 surfDesc, SkBudgeted::kNo,
91 srcBuffer, 0);
92
93 if (proxy) {
Greg Daniel164a9f02016-02-22 09:56:40 -050094 REPORTER_ASSERT(reporter, canCreate);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040095
96 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(
97 proxy, nullptr);
98
99 SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kOpaque_SkAlphaType);
100
101 bool result = sContext->readPixels(dstInfo, dstBuffer, 0, 0, 0);
102 REPORTER_ASSERT(reporter, result);
Greg Daniel164a9f02016-02-22 09:56:40 -0500103 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
104 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -0500105 kWidth,
106 kHeight));
107
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400108 dstInfo = SkImageInfo::Make(10, 2, ct, kOpaque_SkAlphaType);
109 result = sContext->writePixels(dstInfo, srcBuffer, 0, 2, 10);
110 REPORTER_ASSERT(reporter, result);
Robert Phillipse78b7252017-04-06 07:59:41 -0400111
Greg Daniel164a9f02016-02-22 09:56:40 -0500112 memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400113
114 result = sContext->readPixels(dstInfo, dstBuffer, 0, 2, 10);
115 REPORTER_ASSERT(reporter, result);
116
Greg Daniel164a9f02016-02-22 09:56:40 -0500117 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
118 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -0500119 10,
120 2));
Greg Daniel164a9f02016-02-22 09:56:40 -0500121 } else {
122 REPORTER_ASSERT(reporter, !canCreate);
123 }
124
125 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400126
127 proxy = GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
128 surfDesc, SkBudgeted::kNo,
129 srcBuffer, 0);
130 if (proxy) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500131 REPORTER_ASSERT(reporter, canCreate);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400132
133 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(
134 proxy, nullptr);
135
136 SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kOpaque_SkAlphaType);
137
138 bool result = sContext->readPixels(dstInfo, dstBuffer, 0, 0, 0);
139 REPORTER_ASSERT(reporter, result);
Greg Daniel164a9f02016-02-22 09:56:40 -0500140 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
141 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -0500142 kWidth,
143 kHeight));
144
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400145 dstInfo = SkImageInfo::Make(4, 5, ct, kOpaque_SkAlphaType);
146 result = sContext->writePixels(dstInfo, srcBuffer, 0, 5, 4);
147 REPORTER_ASSERT(reporter, result);
Robert Phillipse78b7252017-04-06 07:59:41 -0400148
Greg Daniel164a9f02016-02-22 09:56:40 -0500149 memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400150
151 result = sContext->readPixels(dstInfo, dstBuffer, 0, 5, 4);
152 REPORTER_ASSERT(reporter, result);
153
Greg Daniel164a9f02016-02-22 09:56:40 -0500154 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
155 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -0500156 4,
157 5));
158
Greg Daniel164a9f02016-02-22 09:56:40 -0500159 } else {
160 REPORTER_ASSERT(reporter, !canCreate);
161 }
162}
163
bsalomondc0fcd42016-04-11 14:21:33 -0700164DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkUploadPixelsTests, reporter, ctxInfo) {
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400165 // RGBA
bsalomon8b7451a2016-05-11 06:33:06 -0700166 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, false, false);
167 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, true, false);
168 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, false, true);
169 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, true, true);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400170
171 // BGRA
bsalomon8b7451a2016-05-11 06:33:06 -0700172 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, false, false);
173 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, true, false);
174 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, false, true);
175 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, true, true);
Greg Daniel164a9f02016-02-22 09:56:40 -0500176}
177
178#endif