blob: 4b63e2ea692942c9dff1fa1e70fad43caec2168a [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 Phillips26c90e02017-03-14 14:39:29 -040094 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
Robert Phillips901f29a2017-01-24 16:24:41 -050095 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 }
Brian Osman2240be92017-10-18 13:15:13 -0400120 paint.addColorTextureProcessor(tContext->asTextureProxyRef(), vm);
robertphillips175dd9b2016-04-28 14:32:04 -0700121
Brian Salomonb74ef032017-08-10 12:46:01 -0400122 renderTargetContext->drawRect(clip, GrPaint::Clone(paint), GrAA::kNo, vm,
Brian Salomon82f44312017-01-11 13:42:54 -0500123 SkRect::MakeWH(2 * S, 2 * S));
robertphillips175dd9b2016-04-28 14:32:04 -0700124
125 // now update the lower right of the texture in first pass
126 // or upper right in second pass
robertphillips175dd9b2016-04-28 14:32:04 -0700127 for (int y = 0; y < S; ++y) {
128 for (int x = 0; x < S; ++x) {
Robert Phillips901f29a2017-01-24 16:24:41 -0500129 gTextureData[y * kStride + x] = ((x + y) % 2) ? (i ? green : red) : blue;
robertphillips175dd9b2016-04-28 14:32:04 -0700130 }
131 }
Robert Phillips901f29a2017-01-24 16:24:41 -0500132
133 if (!tContext->writePixels(ii, gTextureData.get(), 4 * kStride, S, i ? 0 : S)) {
134 continue;
135 }
136
Brian Salomon82f44312017-01-11 13:42:54 -0500137 renderTargetContext->drawRect(clip, std::move(paint), GrAA::kNo, vm,
138 SkRect::MakeWH(2 * S, 2 * S));
robertphillips175dd9b2016-04-28 14:32:04 -0700139 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000140}
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000141#endif
robertphillips175dd9b2016-04-28 14:32:04 -0700142