blob: d05ca779f35cd20f3815246f87516658637ab54a [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
Jim Van Verth91589312017-06-22 12:52:46 -040012#if SK_SUPPORT_GPU && defined(SK_VULKAN)
Greg Daniel164a9f02016-02-22 09:56:40 -050013
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,
Brian Salomon7128fdd2017-05-22 14:00:07 -040055 bool renderTarget) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050056 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
57
Greg Daniel164a9f02016-02-22 09:56:40 -050058 const int kWidth = 16;
59 const int kHeight = 16;
60 SkAutoTMalloc<GrColor> srcBuffer(kWidth*kHeight);
61 SkAutoTMalloc<GrColor> dstBuffer(kWidth*kHeight);
62
63 fill_pixel_data(kWidth, kHeight, srcBuffer.get());
64
Greg Daniel164a9f02016-02-22 09:56:40 -050065 GrSurfaceDesc surfDesc;
66 surfDesc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
Greg Daniel164a9f02016-02-22 09:56:40 -050067 surfDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
68 surfDesc.fWidth = kWidth;
69 surfDesc.fHeight = kHeight;
70 surfDesc.fConfig = config;
71 surfDesc.fSampleCnt = 0;
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040072
73 SkColorType ct;
74 SkAssertResult(GrPixelConfigToColorType(config, &ct));
75
Robert Phillips1afd4cd2018-01-08 13:40:32 -050076 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(proxyProvider,
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040077 surfDesc, SkBudgeted::kNo,
78 srcBuffer, 0);
Brian Salomon7128fdd2017-05-22 14:00:07 -040079 REPORTER_ASSERT(reporter, proxy);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040080 if (proxy) {
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040081 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(
82 proxy, nullptr);
83
84 SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kOpaque_SkAlphaType);
85
86 bool result = sContext->readPixels(dstInfo, dstBuffer, 0, 0, 0);
87 REPORTER_ASSERT(reporter, result);
Greg Daniel164a9f02016-02-22 09:56:40 -050088 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
89 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -050090 kWidth,
91 kHeight));
92
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040093 dstInfo = SkImageInfo::Make(10, 2, ct, kOpaque_SkAlphaType);
94 result = sContext->writePixels(dstInfo, srcBuffer, 0, 2, 10);
95 REPORTER_ASSERT(reporter, result);
Robert Phillipse78b7252017-04-06 07:59:41 -040096
Greg Daniel164a9f02016-02-22 09:56:40 -050097 memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040098
99 result = sContext->readPixels(dstInfo, dstBuffer, 0, 2, 10);
100 REPORTER_ASSERT(reporter, result);
101
Greg Daniel164a9f02016-02-22 09:56:40 -0500102 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
103 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -0500104 10,
105 2));
Greg Daniel164a9f02016-02-22 09:56:40 -0500106 }
107
108 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400109
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500110 proxy = GrSurfaceProxy::MakeDeferred(proxyProvider, surfDesc, SkBudgeted::kNo, srcBuffer, 0);
Brian Salomon7128fdd2017-05-22 14:00:07 -0400111 REPORTER_ASSERT(reporter, proxy);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400112 if (proxy) {
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400113 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(
114 proxy, nullptr);
115
116 SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kOpaque_SkAlphaType);
117
118 bool result = sContext->readPixels(dstInfo, dstBuffer, 0, 0, 0);
119 REPORTER_ASSERT(reporter, result);
Greg Daniel164a9f02016-02-22 09:56:40 -0500120 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
121 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -0500122 kWidth,
123 kHeight));
124
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400125 dstInfo = SkImageInfo::Make(4, 5, ct, kOpaque_SkAlphaType);
126 result = sContext->writePixels(dstInfo, srcBuffer, 0, 5, 4);
127 REPORTER_ASSERT(reporter, result);
Robert Phillipse78b7252017-04-06 07:59:41 -0400128
Greg Daniel164a9f02016-02-22 09:56:40 -0500129 memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400130
131 result = sContext->readPixels(dstInfo, dstBuffer, 0, 5, 4);
132 REPORTER_ASSERT(reporter, result);
133
Greg Daniel164a9f02016-02-22 09:56:40 -0500134 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
135 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -0500136 4,
137 5));
138
Greg Daniel164a9f02016-02-22 09:56:40 -0500139 }
140}
141
bsalomondc0fcd42016-04-11 14:21:33 -0700142DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkUploadPixelsTests, reporter, ctxInfo) {
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400143 // RGBA
Brian Salomon7128fdd2017-05-22 14:00:07 -0400144 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, false);
145 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, true);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400146
147 // BGRA
Brian Salomon7128fdd2017-05-22 14:00:07 -0400148 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, false);
149 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, true);
Greg Daniel164a9f02016-02-22 09:56:40 -0500150}
151
152#endif