blob: 3fa9419f89f869b6f0ad81ec9e3a7893220dda6a [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) {
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
Greg Daniel164a9f02016-02-22 09:56:40 -050063 GrSurfaceDesc surfDesc;
64 surfDesc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
Greg Daniel164a9f02016-02-22 09:56:40 -050065 surfDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
66 surfDesc.fWidth = kWidth;
67 surfDesc.fHeight = kHeight;
68 surfDesc.fConfig = config;
69 surfDesc.fSampleCnt = 0;
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040070
71 SkColorType ct;
72 SkAssertResult(GrPixelConfigToColorType(config, &ct));
73
74 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
75 surfDesc, SkBudgeted::kNo,
76 srcBuffer, 0);
Brian Salomon7128fdd2017-05-22 14:00:07 -040077 REPORTER_ASSERT(reporter, proxy);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040078 if (proxy) {
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040079 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(
80 proxy, nullptr);
81
82 SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kOpaque_SkAlphaType);
83
84 bool result = sContext->readPixels(dstInfo, dstBuffer, 0, 0, 0);
85 REPORTER_ASSERT(reporter, result);
Greg Daniel164a9f02016-02-22 09:56:40 -050086 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
87 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -050088 kWidth,
89 kHeight));
90
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040091 dstInfo = SkImageInfo::Make(10, 2, ct, kOpaque_SkAlphaType);
92 result = sContext->writePixels(dstInfo, srcBuffer, 0, 2, 10);
93 REPORTER_ASSERT(reporter, result);
Robert Phillipse78b7252017-04-06 07:59:41 -040094
Greg Daniel164a9f02016-02-22 09:56:40 -050095 memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040096
97 result = sContext->readPixels(dstInfo, dstBuffer, 0, 2, 10);
98 REPORTER_ASSERT(reporter, result);
99
Greg Daniel164a9f02016-02-22 09:56:40 -0500100 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
101 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -0500102 10,
103 2));
Greg Daniel164a9f02016-02-22 09:56:40 -0500104 }
105
106 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400107
108 proxy = GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
109 surfDesc, SkBudgeted::kNo,
110 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