blob: bf4094cf4ff4d6a01a738ca3fbc8373cc51c4ae3 [file] [log] [blame]
sugoi24dcac22014-07-07 15:09:48 -07001
2/*
3 * Copyright 2014 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"
joshualitt04194f32016-01-13 10:08:27 -080016#include "GrDrawContext.h"
17#include "GrPipelineBuilder.h"
sugoi24dcac22014-07-07 15:09:48 -070018#include "SkBitmap.h"
19#include "SkGr.h"
20#include "SkGradientShader.h"
joshualitt04194f32016-01-13 10:08:27 -080021#include "batches/GrDrawBatch.h"
22#include "batches/GrRectBatchFactory.h"
bsalomonf267c1e2016-02-01 13:16:14 -080023#include "effects/GrYUVEffect.h"
sugoi24dcac22014-07-07 15:09:48 -070024
sugoi4ccce7e2015-02-13 13:57:09 -080025#define YSIZE 8
26#define USIZE 4
27#define VSIZE 4
28
sugoi24dcac22014-07-07 15:09:48 -070029namespace skiagm {
30/**
31 * This GM directly exercises GrYUVtoRGBEffect.
32 */
33class YUVtoRGBEffect : public GM {
34public:
35 YUVtoRGBEffect() {
36 this->setBGColor(0xFFFFFFFF);
37 }
38
39protected:
mtklein36352bf2015-03-25 18:17:31 -070040 SkString onShortName() override {
sugoi24dcac22014-07-07 15:09:48 -070041 return SkString("yuv_to_rgb_effect");
42 }
43
mtklein36352bf2015-03-25 18:17:31 -070044 SkISize onISize() override {
rileya13400392015-07-20 15:00:03 -070045 return SkISize::Make(238, 120);
sugoi24dcac22014-07-07 15:09:48 -070046 }
47
mtklein36352bf2015-03-25 18:17:31 -070048 void onOnceBeforeDraw() override {
sugoi4ccce7e2015-02-13 13:57:09 -080049 SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE);
50 fBmp[0].allocPixels(yinfo);
51 SkImageInfo uinfo = SkImageInfo::MakeA8(USIZE, USIZE);
52 fBmp[1].allocPixels(uinfo);
53 SkImageInfo vinfo = SkImageInfo::MakeA8(VSIZE, VSIZE);
54 fBmp[2].allocPixels(vinfo);
sugoi24dcac22014-07-07 15:09:48 -070055 unsigned char* pixels[3];
56 for (int i = 0; i < 3; ++i) {
57 pixels[i] = (unsigned char*)fBmp[i].getPixels();
58 }
59 int color[] = {0, 85, 170};
60 const int limit[] = {255, 0, 255};
61 const int invl[] = {0, 255, 0};
62 const int inc[] = {1, -1, 1};
sugoi4ccce7e2015-02-13 13:57:09 -080063 for (int i = 0; i < 3; ++i) {
64 const size_t nbBytes = fBmp[i].rowBytes() * fBmp[i].height();
65 for (size_t j = 0; j < nbBytes; ++j) {
sugoi24dcac22014-07-07 15:09:48 -070066 pixels[i][j] = (unsigned char)color[i];
67 color[i] = (color[i] == limit[i]) ? invl[i] : color[i] + inc[i];
68 }
69 }
70 }
71
mtklein36352bf2015-03-25 18:17:31 -070072 void onDraw(SkCanvas* canvas) override {
sugoi24dcac22014-07-07 15:09:48 -070073 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
halcanary96fcdcc2015-08-27 07:41:13 -070074 if (nullptr == rt) {
sugoi24dcac22014-07-07 15:09:48 -070075 return;
76 }
77 GrContext* context = rt->getContext();
halcanary96fcdcc2015-08-27 07:41:13 -070078 if (nullptr == context) {
halcanary2a243382015-09-09 08:16:41 -070079 skiagm::GM::DrawGpuOnlyMessage(canvas);
sugoi24dcac22014-07-07 15:09:48 -070080 return;
81 }
82
joshualitt04194f32016-01-13 10:08:27 -080083 SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(rt));
84 if (!drawContext) {
sugoi24dcac22014-07-07 15:09:48 -070085 return;
86 }
87
bsalomonbcf0a522014-10-08 08:40:09 -070088 SkAutoTUnref<GrTexture> texture[3];
bsalomonafa95e22015-10-12 10:39:46 -070089 texture[0].reset(GrRefCachedBitmapTexture(context, fBmp[0],
90 GrTextureParams::ClampBilerp()));
91 texture[1].reset(GrRefCachedBitmapTexture(context, fBmp[1],
92 GrTextureParams::ClampBilerp()));
93 texture[2].reset(GrRefCachedBitmapTexture(context, fBmp[2],
94 GrTextureParams::ClampBilerp()));
bsalomonbcf0a522014-10-08 08:40:09 -070095
96 if (!texture[0] || !texture[1] || !texture[2]) {
sugoi24dcac22014-07-07 15:09:48 -070097 return;
98 }
99
100 static const SkScalar kDrawPad = 10.f;
101 static const SkScalar kTestPad = 10.f;
sugoi4ccce7e2015-02-13 13:57:09 -0800102 static const SkScalar kColorSpaceOffset = 36.f;
103 SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}};
sugoi24dcac22014-07-07 15:09:48 -0700104
rileyaabaef862014-09-12 17:45:58 -0700105 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace;
106 ++space) {
bsalomonbcf0a522014-10-08 08:40:09 -0700107 SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp[0].width()),
108 SkIntToScalar(fBmp[0].height()));
109 renderRect.outset(kDrawPad, kDrawPad);
sugoi24dcac22014-07-07 15:09:48 -0700110
bsalomonbcf0a522014-10-08 08:40:09 -0700111 SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset;
112 SkScalar x = kDrawPad + kTestPad;
sugoi24dcac22014-07-07 15:09:48 -0700113
bsalomonbcf0a522014-10-08 08:40:09 -0700114 const int indices[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2},
115 {1, 2, 0}, {2, 0, 1}, {2, 1, 0}};
sugoi24dcac22014-07-07 15:09:48 -0700116
bsalomonbcf0a522014-10-08 08:40:09 -0700117 for (int i = 0; i < 6; ++i) {
joshualitt2cdec312015-07-09 07:31:31 -0700118 GrPipelineBuilder pipelineBuilder;
egdanielc4b72722015-11-23 13:20:41 -0800119 pipelineBuilder.setXPFactory(
120 GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref();
bsalomonf267c1e2016-02-01 13:16:14 -0800121 SkAutoTUnref<const GrFragmentProcessor> fp(
122 GrYUVEffect::CreateYUVToRGB(texture[indices[i][0]],
123 texture[indices[i][1]],
124 texture[indices[i][2]],
125 sizes,
126 static_cast<SkYUVColorSpace>(space)));
bsalomonbcf0a522014-10-08 08:40:09 -0700127 if (fp) {
128 SkMatrix viewMatrix;
129 viewMatrix.setTranslate(x, y);
egdaniel8dd688b2015-01-22 10:16:09 -0800130 pipelineBuilder.setRenderTarget(rt);
bsalomonac856c92015-08-27 06:30:17 -0700131 pipelineBuilder.addColorFragmentProcessor(fp);
joshualitt04194f32016-01-13 10:08:27 -0800132 SkAutoTUnref<GrDrawBatch> batch(
133 GrRectBatchFactory::CreateNonAAFill(GrColor_WHITE, viewMatrix,
134 renderRect, nullptr, nullptr));
135 drawContext->internal_drawBatch(pipelineBuilder, batch);
bsalomonbcf0a522014-10-08 08:40:09 -0700136 }
137 x += renderRect.width() + kTestPad;
138 }
sugoi24dcac22014-07-07 15:09:48 -0700139 }
bsalomonbcf0a522014-10-08 08:40:09 -0700140 }
sugoi24dcac22014-07-07 15:09:48 -0700141
142private:
143 SkBitmap fBmp[3];
144
145 typedef GM INHERITED;
146};
147
halcanary385fe4d2015-08-26 13:07:48 -0700148DEF_GM(return new YUVtoRGBEffect;)
sugoi24dcac22014-07-07 15:09:48 -0700149}
150
151#endif