blob: 372ff5dc11846b0809450e507905b1e42b5af547 [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"
Brian Osman11052242016-10-27 14:47:55 -040014#include "GrRenderTargetContext.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070015#include "GrFixedClip.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080016#include "SkColorPriv.h"
egdaniel95131432014-12-09 11:15:43 -080017#include "effects/GrPorterDuffXferProcessor.h"
18#include "effects/GrSimpleTextureEffect.h"
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000019
mtkleindbfd7ab2016-09-01 11:24:54 -070020constexpr int S = 200;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000021
halcanary2a243382015-09-09 08:16:41 -070022DEF_SIMPLE_GM_BG(texdata, canvas, 2 * S, 2 * S, SK_ColorBLACK) {
Brian Osman11052242016-10-27 14:47:55 -040023 GrRenderTargetContext* renderTargetContext =
24 canvas->internal_private_accessTopLayerRenderTargetContext();
25 if (!renderTargetContext) {
robertphillips175dd9b2016-04-28 14:32:04 -070026 skiagm::GM::DrawGpuOnlyMessage(canvas);
27 return;
28 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000029
robertphillips175dd9b2016-04-28 14:32:04 -070030 GrContext* context = canvas->getGrContext();
31 if (!context) {
32 return;
33 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000034
robertphillips175dd9b2016-04-28 14:32:04 -070035 SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S));
mtkleindbfd7ab2016-09-01 11:24:54 -070036 constexpr int stride = 2 * S;
37 const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40);
38 const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff);
39 const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00);
40 const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80);
41 const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00);
42 const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00);
robertphillips175dd9b2016-04-28 14:32:04 -070043 for (int i = 0; i < 2; ++i) {
44 int offset = 0;
45 // fill upper-left
46 for (int y = 0; y < S; ++y) {
47 for (int x = 0; x < S; ++x) {
48 gTextureData[offset + y * stride + x] = gray;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000049 }
50 }
robertphillips175dd9b2016-04-28 14:32:04 -070051 // fill upper-right
52 offset = S;
53 for (int y = 0; y < S; ++y) {
54 for (int x = 0; x < S; ++x) {
55 gTextureData[offset + y * stride + x] = white;
56 }
57 }
58 // fill lower left
59 offset = S * stride;
60 for (int y = 0; y < S; ++y) {
61 for (int x = 0; x < S; ++x) {
62 gTextureData[offset + y * stride + x] = black;
63 }
64 }
65 // fill lower right
66 offset = S * stride + S;
67 for (int y = 0; y < S; ++y) {
68 for (int x = 0; x < S; ++x) {
69 gTextureData[offset + y * stride + x] = gray;
70 }
71 }
72
73 GrSurfaceDesc desc;
robertphillips677da9d2016-05-11 05:15:55 -070074 desc.fOrigin = i ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
robertphillips175dd9b2016-04-28 14:32:04 -070075 desc.fConfig = kSkia8888_GrPixelConfig;
76 desc.fWidth = 2 * S;
77 desc.fHeight = 2 * S;
78 GrTexture* texture = context->textureProvider()->createTexture(
79 desc, SkBudgeted::kNo, gTextureData.get(), 0);
80
81 if (!texture) {
82 return;
83 }
Hal Canarycefc4312016-11-04 16:26:16 -040084 sk_sp<GrTexture> au(texture);
robertphillips175dd9b2016-04-28 14:32:04 -070085
86 // setup new clip
cdalton846c0512016-05-13 10:25:00 -070087 GrFixedClip clip(SkIRect::MakeWH(2*S, 2*S));
robertphillips175dd9b2016-04-28 14:32:04 -070088
89 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -040090 paint.setPorterDuffXPFactory(SkBlendMode::kSrcOver);
robertphillips175dd9b2016-04-28 14:32:04 -070091
92 SkMatrix vm;
93 if (i) {
94 vm.setRotate(90 * SK_Scalar1,
95 S * SK_Scalar1,
96 S * SK_Scalar1);
97 } else {
98 vm.reset();
99 }
Robert Phillips67c18d62017-01-20 12:44:06 -0500100 paint.addColorTextureProcessor(texture, nullptr, vm);
robertphillips175dd9b2016-04-28 14:32:04 -0700101
Brian Salomon82f44312017-01-11 13:42:54 -0500102 renderTargetContext->drawRect(clip, GrPaint(paint), GrAA::kNo, vm,
103 SkRect::MakeWH(2 * S, 2 * S));
robertphillips175dd9b2016-04-28 14:32:04 -0700104
105 // now update the lower right of the texture in first pass
106 // or upper right in second pass
107 offset = 0;
108 for (int y = 0; y < S; ++y) {
109 for (int x = 0; x < S; ++x) {
110 gTextureData[offset + y * stride + x] =
111 ((x + y) % 2) ? (i ? green : red) : blue;
112 }
113 }
114 texture->writePixels(S, (i ? 0 : S), S, S,
115 texture->config(), gTextureData.get(),
116 4 * stride);
Brian Salomon82f44312017-01-11 13:42:54 -0500117 renderTargetContext->drawRect(clip, std::move(paint), GrAA::kNo, vm,
118 SkRect::MakeWH(2 * S, 2 * S));
robertphillips175dd9b2016-04-28 14:32:04 -0700119 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000120}
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000121#endif
robertphillips175dd9b2016-04-28 14:32:04 -0700122