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