blob: 576451b0fd9bb6b51b0efe8e60188a37a2b6bee8 [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
30 fill_pixel_data(kWidth, kHeight, srcBuffer.get());
31
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);
Greg Daniel164a9f02016-02-22 09:56:40 -050045 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
46 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -050047 kWidth,
48 kHeight));
49
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
Greg Daniel164a9f02016-02-22 09:56:40 -050059 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
60 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -050061 10,
62 2));
Greg Daniel164a9f02016-02-22 09:56:40 -050063 }
64
Brian Salomon4eda7102019-10-21 15:04:52 -040065 proxy = sk_gpu_test::MakeTextureProxyFromData(
66 context, renderable, kBottomLeft_GrSurfaceOrigin,
67 {grCT, kPremul_SkAlphaType, nullptr, kWidth, kHeight}, srcBuffer, 0);
Brian Salomon7128fdd2017-05-22 14:00:07 -040068 REPORTER_ASSERT(reporter, proxy);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040069 if (proxy) {
Brian Salomonbf6b9792019-08-21 09:38:10 -040070 auto sContext = context->priv().makeWrappedSurfaceContext(
Brian Salomond6287472019-06-24 15:50:07 -040071 proxy, SkColorTypeToGrColorType(ct), kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040072
Brian Salomon5fba7ad2018-03-22 10:01:16 -040073 SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040074
Brian Salomon1d435302019-07-01 13:05:28 -040075 bool result = sContext->readPixels(dstInfo, dstBuffer, 0, {0, 0});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040076 REPORTER_ASSERT(reporter, result);
Greg Daniel164a9f02016-02-22 09:56:40 -050077 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
78 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -050079 kWidth,
80 kHeight));
81
Brian Salomon5fba7ad2018-03-22 10:01:16 -040082 dstInfo = SkImageInfo::Make(4, 5, ct, kPremul_SkAlphaType);
Brian Salomon1d435302019-07-01 13:05:28 -040083 result = sContext->writePixels(dstInfo, srcBuffer, 0, {5, 4});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040084 REPORTER_ASSERT(reporter, result);
Robert Phillipse78b7252017-04-06 07:59:41 -040085
Greg Daniel164a9f02016-02-22 09:56:40 -050086 memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040087
Brian Salomon1d435302019-07-01 13:05:28 -040088 result = sContext->readPixels(dstInfo, dstBuffer, 0, {5, 4});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040089 REPORTER_ASSERT(reporter, result);
90
Greg Daniel164a9f02016-02-22 09:56:40 -050091 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
92 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -050093 4,
94 5));
95
Greg Daniel164a9f02016-02-22 09:56:40 -050096 }
97}
98
Timothy Lianga8046af2018-07-19 09:58:00 -040099DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrUploadPixelsTests, reporter, ctxInfo) {
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400100 // RGBA
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400101 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, GrRenderable::kNo);
102 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, GrRenderable::kYes);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400103
104 // BGRA
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400105 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, GrRenderable::kNo);
106 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, GrRenderable::kYes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500107}