blob: 299693ec4477fed9e2dd73e5478a8bd32130a267 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040013#include "src/gpu/GrImageInfo.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040014#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/SkGr.h"
16#include "tests/Test.h"
17#include "tests/TestUtils.h"
18#include "tools/gpu/GrContextFactory.h"
19#include "tools/gpu/ProxyUtils.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050020
kkinnunen2ae4b2e2016-03-31 08:08:20 -070021using sk_gpu_test::GrContextFactory;
Greg Daniel164a9f02016-02-22 09:56:40 -050022
Brian Salomon58389b92018-03-07 13:01:25 -050023void basic_texture_test(skiatest::Reporter* reporter, GrContext* context, SkColorType ct,
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040024 GrRenderable renderable) {
Greg Daniel164a9f02016-02-22 09:56:40 -050025 const int kWidth = 16;
26 const int kHeight = 16;
27 SkAutoTMalloc<GrColor> srcBuffer(kWidth*kHeight);
28 SkAutoTMalloc<GrColor> dstBuffer(kWidth*kHeight);
29
Brian Salomon28a8f282019-10-24 20:07:39 -040030 FillPixelData(kWidth, kHeight, srcBuffer.get());
Greg Daniel164a9f02016-02-22 09:56:40 -050031
Brian Salomon4eda7102019-10-21 15:04:52 -040032 auto grCT = SkColorTypeToGrColorType(ct);
33 auto proxy = sk_gpu_test::MakeTextureProxyFromData(
34 context, renderable, kTopLeft_GrSurfaceOrigin,
35 {grCT, kPremul_SkAlphaType, nullptr, kWidth, kHeight}, srcBuffer, 0);
Brian Salomon7128fdd2017-05-22 14:00:07 -040036 REPORTER_ASSERT(reporter, proxy);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040037 if (proxy) {
Greg Daniel3912a4b2020-01-14 09:56:04 -050038 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(), grCT);
39 GrSurfaceProxyView view(proxy, kTopLeft_GrSurfaceOrigin, swizzle);
40 auto sContext = GrSurfaceContext::Make(context, std::move(view), grCT, kPremul_SkAlphaType,
41 nullptr);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040042
Brian Salomon5fba7ad2018-03-22 10:01:16 -040043 SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040044
Brian Salomon1d435302019-07-01 13:05:28 -040045 bool result = sContext->readPixels(dstInfo, dstBuffer, 0, {0, 0});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040046 REPORTER_ASSERT(reporter, result);
Brian Salomon28a8f282019-10-24 20:07:39 -040047 REPORTER_ASSERT(reporter,
48 DoesFullBufferContainCorrectColor(srcBuffer, dstBuffer, kWidth, kHeight));
Greg Daniel164a9f02016-02-22 09:56:40 -050049
Brian Salomon5fba7ad2018-03-22 10:01:16 -040050 dstInfo = SkImageInfo::Make(10, 2, ct, kPremul_SkAlphaType);
Brian Salomon1d435302019-07-01 13:05:28 -040051 result = sContext->writePixels(dstInfo, srcBuffer, 0, {2, 10});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040052 REPORTER_ASSERT(reporter, result);
Robert Phillipse78b7252017-04-06 07:59:41 -040053
Greg Daniel164a9f02016-02-22 09:56:40 -050054 memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040055
Brian Salomon1d435302019-07-01 13:05:28 -040056 result = sContext->readPixels(dstInfo, dstBuffer, 0, {2, 10});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040057 REPORTER_ASSERT(reporter, result);
58
Brian Salomon28a8f282019-10-24 20:07:39 -040059 REPORTER_ASSERT(reporter, DoesFullBufferContainCorrectColor(srcBuffer, dstBuffer, 10, 2));
Greg Daniel164a9f02016-02-22 09:56:40 -050060 }
61
Brian Salomon4eda7102019-10-21 15:04:52 -040062 proxy = sk_gpu_test::MakeTextureProxyFromData(
63 context, renderable, kBottomLeft_GrSurfaceOrigin,
64 {grCT, kPremul_SkAlphaType, nullptr, kWidth, kHeight}, srcBuffer, 0);
Brian Salomon7128fdd2017-05-22 14:00:07 -040065 REPORTER_ASSERT(reporter, proxy);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040066 if (proxy) {
Greg Daniel3912a4b2020-01-14 09:56:04 -050067 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(), grCT);
68 GrSurfaceProxyView view(proxy, kBottomLeft_GrSurfaceOrigin, swizzle);
69 auto sContext = GrSurfaceContext::Make(context, std::move(view), grCT, kPremul_SkAlphaType,
70 nullptr);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040071
Brian Salomon5fba7ad2018-03-22 10:01:16 -040072 SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040073
Brian Salomon1d435302019-07-01 13:05:28 -040074 bool result = sContext->readPixels(dstInfo, dstBuffer, 0, {0, 0});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040075 REPORTER_ASSERT(reporter, result);
Brian Salomon28a8f282019-10-24 20:07:39 -040076 REPORTER_ASSERT(reporter,
77 DoesFullBufferContainCorrectColor(srcBuffer, dstBuffer, kWidth, kHeight));
Greg Daniel164a9f02016-02-22 09:56:40 -050078
Brian Salomon5fba7ad2018-03-22 10:01:16 -040079 dstInfo = SkImageInfo::Make(4, 5, ct, kPremul_SkAlphaType);
Brian Salomon1d435302019-07-01 13:05:28 -040080 result = sContext->writePixels(dstInfo, srcBuffer, 0, {5, 4});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040081 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
Brian Salomon1d435302019-07-01 13:05:28 -040085 result = sContext->readPixels(dstInfo, dstBuffer, 0, {5, 4});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040086 REPORTER_ASSERT(reporter, result);
87
Brian Salomon28a8f282019-10-24 20:07:39 -040088 REPORTER_ASSERT(reporter, DoesFullBufferContainCorrectColor(srcBuffer, dstBuffer, 4, 5));
Greg Daniel164a9f02016-02-22 09:56:40 -050089
Greg Daniel164a9f02016-02-22 09:56:40 -050090 }
91}
92
Timothy Lianga8046af2018-07-19 09:58:00 -040093DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrUploadPixelsTests, reporter, ctxInfo) {
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040094 // RGBA
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040095 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, GrRenderable::kNo);
96 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, GrRenderable::kYes);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040097
98 // BGRA
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040099 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, GrRenderable::kNo);
100 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, GrRenderable::kYes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500101}