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