blob: 27d30bda65454eacf88efa09044248080622a944 [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"
Brian Salomon58389b92018-03-07 13:01:25 -050018#include "ProxyUtils.h"
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040019#include "SkGr.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050020#include "Test.h"
21#include "vk/GrVkGpu.h"
22
kkinnunen2ae4b2e2016-03-31 08:08:20 -070023using sk_gpu_test::GrContextFactory;
Greg Daniel164a9f02016-02-22 09:56:40 -050024
25void fill_pixel_data(int width, int height, GrColor* data) {
26
27 // build red-green gradient
28 for (int j = 0; j < height; ++j) {
29 for (int i = 0; i < width; ++i) {
30 unsigned int red = (unsigned int)(256.f*(i / (float)width));
31 unsigned int green = (unsigned int)(256.f*(j / (float)height));
32 data[i + j*width] = GrColorPackRGBA(red - (red>>8), green - (green>>8), 0xff, 0xff);
33 }
34 }
35}
36
37bool does_full_buffer_contain_correct_color(GrColor* srcBuffer,
38 GrColor* dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -050039 int width,
40 int height) {
41 GrColor* srcPtr = srcBuffer;
42 GrColor* dstPtr = dstBuffer;
43 for (int j = 0; j < height; ++j) {
44 for (int i = 0; i < width; ++i) {
45 if (srcPtr[i] != dstPtr[i]) {
46 return false;
47 }
48 }
49 srcPtr += width;
50 dstPtr += width;
51 }
52 return true;
53}
54
Brian Salomon58389b92018-03-07 13:01:25 -050055void basic_texture_test(skiatest::Reporter* reporter, GrContext* context, SkColorType ct,
Brian Salomon7128fdd2017-05-22 14:00:07 -040056 bool renderTarget) {
Greg Daniel164a9f02016-02-22 09:56:40 -050057 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
Brian Salomon58389b92018-03-07 13:01:25 -050064 auto proxy = sk_gpu_test::MakeTextureProxyFromData(context, renderTarget, kWidth, kHeight, ct,
65 kTopLeft_GrSurfaceOrigin, srcBuffer, 0);
Brian Salomon7128fdd2017-05-22 14:00:07 -040066 REPORTER_ASSERT(reporter, proxy);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040067 if (proxy) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050068 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(proxy);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040069
Brian Salomon5fba7ad2018-03-22 10:01:16 -040070 SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040071
72 bool result = sContext->readPixels(dstInfo, dstBuffer, 0, 0, 0);
73 REPORTER_ASSERT(reporter, result);
Greg Daniel164a9f02016-02-22 09:56:40 -050074 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
75 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -050076 kWidth,
77 kHeight));
78
Brian Salomon5fba7ad2018-03-22 10:01:16 -040079 dstInfo = SkImageInfo::Make(10, 2, ct, kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040080 result = sContext->writePixels(dstInfo, srcBuffer, 0, 2, 10);
81 REPORTER_ASSERT(reporter, result);
Robert Phillipse78b7252017-04-06 07:59:41 -040082
Greg Daniel164a9f02016-02-22 09:56:40 -050083 memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040084
85 result = sContext->readPixels(dstInfo, dstBuffer, 0, 2, 10);
86 REPORTER_ASSERT(reporter, result);
87
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 10,
91 2));
Greg Daniel164a9f02016-02-22 09:56:40 -050092 }
93
Brian Salomon58389b92018-03-07 13:01:25 -050094 proxy = sk_gpu_test::MakeTextureProxyFromData(context, renderTarget, kWidth, kHeight, ct,
95 kBottomLeft_GrSurfaceOrigin, srcBuffer, 0);
Brian Salomon7128fdd2017-05-22 14:00:07 -040096 REPORTER_ASSERT(reporter, proxy);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040097 if (proxy) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050098 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(proxy);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040099
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400100 SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400101
102 bool result = sContext->readPixels(dstInfo, dstBuffer, 0, 0, 0);
103 REPORTER_ASSERT(reporter, result);
Greg Daniel164a9f02016-02-22 09:56:40 -0500104 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
105 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -0500106 kWidth,
107 kHeight));
108
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400109 dstInfo = SkImageInfo::Make(4, 5, ct, kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400110 result = sContext->writePixels(dstInfo, srcBuffer, 0, 5, 4);
111 REPORTER_ASSERT(reporter, result);
Robert Phillipse78b7252017-04-06 07:59:41 -0400112
Greg Daniel164a9f02016-02-22 09:56:40 -0500113 memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400114
115 result = sContext->readPixels(dstInfo, dstBuffer, 0, 5, 4);
116 REPORTER_ASSERT(reporter, result);
117
Greg Daniel164a9f02016-02-22 09:56:40 -0500118 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
119 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -0500120 4,
121 5));
122
Greg Daniel164a9f02016-02-22 09:56:40 -0500123 }
124}
125
bsalomondc0fcd42016-04-11 14:21:33 -0700126DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkUploadPixelsTests, reporter, ctxInfo) {
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400127 // RGBA
Brian Salomon58389b92018-03-07 13:01:25 -0500128 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, false);
129 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, true);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400130
131 // BGRA
Brian Salomon58389b92018-03-07 13:01:25 -0500132 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, false);
133 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, true);
Greg Daniel164a9f02016-02-22 09:56:40 -0500134}
135
136#endif