blob: 41acffaf18ee9bbd9cbb7ff3012c11e2d2ca9c7d [file] [log] [blame]
bsalomone179a912016-01-20 06:18:10 -08001/*
2 * Copyright 2016 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 */
7
8// This test only works with the GPU backend.
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "gm/gm.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkBitmap.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkCanvas.h"
13#include "include/core/SkColor.h"
14#include "include/core/SkFilterQuality.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040016#include "include/core/SkImageInfo.h"
17#include "include/core/SkPaint.h"
18#include "include/core/SkPoint.h"
19#include "include/core/SkRect.h"
20#include "include/core/SkRefCnt.h"
21#include "include/core/SkScalar.h"
22#include "include/core/SkShader.h"
23#include "include/core/SkSize.h"
24#include "include/core/SkString.h"
25#include "include/core/SkTileMode.h"
Robert Phillips08ba0852019-05-22 20:23:43 +000026#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "include/effects/SkGradientShader.h"
28#include "include/gpu/GrBackendSurface.h"
29#include "include/gpu/GrContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040030#include "include/gpu/GrTypes.h"
31#include "include/gpu/gl/GrGLFunctions.h"
32#include "include/gpu/gl/GrGLInterface.h"
33#include "include/gpu/gl/GrGLTypes.h"
34#include "include/private/GrTypesPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/GrContextPriv.h"
36#include "src/gpu/GrGpu.h"
37#include "src/gpu/gl/GrGLContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040038#include "src/gpu/gl/GrGLDefines.h"
39#include "src/gpu/gl/GrGLUtil.h"
40
41#include <algorithm>
42#include <cstdint>
43#include <memory>
44
45class GrRenderTargetContext;
bsalomone179a912016-01-20 06:18:10 -080046
47namespace skiagm {
Chris Dalton3a778372019-02-07 15:23:36 -070048class RectangleTexture : public GpuGM {
bsalomone179a912016-01-20 06:18:10 -080049public:
50 RectangleTexture() {
51 this->setBGColor(0xFFFFFFFF);
52 }
53
54protected:
55 SkString onShortName() override {
56 return SkString("rectangle_texture");
57 }
58
Brian Salomon246bc3d2018-12-06 15:33:02 -050059 SkISize onISize() override { return SkISize::Make(1200, 500); }
bsalomone179a912016-01-20 06:18:10 -080060
61 void fillPixels(int width, int height, void *pixels) {
62 SkBitmap bmp;
Brian Salomond696f8e2019-05-13 10:52:41 -040063 bmp.setInfo(SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
bsalomone179a912016-01-20 06:18:10 -080064 bmp.setPixels(pixels);
65 SkPaint paint;
66 SkCanvas canvas(bmp);
67 SkPoint pts[] = { {0, 0}, {0, SkIntToScalar(height)} };
68 SkColor colors0[] = { 0xFF1060B0 , 0xFF102030 };
Mike Reedfae8fce2019-04-03 10:27:45 -040069 paint.setShader(SkGradientShader::MakeLinear(pts, colors0, nullptr, 2, SkTileMode::kClamp));
bsalomone179a912016-01-20 06:18:10 -080070 canvas.drawPaint(paint);
71
Brian Salomon246bc3d2018-12-06 15:33:02 -050072 SkColor colors1[] = {0xFFA07010, 0xFFA02080};
bsalomone179a912016-01-20 06:18:10 -080073 paint.setAntiAlias(true);
Mike Reedfae8fce2019-04-03 10:27:45 -040074 paint.setShader(SkGradientShader::MakeLinear(pts, colors1, nullptr, 2, SkTileMode::kClamp));
bsalomone179a912016-01-20 06:18:10 -080075 canvas.drawCircle(SkIntToScalar(width) / 2, SkIntToScalar(height) / 2,
76 SkIntToScalar(width + height) / 5, paint);
77 }
78
Mike Klein16b1efb2019-04-02 10:01:11 -040079 static const GrGLContext* GetGLContextIfSupported(GrContext* context) {
Brian Salomond696f8e2019-05-13 10:52:41 -040080 if (context->backend() != GrBackendApi::kOpenGL) {
bsalomone179a912016-01-20 06:18:10 -080081 return nullptr;
82 }
Brian Salomond696f8e2019-05-13 10:52:41 -040083 auto* caps = static_cast<const GrGLCaps*>(context->priv().caps());
84 if (!caps->rectangleTextureSupport()) {
bsalomone179a912016-01-20 06:18:10 -080085 return nullptr;
86 }
Brian Salomond696f8e2019-05-13 10:52:41 -040087 return context->priv().getGpu()->glContextForTesting();
Mike Klein16b1efb2019-04-02 10:01:11 -040088 }
89
90 sk_sp<SkImage> createRectangleTextureImg(GrContext* context, GrSurfaceOrigin origin, int width,
91 int height, const uint32_t* pixels) {
92 const GrGLContext* glCtx = GetGLContextIfSupported(context);
93 if (!glCtx) {
94 return nullptr;
95 }
bsalomone179a912016-01-20 06:18:10 -080096
bsalomone179a912016-01-20 06:18:10 -080097 const GrGLInterface* gl = glCtx->interface();
Brian Salomon246bc3d2018-12-06 15:33:02 -050098 // Useful for debugging whether errors result from use of RECTANGLE
99 // static constexpr GrGLenum kTarget = GR_GL_TEXTURE_2D;
100 static constexpr GrGLenum kTarget = GR_GL_TEXTURE_RECTANGLE;
bsalomon3c481002016-03-21 09:04:26 -0700101 GrGLuint id = 0;
bsalomone179a912016-01-20 06:18:10 -0800102 GR_GL_CALL(gl, GenTextures(1, &id));
Brian Salomon246bc3d2018-12-06 15:33:02 -0500103 GR_GL_CALL(gl, BindTexture(kTarget, id));
104 GR_GL_CALL(gl, TexParameteri(kTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
105 GR_GL_CALL(gl, TexParameteri(kTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
106 GR_GL_CALL(gl, TexParameteri(kTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
107 GR_GL_CALL(gl, TexParameteri(kTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
108 std::unique_ptr<uint32_t[]> tempPixels;
109 if (origin == kBottomLeft_GrSurfaceOrigin) {
110 tempPixels.reset(new uint32_t[width * height]);
111 for (int y = 0; y < height; ++y) {
112 std::copy_n(pixels + width * (height - y - 1), width, tempPixels.get() + width * y);
113 }
114 pixels = tempPixels.get();
115 }
Brian Salomond696f8e2019-05-13 10:52:41 -0400116 GR_GL_CALL(gl, TexImage2D(kTarget, 0, GR_GL_RGBA, width, height, 0, GR_GL_RGBA,
Brian Salomon246bc3d2018-12-06 15:33:02 -0500117 GR_GL_UNSIGNED_BYTE, pixels));
bsalomone179a912016-01-20 06:18:10 -0800118
119 context->resetContext();
120 GrGLTextureInfo info;
121 info.fID = id;
Brian Salomon246bc3d2018-12-06 15:33:02 -0500122 info.fTarget = kTarget;
Greg Danielf5d87582017-12-18 14:48:15 -0500123 info.fFormat = GR_GL_RGBA8;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000124
Greg Danielf5d87582017-12-18 14:48:15 -0500125 GrBackendTexture rectangleTex(width, height, GrMipMapped::kNo, info);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000126
Brian Salomon246bc3d2018-12-06 15:33:02 -0500127 if (sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(context, rectangleTex, origin,
Greg Danielf5d87582017-12-18 14:48:15 -0500128 kRGBA_8888_SkColorType)) {
bsalomone179a912016-01-20 06:18:10 -0800129 return image;
130 }
131 GR_GL_CALL(gl, DeleteTextures(1, &id));
132 return nullptr;
133 }
134
Chris Dalton50e24d72019-02-07 16:20:09 -0700135 DrawResult onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas,
136 SkString* errorMsg) override {
Mike Klein16b1efb2019-04-02 10:01:11 -0400137 if (!GetGLContextIfSupported(context)) {
138 *errorMsg = "this GM requires an OpenGL 3.1+ context";
139 return DrawResult::kSkip;
140 }
141
mtkleindbfd7ab2016-09-01 11:24:54 -0700142 constexpr int kWidth = 50;
143 constexpr int kHeight = 50;
144 constexpr SkScalar kPad = 5.f;
bsalomone179a912016-01-20 06:18:10 -0800145
146 SkPMColor pixels[kWidth * kHeight];
147 this->fillPixels(kWidth, kHeight, pixels);
bsalomone179a912016-01-20 06:18:10 -0800148
Brian Salomon246bc3d2018-12-06 15:33:02 -0500149 sk_sp<SkImage> rectImgs[] = {
150 this->createRectangleTextureImg(context, kTopLeft_GrSurfaceOrigin, kWidth, kHeight,
151 pixels),
152 this->createRectangleTextureImg(context, kBottomLeft_GrSurfaceOrigin, kWidth,
153 kHeight, pixels),
154 };
155 SkASSERT(SkToBool(rectImgs[0]) == SkToBool(rectImgs[1]));
156 if (!rectImgs[0]) {
Chris Dalton50e24d72019-02-07 16:20:09 -0700157 *errorMsg = "Could not create rectangle texture image.";
158 return DrawResult::kFail;
bsalomone179a912016-01-20 06:18:10 -0800159 }
160
mtkleindbfd7ab2016-09-01 11:24:54 -0700161 constexpr SkFilterQuality kQualities[] = {
Brian Salomon246bc3d2018-12-06 15:33:02 -0500162 kNone_SkFilterQuality,
163 kLow_SkFilterQuality,
164 kMedium_SkFilterQuality,
165 kHigh_SkFilterQuality,
bsalomone179a912016-01-20 06:18:10 -0800166 };
167
Brian Salomon246bc3d2018-12-06 15:33:02 -0500168 constexpr SkScalar kScales[] = {1.0f, 1.2f, 0.75f};
bsalomone179a912016-01-20 06:18:10 -0800169
170 canvas->translate(kPad, kPad);
Brian Salomon246bc3d2018-12-06 15:33:02 -0500171 for (size_t i = 0; i < SK_ARRAY_COUNT(rectImgs); ++i) {
172 for (auto s : kScales) {
173 canvas->save();
174 canvas->scale(s, s);
175 for (auto q : kQualities) {
176 // drawImage
177 SkPaint plainPaint;
178 plainPaint.setFilterQuality(q);
179 canvas->drawImage(rectImgs[i], 0, 0, &plainPaint);
180 canvas->translate(kWidth + kPad, 0);
bsalomone179a912016-01-20 06:18:10 -0800181
Brian Salomon246bc3d2018-12-06 15:33:02 -0500182 // clamp/clamp shader
183 SkPaint clampPaint;
184 clampPaint.setFilterQuality(q);
185 clampPaint.setShader(rectImgs[i]->makeShader());
186 canvas->drawRect(SkRect::MakeWH(1.5f * kWidth, 1.5f * kHeight), clampPaint);
187 canvas->translate(kWidth * 1.5f + kPad, 0);
bsalomone179a912016-01-20 06:18:10 -0800188
Brian Salomon246bc3d2018-12-06 15:33:02 -0500189 // repeat/mirror shader
190 SkPaint repeatPaint;
191 repeatPaint.setFilterQuality(q);
Mike Reedfae8fce2019-04-03 10:27:45 -0400192 repeatPaint.setShader(rectImgs[i]->makeShader(SkTileMode::kRepeat,
193 SkTileMode::kMirror));
Brian Salomon246bc3d2018-12-06 15:33:02 -0500194 canvas->drawRect(SkRect::MakeWH(1.5f * kWidth, 1.5f * kHeight), repeatPaint);
195 canvas->translate(1.5f * kWidth + kPad, 0);
196
197 // drawImageRect with kStrict
198 auto srcRect = SkRect::MakeXYWH(.25f * rectImgs[i]->width(),
199 .25f * rectImgs[i]->height(),
200 .50f * rectImgs[i]->width(),
201 .50f * rectImgs[i]->height());
202 auto dstRect = SkRect::MakeXYWH(0, 0,
203 .50f * rectImgs[i]->width(),
204 .50f * rectImgs[i]->height());
205 canvas->drawImageRect(rectImgs[i], srcRect, dstRect, &plainPaint,
206 SkCanvas::kStrict_SrcRectConstraint);
207 canvas->translate(kWidth * .5f + kPad, 0);
208 }
209 canvas->restore();
210 canvas->translate(0, kPad + 1.5f * kHeight * s);
bsalomone179a912016-01-20 06:18:10 -0800211 }
bsalomone179a912016-01-20 06:18:10 -0800212 }
Chris Dalton50e24d72019-02-07 16:20:09 -0700213 return DrawResult::kOk;
bsalomone179a912016-01-20 06:18:10 -0800214 }
215
216private:
217 typedef GM INHERITED;
218};
219
220DEF_GM(return new RectangleTexture;)
221}