blob: 2188c307cd6c8ac085610e5e3c31c4f59f80d6bf [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
10#include "SkTypes.h"
11
Greg Daniel164a9f02016-02-22 09:56:40 -050012#include "GrContextFactory.h"
Robert Phillipse78b7252017-04-06 07:59:41 -040013#include "GrContextPriv.h"
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040014#include "GrSurfaceProxy.h"
Brian Salomon58389b92018-03-07 13:01:25 -050015#include "ProxyUtils.h"
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040016#include "SkGr.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050017#include "Test.h"
Timothy Liang760dbc42018-07-17 13:28:20 -040018#include "TestUtils.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,
Brian Salomon7128fdd2017-05-22 14:00:07 -040023 bool renderTarget) {
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
Brian Salomon58389b92018-03-07 13:01:25 -050031 auto proxy = sk_gpu_test::MakeTextureProxyFromData(context, renderTarget, kWidth, kHeight, ct,
32 kTopLeft_GrSurfaceOrigin, srcBuffer, 0);
Brian Salomon7128fdd2017-05-22 14:00:07 -040033 REPORTER_ASSERT(reporter, proxy);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040034 if (proxy) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050035 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(proxy);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040036
Brian Salomon5fba7ad2018-03-22 10:01:16 -040037 SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040038
39 bool result = sContext->readPixels(dstInfo, dstBuffer, 0, 0, 0);
40 REPORTER_ASSERT(reporter, result);
Greg Daniel164a9f02016-02-22 09:56:40 -050041 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
42 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -050043 kWidth,
44 kHeight));
45
Brian Salomon5fba7ad2018-03-22 10:01:16 -040046 dstInfo = SkImageInfo::Make(10, 2, ct, kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040047 result = sContext->writePixels(dstInfo, srcBuffer, 0, 2, 10);
48 REPORTER_ASSERT(reporter, result);
Robert Phillipse78b7252017-04-06 07:59:41 -040049
Greg Daniel164a9f02016-02-22 09:56:40 -050050 memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040051
52 result = sContext->readPixels(dstInfo, dstBuffer, 0, 2, 10);
53 REPORTER_ASSERT(reporter, result);
54
Greg Daniel164a9f02016-02-22 09:56:40 -050055 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
56 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -050057 10,
58 2));
Greg Daniel164a9f02016-02-22 09:56:40 -050059 }
60
Brian Salomon58389b92018-03-07 13:01:25 -050061 proxy = sk_gpu_test::MakeTextureProxyFromData(context, renderTarget, kWidth, kHeight, ct,
62 kBottomLeft_GrSurfaceOrigin, srcBuffer, 0);
Brian Salomon7128fdd2017-05-22 14:00:07 -040063 REPORTER_ASSERT(reporter, proxy);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040064 if (proxy) {
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050065 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(proxy);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040066
Brian Salomon5fba7ad2018-03-22 10:01:16 -040067 SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040068
69 bool result = sContext->readPixels(dstInfo, dstBuffer, 0, 0, 0);
70 REPORTER_ASSERT(reporter, result);
Greg Daniel164a9f02016-02-22 09:56:40 -050071 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
72 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -050073 kWidth,
74 kHeight));
75
Brian Salomon5fba7ad2018-03-22 10:01:16 -040076 dstInfo = SkImageInfo::Make(4, 5, ct, kPremul_SkAlphaType);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040077 result = sContext->writePixels(dstInfo, srcBuffer, 0, 5, 4);
78 REPORTER_ASSERT(reporter, result);
Robert Phillipse78b7252017-04-06 07:59:41 -040079
Greg Daniel164a9f02016-02-22 09:56:40 -050080 memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040081
82 result = sContext->readPixels(dstInfo, dstBuffer, 0, 5, 4);
83 REPORTER_ASSERT(reporter, result);
84
Greg Daniel164a9f02016-02-22 09:56:40 -050085 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
86 dstBuffer,
Greg Daniel164a9f02016-02-22 09:56:40 -050087 4,
88 5));
89
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
Brian Salomon58389b92018-03-07 13:01:25 -050095 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, false);
96 basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, true);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -040097
98 // BGRA
Brian Salomon58389b92018-03-07 13:01:25 -050099 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, false);
100 basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, true);
Greg Daniel164a9f02016-02-22 09:56:40 -0500101}