blob: 2ba26967b1f6ec5c80b9e309d6734ab758bcc53f [file] [log] [blame]
bsalomonf267c1e2016-02-01 13:16:14 -08001/*
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
reed9ce9d672016-03-17 10:51:11 -070015static sk_sp<SkImage> create_image(GrContext* context, int width, int height) {
reede8f30622016-03-23 18:59:25 -070016 sk_sp<SkSurface> surface;
bsalomonf267c1e2016-02-01 13:16:14 -080017 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
18 if (context) {
reede8f30622016-03-23 18:59:25 -070019 surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info);
bsalomonf267c1e2016-02-01 13:16:14 -080020 } else {
reede8f30622016-03-23 18:59:25 -070021 surface = SkSurface::MakeRaster(info);
bsalomonf267c1e2016-02-01 13:16:14 -080022 }
23 if (!surface) {
24 return nullptr;
25 }
26 // Create an RGB image from which we will extract planes
27 SkPaint paint;
mtkleindbfd7ab2016-09-01 11:24:54 -070028 constexpr SkColor kColors[] =
bsalomonf267c1e2016-02-01 13:16:14 -080029 { SK_ColorBLUE, SK_ColorYELLOW, SK_ColorGREEN, SK_ColorWHITE };
30 SkScalar r = (width + height) / 4.f;
reed1a9b9642016-03-13 14:13:58 -070031 paint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(0,0), r, kColors,
32 nullptr, SK_ARRAY_COUNT(kColors),
33 SkShader::kMirror_TileMode));
bsalomonf267c1e2016-02-01 13:16:14 -080034
35 surface->getCanvas()->drawPaint(paint);
reed9ce9d672016-03-17 10:51:11 -070036 return surface->makeImageSnapshot();
bsalomonf267c1e2016-02-01 13:16:14 -080037}
38
39DEF_SIMPLE_GM(image_to_yuv_planes, canvas, 120, 525) {
mtkleindbfd7ab2016-09-01 11:24:54 -070040 constexpr SkScalar kPad = 5.f;
41 constexpr int kImageSize = 32;
bsalomonf267c1e2016-02-01 13:16:14 -080042
43 GrContext *context = canvas->getGrContext();
reed9ce9d672016-03-17 10:51:11 -070044 sk_sp<SkImage> rgbImage(create_image(context, kImageSize, kImageSize));
bsalomonf267c1e2016-02-01 13:16:14 -080045 if (!rgbImage) {
46 return;
47 }
48
reed9ce9d672016-03-17 10:51:11 -070049 canvas->drawImage(rgbImage.get(), kPad, kPad);
bsalomonf267c1e2016-02-01 13:16:14 -080050 // Test cases where all three planes are the same size, where just u and v are the same size,
51 // and where all differ.
mtkleindbfd7ab2016-09-01 11:24:54 -070052 constexpr SkISize kSizes[][3] = {
bsalomonf267c1e2016-02-01 13:16:14 -080053 {{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.
mtkleindbfd7ab2016-09-01 11:24:54 -070059 constexpr size_t kRowBytes[][3] {
bsalomonf267c1e2016-02-01 13:16:14 -080060 {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 }
Ben Wagner7ecc5962016-11-02 17:07:33 -040075 std::unique_ptr<uint8_t[]> yPlane(new uint8_t[realRowBytes[0] * sizes[0].fHeight]);
76 std::unique_ptr<uint8_t[]> uPlane(new uint8_t[realRowBytes[1] * sizes[1].fHeight]);
77 std::unique_ptr<uint8_t[]> vPlane(new uint8_t[realRowBytes[2] * sizes[2].fHeight]);
bsalomonf267c1e2016-02-01 13:16:14 -080078
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}