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