OLD | NEW |
(Empty) | |
| 1 |
| 2 /* |
| 3 * Copyright 2015 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 "gl/GrGLInterface.h" |
| 17 #include "gl/GrGLUtil.h" |
| 18 #include "GrTest.h" |
| 19 #include "SkBitmap.h" |
| 20 #include "SkGradientShader.h" |
| 21 #include "SkImage.h" |
| 22 |
| 23 namespace skiagm { |
| 24 class ImageFromYUVTextures : public GM { |
| 25 public: |
| 26 ImageFromYUVTextures() { |
| 27 this->setBGColor(0xFFFFFFFF); |
| 28 } |
| 29 |
| 30 protected: |
| 31 SkString onShortName() override { |
| 32 return SkString("image_from_yuv_textures"); |
| 33 } |
| 34 |
| 35 SkISize onISize() override { |
| 36 return SkISize::Make(50, 135); |
| 37 } |
| 38 |
| 39 void onOnceBeforeDraw() override { |
| 40 // We create an RGB bitmap and then extract YUV bmps where the U and V b
itmaps are |
| 41 // subsampled by 2 in both dimensions. |
| 42 SkPaint paint; |
| 43 static const SkColor kColors[] = |
| 44 { SK_ColorBLUE, SK_ColorYELLOW, SK_ColorGREEN, SK_ColorWHITE }; |
| 45 paint.setShader(SkGradientShader::CreateRadial(SkPoint::Make(0,0), kBmpS
ize / 2.f, kColors, |
| 46 NULL, SK_ARRAY_COUNT(kCol
ors), |
| 47 SkShader::kMirror_TileMod
e))->unref(); |
| 48 SkBitmap rgbBmp; |
| 49 rgbBmp.allocN32Pixels(kBmpSize, kBmpSize, true); |
| 50 SkCanvas canvas(rgbBmp); |
| 51 canvas.drawPaint(paint); |
| 52 SkPMColor* rgbColors = static_cast<SkPMColor*>(rgbBmp.getPixels()); |
| 53 |
| 54 SkImageInfo yinfo = SkImageInfo::MakeA8(kBmpSize, kBmpSize); |
| 55 fYUVBmps[0].allocPixels(yinfo); |
| 56 SkImageInfo uinfo = SkImageInfo::MakeA8(kBmpSize / 2, kBmpSize / 2); |
| 57 fYUVBmps[1].allocPixels(uinfo); |
| 58 SkImageInfo vinfo = SkImageInfo::MakeA8(kBmpSize / 2, kBmpSize / 2); |
| 59 fYUVBmps[2].allocPixels(vinfo); |
| 60 unsigned char* yPixels; |
| 61 signed char* uvPixels[2]; |
| 62 yPixels = static_cast<unsigned char*>(fYUVBmps[0].getPixels()); |
| 63 uvPixels[0] = static_cast<signed char*>(fYUVBmps[1].getPixels()); |
| 64 uvPixels[1] = static_cast<signed char*>(fYUVBmps[2 |
| 65 ].getPixels()); |
| 66 |
| 67 // Here we encode using the NTC encoding (even though we will draw it wi
th all the supported |
| 68 // yuv color spaces when converted back to RGB) |
| 69 for (int i = 0; i < kBmpSize * kBmpSize; ++i) { |
| 70 yPixels[i] = static_cast<unsigned char>(0.299f * SkGetPackedR32(rgbC
olors[i]) + |
| 71 0.587f * SkGetPackedG32(rgbC
olors[i]) + |
| 72 0.114f * SkGetPackedB32(rgbC
olors[i])); |
| 73 } |
| 74 for (int j = 0; j < kBmpSize / 2; ++j) { |
| 75 for (int i = 0; i < kBmpSize / 2; ++i) { |
| 76 // Average together 4 pixels of RGB. |
| 77 int rgb[] = { 0, 0, 0 }; |
| 78 for (int y = 0; y < 2; ++y) { |
| 79 for (int x = 0; x < 2; ++x) { |
| 80 int rgbIndex = (2 * j + y) * kBmpSize + 2 * i + x; |
| 81 rgb[0] += SkGetPackedR32(rgbColors[rgbIndex]); |
| 82 rgb[1] += SkGetPackedG32(rgbColors[rgbIndex]); |
| 83 rgb[2] += SkGetPackedB32(rgbColors[rgbIndex]); |
| 84 } |
| 85 } |
| 86 for (int c = 0; c < 3; ++c) { |
| 87 rgb[c] /= 4; |
| 88 } |
| 89 int uvIndex = j * kBmpSize / 2 + i; |
| 90 uvPixels[0][uvIndex] = static_cast<signed char>( |
| 91 ((-38 * rgb[0] - 74 * rgb[1] + 112 * rgb[2] + 128) >> 8) +
128); |
| 92 uvPixels[1][uvIndex] = static_cast<signed char>( |
| 93 ((112 * rgb[0] - 94 * rgb[1] - 18 * rgb[2] + 128) >> 8) +
128); |
| 94 } |
| 95 } |
| 96 fRGBImage.reset(SkImage::NewRasterCopy(rgbBmp.info(), rgbColors, rgbBmp.
rowBytes())); |
| 97 } |
| 98 |
| 99 void createYUVTextures(GrContext* context, GrGLuint yuvIDs[3]) { |
| 100 GrTestTarget tt; |
| 101 context->getTestTarget(&tt); |
| 102 if (!tt.target()) { |
| 103 SkDEBUGFAIL("Couldn't get Gr test target."); |
| 104 return; |
| 105 } |
| 106 |
| 107 // We currently hav only implemented the texture uploads for GL. |
| 108 const GrGLInterface* gl = tt.glInterface(); |
| 109 if (!gl) { |
| 110 return; |
| 111 } |
| 112 |
| 113 GR_GL_CALL(gl, GenTextures(3, yuvIDs)); |
| 114 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0)); |
| 115 GR_GL_CALL(gl, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1)); |
| 116 for (int i = 0; i < 3; ++i) { |
| 117 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, yuvIDs[i])); |
| 118 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MAG_FIL
TER, |
| 119 GR_GL_NEAREST)); |
| 120 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MIN_FIL
TER, |
| 121 GR_GL_NEAREST)); |
| 122 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_WRAP_S, |
| 123 GR_GL_CLAMP_TO_EDGE)); |
| 124 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_WRAP_T, |
| 125 GR_GL_CLAMP_TO_EDGE)); |
| 126 SkASSERT(fYUVBmps[i].width() == SkToInt(fYUVBmps[i].rowBytes())); |
| 127 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RED, fYUVBmps[i
].width(), |
| 128 fYUVBmps[i].height(), 0, GR_GL_RED, GR_GL_
UNSIGNED_BYTE, |
| 129 fYUVBmps[i].getPixels())); |
| 130 } |
| 131 context->resetContext(); |
| 132 } |
| 133 |
| 134 void deleteYUVTextures(GrContext* context, const GrGLuint yuvIDs[3]) { |
| 135 GrTestTarget tt; |
| 136 context->getTestTarget(&tt); |
| 137 if (!tt.target()) { |
| 138 SkDEBUGFAIL("Couldn't get Gr test target."); |
| 139 return; |
| 140 } |
| 141 |
| 142 const GrGLInterface* gl = tt.glInterface(); |
| 143 if (!gl) { |
| 144 return; |
| 145 } |
| 146 GR_GL_CALL(gl, DeleteTextures(3, yuvIDs)); |
| 147 context->resetContext(); |
| 148 } |
| 149 |
| 150 void onDraw(SkCanvas* canvas) override { |
| 151 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget
(); |
| 152 GrContext* context; |
| 153 if (!rt || !(context = rt->getContext())) { |
| 154 this->drawGpuOnlyMessage(canvas); |
| 155 return; |
| 156 } |
| 157 |
| 158 GrGLuint yuvIDs[3]; |
| 159 this->createYUVTextures(context, yuvIDs); |
| 160 |
| 161 static const SkScalar kPad = 10.f; |
| 162 |
| 163 GrBackendObject backendTextureObjects[] = { |
| 164 static_cast<GrBackendObject>(yuvIDs[0]), |
| 165 static_cast<GrBackendObject>(yuvIDs[1]), |
| 166 static_cast<GrBackendObject>(yuvIDs[2]) |
| 167 }; |
| 168 SkISize sizes[] = { |
| 169 { fYUVBmps[0].width(), fYUVBmps[0].height()}, |
| 170 { fYUVBmps[1].width(), fYUVBmps[1].height()}, |
| 171 { fYUVBmps[2].width(), fYUVBmps[2].height()}, |
| 172 }; |
| 173 SkTArray<SkImage*> images; |
| 174 images.push_back(SkRef(fRGBImage.get())); |
| 175 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpa
ce; ++space) { |
| 176 images.push_back(SkImage::NewFromYUVTexturesCopy(context, |
| 177 static_cast<SkYUVCo
lorSpace>(space), |
| 178 backendTextureObjec
ts, sizes, |
| 179 kTopLeft_GrSurfaceO
rigin)); |
| 180 } |
| 181 this->deleteYUVTextures(context, yuvIDs); |
| 182 for (int i = 0; i < images.count(); ++ i) { |
| 183 SkScalar y = (i + 1) * kPad + i * fYUVBmps[0].height(); |
| 184 SkScalar x = kPad; |
| 185 |
| 186 canvas->drawImage(images[i], x, y); |
| 187 images[i]->unref(); |
| 188 images[i] = NULL; |
| 189 } |
| 190 } |
| 191 |
| 192 private: |
| 193 SkAutoTUnref<SkImage> fRGBImage; |
| 194 SkBitmap fYUVBmps[3]; |
| 195 |
| 196 static const int kBmpSize = 32; |
| 197 |
| 198 typedef GM INHERITED; |
| 199 }; |
| 200 |
| 201 DEF_GM( return SkNEW(ImageFromYUVTextures); ) |
| 202 } |
| 203 |
| 204 #endif |
OLD | NEW |