bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 1 | /* |
| 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 Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 8 | // This test only works with the GL backend. |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "gm/gm.h" |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 11 | |
| 12 | #ifdef SK_GL |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkBitmap.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 14 | #include "include/core/SkCanvas.h" |
| 15 | #include "include/core/SkColor.h" |
| 16 | #include "include/core/SkFilterQuality.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/core/SkImage.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 18 | #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 Phillips | 08ba085 | 2019-05-22 20:23:43 +0000 | [diff] [blame] | 28 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "include/effects/SkGradientShader.h" |
| 30 | #include "include/gpu/GrBackendSurface.h" |
Robert Phillips | b87b39b | 2020-07-01 14:45:24 -0400 | [diff] [blame] | 31 | #include "include/gpu/GrDirectContext.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 32 | #include "include/gpu/GrTypes.h" |
Brian Salomon | 0f39699 | 2020-06-19 19:51:21 -0400 | [diff] [blame] | 33 | #include "src/core/SkAutoPixmapStorage.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 34 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 35 | #include "src/gpu/GrGpu.h" |
Brian Salomon | 0f39699 | 2020-06-19 19:51:21 -0400 | [diff] [blame] | 36 | #include "src/gpu/gl/GrGLCaps.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 37 | #include "src/gpu/gl/GrGLDefines.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 38 | |
| 39 | #include <algorithm> |
| 40 | #include <cstdint> |
| 41 | #include <memory> |
| 42 | |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 43 | class GrSurfaceDrawContext; |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 44 | |
| 45 | namespace skiagm { |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 46 | class RectangleTexture : public GpuGM { |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 47 | public: |
| 48 | RectangleTexture() { |
| 49 | this->setBGColor(0xFFFFFFFF); |
| 50 | } |
| 51 | |
Brian Salomon | 3fcf83a | 2020-02-23 21:29:01 -0500 | [diff] [blame] | 52 | private: |
| 53 | enum class ImageType { |
| 54 | kGradientCircle, |
| 55 | k2x2 |
| 56 | }; |
| 57 | |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 58 | SkString onShortName() override { |
| 59 | return SkString("rectangle_texture"); |
| 60 | } |
| 61 | |
Brian Salomon | 3fcf83a | 2020-02-23 21:29:01 -0500 | [diff] [blame] | 62 | SkISize onISize() override { return SkISize::Make(1180, 710); } |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 63 | |
Brian Salomon | 3fcf83a | 2020-02-23 21:29:01 -0500 | [diff] [blame] | 64 | 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; |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Adlai Holler | e34b282 | 2020-07-29 12:50:56 -0400 | [diff] [blame] | 97 | sk_sp<SkImage> createRectangleTextureImg(GrDirectContext* dContext, GrSurfaceOrigin origin, |
Brian Salomon | 3fcf83a | 2020-02-23 21:29:01 -0500 | [diff] [blame] | 98 | const SkBitmap content) { |
| 99 | SkASSERT(content.colorType() == kRGBA_8888_SkColorType); |
Brian Salomon | 0f39699 | 2020-06-19 19:51:21 -0400 | [diff] [blame] | 100 | auto format = GrBackendFormat::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_RECTANGLE); |
Adlai Holler | e34b282 | 2020-07-29 12:50:56 -0400 | [diff] [blame] | 101 | auto bet = dContext->createBackendTexture(content.width(), content.height(), format, |
Adlai Holler | f7129fc | 2020-08-04 14:07:40 -0400 | [diff] [blame] | 102 | GrMipmapped::kNo, GrRenderable::kNo); |
Brian Salomon | 0f39699 | 2020-06-19 19:51:21 -0400 | [diff] [blame] | 103 | if (!bet.isValid()) { |
Mike Klein | 16b1efb | 2019-04-02 10:01:11 -0400 | [diff] [blame] | 104 | return nullptr; |
| 105 | } |
Brian Salomon | b5f880a | 2020-12-07 11:30:16 -0500 | [diff] [blame] | 106 | if (!dContext->updateBackendTexture(bet, content.pixmap(), origin, nullptr, nullptr)) { |
Adlai Holler | e34b282 | 2020-07-29 12:50:56 -0400 | [diff] [blame] | 107 | dContext->deleteBackendTexture(bet); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 108 | } |
Adlai Holler | e34b282 | 2020-07-29 12:50:56 -0400 | [diff] [blame] | 109 | return SkImage::MakeFromAdoptedTexture(dContext, bet, origin, kRGBA_8888_SkColorType); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Robert Phillips | b87b39b | 2020-07-01 14:45:24 -0400 | [diff] [blame] | 112 | DrawResult onGpuSetup(GrDirectContext* context, SkString* errorMsg) override { |
Robert Phillips | e393901 | 2020-06-26 08:08:22 -0400 | [diff] [blame] | 113 | if (!context || context->abandoned()) { |
| 114 | return DrawResult::kSkip; |
| 115 | } |
| 116 | |
Brian Salomon | 0f39699 | 2020-06-19 19:51:21 -0400 | [diff] [blame] | 117 | if (context->backend() != GrBackendApi::kOpenGL_GrBackend || |
| 118 | !static_cast<const GrGLCaps*>(context->priv().caps())->rectangleTextureSupport()) { |
Brian Salomon | 3fcf83a | 2020-02-23 21:29:01 -0500 | [diff] [blame] | 119 | *errorMsg = "This GM requires an OpenGL context that supports texture rectangles."; |
Mike Klein | 16b1efb | 2019-04-02 10:01:11 -0400 | [diff] [blame] | 120 | return DrawResult::kSkip; |
| 121 | } |
| 122 | |
Brian Salomon | 3fcf83a | 2020-02-23 21:29:01 -0500 | [diff] [blame] | 123 | auto gradCircle = this->makeImagePixels(50, ImageType::kGradientCircle); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 124 | |
Robert Phillips | e393901 | 2020-06-26 08:08:22 -0400 | [diff] [blame] | 125 | 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 Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 132 | return DrawResult::kFail; |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Robert Phillips | e393901 | 2020-06-26 08:08:22 -0400 | [diff] [blame] | 135 | 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 Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 150 | DrawResult onDraw(GrRecordingContext*, GrSurfaceDrawContext*, SkCanvas* canvas, |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 151 | SkString*) override { |
Robert Phillips | e393901 | 2020-06-26 08:08:22 -0400 | [diff] [blame] | 152 | SkASSERT(fGradImgs[0] && fGradImgs[1] && fSmallImg); |
| 153 | |
| 154 | static constexpr SkScalar kPad = 5.f; |
| 155 | |
Mike Reed | 039f136 | 2021-01-27 21:21:08 -0500 | [diff] [blame] | 156 | 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}), |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 161 | }; |
| 162 | |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 163 | constexpr SkScalar kScales[] = {1.0f, 1.2f, 0.75f}; |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 164 | |
| 165 | canvas->translate(kPad, kPad); |
Robert Phillips | e393901 | 2020-06-26 08:08:22 -0400 | [diff] [blame] | 166 | for (size_t i = 0; i < kNumGradImages; ++i) { |
| 167 | auto img = fGradImgs[i]; |
Brian Salomon | 3fcf83a | 2020-02-23 21:29:01 -0500 | [diff] [blame] | 168 | int w = img->width(); |
| 169 | int h = img->height(); |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 170 | for (auto s : kScales) { |
| 171 | canvas->save(); |
| 172 | canvas->scale(s, s); |
Mike Reed | 039f136 | 2021-01-27 21:21:08 -0500 | [diff] [blame] | 173 | for (auto s : kSamplings) { |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 174 | // drawImage |
Mike Reed | 039f136 | 2021-01-27 21:21:08 -0500 | [diff] [blame] | 175 | canvas->drawImage(img, 0, 0, s); |
Brian Salomon | 3fcf83a | 2020-02-23 21:29:01 -0500 | [diff] [blame] | 176 | canvas->translate(w + kPad, 0); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 177 | |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 178 | // clamp/clamp shader |
| 179 | SkPaint clampPaint; |
Mike Reed | 039f136 | 2021-01-27 21:21:08 -0500 | [diff] [blame] | 180 | clampPaint.setShader(fGradImgs[i]->makeShader(s)); |
Brian Salomon | 3fcf83a | 2020-02-23 21:29:01 -0500 | [diff] [blame] | 181 | canvas->drawRect(SkRect::MakeWH(1.5f*w, 1.5f*h), clampPaint); |
| 182 | canvas->translate(1.5f*w + kPad, 0); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 183 | |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 184 | // repeat/mirror shader |
| 185 | SkPaint repeatPaint; |
Robert Phillips | e393901 | 2020-06-26 08:08:22 -0400 | [diff] [blame] | 186 | repeatPaint.setShader(fGradImgs[i]->makeShader(SkTileMode::kRepeat, |
Mike Reed | b86cd3d | 2020-12-10 14:55:43 -0500 | [diff] [blame] | 187 | SkTileMode::kMirror, |
Mike Reed | 039f136 | 2021-01-27 21:21:08 -0500 | [diff] [blame] | 188 | s)); |
Brian Salomon | 3fcf83a | 2020-02-23 21:29:01 -0500 | [diff] [blame] | 189 | canvas->drawRect(SkRect::MakeWH(1.5f*w, 1.5f*h), repeatPaint); |
| 190 | canvas->translate(1.5f*w + kPad, 0); |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 191 | |
| 192 | // drawImageRect with kStrict |
Brian Salomon | 3fcf83a | 2020-02-23 21:29:01 -0500 | [diff] [blame] | 193 | auto srcRect = SkRect::MakeXYWH(.25f*w, .25f*h, .50f*w, .50f*h); |
| 194 | auto dstRect = SkRect::MakeXYWH( 0, 0, .50f*w, .50f*h); |
Mike Reed | 039f136 | 2021-01-27 21:21:08 -0500 | [diff] [blame] | 195 | canvas->drawImageRect(fGradImgs[i], srcRect, dstRect, s, nullptr, |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 196 | SkCanvas::kStrict_SrcRectConstraint); |
Brian Salomon | 3fcf83a | 2020-02-23 21:29:01 -0500 | [diff] [blame] | 197 | canvas->translate(.5f*w + kPad, 0); |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 198 | } |
| 199 | canvas->restore(); |
Brian Salomon | 3fcf83a | 2020-02-23 21:29:01 -0500 | [diff] [blame] | 200 | canvas->translate(0, kPad + 1.5f*h*s); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 201 | } |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 202 | } |
Brian Salomon | 3fcf83a | 2020-02-23 21:29:01 -0500 | [diff] [blame] | 203 | |
Brian Salomon | 3fcf83a | 2020-02-23 21:29:01 -0500 | [diff] [blame] | 204 | static constexpr SkScalar kOutset = 25.f; |
| 205 | canvas->translate(kOutset, kOutset); |
Robert Phillips | e393901 | 2020-06-26 08:08:22 -0400 | [diff] [blame] | 206 | auto dstRect = SkRect::Make(fSmallImg->dimensions()).makeOutset(kOutset, kOutset); |
Brian Salomon | 3fcf83a | 2020-02-23 21:29:01 -0500 | [diff] [blame] | 207 | |
| 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 Salomon | 3fcf83a | 2020-02-23 21:29:01 -0500 | [diff] [blame] | 220 | SkPaint paint; |
Mike Reed | b86cd3d | 2020-12-10 14:55:43 -0500 | [diff] [blame] | 221 | paint.setShader(fSmallImg->makeShader(static_cast<SkTileMode>(tx), |
| 222 | static_cast<SkTileMode>(ty), |
| 223 | SkSamplingOptions((SkFilterQuality)fq), |
| 224 | lm)); |
Brian Salomon | 3fcf83a | 2020-02-23 21:29:01 -0500 | [diff] [blame] | 225 | 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 Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 235 | return DrawResult::kOk; |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | private: |
Robert Phillips | e393901 | 2020-06-26 08:08:22 -0400 | [diff] [blame] | 239 | static const int kNumGradImages = 2; |
| 240 | |
| 241 | sk_sp<SkImage> fGradImgs[kNumGradImages]; |
| 242 | sk_sp<SkImage> fSmallImg; |
| 243 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 244 | using INHERITED = GM; |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | DEF_GM(return new RectangleTexture;) |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 248 | } // namespace skiagm |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 249 | #endif |