blob: f21bc8f6026bac87dec8b657bbc7b6c4338e8572 [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"
16#include "include/core/SkFilterQuality.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040018#include "include/core/SkImageInfo.h"
19#include "include/core/SkPaint.h"
20#include "include/core/SkPoint.h"
21#include "include/core/SkRect.h"
22#include "include/core/SkRefCnt.h"
23#include "include/core/SkScalar.h"
24#include "include/core/SkShader.h"
25#include "include/core/SkSize.h"
26#include "include/core/SkString.h"
27#include "include/core/SkTileMode.h"
Robert Phillips08ba0852019-05-22 20:23:43 +000028#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "include/effects/SkGradientShader.h"
30#include "include/gpu/GrBackendSurface.h"
Robert Phillipsb87b39b2020-07-01 14:45:24 -040031#include "include/gpu/GrDirectContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040032#include "include/gpu/GrTypes.h"
Brian Salomon0f396992020-06-19 19:51:21 -040033#include "src/core/SkAutoPixmapStorage.h"
Adlai Hollera0693042020-10-14 11:23:11 -040034#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/GrGpu.h"
Brian Salomon0f396992020-06-19 19:51:21 -040036#include "src/gpu/gl/GrGLCaps.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040037#include "src/gpu/gl/GrGLDefines.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040038
39#include <algorithm>
40#include <cstdint>
41#include <memory>
42
Brian Salomoneebe7352020-12-09 16:37:04 -050043class GrSurfaceDrawContext;
bsalomone179a912016-01-20 06:18:10 -080044
45namespace skiagm {
Chris Dalton3a778372019-02-07 15:23:36 -070046class RectangleTexture : public GpuGM {
bsalomone179a912016-01-20 06:18:10 -080047public:
48 RectangleTexture() {
49 this->setBGColor(0xFFFFFFFF);
50 }
51
Brian Salomon3fcf83a2020-02-23 21:29:01 -050052private:
53 enum class ImageType {
54 kGradientCircle,
55 k2x2
56 };
57
bsalomone179a912016-01-20 06:18:10 -080058 SkString onShortName() override {
59 return SkString("rectangle_texture");
60 }
61
Brian Salomon3fcf83a2020-02-23 21:29:01 -050062 SkISize onISize() override { return SkISize::Make(1180, 710); }
bsalomone179a912016-01-20 06:18:10 -080063
Brian Salomon3fcf83a2020-02-23 21:29:01 -050064 SkBitmap makeImagePixels(int size, ImageType type) {
65 auto ii = SkImageInfo::Make(size, size, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
66 switch (type) {
67 case ImageType::kGradientCircle: {
68 SkBitmap bmp;
69 bmp.allocPixels(ii);
70 SkPaint paint;
71 SkCanvas canvas(bmp);
72 SkPoint pts[] = {{0, 0}, {0, SkIntToScalar(size)}};
73 SkColor colors0[] = {0xFF1060B0, 0xFF102030};
74 paint.setShader(
75 SkGradientShader::MakeLinear(pts, colors0, nullptr, 2, SkTileMode::kClamp));
76 canvas.drawPaint(paint);
77 SkColor colors1[] = {0xFFA07010, 0xFFA02080};
78 paint.setAntiAlias(true);
79 paint.setShader(
80 SkGradientShader::MakeLinear(pts, colors1, nullptr, 2, SkTileMode::kClamp));
81 canvas.drawCircle(size/2.f, size/2.f, 2.f*size/5, paint);
82 return bmp;
83 }
84 case ImageType::k2x2: {
85 SkBitmap bmp;
86 bmp.allocPixels(ii);
87 *bmp.getAddr32(0, 0) = 0xFF0000FF;
88 *bmp.getAddr32(1, 0) = 0xFF00FF00;
89 *bmp.getAddr32(0, 1) = 0xFFFF0000;
90 *bmp.getAddr32(1, 1) = 0xFFFFFFFF;
91 return bmp;
92 }
93 }
94 SkUNREACHABLE;
bsalomone179a912016-01-20 06:18:10 -080095 }
96
Adlai Hollere34b2822020-07-29 12:50:56 -040097 sk_sp<SkImage> createRectangleTextureImg(GrDirectContext* dContext, GrSurfaceOrigin origin,
Brian Salomon3fcf83a2020-02-23 21:29:01 -050098 const SkBitmap content) {
99 SkASSERT(content.colorType() == kRGBA_8888_SkColorType);
Brian Salomon0f396992020-06-19 19:51:21 -0400100 auto format = GrBackendFormat::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_RECTANGLE);
Adlai Hollere34b2822020-07-29 12:50:56 -0400101 auto bet = dContext->createBackendTexture(content.width(), content.height(), format,
Adlai Hollerf7129fc2020-08-04 14:07:40 -0400102 GrMipmapped::kNo, GrRenderable::kNo);
Brian Salomon0f396992020-06-19 19:51:21 -0400103 if (!bet.isValid()) {
Mike Klein16b1efb2019-04-02 10:01:11 -0400104 return nullptr;
105 }
Brian Salomonb5f880a2020-12-07 11:30:16 -0500106 if (!dContext->updateBackendTexture(bet, content.pixmap(), origin, nullptr, nullptr)) {
Adlai Hollere34b2822020-07-29 12:50:56 -0400107 dContext->deleteBackendTexture(bet);
bsalomone179a912016-01-20 06:18:10 -0800108 }
Adlai Hollere34b2822020-07-29 12:50:56 -0400109 return SkImage::MakeFromAdoptedTexture(dContext, bet, origin, kRGBA_8888_SkColorType);
bsalomone179a912016-01-20 06:18:10 -0800110 }
111
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400112 DrawResult onGpuSetup(GrDirectContext* context, SkString* errorMsg) override {
Robert Phillipse3939012020-06-26 08:08:22 -0400113 if (!context || context->abandoned()) {
114 return DrawResult::kSkip;
115 }
116
Brian Salomon0f396992020-06-19 19:51:21 -0400117 if (context->backend() != GrBackendApi::kOpenGL_GrBackend ||
118 !static_cast<const GrGLCaps*>(context->priv().caps())->rectangleTextureSupport()) {
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500119 *errorMsg = "This GM requires an OpenGL context that supports texture rectangles.";
Mike Klein16b1efb2019-04-02 10:01:11 -0400120 return DrawResult::kSkip;
121 }
122
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500123 auto gradCircle = this->makeImagePixels(50, ImageType::kGradientCircle);
bsalomone179a912016-01-20 06:18:10 -0800124
Robert Phillipse3939012020-06-26 08:08:22 -0400125 fGradImgs[0] = this->createRectangleTextureImg(context, kTopLeft_GrSurfaceOrigin,
126 gradCircle);
127 fGradImgs[1] = this->createRectangleTextureImg(context, kBottomLeft_GrSurfaceOrigin,
128 gradCircle);
129 SkASSERT(SkToBool(fGradImgs[0]) == SkToBool(fGradImgs[1]));
130 if (!fGradImgs[0]) {
131 *errorMsg = "Could not create gradient rectangle texture images.";
Chris Dalton50e24d72019-02-07 16:20:09 -0700132 return DrawResult::kFail;
bsalomone179a912016-01-20 06:18:10 -0800133 }
134
Robert Phillipse3939012020-06-26 08:08:22 -0400135 fSmallImg = this->createRectangleTextureImg(context, kTopLeft_GrSurfaceOrigin,
136 this->makeImagePixels(2, ImageType::k2x2));
137 if (!fSmallImg) {
138 *errorMsg = "Could not create 2x2 rectangle texture image.";
139 return DrawResult::kFail;
140 }
141
142 return DrawResult::kOk;
143 }
144
145 void onGpuTeardown() override {
146 fGradImgs[0] = fGradImgs[1] = nullptr;
147 fSmallImg = nullptr;
148 }
149
Brian Salomoneebe7352020-12-09 16:37:04 -0500150 DrawResult onDraw(GrRecordingContext*, GrSurfaceDrawContext*, SkCanvas* canvas,
Robert Phillips95c250c2020-06-29 15:36:12 -0400151 SkString*) override {
Robert Phillipse3939012020-06-26 08:08:22 -0400152 SkASSERT(fGradImgs[0] && fGradImgs[1] && fSmallImg);
153
154 static constexpr SkScalar kPad = 5.f;
155
Mike Reed039f1362021-01-27 21:21:08 -0500156 const SkSamplingOptions kSamplings[] = {
157 SkSamplingOptions(SkFilterMode::kNearest),
158 SkSamplingOptions(SkFilterMode::kLinear),
159 SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
160 SkSamplingOptions({1.0f/3, 1.0f/3}),
bsalomone179a912016-01-20 06:18:10 -0800161 };
162
Brian Salomon246bc3d2018-12-06 15:33:02 -0500163 constexpr SkScalar kScales[] = {1.0f, 1.2f, 0.75f};
bsalomone179a912016-01-20 06:18:10 -0800164
165 canvas->translate(kPad, kPad);
Robert Phillipse3939012020-06-26 08:08:22 -0400166 for (size_t i = 0; i < kNumGradImages; ++i) {
167 auto img = fGradImgs[i];
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500168 int w = img->width();
169 int h = img->height();
Brian Salomon246bc3d2018-12-06 15:33:02 -0500170 for (auto s : kScales) {
171 canvas->save();
172 canvas->scale(s, s);
Mike Reed039f1362021-01-27 21:21:08 -0500173 for (auto s : kSamplings) {
Brian Salomon246bc3d2018-12-06 15:33:02 -0500174 // drawImage
Mike Reed039f1362021-01-27 21:21:08 -0500175 canvas->drawImage(img, 0, 0, s);
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500176 canvas->translate(w + kPad, 0);
bsalomone179a912016-01-20 06:18:10 -0800177
Brian Salomon246bc3d2018-12-06 15:33:02 -0500178 // clamp/clamp shader
179 SkPaint clampPaint;
Mike Reed039f1362021-01-27 21:21:08 -0500180 clampPaint.setShader(fGradImgs[i]->makeShader(s));
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500181 canvas->drawRect(SkRect::MakeWH(1.5f*w, 1.5f*h), clampPaint);
182 canvas->translate(1.5f*w + kPad, 0);
bsalomone179a912016-01-20 06:18:10 -0800183
Brian Salomon246bc3d2018-12-06 15:33:02 -0500184 // repeat/mirror shader
185 SkPaint repeatPaint;
Robert Phillipse3939012020-06-26 08:08:22 -0400186 repeatPaint.setShader(fGradImgs[i]->makeShader(SkTileMode::kRepeat,
Mike Reedb86cd3d2020-12-10 14:55:43 -0500187 SkTileMode::kMirror,
Mike Reed039f1362021-01-27 21:21:08 -0500188 s));
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500189 canvas->drawRect(SkRect::MakeWH(1.5f*w, 1.5f*h), repeatPaint);
190 canvas->translate(1.5f*w + kPad, 0);
Brian Salomon246bc3d2018-12-06 15:33:02 -0500191
192 // drawImageRect with kStrict
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500193 auto srcRect = SkRect::MakeXYWH(.25f*w, .25f*h, .50f*w, .50f*h);
194 auto dstRect = SkRect::MakeXYWH( 0, 0, .50f*w, .50f*h);
Mike Reed039f1362021-01-27 21:21:08 -0500195 canvas->drawImageRect(fGradImgs[i], srcRect, dstRect, s, nullptr,
Brian Salomon246bc3d2018-12-06 15:33:02 -0500196 SkCanvas::kStrict_SrcRectConstraint);
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500197 canvas->translate(.5f*w + kPad, 0);
Brian Salomon246bc3d2018-12-06 15:33:02 -0500198 }
199 canvas->restore();
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500200 canvas->translate(0, kPad + 1.5f*h*s);
bsalomone179a912016-01-20 06:18:10 -0800201 }
bsalomone179a912016-01-20 06:18:10 -0800202 }
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500203
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500204 static constexpr SkScalar kOutset = 25.f;
205 canvas->translate(kOutset, kOutset);
Robert Phillipse3939012020-06-26 08:08:22 -0400206 auto dstRect = SkRect::Make(fSmallImg->dimensions()).makeOutset(kOutset, kOutset);
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500207
208 for (int fq = kNone_SkFilterQuality; fq <= kLast_SkFilterQuality; ++fq) {
209 if (fq == kMedium_SkFilterQuality) {
210 // Medium is the same as Low for upscaling.
211 continue;
212 }
213 canvas->save();
214 for (int ty = 0; ty < kSkTileModeCount; ++ty) {
215 canvas->save();
216 for (int tx = 0; tx < kSkTileModeCount; ++tx) {
217 SkMatrix lm;
218 lm.setRotate(45.f, 1, 1);
219 lm.postScale(6.5f, 6.5f);
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500220 SkPaint paint;
Mike Reedb86cd3d2020-12-10 14:55:43 -0500221 paint.setShader(fSmallImg->makeShader(static_cast<SkTileMode>(tx),
222 static_cast<SkTileMode>(ty),
223 SkSamplingOptions((SkFilterQuality)fq),
224 lm));
Brian Salomon3fcf83a2020-02-23 21:29:01 -0500225 canvas->drawRect(dstRect, paint);
226 canvas->translate(dstRect.width() + kPad, 0);
227 }
228 canvas->restore();
229 canvas->translate(0, dstRect.height() + kPad);
230 }
231 canvas->restore();
232 canvas->translate((dstRect.width() + kPad)*kSkTileModeCount, 0);
233 }
234
Chris Dalton50e24d72019-02-07 16:20:09 -0700235 return DrawResult::kOk;
bsalomone179a912016-01-20 06:18:10 -0800236 }
237
238private:
Robert Phillipse3939012020-06-26 08:08:22 -0400239 static const int kNumGradImages = 2;
240
241 sk_sp<SkImage> fGradImgs[kNumGradImages];
242 sk_sp<SkImage> fSmallImg;
243
John Stiles7571f9e2020-09-02 22:42:33 -0400244 using INHERITED = GM;
bsalomone179a912016-01-20 06:18:10 -0800245};
246
247DEF_GM(return new RectangleTexture;)
John Stilesa6841be2020-08-06 14:11:56 -0400248} // namespace skiagm
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400249#endif