Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 | |
| 2 /* | |
| 3 * Copyright 2016 Google Inc. | |
| 4 * | |
| 5 * Use of this source code is governed by a BSD-style license that can be | |
| 6 * found in the LICENSE file. | |
| 7 */ | |
| 8 | |
| 9 // This test only works with the GPU backend. | |
| 10 | |
| 11 #include "gm.h" | |
| 12 | |
| 13 #if SK_SUPPORT_GPU | |
| 14 | |
| 15 #include "GrContext.h" | |
| 16 #include "GrTest.h" | |
| 17 #include "SkBitmap.h" | |
| 18 #include "SkGradientShader.h" | |
| 19 #include "SkImage.h" | |
| 20 | |
| 21 namespace skiagm { | |
| 22 class RectangleTexture : public GM { | |
| 23 public: | |
| 24 RectangleTexture() { | |
| 25 this->setBGColor(0xFFFFFFFF); | |
| 26 } | |
| 27 | |
| 28 protected: | |
| 29 SkString onShortName() override { | |
| 30 return SkString("rectangle_texture"); | |
| 31 } | |
| 32 | |
| 33 SkISize onISize() override { | |
| 34 return SkISize::Make(1035, 240); | |
| 35 } | |
| 36 | |
| 37 void fillPixels(int width, int height, void *pixels) { | |
| 38 SkBitmap bmp; | |
| 39 bmp.setInfo(SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType), wi dth * 4); | |
| 40 bmp.setPixels(pixels); | |
| 41 SkPaint paint; | |
| 42 SkCanvas canvas(bmp); | |
| 43 SkPoint pts[] = { {0.f, 0.f}, {0.f, SkIntToScalar(height)} }; | |
| 44 SkColor colors0[] = { 0xFF1060B0 , 0xFF102030 }; | |
| 45 paint.setShader(SkGradientShader::CreateLinear(pts, colors0, nullptr, 2, | |
| 46 SkShader::kClamp_TileMode ))->unref(); | |
| 47 canvas.drawPaint(paint); | |
| 48 | |
| 49 SkColor colors1[] = { 0xFFA07010 , 0xFFA02080 }; | |
| 50 paint.setAntiAlias(true); | |
| 51 paint.setShader(SkGradientShader::CreateLinear(pts, colors1, nullptr, 2, | |
| 52 SkShader::kClamp_TileMode ))->unref(); | |
| 53 canvas.drawCircle(SkIntToScalar(width) / 2, SkIntToScalar(height) / 2, | |
| 54 SkIntToScalar(width + height) / 5, paint); | |
| 55 } | |
| 56 | |
| 57 SkImage* createRectangleTextureImg(GrContext* context, int width, int height , void* pixels) { | |
| 58 if (!context) { | |
| 59 return nullptr; | |
| 60 } | |
| 61 GrGpu* gpu = context->getGpu(); | |
| 62 if (!gpu) { | |
| 63 return nullptr; | |
| 64 } | |
| 65 const GrGLContext* glCtx = gpu->glContextForTesting(); | |
| 66 if (!glCtx) { | |
| 67 return nullptr; | |
| 68 } | |
| 69 | |
| 70 if (!(kGL_GrGLStandard == glCtx->standard() && glCtx->version() >= GR_GL _VER(3, 1)) && | |
|
egdaniel
2016/01/19 22:28:06
care to check glsl version?
bsalomon
2016/01/19 22:37:51
If the GrContext doesn't support rectangle (b/c of
| |
| 71 !glCtx->hasExtension("GL_ARB_texture_rectangle")) { | |
| 72 return nullptr; | |
| 73 } | |
| 74 | |
| 75 GrGLenum format; | |
| 76 if (kSkia8888_GrPixelConfig == kBGRA_8888_GrPixelConfig) { | |
| 77 format = GR_GL_BGRA; | |
| 78 } else { | |
| 79 SkASSERT(kSkia8888_GrPixelConfig == kRGBA_8888_GrPixelConfig); | |
| 80 format = GR_GL_RGBA; | |
| 81 } | |
| 82 | |
| 83 const GrGLInterface* gl = glCtx->interface(); | |
| 84 // Useful for debugging whether errors result from use of RECTANGLE | |
| 85 // #define TARGET GR_GL_TEXTURE_2D | |
| 86 #define TARGET GR_GL_TEXTURE_RECTANGLE | |
| 87 GrGLuint id; | |
| 88 GR_GL_CALL(gl, GenTextures(1, &id)); | |
| 89 GR_GL_CALL(gl, BindTexture(TARGET, id)); | |
| 90 GR_GL_CALL(gl, TexParameteri(TARGET, GR_GL_TEXTURE_MAG_FILTER, | |
| 91 GR_GL_NEAREST)); | |
| 92 GR_GL_CALL(gl, TexParameteri(TARGET, GR_GL_TEXTURE_MIN_FILTER, | |
| 93 GR_GL_NEAREST)); | |
| 94 GR_GL_CALL(gl, TexParameteri(TARGET, GR_GL_TEXTURE_WRAP_S, | |
| 95 GR_GL_CLAMP_TO_EDGE)); | |
| 96 GR_GL_CALL(gl, TexParameteri(TARGET, GR_GL_TEXTURE_WRAP_T, | |
| 97 GR_GL_CLAMP_TO_EDGE)); | |
| 98 GR_GL_CALL(gl, TexImage2D(TARGET, 0, GR_GL_RGBA, width, height, 0, | |
| 99 format, GR_GL_UNSIGNED_BYTE, pixels)); | |
| 100 | |
| 101 | |
| 102 context->resetContext(); | |
| 103 GrGLTextureInfo info; | |
| 104 info.fID = id; | |
| 105 info.fTarget = TARGET; | |
| 106 GrBackendTextureDesc desc; | |
| 107 desc.fConfig = kRGBA_8888_GrPixelConfig; | |
|
egdaniel
2016/01/19 22:28:05
should this ever be BGRA?
bsalomon
2016/01/19 22:37:51
Added comment that texture is always RGBA, but "pi
| |
| 108 desc.fWidth = width; | |
| 109 desc.fHeight = height; | |
| 110 desc.fOrigin = kTopLeft_GrSurfaceOrigin; | |
| 111 desc.fTextureHandle = reinterpret_cast<GrBackendObject>(&info); | |
| 112 if (SkImage* image = SkImage::NewFromAdoptedTexture(context, desc)) { | |
| 113 return image; | |
| 114 } | |
| 115 GR_GL_CALL(gl, DeleteTextures(1, &id)); | |
| 116 return nullptr; | |
| 117 } | |
| 118 | |
| 119 void deleteYUVTextures(GrContext* context, const GrBackendObject yuvHandles[ 3]) { | |
|
egdaniel
2016/01/19 22:28:05
function for other gm?
bsalomon
2016/01/19 22:37:51
Done.
| |
| 120 | |
| 121 const GrGpu* gpu = context->getGpu(); | |
| 122 if (!gpu) { | |
| 123 return; | |
| 124 } | |
| 125 | |
| 126 for (int i = 0; i < 3; ++i) { | |
| 127 gpu->deleteTestingOnlyBackendTexture(yuvHandles[i]); | |
| 128 } | |
| 129 | |
| 130 context->resetContext(); | |
| 131 } | |
| 132 | |
| 133 void onDraw(SkCanvas* canvas) override { | |
| 134 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget (); | |
| 135 GrContext* context; | |
| 136 if (!rt || !(context = rt->getContext())) { | |
| 137 skiagm::GM::DrawGpuOnlyMessage(canvas); | |
| 138 return; | |
| 139 } | |
| 140 | |
| 141 static const int kWidth = 50; | |
| 142 static const int kHeight = 50; | |
| 143 static const SkScalar kPad = 5.f; | |
| 144 | |
| 145 SkPMColor pixels[kWidth * kHeight]; | |
| 146 this->fillPixels(kWidth, kHeight, pixels); | |
| 147 SkAutoTUnref<SkImage> rectImg(this->createRectangleTextureImg(context, k Width, kHeight, | |
| 148 pixels)); | |
| 149 | |
| 150 if (!rectImg) { | |
| 151 SkPaint paint; | |
| 152 paint.setAntiAlias(true); | |
| 153 static const char* kMsg = "Could not create rectangle texture image. "; | |
| 154 canvas->drawText(kMsg, strlen(kMsg), 10.f, 100, paint); | |
| 155 return; | |
| 156 } | |
| 157 | |
| 158 static const SkFilterQuality kQualities[] = { | |
| 159 kNone_SkFilterQuality, | |
| 160 kLow_SkFilterQuality, | |
| 161 kMedium_SkFilterQuality, | |
| 162 kHigh_SkFilterQuality, | |
| 163 }; | |
| 164 | |
| 165 static const SkScalar kScales[] = { 1.0f, 1.2f, 0.75f }; | |
| 166 | |
| 167 canvas->translate(kPad, kPad); | |
| 168 for (auto s : kScales) { | |
| 169 canvas->save(); | |
| 170 canvas->scale(s, s); | |
| 171 for (auto q : kQualities) { | |
| 172 SkPaint plainPaint; | |
| 173 plainPaint.setFilterQuality(q); | |
| 174 canvas->drawImage(rectImg, 0, 0, &plainPaint); | |
| 175 canvas->translate(kWidth + kPad, 0); | |
| 176 | |
| 177 SkPaint clampPaint; | |
| 178 clampPaint.setFilterQuality(q); | |
| 179 clampPaint.setShader(rectImg->newShader(SkShader::kClamp_Til eMode, | |
| 180 SkShader::kClamp_Til eMode))->unref(); | |
| 181 canvas->drawRect(SkRect::MakeWH(1.5 * kWidth, 1.5 * kHeight) , clampPaint); | |
| 182 canvas->translate(kWidth * 1.5 + kPad, 0); | |
| 183 | |
| 184 SkPaint repeatPaint; | |
| 185 repeatPaint.setFilterQuality(q); | |
| 186 repeatPaint.setShader(rectImg->newShader(SkShader::kRepeat_T ileMode, | |
| 187 SkShader::kMirror_T ileMode))->unref(); | |
| 188 canvas->drawRect(SkRect::MakeIWH(1.5 * kWidth, 1.5 * kHeight ), repeatPaint); | |
| 189 canvas->translate(1.5 * kWidth + kPad, 0); | |
| 190 } | |
| 191 canvas->restore(); | |
| 192 canvas->translate(0, kPad + 1.5 * kHeight * s); | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 private: | |
| 197 typedef GM INHERITED; | |
| 198 }; | |
| 199 | |
| 200 DEF_GM(return new RectangleTexture;) | |
| 201 } | |
| 202 | |
| 203 #endif | |
| OLD | NEW |