blob: 70afd22ec56b816ad8c0df0e4af9c224d3567ed5 [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"
Brian Osman11052242016-10-27 14:47:55 -040015#include "GrRenderTargetContext.h"
Robert Phillips901f29a2017-01-24 16:24:41 -050016#include "GrTextureContext.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070017#include "GrFixedClip.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080018#include "SkColorPriv.h"
egdaniel95131432014-12-09 11:15:43 -080019#include "effects/GrPorterDuffXferProcessor.h"
20#include "effects/GrSimpleTextureEffect.h"
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000021
mtkleindbfd7ab2016-09-01 11:24:54 -070022constexpr int S = 200;
Robert Phillips901f29a2017-01-24 16:24:41 -050023constexpr int kStride = 2 * S;
24
25// Fill in the pixels:
26// gray | white
27// -------------
28// black | gray
29static void fill_in_pixels(SkPMColor* pixels) {
30 const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40);
31 const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff);
32 const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00);
33
34 int offset = 0;
35
36 // fill upper-left
37 for (int y = 0; y < S; ++y) {
38 for (int x = 0; x < S; ++x) {
39 pixels[offset + y * kStride + x] = gray;
40 }
41 }
42 // fill upper-right
43 offset = S;
44 for (int y = 0; y < S; ++y) {
45 for (int x = 0; x < S; ++x) {
46 pixels[offset + y * kStride + x] = white;
47 }
48 }
49 // fill lower left
50 offset = S * kStride;
51 for (int y = 0; y < S; ++y) {
52 for (int x = 0; x < S; ++x) {
53 pixels[offset + y * kStride + x] = black;
54 }
55 }
56 // fill lower right
57 offset = S * kStride + S;
58 for (int y = 0; y < S; ++y) {
59 for (int x = 0; x < S; ++x) {
60 pixels[offset + y * kStride + x] = gray;
61 }
62 }
63}
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000064
halcanary2a243382015-09-09 08:16:41 -070065DEF_SIMPLE_GM_BG(texdata, canvas, 2 * S, 2 * S, SK_ColorBLACK) {
Brian Osman11052242016-10-27 14:47:55 -040066 GrRenderTargetContext* renderTargetContext =
67 canvas->internal_private_accessTopLayerRenderTargetContext();
68 if (!renderTargetContext) {
robertphillips175dd9b2016-04-28 14:32:04 -070069 skiagm::GM::DrawGpuOnlyMessage(canvas);
70 return;
71 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000072
robertphillips175dd9b2016-04-28 14:32:04 -070073 GrContext* context = canvas->getGrContext();
74 if (!context) {
75 return;
76 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000077
Robert Phillips901f29a2017-01-24 16:24:41 -050078 const SkImageInfo ii = SkImageInfo::Make(S, S, kBGRA_8888_SkColorType, kPremul_SkAlphaType);
79
robertphillips175dd9b2016-04-28 14:32:04 -070080 SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S));
mtkleindbfd7ab2016-09-01 11:24:54 -070081 const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00);
82 const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80);
83 const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00);
robertphillips175dd9b2016-04-28 14:32:04 -070084 for (int i = 0; i < 2; ++i) {
Robert Phillips901f29a2017-01-24 16:24:41 -050085 fill_in_pixels(gTextureData.get());
robertphillips175dd9b2016-04-28 14:32:04 -070086
87 GrSurfaceDesc desc;
robertphillips677da9d2016-05-11 05:15:55 -070088 desc.fOrigin = i ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
Robert Phillips901f29a2017-01-24 16:24:41 -050089 desc.fConfig = kBGRA_8888_GrPixelConfig;
robertphillips175dd9b2016-04-28 14:32:04 -070090 desc.fWidth = 2 * S;
91 desc.fHeight = 2 * S;
robertphillips175dd9b2016-04-28 14:32:04 -070092
Robert Phillips901f29a2017-01-24 16:24:41 -050093 sk_sp<GrSurfaceProxy> proxy = GrSurfaceProxy::MakeDeferred(*context->caps(),
94 context->textureProvider(),
95 desc, SkBudgeted::kNo,
96 gTextureData.get(), 0);
97 if (!proxy) {
robertphillips175dd9b2016-04-28 14:32:04 -070098 return;
99 }
Robert Phillips901f29a2017-01-24 16:24:41 -0500100
101 sk_sp<GrSurfaceContext> tContext = context->contextPriv().makeWrappedSurfaceContext(
102 std::move(proxy), nullptr);
103
104 if (!tContext) {
105 return;
106 }
robertphillips175dd9b2016-04-28 14:32:04 -0700107
108 // setup new clip
cdalton846c0512016-05-13 10:25:00 -0700109 GrFixedClip clip(SkIRect::MakeWH(2*S, 2*S));
robertphillips175dd9b2016-04-28 14:32:04 -0700110
111 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400112 paint.setPorterDuffXPFactory(SkBlendMode::kSrcOver);
robertphillips175dd9b2016-04-28 14:32:04 -0700113
114 SkMatrix vm;
115 if (i) {
Robert Phillips901f29a2017-01-24 16:24:41 -0500116 vm.setRotate(90 * SK_Scalar1, S * SK_Scalar1, S * SK_Scalar1);
robertphillips175dd9b2016-04-28 14:32:04 -0700117 } else {
118 vm.reset();
119 }
Robert Phillips901f29a2017-01-24 16:24:41 -0500120 paint.addColorTextureProcessor(context, sk_ref_sp(tContext->asDeferredTexture()),
121 nullptr, vm);
robertphillips175dd9b2016-04-28 14:32:04 -0700122
Brian Salomon82f44312017-01-11 13:42:54 -0500123 renderTargetContext->drawRect(clip, GrPaint(paint), GrAA::kNo, vm,
124 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