blob: 9662ea57b11e67b08cf74d16ee9a475b264639d7 [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) {
Brian Salomonbf6b9792019-08-21 09:38:10 -040038 auto sContext = context->priv().makeWrappedSurfaceContext(
Brian Salomond6287472019-06-24 15:50:07 -040039 proxy, SkColorTypeToGrColorType(ct), kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040040
Brian Salomon5fba7ad2018-03-22 10:01:16 -040041 SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040042
Brian Salomon1d435302019-07-01 13:05:28 -040043 bool result = sContext->readPixels(dstInfo, dstBuffer, 0, {0, 0});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040044 REPORTER_ASSERT(reporter, result);
Brian Salomon28a8f282019-10-24 20:07:39 -040045 REPORTER_ASSERT(reporter,
46 DoesFullBufferContainCorrectColor(srcBuffer, dstBuffer, kWidth, kHeight));
Greg Daniel164a9f02016-02-22 09:56:40 -050047
Brian Salomon5fba7ad2018-03-22 10:01:16 -040048 dstInfo = SkImageInfo::Make(10, 2, ct, kPremul_SkAlphaType);
Brian Salomon1d435302019-07-01 13:05:28 -040049 result = sContext->writePixels(dstInfo, srcBuffer, 0, {2, 10});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040050 REPORTER_ASSERT(reporter, result);
Robert Phillipse78b7252017-04-06 07:59:41 -040051
Greg Daniel164a9f02016-02-22 09:56:40 -050052 memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040053
Brian Salomon1d435302019-07-01 13:05:28 -040054 result = sContext->readPixels(dstInfo, dstBuffer, 0, {2, 10});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040055 REPORTER_ASSERT(reporter, result);
56
Brian Salomon28a8f282019-10-24 20:07:39 -040057 REPORTER_ASSERT(reporter, DoesFullBufferContainCorrectColor(srcBuffer, dstBuffer, 10, 2));
Greg Daniel164a9f02016-02-22 09:56:40 -050058 }
59
Brian Salomon4eda7102019-10-21 15:04:52 -040060 proxy = sk_gpu_test::MakeTextureProxyFromData(
61 context, renderable, kBottomLeft_GrSurfaceOrigin,
62 {grCT, kPremul_SkAlphaType, nullptr, kWidth, kHeight}, srcBuffer, 0);
Brian Salomon7128fdd2017-05-22 14:00:07 -040063 REPORTER_ASSERT(reporter, proxy);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040064 if (proxy) {
Brian Salomonbf6b9792019-08-21 09:38:10 -040065 auto sContext = context->priv().makeWrappedSurfaceContext(
Brian Salomond6287472019-06-24 15:50:07 -040066 proxy, SkColorTypeToGrColorType(ct), kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040067
Brian Salomon5fba7ad2018-03-22 10:01:16 -040068 SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040069
Brian Salomon1d435302019-07-01 13:05:28 -040070 bool result = sContext->readPixels(dstInfo, dstBuffer, 0, {0, 0});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040071 REPORTER_ASSERT(reporter, result);
Brian Salomon28a8f282019-10-24 20:07:39 -040072 REPORTER_ASSERT(reporter,
73 DoesFullBufferContainCorrectColor(srcBuffer, dstBuffer, kWidth, kHeight));
Greg Daniel164a9f02016-02-22 09:56:40 -050074
Brian Salomon5fba7ad2018-03-22 10:01:16 -040075 dstInfo = SkImageInfo::Make(4, 5, ct, kPremul_SkAlphaType);
Brian Salomon1d435302019-07-01 13:05:28 -040076 result = sContext->writePixels(dstInfo, srcBuffer, 0, {5, 4});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040077 REPORTER_ASSERT(reporter, result);
Robert Phillipse78b7252017-04-06 07:59:41 -040078
Greg Daniel164a9f02016-02-22 09:56:40 -050079 memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040080
Brian Salomon1d435302019-07-01 13:05:28 -040081 result = sContext->readPixels(dstInfo, dstBuffer, 0, {5, 4});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040082 REPORTER_ASSERT(reporter, result);
83
Brian Salomon28a8f282019-10-24 20:07:39 -040084 REPORTER_ASSERT(reporter, DoesFullBufferContainCorrectColor(srcBuffer, dstBuffer, 4, 5));
Greg Daniel164a9f02016-02-22 09:56:40 -050085
Greg Daniel164a9f02016-02-22 09:56:40 -050086 }
87}
88
Timothy Lianga8046af2018-07-19 09:58:00 -040089DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrUploadPixelsTests, reporter, ctxInfo) {
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040090 // RGBA
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040091 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, GrRenderable::kNo);
92 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, GrRenderable::kYes);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040093
94 // BGRA
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040095 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, GrRenderable::kNo);
96 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, GrRenderable::kYes);
Greg Daniel164a9f02016-02-22 09:56:40 -050097}