blob: a1294cd2dc4ee5e8436f63ed4d230badedbd7aad [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050011
Robert Phillips6d344c32020-07-06 10:56:46 -040012#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040014#include "src/gpu/GrImageInfo.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040015#include "src/gpu/GrSurfaceContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040016#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/SkGr.h"
18#include "tests/Test.h"
19#include "tests/TestUtils.h"
20#include "tools/gpu/GrContextFactory.h"
21#include "tools/gpu/ProxyUtils.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050022
kkinnunen2ae4b2e2016-03-31 08:08:20 -070023using sk_gpu_test::GrContextFactory;
Greg Daniel164a9f02016-02-22 09:56:40 -050024
Robert Phillips00f78de2020-07-01 16:09:43 -040025void basic_texture_test(skiatest::Reporter* reporter, GrDirectContext* context, SkColorType ct,
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040026 GrRenderable renderable) {
Greg Daniel164a9f02016-02-22 09:56:40 -050027 const int kWidth = 16;
28 const int kHeight = 16;
29 SkAutoTMalloc<GrColor> srcBuffer(kWidth*kHeight);
30 SkAutoTMalloc<GrColor> dstBuffer(kWidth*kHeight);
31
Brian Salomon28a8f282019-10-24 20:07:39 -040032 FillPixelData(kWidth, kHeight, srcBuffer.get());
Greg Daniel164a9f02016-02-22 09:56:40 -050033
Brian Salomon4eda7102019-10-21 15:04:52 -040034 auto grCT = SkColorTypeToGrColorType(ct);
35 auto proxy = sk_gpu_test::MakeTextureProxyFromData(
36 context, renderable, kTopLeft_GrSurfaceOrigin,
37 {grCT, kPremul_SkAlphaType, nullptr, kWidth, kHeight}, srcBuffer, 0);
Brian Salomon7128fdd2017-05-22 14:00:07 -040038 REPORTER_ASSERT(reporter, proxy);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040039 if (proxy) {
Greg Daniel3912a4b2020-01-14 09:56:04 -050040 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(), grCT);
41 GrSurfaceProxyView view(proxy, kTopLeft_GrSurfaceOrigin, swizzle);
42 auto sContext = GrSurfaceContext::Make(context, std::move(view), grCT, kPremul_SkAlphaType,
43 nullptr);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040044
Brian Salomon5fba7ad2018-03-22 10:01:16 -040045 SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040046
Brian Salomon1d435302019-07-01 13:05:28 -040047 bool result = sContext->readPixels(dstInfo, dstBuffer, 0, {0, 0});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040048 REPORTER_ASSERT(reporter, result);
Brian Salomon28a8f282019-10-24 20:07:39 -040049 REPORTER_ASSERT(reporter,
50 DoesFullBufferContainCorrectColor(srcBuffer, dstBuffer, kWidth, kHeight));
Greg Daniel164a9f02016-02-22 09:56:40 -050051
Brian Salomon5fba7ad2018-03-22 10:01:16 -040052 dstInfo = SkImageInfo::Make(10, 2, ct, kPremul_SkAlphaType);
Brian Salomon1d435302019-07-01 13:05:28 -040053 result = sContext->writePixels(dstInfo, srcBuffer, 0, {2, 10});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040054 REPORTER_ASSERT(reporter, result);
Robert Phillipse78b7252017-04-06 07:59:41 -040055
Greg Daniel164a9f02016-02-22 09:56:40 -050056 memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040057
Brian Salomon1d435302019-07-01 13:05:28 -040058 result = sContext->readPixels(dstInfo, dstBuffer, 0, {2, 10});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040059 REPORTER_ASSERT(reporter, result);
60
Brian Salomon28a8f282019-10-24 20:07:39 -040061 REPORTER_ASSERT(reporter, DoesFullBufferContainCorrectColor(srcBuffer, dstBuffer, 10, 2));
Greg Daniel164a9f02016-02-22 09:56:40 -050062 }
63
Brian Salomon4eda7102019-10-21 15:04:52 -040064 proxy = sk_gpu_test::MakeTextureProxyFromData(
65 context, renderable, kBottomLeft_GrSurfaceOrigin,
66 {grCT, kPremul_SkAlphaType, nullptr, kWidth, kHeight}, srcBuffer, 0);
Brian Salomon7128fdd2017-05-22 14:00:07 -040067 REPORTER_ASSERT(reporter, proxy);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040068 if (proxy) {
Greg Daniel3912a4b2020-01-14 09:56:04 -050069 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(), grCT);
70 GrSurfaceProxyView view(proxy, kBottomLeft_GrSurfaceOrigin, swizzle);
71 auto sContext = GrSurfaceContext::Make(context, std::move(view), grCT, kPremul_SkAlphaType,
72 nullptr);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040073
Brian Salomon5fba7ad2018-03-22 10:01:16 -040074 SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040075
Brian Salomon1d435302019-07-01 13:05:28 -040076 bool result = sContext->readPixels(dstInfo, dstBuffer, 0, {0, 0});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040077 REPORTER_ASSERT(reporter, result);
Brian Salomon28a8f282019-10-24 20:07:39 -040078 REPORTER_ASSERT(reporter,
79 DoesFullBufferContainCorrectColor(srcBuffer, dstBuffer, kWidth, kHeight));
Greg Daniel164a9f02016-02-22 09:56:40 -050080
Brian Salomon5fba7ad2018-03-22 10:01:16 -040081 dstInfo = SkImageInfo::Make(4, 5, ct, kPremul_SkAlphaType);
Brian Salomon1d435302019-07-01 13:05:28 -040082 result = sContext->writePixels(dstInfo, srcBuffer, 0, {5, 4});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040083 REPORTER_ASSERT(reporter, result);
Robert Phillipse78b7252017-04-06 07:59:41 -040084
Greg Daniel164a9f02016-02-22 09:56:40 -050085 memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040086
Brian Salomon1d435302019-07-01 13:05:28 -040087 result = sContext->readPixels(dstInfo, dstBuffer, 0, {5, 4});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040088 REPORTER_ASSERT(reporter, result);
89
Brian Salomon28a8f282019-10-24 20:07:39 -040090 REPORTER_ASSERT(reporter, DoesFullBufferContainCorrectColor(srcBuffer, dstBuffer, 4, 5));
Greg Daniel164a9f02016-02-22 09:56:40 -050091
Greg Daniel164a9f02016-02-22 09:56:40 -050092 }
93}
94
Timothy Lianga8046af2018-07-19 09:58:00 -040095DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrUploadPixelsTests, reporter, ctxInfo) {
Robert Phillips00f78de2020-07-01 16:09:43 -040096 auto direct = ctxInfo.directContext();
97
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040098 // RGBA
Robert Phillips00f78de2020-07-01 16:09:43 -040099 basic_texture_test(reporter, direct, kRGBA_8888_SkColorType, GrRenderable::kNo);
100 basic_texture_test(reporter, direct, kRGBA_8888_SkColorType, GrRenderable::kYes);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400101
102 // BGRA
Robert Phillips00f78de2020-07-01 16:09:43 -0400103 basic_texture_test(reporter, direct, kBGRA_8888_SkColorType, GrRenderable::kNo);
104 basic_texture_test(reporter, direct, kBGRA_8888_SkColorType, GrRenderable::kYes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500105}