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 | |
| 10 | #include "gm.h" |
| 11 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 12 | #include "GrBackendSurface.h" |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 13 | #include "GrContext.h" |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 14 | #include "GrContextPriv.h" |
robertphillips | 87f15c8 | 2016-05-20 11:14:33 -0700 | [diff] [blame] | 15 | #include "GrGpu.h" |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 16 | #include "GrTest.h" |
robertphillips | 87f15c8 | 2016-05-20 11:14:33 -0700 | [diff] [blame] | 17 | #include "gl/GrGLContext.h" |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 18 | #include "SkBitmap.h" |
| 19 | #include "SkGradientShader.h" |
| 20 | #include "SkImage.h" |
| 21 | |
| 22 | namespace skiagm { |
| 23 | class RectangleTexture : public GM { |
| 24 | public: |
| 25 | RectangleTexture() { |
| 26 | this->setBGColor(0xFFFFFFFF); |
| 27 | } |
| 28 | |
| 29 | protected: |
| 30 | SkString onShortName() override { |
| 31 | return SkString("rectangle_texture"); |
| 32 | } |
| 33 | |
| 34 | SkISize onISize() override { |
| 35 | return SkISize::Make(1035, 240); |
| 36 | } |
| 37 | |
| 38 | void fillPixels(int width, int height, void *pixels) { |
| 39 | SkBitmap bmp; |
| 40 | bmp.setInfo(SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType), width * 4); |
| 41 | bmp.setPixels(pixels); |
| 42 | SkPaint paint; |
| 43 | SkCanvas canvas(bmp); |
| 44 | SkPoint pts[] = { {0, 0}, {0, SkIntToScalar(height)} }; |
| 45 | SkColor colors0[] = { 0xFF1060B0 , 0xFF102030 }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 46 | paint.setShader(SkGradientShader::MakeLinear(pts, colors0, nullptr, 2, |
| 47 | SkShader::kClamp_TileMode)); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 48 | canvas.drawPaint(paint); |
| 49 | |
| 50 | SkColor colors1[] = { 0xFFA07010 , 0xFFA02080 }; |
| 51 | paint.setAntiAlias(true); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 52 | paint.setShader(SkGradientShader::MakeLinear(pts, colors1, nullptr, 2, |
| 53 | SkShader::kClamp_TileMode)); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 54 | canvas.drawCircle(SkIntToScalar(width) / 2, SkIntToScalar(height) / 2, |
| 55 | SkIntToScalar(width + height) / 5, paint); |
| 56 | } |
| 57 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 58 | sk_sp<SkImage> createRectangleTextureImg(GrContext* context, int width, int height, |
| 59 | void* pixels) { |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 60 | if (!context) { |
| 61 | return nullptr; |
| 62 | } |
Khushal | c421ca1 | 2018-06-26 14:38:34 -0700 | [diff] [blame] | 63 | if (context->abandoned()) { |
Robert Phillips | 6d36370 | 2018-06-25 08:53:09 -0400 | [diff] [blame] | 64 | return nullptr; |
| 65 | } |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 66 | GrGpu* gpu = context->contextPriv().getGpu(); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 67 | if (!gpu) { |
| 68 | return nullptr; |
| 69 | } |
| 70 | const GrGLContext* glCtx = gpu->glContextForTesting(); |
| 71 | if (!glCtx) { |
| 72 | return nullptr; |
| 73 | } |
| 74 | |
| 75 | if (!(kGL_GrGLStandard == glCtx->standard() && glCtx->version() >= GR_GL_VER(3, 1)) && |
Weiliang Chen | 1e04b36 | 2018-07-12 17:13:35 -0400 | [diff] [blame^] | 76 | !(glCtx->hasExtension("GL_ARB_texture_rectangle") || |
| 77 | glCtx->hasExtension("GL_ANGLE_texture_rectangle"))) { |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 78 | return nullptr; |
| 79 | } |
| 80 | |
| 81 | // We will always create the GL texture as GL_RGBA, however the pixels uploaded may be |
| 82 | // be RGBA or BGRA, depending on how SkPMColor was compiled. |
| 83 | GrGLenum format; |
| 84 | if (kSkia8888_GrPixelConfig == kBGRA_8888_GrPixelConfig) { |
| 85 | format = GR_GL_BGRA; |
| 86 | } else { |
| 87 | SkASSERT(kSkia8888_GrPixelConfig == kRGBA_8888_GrPixelConfig); |
| 88 | format = GR_GL_RGBA; |
| 89 | } |
| 90 | |
| 91 | const GrGLInterface* gl = glCtx->interface(); |
| 92 | // Useful for debugging whether errors result from use of RECTANGLE |
| 93 | // #define TARGET GR_GL_TEXTURE_2D |
| 94 | #define TARGET GR_GL_TEXTURE_RECTANGLE |
bsalomon | 3c48100 | 2016-03-21 09:04:26 -0700 | [diff] [blame] | 95 | GrGLuint id = 0; |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 96 | GR_GL_CALL(gl, GenTextures(1, &id)); |
| 97 | GR_GL_CALL(gl, BindTexture(TARGET, id)); |
| 98 | GR_GL_CALL(gl, TexParameteri(TARGET, GR_GL_TEXTURE_MAG_FILTER, |
| 99 | GR_GL_NEAREST)); |
| 100 | GR_GL_CALL(gl, TexParameteri(TARGET, GR_GL_TEXTURE_MIN_FILTER, |
| 101 | GR_GL_NEAREST)); |
| 102 | GR_GL_CALL(gl, TexParameteri(TARGET, GR_GL_TEXTURE_WRAP_S, |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 103 | GR_GL_CLAMP_TO_EDGE)); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 104 | GR_GL_CALL(gl, TexParameteri(TARGET, GR_GL_TEXTURE_WRAP_T, |
| 105 | GR_GL_CLAMP_TO_EDGE)); |
| 106 | GR_GL_CALL(gl, TexImage2D(TARGET, 0, GR_GL_RGBA, width, height, 0, |
| 107 | format, GR_GL_UNSIGNED_BYTE, pixels)); |
| 108 | |
| 109 | |
| 110 | context->resetContext(); |
| 111 | GrGLTextureInfo info; |
| 112 | info.fID = id; |
| 113 | info.fTarget = TARGET; |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 114 | info.fFormat = GR_GL_RGBA8; |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 115 | |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 116 | GrBackendTexture rectangleTex(width, height, GrMipMapped::kNo, info); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 117 | |
| 118 | if (sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(context, rectangleTex, |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 119 | kTopLeft_GrSurfaceOrigin, |
| 120 | kRGBA_8888_SkColorType)) { |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 121 | return image; |
| 122 | } |
| 123 | GR_GL_CALL(gl, DeleteTextures(1, &id)); |
| 124 | return nullptr; |
| 125 | } |
| 126 | |
| 127 | void onDraw(SkCanvas* canvas) override { |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 128 | GrContext *context = canvas->getGrContext(); |
| 129 | if (!context) { |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 130 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
| 131 | return; |
| 132 | } |
| 133 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 134 | constexpr int kWidth = 50; |
| 135 | constexpr int kHeight = 50; |
| 136 | constexpr SkScalar kPad = 5.f; |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 137 | |
| 138 | SkPMColor pixels[kWidth * kHeight]; |
| 139 | this->fillPixels(kWidth, kHeight, pixels); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 140 | sk_sp<SkImage> rectImg(this->createRectangleTextureImg(context, kWidth, kHeight, pixels)); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 141 | |
| 142 | if (!rectImg) { |
| 143 | SkPaint paint; |
| 144 | paint.setAntiAlias(true); |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 145 | const char* kMsg = "Could not create rectangle texture image."; |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 146 | canvas->drawString(kMsg, 10, 100, paint); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 147 | return; |
| 148 | } |
| 149 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 150 | constexpr SkFilterQuality kQualities[] = { |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 151 | kNone_SkFilterQuality, |
| 152 | kLow_SkFilterQuality, |
| 153 | kMedium_SkFilterQuality, |
| 154 | kHigh_SkFilterQuality, |
| 155 | }; |
| 156 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 157 | constexpr SkScalar kScales[] = { 1.0f, 1.2f, 0.75f }; |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 158 | |
| 159 | canvas->translate(kPad, kPad); |
| 160 | for (auto s : kScales) { |
| 161 | canvas->save(); |
| 162 | canvas->scale(s, s); |
| 163 | for (auto q : kQualities) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 164 | SkPaint plainPaint; |
| 165 | plainPaint.setFilterQuality(q); |
| 166 | canvas->drawImage(rectImg.get(), 0, 0, &plainPaint); |
| 167 | canvas->translate(kWidth + kPad, 0); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 168 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 169 | SkPaint clampPaint; |
| 170 | clampPaint.setFilterQuality(q); |
Mike Reed | 0acd795 | 2017-04-28 11:12:19 -0400 | [diff] [blame] | 171 | clampPaint.setShader(rectImg->makeShader()); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 172 | canvas->drawRect(SkRect::MakeWH(1.5f * kWidth, 1.5f * kHeight), clampPaint); |
| 173 | canvas->translate(kWidth * 1.5f + kPad, 0); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 174 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 175 | SkPaint repeatPaint; |
| 176 | repeatPaint.setFilterQuality(q); |
| 177 | repeatPaint.setShader(rectImg->makeShader(SkShader::kRepeat_TileMode, |
| 178 | SkShader::kMirror_TileMode)); |
| 179 | canvas->drawRect(SkRect::MakeWH(1.5f * kWidth, 1.5f * kHeight), repeatPaint); |
| 180 | canvas->translate(1.5f * kWidth + kPad, 0); |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 181 | } |
| 182 | canvas->restore(); |
| 183 | canvas->translate(0, kPad + 1.5f * kHeight * s); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | private: |
| 188 | typedef GM INHERITED; |
| 189 | }; |
| 190 | |
| 191 | DEF_GM(return new RectangleTexture;) |
| 192 | } |