blob: b0e87611fa22ec7f011e890292f89045608e53c0 [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
Brian Salomonf4ba4ec2020-03-19 15:54:28 -04008// This test only works with the GL backend.
bsalomone179a912016-01-20 06:18:10 -08009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "gm/gm.h"
Brian Salomonf4ba4ec2020-03-19 15:54:28 -040011
12#ifdef SK_GL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkBitmap.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkCanvas.h"
15#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040017#include "include/core/SkImageInfo.h"
18#include "include/core/SkPaint.h"
19#include "include/core/SkPoint.h"
20#include "include/core/SkRect.h"
21#include "include/core/SkRefCnt.h"
22#include "include/core/SkScalar.h"
23#include "include/core/SkShader.h"
24#include "include/core/SkSize.h"
25#include "include/core/SkString.h"
26#include "include/core/SkTileMode.h"
Robert Phillips08ba0852019-05-22 20:23:43 +000027#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "include/effects/SkGradientShader.h"
29#include "include/gpu/GrBackendSurface.h"
Robert Phillipsb87b39b2020-07-01 14:45:24 -040030#include "include/gpu/GrDirectContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040031#include "include/gpu/GrTypes.h"
Brian Salomon0f396992020-06-19 19:51:21 -040032#include "src/core/SkAutoPixmapStorage.h"
Adlai Hollera0693042020-10-14 11:23:11 -040033#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/GrGpu.h"
Brian Salomon0f396992020-06-19 19:51:21 -040035#include "src/gpu/gl/GrGLCaps.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040036#include "src/gpu/gl/GrGLDefines.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040037
38#include <algorithm>
39#include <cstdint>
40#include <memory>
41
bsalomone179a912016-01-20 06:18:10 -080042namespace skiagm {
Robert Phillipsedcd4312021-06-03 10:14:16 -040043class RectangleTexture : public GM {
bsalomone179a912016-01-20 06:18:10 -080044public:
45 RectangleTexture() {
46 this->setBGColor(0xFFFFFFFF);
47 }
48
Brian Salomon3fcf83a2020-02-23 21:29:01 -050049private:
50 enum class ImageType {
51 kGradientCircle,
52 k2x2
53 };
54
bsalomone179a912016-01-20 06:18:10 -080055 SkString onShortName() override {
56 return SkString("rectangle_texture");
57 }
58
Brian Salomon3fcf83a2020-02-23 21:29:01 -050059 SkISize onISize() override { return SkISize::Make(1180, 710); }
bsalomone179a912016-01-20 06:18:10 -080060
Brian Salomon3fcf83a2020-02-23 21:29:01 -050061 SkBitmap makeImagePixels(int size, ImageType type) {
62 auto ii = SkImageInfo::Make(size, size, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
63 switch (type) {
64 case ImageType::kGradientCircle: {
65 SkBitmap bmp;
66 bmp.allocPixels(ii);
67 SkPaint paint;
68 SkCanvas canvas(bmp);
69 SkPoint pts[] = {{0, 0}, {0, SkIntToScalar(size)}};
70 SkColor colors0[] = {0xFF1060B0, 0xFF102030};
71 paint.setShader(
72 SkGradientShader::MakeLinear(pts, colors0, nullptr, 2, SkTileMode::kClamp));
73 canvas.drawPaint(paint);
74 SkColor colors1[] = {0xFFA07010, 0xFFA02080};
75 paint.setAntiAlias(true);
76 paint.setShader(
77 SkGradientShader::MakeLinear(pts, colors1, nullptr, 2, SkTileMode::kClamp));
78 canvas.drawCircle(size/2.f, size/2.f, 2.f*size/5, paint);
79 return bmp;
80 }
81 case ImageType::k2x2: {
82 SkBitmap bmp;
83 bmp.allocPixels(ii);
84 *bmp.getAddr32(0, 0) = 0xFF0000FF;
85 *bmp.getAddr32(1, 0) = 0xFF00FF00;
86 *bmp.getAddr32(0, 1) = 0xFFFF0000;
87 *bmp.getAddr32(1, 1) = 0xFFFFFFFF;
88 return bmp;
89 }
90 }
91 SkUNREACHABLE;
bsalomone179a912016-01-20 06:18:10 -080092 }
93
Adlai Hollere34b2822020-07-29 12:50:56 -040094 sk_sp<SkImage> createRectangleTextureImg(GrDirectContext* dContext, GrSurfaceOrigin origin,
Brian Salomon3fcf83a2020-02-23 21:29:01 -050095 const SkBitmap content) {
96 SkASSERT(content.colorType() == kRGBA_8888_SkColorType);
Brian Salomon0f396992020-06-19 19:51:21 -040097 auto format = GrBackendFormat::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_RECTANGLE);
Adlai Hollere34b2822020-07-29 12:50:56 -040098 auto bet = dContext->createBackendTexture(content.width(), content.height(), format,
Adlai Hollerf7129fc2020-08-04 14:07:40 -040099 GrMipmapped::kNo, GrRenderable::kNo);
Brian Salomon0f396992020-06-19 19:51:21 -0400100 if (!bet.isValid()) {
Mike Klein16b1efb2019-04-02 10:01:11 -0400101 return nullptr;
102 }
Brian Salomonb5f880a2020-12-07 11:30:16 -0500103 if (!dContext->updateBackendTexture(bet, content.pixmap(), origin, nullptr, nullptr)) {
Adlai Hollere34b2822020-07-29 12:50:56 -0400104 dContext->deleteBackendTexture(bet);
bsalomone179a912016-01-20 06:18:10 -0800105 }
Adlai Hollere34b2822020-07-29 12:50:56 -0400106 return SkImage::MakeFromAdoptedTexture(dContext, bet, origin, kRGBA_8888_SkColorType);
bsalomone179a912016-01-20 06:18:10 -0800107 }
108
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400109 DrawResult onGpuSetup(GrDirectContext* context, SkString* errorMsg) override {
Robert Phillipse3939012020-06-26 08:08:22 -0400110 if (!context || context->abandoned()) {
111 return DrawResult::kSkip;
112 }
113
Brian Salomon0f396992020-06-19 19:51:21 -0400114 if (context->backend() != GrBackendApi::kOpenGL_GrBackend ||
115 !static_cast<const GrGLCaps*>(context->priv().caps())->rectangleTextureSupport()) {
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500116 *errorMsg = "This GM requires an OpenGL context that supports texture rectangles.";
Mike Klein16b1efb2019-04-02 10:01:11 -0400117 return DrawResult::kSkip;
118 }
119
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500120 auto gradCircle = this->makeImagePixels(50, ImageType::kGradientCircle);
bsalomone179a912016-01-20 06:18:10 -0800121
Robert Phillipse3939012020-06-26 08:08:22 -0400122 fGradImgs[0] = this->createRectangleTextureImg(context, kTopLeft_GrSurfaceOrigin,
123 gradCircle);
124 fGradImgs[1] = this->createRectangleTextureImg(context, kBottomLeft_GrSurfaceOrigin,
125 gradCircle);
126 SkASSERT(SkToBool(fGradImgs[0]) == SkToBool(fGradImgs[1]));
127 if (!fGradImgs[0]) {
128 *errorMsg = "Could not create gradient rectangle texture images.";
Chris Dalton50e24d72019-02-07 16:20:09 -0700129 return DrawResult::kFail;
bsalomone179a912016-01-20 06:18:10 -0800130 }
131
Robert Phillipse3939012020-06-26 08:08:22 -0400132 fSmallImg = this->createRectangleTextureImg(context, kTopLeft_GrSurfaceOrigin,
133 this->makeImagePixels(2, ImageType::k2x2));
134 if (!fSmallImg) {
135 *errorMsg = "Could not create 2x2 rectangle texture image.";
136 return DrawResult::kFail;
137 }
138
139 return DrawResult::kOk;
140 }
141
142 void onGpuTeardown() override {
143 fGradImgs[0] = fGradImgs[1] = nullptr;
144 fSmallImg = nullptr;
145 }
146
Robert Phillipsedcd4312021-06-03 10:14:16 -0400147 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
Robert Phillipse3939012020-06-26 08:08:22 -0400148 SkASSERT(fGradImgs[0] && fGradImgs[1] && fSmallImg);
149
150 static constexpr SkScalar kPad = 5.f;
151
Mike Reed039f1362021-01-27 21:21:08 -0500152 const SkSamplingOptions kSamplings[] = {
153 SkSamplingOptions(SkFilterMode::kNearest),
154 SkSamplingOptions(SkFilterMode::kLinear),
155 SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
Mike Reedf3ac2af2021-02-05 12:55:38 -0500156 SkSamplingOptions(SkCubicResampler::Mitchell()),
bsalomone179a912016-01-20 06:18:10 -0800157 };
158
Brian Salomon246bc3d2018-12-06 15:33:02 -0500159 constexpr SkScalar kScales[] = {1.0f, 1.2f, 0.75f};
bsalomone179a912016-01-20 06:18:10 -0800160
161 canvas->translate(kPad, kPad);
Robert Phillipse3939012020-06-26 08:08:22 -0400162 for (size_t i = 0; i < kNumGradImages; ++i) {
163 auto img = fGradImgs[i];
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500164 int w = img->width();
165 int h = img->height();
John Stilesacf71642021-08-12 22:33:57 -0400166 for (auto scale : kScales) {
Brian Salomon246bc3d2018-12-06 15:33:02 -0500167 canvas->save();
John Stilesacf71642021-08-12 22:33:57 -0400168 canvas->scale(scale, scale);
Mike Reed039f1362021-01-27 21:21:08 -0500169 for (auto s : kSamplings) {
Brian Salomon246bc3d2018-12-06 15:33:02 -0500170 // drawImage
Mike Reed039f1362021-01-27 21:21:08 -0500171 canvas->drawImage(img, 0, 0, s);
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500172 canvas->translate(w + kPad, 0);
bsalomone179a912016-01-20 06:18:10 -0800173
Brian Salomon246bc3d2018-12-06 15:33:02 -0500174 // clamp/clamp shader
175 SkPaint clampPaint;
Mike Reed039f1362021-01-27 21:21:08 -0500176 clampPaint.setShader(fGradImgs[i]->makeShader(s));
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500177 canvas->drawRect(SkRect::MakeWH(1.5f*w, 1.5f*h), clampPaint);
178 canvas->translate(1.5f*w + kPad, 0);
bsalomone179a912016-01-20 06:18:10 -0800179
Brian Salomon246bc3d2018-12-06 15:33:02 -0500180 // repeat/mirror shader
181 SkPaint repeatPaint;
Robert Phillipse3939012020-06-26 08:08:22 -0400182 repeatPaint.setShader(fGradImgs[i]->makeShader(SkTileMode::kRepeat,
Mike Reedb86cd3d2020-12-10 14:55:43 -0500183 SkTileMode::kMirror,
Mike Reed039f1362021-01-27 21:21:08 -0500184 s));
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500185 canvas->drawRect(SkRect::MakeWH(1.5f*w, 1.5f*h), repeatPaint);
186 canvas->translate(1.5f*w + kPad, 0);
Brian Salomon246bc3d2018-12-06 15:33:02 -0500187
188 // drawImageRect with kStrict
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500189 auto srcRect = SkRect::MakeXYWH(.25f*w, .25f*h, .50f*w, .50f*h);
190 auto dstRect = SkRect::MakeXYWH( 0, 0, .50f*w, .50f*h);
Mike Reed039f1362021-01-27 21:21:08 -0500191 canvas->drawImageRect(fGradImgs[i], srcRect, dstRect, s, nullptr,
Brian Salomon246bc3d2018-12-06 15:33:02 -0500192 SkCanvas::kStrict_SrcRectConstraint);
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500193 canvas->translate(.5f*w + kPad, 0);
Brian Salomon246bc3d2018-12-06 15:33:02 -0500194 }
195 canvas->restore();
John Stilesacf71642021-08-12 22:33:57 -0400196 canvas->translate(0, kPad + 1.5f*h*scale);
bsalomone179a912016-01-20 06:18:10 -0800197 }
bsalomone179a912016-01-20 06:18:10 -0800198 }
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500199
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500200 static constexpr SkScalar kOutset = 25.f;
201 canvas->translate(kOutset, kOutset);
Robert Phillipse3939012020-06-26 08:08:22 -0400202 auto dstRect = SkRect::Make(fSmallImg->dimensions()).makeOutset(kOutset, kOutset);
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500203
Mike Reed2bdf6ed2021-07-15 21:34:48 -0400204 const SkSamplingOptions gSamplings[] = {
205 SkSamplingOptions(SkFilterMode::kNearest),
206 SkSamplingOptions(SkFilterMode::kLinear),
207 SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
208 SkSamplingOptions(SkCubicResampler::Mitchell()),
209 };
210
211 for (const auto& sampling : gSamplings) {
212 if (!sampling.useCubic && sampling.mipmap != SkMipmapMode::kNone) {
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500213 // Medium is the same as Low for upscaling.
214 continue;
215 }
216 canvas->save();
217 for (int ty = 0; ty < kSkTileModeCount; ++ty) {
218 canvas->save();
219 for (int tx = 0; tx < kSkTileModeCount; ++tx) {
220 SkMatrix lm;
221 lm.setRotate(45.f, 1, 1);
222 lm.postScale(6.5f, 6.5f);
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500223 SkPaint paint;
Mike Reedb86cd3d2020-12-10 14:55:43 -0500224 paint.setShader(fSmallImg->makeShader(static_cast<SkTileMode>(tx),
225 static_cast<SkTileMode>(ty),
Mike Reed2bdf6ed2021-07-15 21:34:48 -0400226 sampling,
Mike Reedb86cd3d2020-12-10 14:55:43 -0500227 lm));
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500228 canvas->drawRect(dstRect, paint);
229 canvas->translate(dstRect.width() + kPad, 0);
230 }
231 canvas->restore();
232 canvas->translate(0, dstRect.height() + kPad);
233 }
234 canvas->restore();
235 canvas->translate((dstRect.width() + kPad)*kSkTileModeCount, 0);
236 }
237
Chris Dalton50e24d72019-02-07 16:20:09 -0700238 return DrawResult::kOk;
bsalomone179a912016-01-20 06:18:10 -0800239 }
240
241private:
Robert Phillipse3939012020-06-26 08:08:22 -0400242 static const int kNumGradImages = 2;
243
244 sk_sp<SkImage> fGradImgs[kNumGradImages];
245 sk_sp<SkImage> fSmallImg;
246
John Stiles7571f9e2020-09-02 22:42:33 -0400247 using INHERITED = GM;
bsalomone179a912016-01-20 06:18:10 -0800248};
249
250DEF_GM(return new RectangleTexture;)
John Stilesa6841be2020-08-06 14:11:56 -0400251} // namespace skiagm
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400252#endif