blob: 40a2f03f27e223f50ed04541b6e9e9f9d9165311 [file] [log] [blame]
sugoi24dcac22014-07-07 15:09:48 -07001/*
2 * Copyright 2014 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
14#include "GrContext.h"
robertphillips391395d2016-03-02 09:26:36 -080015#include "GrDrawContextPriv.h"
joshualitt04194f32016-01-13 10:08:27 -080016#include "GrPipelineBuilder.h"
sugoi24dcac22014-07-07 15:09:48 -070017#include "SkBitmap.h"
18#include "SkGr.h"
19#include "SkGradientShader.h"
joshualitt04194f32016-01-13 10:08:27 -080020#include "batches/GrDrawBatch.h"
21#include "batches/GrRectBatchFactory.h"
bsalomonf267c1e2016-02-01 13:16:14 -080022#include "effects/GrYUVEffect.h"
sugoi24dcac22014-07-07 15:09:48 -070023
sugoi4ccce7e2015-02-13 13:57:09 -080024#define YSIZE 8
25#define USIZE 4
26#define VSIZE 4
27
sugoi24dcac22014-07-07 15:09:48 -070028namespace skiagm {
29/**
30 * This GM directly exercises GrYUVtoRGBEffect.
31 */
32class YUVtoRGBEffect : public GM {
33public:
34 YUVtoRGBEffect() {
35 this->setBGColor(0xFFFFFFFF);
36 }
37
38protected:
mtklein36352bf2015-03-25 18:17:31 -070039 SkString onShortName() override {
sugoi24dcac22014-07-07 15:09:48 -070040 return SkString("yuv_to_rgb_effect");
41 }
42
mtklein36352bf2015-03-25 18:17:31 -070043 SkISize onISize() override {
rileya13400392015-07-20 15:00:03 -070044 return SkISize::Make(238, 120);
sugoi24dcac22014-07-07 15:09:48 -070045 }
46
mtklein36352bf2015-03-25 18:17:31 -070047 void onOnceBeforeDraw() override {
sugoi4ccce7e2015-02-13 13:57:09 -080048 SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE);
49 fBmp[0].allocPixels(yinfo);
50 SkImageInfo uinfo = SkImageInfo::MakeA8(USIZE, USIZE);
51 fBmp[1].allocPixels(uinfo);
52 SkImageInfo vinfo = SkImageInfo::MakeA8(VSIZE, VSIZE);
53 fBmp[2].allocPixels(vinfo);
sugoi24dcac22014-07-07 15:09:48 -070054 unsigned char* pixels[3];
55 for (int i = 0; i < 3; ++i) {
56 pixels[i] = (unsigned char*)fBmp[i].getPixels();
57 }
58 int color[] = {0, 85, 170};
59 const int limit[] = {255, 0, 255};
60 const int invl[] = {0, 255, 0};
61 const int inc[] = {1, -1, 1};
sugoi4ccce7e2015-02-13 13:57:09 -080062 for (int i = 0; i < 3; ++i) {
63 const size_t nbBytes = fBmp[i].rowBytes() * fBmp[i].height();
64 for (size_t j = 0; j < nbBytes; ++j) {
sugoi24dcac22014-07-07 15:09:48 -070065 pixels[i][j] = (unsigned char)color[i];
66 color[i] = (color[i] == limit[i]) ? invl[i] : color[i] + inc[i];
67 }
68 }
69 }
70
mtklein36352bf2015-03-25 18:17:31 -070071 void onDraw(SkCanvas* canvas) override {
robertphillips175dd9b2016-04-28 14:32:04 -070072 GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext();
73 if (!drawContext) {
halcanary2a243382015-09-09 08:16:41 -070074 skiagm::GM::DrawGpuOnlyMessage(canvas);
robertphillips175dd9b2016-04-28 14:32:04 -070075 return;
sugoi24dcac22014-07-07 15:09:48 -070076 }
77
robertphillips175dd9b2016-04-28 14:32:04 -070078 GrContext* context = canvas->getGrContext();
79 if (!context) {
sugoi24dcac22014-07-07 15:09:48 -070080 return;
81 }
82
bsalomonbcf0a522014-10-08 08:40:09 -070083 SkAutoTUnref<GrTexture> texture[3];
bsalomonafa95e22015-10-12 10:39:46 -070084 texture[0].reset(GrRefCachedBitmapTexture(context, fBmp[0],
85 GrTextureParams::ClampBilerp()));
86 texture[1].reset(GrRefCachedBitmapTexture(context, fBmp[1],
87 GrTextureParams::ClampBilerp()));
88 texture[2].reset(GrRefCachedBitmapTexture(context, fBmp[2],
89 GrTextureParams::ClampBilerp()));
bsalomonbcf0a522014-10-08 08:40:09 -070090
91 if (!texture[0] || !texture[1] || !texture[2]) {
sugoi24dcac22014-07-07 15:09:48 -070092 return;
93 }
94
95 static const SkScalar kDrawPad = 10.f;
96 static const SkScalar kTestPad = 10.f;
sugoi4ccce7e2015-02-13 13:57:09 -080097 static const SkScalar kColorSpaceOffset = 36.f;
98 SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}};
sugoi24dcac22014-07-07 15:09:48 -070099
rileyaabaef862014-09-12 17:45:58 -0700100 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace;
101 ++space) {
bsalomonbcf0a522014-10-08 08:40:09 -0700102 SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp[0].width()),
103 SkIntToScalar(fBmp[0].height()));
104 renderRect.outset(kDrawPad, kDrawPad);
sugoi24dcac22014-07-07 15:09:48 -0700105
bsalomonbcf0a522014-10-08 08:40:09 -0700106 SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset;
107 SkScalar x = kDrawPad + kTestPad;
sugoi24dcac22014-07-07 15:09:48 -0700108
bsalomonbcf0a522014-10-08 08:40:09 -0700109 const int indices[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2},
110 {1, 2, 0}, {2, 0, 1}, {2, 1, 0}};
sugoi24dcac22014-07-07 15:09:48 -0700111
bsalomonbcf0a522014-10-08 08:40:09 -0700112 for (int i = 0; i < 6; ++i) {
joshualitt2cdec312015-07-09 07:31:31 -0700113 GrPipelineBuilder pipelineBuilder;
egdanielc4b72722015-11-23 13:20:41 -0800114 pipelineBuilder.setXPFactory(
115 GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref();
bsalomonf267c1e2016-02-01 13:16:14 -0800116 SkAutoTUnref<const GrFragmentProcessor> fp(
117 GrYUVEffect::CreateYUVToRGB(texture[indices[i][0]],
118 texture[indices[i][1]],
119 texture[indices[i][2]],
120 sizes,
121 static_cast<SkYUVColorSpace>(space)));
bsalomonbcf0a522014-10-08 08:40:09 -0700122 if (fp) {
123 SkMatrix viewMatrix;
124 viewMatrix.setTranslate(x, y);
robertphillips175dd9b2016-04-28 14:32:04 -0700125 pipelineBuilder.setRenderTarget(drawContext->accessRenderTarget());
bsalomonac856c92015-08-27 06:30:17 -0700126 pipelineBuilder.addColorFragmentProcessor(fp);
joshualitt04194f32016-01-13 10:08:27 -0800127 SkAutoTUnref<GrDrawBatch> batch(
128 GrRectBatchFactory::CreateNonAAFill(GrColor_WHITE, viewMatrix,
129 renderRect, nullptr, nullptr));
robertphillips391395d2016-03-02 09:26:36 -0800130 drawContext->drawContextPriv().testingOnly_drawBatch(pipelineBuilder, batch);
bsalomonbcf0a522014-10-08 08:40:09 -0700131 }
132 x += renderRect.width() + kTestPad;
133 }
sugoi24dcac22014-07-07 15:09:48 -0700134 }
bsalomonbcf0a522014-10-08 08:40:09 -0700135 }
sugoi24dcac22014-07-07 15:09:48 -0700136
137private:
138 SkBitmap fBmp[3];
139
140 typedef GM INHERITED;
141};
142
halcanary385fe4d2015-08-26 13:07:48 -0700143DEF_GM(return new YUVtoRGBEffect;)
sugoi24dcac22014-07-07 15:09:48 -0700144}
145
146#endif