blob: e3fe89ffbf4e0d28b83c1206e6d440535799d39a [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"
Greg Danielf91aeb22019-06-18 09:58:02 -040013#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/SkGr.h"
15#include "tests/Test.h"
16#include "tests/TestUtils.h"
17#include "tools/gpu/GrContextFactory.h"
18#include "tools/gpu/ProxyUtils.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050019
kkinnunen2ae4b2e2016-03-31 08:08:20 -070020using sk_gpu_test::GrContextFactory;
Greg Daniel164a9f02016-02-22 09:56:40 -050021
Brian Salomon58389b92018-03-07 13:01:25 -050022void basic_texture_test(skiatest::Reporter* reporter, GrContext* context, SkColorType ct,
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040023 GrRenderable renderable) {
Greg Daniel164a9f02016-02-22 09:56:40 -050024 const int kWidth = 16;
25 const int kHeight = 16;
26 SkAutoTMalloc<GrColor> srcBuffer(kWidth*kHeight);
27 SkAutoTMalloc<GrColor> dstBuffer(kWidth*kHeight);
28
29 fill_pixel_data(kWidth, kHeight, srcBuffer.get());
30
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040031 auto proxy = sk_gpu_test::MakeTextureProxyFromData(context, renderable, kWidth, kHeight, ct,
Brian Salomone7499c72019-06-24 12:12:36 -040032 kPremul_SkAlphaType,
Brian Salomon58389b92018-03-07 13:01:25 -050033 kTopLeft_GrSurfaceOrigin, srcBuffer, 0);
Brian Salomon7128fdd2017-05-22 14:00:07 -040034 REPORTER_ASSERT(reporter, proxy);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040035 if (proxy) {
Brian Salomond6287472019-06-24 15:50:07 -040036 sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext(
37 proxy, SkColorTypeToGrColorType(ct), kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040038
Brian Salomon5fba7ad2018-03-22 10:01:16 -040039 SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040040
Brian Salomon1d435302019-07-01 13:05:28 -040041 bool result = sContext->readPixels(dstInfo, dstBuffer, 0, {0, 0});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040042 REPORTER_ASSERT(reporter, result);
Greg Daniel164a9f02016-02-22 09:56:40 -050043 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
44 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -050045 kWidth,
46 kHeight));
47
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
Greg Daniel164a9f02016-02-22 09:56:40 -050057 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
58 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -050059 10,
60 2));
Greg Daniel164a9f02016-02-22 09:56:40 -050061 }
62
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040063 proxy = sk_gpu_test::MakeTextureProxyFromData(context, renderable, kWidth, kHeight, ct,
Brian Salomone7499c72019-06-24 12:12:36 -040064 kPremul_SkAlphaType, kBottomLeft_GrSurfaceOrigin,
65 srcBuffer, 0);
Brian Salomon7128fdd2017-05-22 14:00:07 -040066 REPORTER_ASSERT(reporter, proxy);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040067 if (proxy) {
Brian Salomond6287472019-06-24 15:50:07 -040068 sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext(
69 proxy, SkColorTypeToGrColorType(ct), kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040070
Brian Salomon5fba7ad2018-03-22 10:01:16 -040071 SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040072
Brian Salomon1d435302019-07-01 13:05:28 -040073 bool result = sContext->readPixels(dstInfo, dstBuffer, 0, {0, 0});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040074 REPORTER_ASSERT(reporter, result);
Greg Daniel164a9f02016-02-22 09:56:40 -050075 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
76 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -050077 kWidth,
78 kHeight));
79
Brian Salomon5fba7ad2018-03-22 10:01:16 -040080 dstInfo = SkImageInfo::Make(4, 5, ct, kPremul_SkAlphaType);
Brian Salomon1d435302019-07-01 13:05:28 -040081 result = sContext->writePixels(dstInfo, srcBuffer, 0, {5, 4});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040082 REPORTER_ASSERT(reporter, result);
Robert Phillipse78b7252017-04-06 07:59:41 -040083
Greg Daniel164a9f02016-02-22 09:56:40 -050084 memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040085
Brian Salomon1d435302019-07-01 13:05:28 -040086 result = sContext->readPixels(dstInfo, dstBuffer, 0, {5, 4});
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040087 REPORTER_ASSERT(reporter, result);
88
Greg Daniel164a9f02016-02-22 09:56:40 -050089 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
90 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -050091 4,
92 5));
93
Greg Daniel164a9f02016-02-22 09:56:40 -050094 }
95}
96
Timothy Lianga8046af2018-07-19 09:58:00 -040097DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrUploadPixelsTests, reporter, ctxInfo) {
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040098 // RGBA
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040099 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, GrRenderable::kNo);
100 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, GrRenderable::kYes);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400101
102 // BGRA
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400103 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, GrRenderable::kNo);
104 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, GrRenderable::kYes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500105}