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 | |
| 8 | // This test only works with the GPU backend. |
| 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "gm/gm.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkBitmap.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 12 | #include "include/core/SkCanvas.h" |
| 13 | #include "include/core/SkColor.h" |
| 14 | #include "include/core/SkFilterQuality.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/core/SkImage.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 16 | #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 Phillips | 08ba085 | 2019-05-22 20:23:43 +0000 | [diff] [blame] | 26 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "include/effects/SkGradientShader.h" |
| 28 | #include "include/gpu/GrBackendSurface.h" |
| 29 | #include "include/gpu/GrContext.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 30 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 35 | #include "src/gpu/GrContextPriv.h" |
| 36 | #include "src/gpu/GrGpu.h" |
| 37 | #include "src/gpu/gl/GrGLContext.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 38 | #include "src/gpu/gl/GrGLDefines.h" |
| 39 | #include "src/gpu/gl/GrGLUtil.h" |
| 40 | |
| 41 | #include <algorithm> |
| 42 | #include <cstdint> |
| 43 | #include <memory> |
| 44 | |
| 45 | class GrRenderTargetContext; |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 46 | |
| 47 | namespace skiagm { |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 48 | class RectangleTexture : public GpuGM { |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 49 | public: |
| 50 | RectangleTexture() { |
| 51 | this->setBGColor(0xFFFFFFFF); |
| 52 | } |
| 53 | |
| 54 | protected: |
| 55 | SkString onShortName() override { |
| 56 | return SkString("rectangle_texture"); |
| 57 | } |
| 58 | |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 59 | SkISize onISize() override { return SkISize::Make(1200, 500); } |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 60 | |
| 61 | void fillPixels(int width, int height, void *pixels) { |
| 62 | SkBitmap bmp; |
Brian Salomon | d696f8e | 2019-05-13 10:52:41 -0400 | [diff] [blame] | 63 | bmp.setInfo(SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kOpaque_SkAlphaType)); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 64 | bmp.setPixels(pixels); |
| 65 | SkPaint paint; |
| 66 | SkCanvas canvas(bmp); |
| 67 | SkPoint pts[] = { {0, 0}, {0, SkIntToScalar(height)} }; |
| 68 | SkColor colors0[] = { 0xFF1060B0 , 0xFF102030 }; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 69 | paint.setShader(SkGradientShader::MakeLinear(pts, colors0, nullptr, 2, SkTileMode::kClamp)); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 70 | canvas.drawPaint(paint); |
| 71 | |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 72 | SkColor colors1[] = {0xFFA07010, 0xFFA02080}; |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 73 | paint.setAntiAlias(true); |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 74 | paint.setShader(SkGradientShader::MakeLinear(pts, colors1, nullptr, 2, SkTileMode::kClamp)); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 75 | canvas.drawCircle(SkIntToScalar(width) / 2, SkIntToScalar(height) / 2, |
| 76 | SkIntToScalar(width + height) / 5, paint); |
| 77 | } |
| 78 | |
Mike Klein | 16b1efb | 2019-04-02 10:01:11 -0400 | [diff] [blame] | 79 | static const GrGLContext* GetGLContextIfSupported(GrContext* context) { |
Brian Salomon | d696f8e | 2019-05-13 10:52:41 -0400 | [diff] [blame] | 80 | if (context->backend() != GrBackendApi::kOpenGL) { |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 81 | return nullptr; |
| 82 | } |
Brian Salomon | d696f8e | 2019-05-13 10:52:41 -0400 | [diff] [blame] | 83 | auto* caps = static_cast<const GrGLCaps*>(context->priv().caps()); |
| 84 | if (!caps->rectangleTextureSupport()) { |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 85 | return nullptr; |
| 86 | } |
Brian Salomon | d696f8e | 2019-05-13 10:52:41 -0400 | [diff] [blame] | 87 | return context->priv().getGpu()->glContextForTesting(); |
Mike Klein | 16b1efb | 2019-04-02 10:01:11 -0400 | [diff] [blame] | 88 | } |
| 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 | } |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 96 | |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 97 | const GrGLInterface* gl = glCtx->interface(); |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 98 | // 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; |
bsalomon | 3c48100 | 2016-03-21 09:04:26 -0700 | [diff] [blame] | 101 | GrGLuint id = 0; |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 102 | GR_GL_CALL(gl, GenTextures(1, &id)); |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 103 | 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 Salomon | d696f8e | 2019-05-13 10:52:41 -0400 | [diff] [blame] | 116 | GR_GL_CALL(gl, TexImage2D(kTarget, 0, GR_GL_RGBA, width, height, 0, GR_GL_RGBA, |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 117 | GR_GL_UNSIGNED_BYTE, pixels)); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 118 | |
| 119 | context->resetContext(); |
| 120 | GrGLTextureInfo info; |
| 121 | info.fID = id; |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 122 | info.fTarget = kTarget; |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 123 | info.fFormat = GR_GL_RGBA8; |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 124 | |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 125 | GrBackendTexture rectangleTex(width, height, GrMipMapped::kNo, info); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 126 | |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 127 | if (sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(context, rectangleTex, origin, |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 128 | kRGBA_8888_SkColorType)) { |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 129 | return image; |
| 130 | } |
| 131 | GR_GL_CALL(gl, DeleteTextures(1, &id)); |
| 132 | return nullptr; |
| 133 | } |
| 134 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 135 | DrawResult onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas, |
| 136 | SkString* errorMsg) override { |
Mike Klein | 16b1efb | 2019-04-02 10:01:11 -0400 | [diff] [blame] | 137 | if (!GetGLContextIfSupported(context)) { |
| 138 | *errorMsg = "this GM requires an OpenGL 3.1+ context"; |
| 139 | return DrawResult::kSkip; |
| 140 | } |
| 141 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 142 | constexpr int kWidth = 50; |
| 143 | constexpr int kHeight = 50; |
| 144 | constexpr SkScalar kPad = 5.f; |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 145 | |
| 146 | SkPMColor pixels[kWidth * kHeight]; |
| 147 | this->fillPixels(kWidth, kHeight, pixels); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 148 | |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 149 | 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 Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 157 | *errorMsg = "Could not create rectangle texture image."; |
| 158 | return DrawResult::kFail; |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 159 | } |
| 160 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 161 | constexpr SkFilterQuality kQualities[] = { |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 162 | kNone_SkFilterQuality, |
| 163 | kLow_SkFilterQuality, |
| 164 | kMedium_SkFilterQuality, |
| 165 | kHigh_SkFilterQuality, |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 166 | }; |
| 167 | |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 168 | constexpr SkScalar kScales[] = {1.0f, 1.2f, 0.75f}; |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 169 | |
| 170 | canvas->translate(kPad, kPad); |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 171 | 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); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 181 | |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 182 | // 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); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 188 | |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 189 | // repeat/mirror shader |
| 190 | SkPaint repeatPaint; |
| 191 | repeatPaint.setFilterQuality(q); |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 192 | repeatPaint.setShader(rectImgs[i]->makeShader(SkTileMode::kRepeat, |
| 193 | SkTileMode::kMirror)); |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 194 | 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); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 211 | } |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 212 | } |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 213 | return DrawResult::kOk; |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | private: |
| 217 | typedef GM INHERITED; |
| 218 | }; |
| 219 | |
| 220 | DEF_GM(return new RectangleTexture;) |
| 221 | } |