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 9#include <SkSurface.h> 10#include "gm.h" 11#include "SkBitmap.h" 12#include "SkGradientShader.h" 13#include "SkImage.h" 14 15static SkImage* create_image(GrContext* context, int width, int height) { 16 SkAutoTUnref<SkSurface> surface; 17 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); 18 if (context) { 19 surface.reset(SkSurface::NewRenderTarget(context, SkBudgeted::kYes, info, 0)); 20 } else { 21 surface.reset(SkSurface::NewRaster(info)); 22 } 23 if (!surface) { 24 return nullptr; 25 } 26 // Create an RGB image from which we will extract planes 27 SkPaint paint; 28 static const SkColor kColors[] = 29 { SK_ColorBLUE, SK_ColorYELLOW, SK_ColorGREEN, SK_ColorWHITE }; 30 SkScalar r = (width + height) / 4.f; 31 paint.setShader(SkGradientShader::CreateRadial(SkPoint::Make(0,0), r, kColors, 32 nullptr, SK_ARRAY_COUNT(kColors), 33 SkShader::kMirror_TileMode))->unref(); 34 35 surface->getCanvas()->drawPaint(paint); 36 return surface->newImageSnapshot(); 37} 38 39DEF_SIMPLE_GM(image_to_yuv_planes, canvas, 120, 525) { 40 static const SkScalar kPad = 5.f; 41 static const int kImageSize = 32; 42 43 GrContext *context = canvas->getGrContext(); 44 SkAutoTUnref<SkImage> rgbImage(create_image(context, kImageSize, kImageSize)); 45 if (!rgbImage) { 46 return; 47 } 48 49 canvas->drawImage(rgbImage, kPad, kPad); 50 // Test cases where all three planes are the same size, where just u and v are the same size, 51 // and where all differ. 52 static const SkISize kSizes[][3] = { 53 {{kImageSize, kImageSize}, {kImageSize , kImageSize }, {kImageSize, kImageSize }}, 54 {{kImageSize, kImageSize}, {kImageSize/2, kImageSize/2}, {kImageSize/2, kImageSize/2}}, 55 {{kImageSize, kImageSize}, {kImageSize/2, kImageSize/2}, {kImageSize/3, kImageSize/3}} 56 }; 57 58 // A mix of rowbytes triples to go with the above sizes. 59 static const size_t kRowBytes[][3] { 60 {0, 0, 0}, 61 {kImageSize, kImageSize/2 + 1, kImageSize}, 62 {kImageSize + 13, kImageSize, kImageSize/3 + 8} 63 }; 64 65 66 SkScalar x = kPad; 67 for (size_t s = 0; s < SK_ARRAY_COUNT(kSizes); ++s) { 68 SkScalar y = rgbImage->height() + 2 * kPad; 69 70 const SkISize *sizes = kSizes[s]; 71 size_t realRowBytes[3]; 72 for (int i = 0; i < 3; ++i) { 73 realRowBytes[i] = kRowBytes[s][i] ? kRowBytes[s][i] : kSizes[s][i].fWidth; 74 } 75 SkAutoTDeleteArray<uint8_t> yPlane(new uint8_t[realRowBytes[0] * sizes[0].fHeight]); 76 SkAutoTDeleteArray<uint8_t> uPlane(new uint8_t[realRowBytes[1] * sizes[1].fHeight]); 77 SkAutoTDeleteArray<uint8_t> vPlane(new uint8_t[realRowBytes[2] * sizes[2].fHeight]); 78 79 void *planes[3] = {yPlane.get(), uPlane.get(), vPlane.get()}; 80 81 // Convert the RGB image to YUV planes using each YUV color space and draw the YUV planes 82 // to the canvas. 83 SkBitmap yuvBmps[3]; 84 yuvBmps[0].setInfo(SkImageInfo::MakeA8(sizes[0].fWidth, sizes[0].fHeight), kRowBytes[s][0]); 85 yuvBmps[1].setInfo(SkImageInfo::MakeA8(sizes[1].fWidth, sizes[1].fHeight), kRowBytes[s][1]); 86 yuvBmps[2].setInfo(SkImageInfo::MakeA8(sizes[2].fWidth, sizes[2].fHeight), kRowBytes[s][2]); 87 yuvBmps[0].setPixels(yPlane.get()); 88 yuvBmps[1].setPixels(uPlane.get()); 89 yuvBmps[2].setPixels(vPlane.get()); 90 91 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) { 92 // Clear the planes so we don't accidentally see the old values if there is a bug in 93 // readYUV8Planes(). 94 memset(yPlane.get(), 0, realRowBytes[0] * sizes[0].fHeight); 95 memset(uPlane.get(), 0, realRowBytes[1] * sizes[1].fHeight); 96 memset(vPlane.get(), 0, realRowBytes[2] * sizes[2].fHeight); 97 if (rgbImage->readYUV8Planes(sizes, planes, kRowBytes[s], 98 static_cast<SkYUVColorSpace>(space))) { 99 yuvBmps[0].notifyPixelsChanged(); 100 yuvBmps[1].notifyPixelsChanged(); 101 yuvBmps[2].notifyPixelsChanged(); 102 for (int i = 0; i < 3; ++i) { 103 canvas->drawBitmap(yuvBmps[i], x, y); 104 y += kPad + yuvBmps[i].height(); 105 } 106 } 107 } 108 109 x += rgbImage->width() + kPad; 110 } 111} 112