blob: 1f7d107a3000146e9bfab6f8c2d618d7860e5004 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00007
8// This test only works with the GPU backend.
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00009
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000010#include "gm.h"
bsalomon@google.coma68937c2012-08-03 15:00:52 +000011
12#if SK_SUPPORT_GPU
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000013#include "GrContext.h"
Robert Phillips901f29a2017-01-24 16:24:41 -050014#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050015#include "GrProxyProvider.h"
Brian Osman11052242016-10-27 14:47:55 -040016#include "GrRenderTargetContext.h"
Robert Phillips901f29a2017-01-24 16:24:41 -050017#include "GrTextureContext.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070018#include "GrFixedClip.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080019#include "SkColorPriv.h"
Robert Phillips40fd7c92017-01-30 08:06:27 -050020#include "SkGr.h"
egdaniel95131432014-12-09 11:15:43 -080021#include "effects/GrPorterDuffXferProcessor.h"
22#include "effects/GrSimpleTextureEffect.h"
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000023
mtkleindbfd7ab2016-09-01 11:24:54 -070024constexpr int S = 200;
Robert Phillips901f29a2017-01-24 16:24:41 -050025constexpr int kStride = 2 * S;
26
27// Fill in the pixels:
28// gray | white
29// -------------
30// black | gray
31static void fill_in_pixels(SkPMColor* pixels) {
32 const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40);
33 const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff);
34 const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00);
35
36 int offset = 0;
37
38 // fill upper-left
39 for (int y = 0; y < S; ++y) {
40 for (int x = 0; x < S; ++x) {
41 pixels[offset + y * kStride + x] = gray;
42 }
43 }
44 // fill upper-right
45 offset = S;
46 for (int y = 0; y < S; ++y) {
47 for (int x = 0; x < S; ++x) {
48 pixels[offset + y * kStride + x] = white;
49 }
50 }
51 // fill lower left
52 offset = S * kStride;
53 for (int y = 0; y < S; ++y) {
54 for (int x = 0; x < S; ++x) {
55 pixels[offset + y * kStride + x] = black;
56 }
57 }
58 // fill lower right
59 offset = S * kStride + S;
60 for (int y = 0; y < S; ++y) {
61 for (int x = 0; x < S; ++x) {
62 pixels[offset + y * kStride + x] = gray;
63 }
64 }
65}
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000066
halcanary2a243382015-09-09 08:16:41 -070067DEF_SIMPLE_GM_BG(texdata, canvas, 2 * S, 2 * S, SK_ColorBLACK) {
Brian Osman11052242016-10-27 14:47:55 -040068 GrRenderTargetContext* renderTargetContext =
69 canvas->internal_private_accessTopLayerRenderTargetContext();
70 if (!renderTargetContext) {
robertphillips175dd9b2016-04-28 14:32:04 -070071 skiagm::GM::DrawGpuOnlyMessage(canvas);
72 return;
73 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000074
robertphillips175dd9b2016-04-28 14:32:04 -070075 GrContext* context = canvas->getGrContext();
76 if (!context) {
77 return;
78 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000079
Robert Phillips0bd24dc2018-01-16 08:06:32 -050080 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips901f29a2017-01-24 16:24:41 -050081 const SkImageInfo ii = SkImageInfo::Make(S, S, kBGRA_8888_SkColorType, kPremul_SkAlphaType);
82
robertphillips175dd9b2016-04-28 14:32:04 -070083 SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S));
mtkleindbfd7ab2016-09-01 11:24:54 -070084 const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00);
85 const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80);
86 const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00);
robertphillips175dd9b2016-04-28 14:32:04 -070087 for (int i = 0; i < 2; ++i) {
Robert Phillips901f29a2017-01-24 16:24:41 -050088 fill_in_pixels(gTextureData.get());
robertphillips175dd9b2016-04-28 14:32:04 -070089
90 GrSurfaceDesc desc;
robertphillips677da9d2016-05-11 05:15:55 -070091 desc.fOrigin = i ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
robertphillips175dd9b2016-04-28 14:32:04 -070092 desc.fWidth = 2 * S;
93 desc.fHeight = 2 * S;
Robert Phillips40fd7c92017-01-30 08:06:27 -050094 desc.fConfig = SkImageInfo2GrPixelConfig(ii, *context->caps());
robertphillips175dd9b2016-04-28 14:32:04 -070095
Robert Phillips0bd24dc2018-01-16 08:06:32 -050096 sk_sp<GrTextureProxy> proxy = proxyProvider->createTextureProxy(desc, SkBudgeted::kNo,
97 gTextureData.get(), 0);
Robert Phillips901f29a2017-01-24 16:24:41 -050098 if (!proxy) {
robertphillips175dd9b2016-04-28 14:32:04 -070099 return;
100 }
Robert Phillips901f29a2017-01-24 16:24:41 -0500101
102 sk_sp<GrSurfaceContext> tContext = context->contextPriv().makeWrappedSurfaceContext(
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500103 std::move(proxy));
Robert Phillips901f29a2017-01-24 16:24:41 -0500104
105 if (!tContext) {
106 return;
107 }
robertphillips175dd9b2016-04-28 14:32:04 -0700108
109 // setup new clip
cdalton846c0512016-05-13 10:25:00 -0700110 GrFixedClip clip(SkIRect::MakeWH(2*S, 2*S));
robertphillips175dd9b2016-04-28 14:32:04 -0700111
112 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400113 paint.setPorterDuffXPFactory(SkBlendMode::kSrcOver);
robertphillips175dd9b2016-04-28 14:32:04 -0700114
115 SkMatrix vm;
116 if (i) {
Robert Phillips901f29a2017-01-24 16:24:41 -0500117 vm.setRotate(90 * SK_Scalar1, S * SK_Scalar1, S * SK_Scalar1);
robertphillips175dd9b2016-04-28 14:32:04 -0700118 } else {
119 vm.reset();
120 }
Brian Osman2240be92017-10-18 13:15:13 -0400121 paint.addColorTextureProcessor(tContext->asTextureProxyRef(), vm);
robertphillips175dd9b2016-04-28 14:32:04 -0700122
Brian Salomonb74ef032017-08-10 12:46:01 -0400123 renderTargetContext->drawRect(clip, GrPaint::Clone(paint), GrAA::kNo, vm,
Brian Salomon82f44312017-01-11 13:42:54 -0500124 SkRect::MakeWH(2 * S, 2 * S));
robertphillips175dd9b2016-04-28 14:32:04 -0700125
126 // now update the lower right of the texture in first pass
127 // or upper right in second pass
robertphillips175dd9b2016-04-28 14:32:04 -0700128 for (int y = 0; y < S; ++y) {
129 for (int x = 0; x < S; ++x) {
Robert Phillips901f29a2017-01-24 16:24:41 -0500130 gTextureData[y * kStride + x] = ((x + y) % 2) ? (i ? green : red) : blue;
robertphillips175dd9b2016-04-28 14:32:04 -0700131 }
132 }
Robert Phillips901f29a2017-01-24 16:24:41 -0500133
134 if (!tContext->writePixels(ii, gTextureData.get(), 4 * kStride, S, i ? 0 : S)) {
135 continue;
136 }
137
Brian Salomon82f44312017-01-11 13:42:54 -0500138 renderTargetContext->drawRect(clip, std::move(paint), GrAA::kNo, vm,
139 SkRect::MakeWH(2 * S, 2 * S));
robertphillips175dd9b2016-04-28 14:32:04 -0700140 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000141}
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000142#endif
robertphillips175dd9b2016-04-28 14:32:04 -0700143