blob: 6b229a3db366875aa9b7f14c9d2445770bf29f08 [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"
Robert Phillips40fd7c92017-01-30 08:06:27 -050019#include "SkGr.h"
egdaniel95131432014-12-09 11:15:43 -080020#include "effects/GrPorterDuffXferProcessor.h"
21#include "effects/GrSimpleTextureEffect.h"
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000022
mtkleindbfd7ab2016-09-01 11:24:54 -070023constexpr int S = 200;
Robert Phillips901f29a2017-01-24 16:24:41 -050024constexpr int kStride = 2 * S;
25
26// Fill in the pixels:
27// gray | white
28// -------------
29// black | gray
30static void fill_in_pixels(SkPMColor* pixels) {
31 const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40);
32 const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff);
33 const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00);
34
35 int offset = 0;
36
37 // fill upper-left
38 for (int y = 0; y < S; ++y) {
39 for (int x = 0; x < S; ++x) {
40 pixels[offset + y * kStride + x] = gray;
41 }
42 }
43 // fill upper-right
44 offset = S;
45 for (int y = 0; y < S; ++y) {
46 for (int x = 0; x < S; ++x) {
47 pixels[offset + y * kStride + x] = white;
48 }
49 }
50 // fill lower left
51 offset = S * kStride;
52 for (int y = 0; y < S; ++y) {
53 for (int x = 0; x < S; ++x) {
54 pixels[offset + y * kStride + x] = black;
55 }
56 }
57 // fill lower right
58 offset = S * kStride + S;
59 for (int y = 0; y < S; ++y) {
60 for (int x = 0; x < S; ++x) {
61 pixels[offset + y * kStride + x] = gray;
62 }
63 }
64}
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000065
halcanary2a243382015-09-09 08:16:41 -070066DEF_SIMPLE_GM_BG(texdata, canvas, 2 * S, 2 * S, SK_ColorBLACK) {
Brian Osman11052242016-10-27 14:47:55 -040067 GrRenderTargetContext* renderTargetContext =
68 canvas->internal_private_accessTopLayerRenderTargetContext();
69 if (!renderTargetContext) {
robertphillips175dd9b2016-04-28 14:32:04 -070070 skiagm::GM::DrawGpuOnlyMessage(canvas);
71 return;
72 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000073
robertphillips175dd9b2016-04-28 14:32:04 -070074 GrContext* context = canvas->getGrContext();
75 if (!context) {
76 return;
77 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000078
Robert Phillips901f29a2017-01-24 16:24:41 -050079 const SkImageInfo ii = SkImageInfo::Make(S, S, kBGRA_8888_SkColorType, kPremul_SkAlphaType);
80
robertphillips175dd9b2016-04-28 14:32:04 -070081 SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S));
mtkleindbfd7ab2016-09-01 11:24:54 -070082 const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00);
83 const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80);
84 const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00);
robertphillips175dd9b2016-04-28 14:32:04 -070085 for (int i = 0; i < 2; ++i) {
Robert Phillips901f29a2017-01-24 16:24:41 -050086 fill_in_pixels(gTextureData.get());
robertphillips175dd9b2016-04-28 14:32:04 -070087
88 GrSurfaceDesc desc;
robertphillips677da9d2016-05-11 05:15:55 -070089 desc.fOrigin = i ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
robertphillips175dd9b2016-04-28 14:32:04 -070090 desc.fWidth = 2 * S;
91 desc.fHeight = 2 * S;
Robert Phillips40fd7c92017-01-30 08:06:27 -050092 desc.fConfig = SkImageInfo2GrPixelConfig(ii, *context->caps());
robertphillips175dd9b2016-04-28 14:32:04 -070093
Robert Phillips901f29a2017-01-24 16:24:41 -050094 sk_sp<GrSurfaceProxy> proxy = GrSurfaceProxy::MakeDeferred(*context->caps(),
95 context->textureProvider(),
96 desc, SkBudgeted::kNo,
97 gTextureData.get(), 0);
98 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(
103 std::move(proxy), nullptr);
104
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 }
Robert Phillips901f29a2017-01-24 16:24:41 -0500121 paint.addColorTextureProcessor(context, sk_ref_sp(tContext->asDeferredTexture()),
122 nullptr, vm);
robertphillips175dd9b2016-04-28 14:32:04 -0700123
Brian Salomon82f44312017-01-11 13:42:54 -0500124 renderTargetContext->drawRect(clip, GrPaint(paint), GrAA::kNo, vm,
125 SkRect::MakeWH(2 * S, 2 * S));
robertphillips175dd9b2016-04-28 14:32:04 -0700126
127 // now update the lower right of the texture in first pass
128 // or upper right in second pass
robertphillips175dd9b2016-04-28 14:32:04 -0700129 for (int y = 0; y < S; ++y) {
130 for (int x = 0; x < S; ++x) {
Robert Phillips901f29a2017-01-24 16:24:41 -0500131 gTextureData[y * kStride + x] = ((x + y) % 2) ? (i ? green : red) : blue;
robertphillips175dd9b2016-04-28 14:32:04 -0700132 }
133 }
Robert Phillips901f29a2017-01-24 16:24:41 -0500134
135 if (!tContext->writePixels(ii, gTextureData.get(), 4 * kStride, S, i ? 0 : S)) {
136 continue;
137 }
138
Brian Salomon82f44312017-01-11 13:42:54 -0500139 renderTargetContext->drawRect(clip, std::move(paint), GrAA::kNo, vm,
140 SkRect::MakeWH(2 * S, 2 * S));
robertphillips175dd9b2016-04-28 14:32:04 -0700141 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000142}
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000143#endif
robertphillips175dd9b2016-04-28 14:32:04 -0700144