blob: 8daf34829c20e1663b52f2fbeeb2a834ff607218 [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#include "GrYUVtoRGBEffect.h"
9
10#include "GrCoordTransform.h"
egdaniel605dd0f2014-11-12 08:35:25 -080011#include "GrInvariantOutput.h"
joshualittb0a8a372014-09-23 09:50:21 -070012#include "GrProcessor.h"
13#include "gl/GrGLProcessor.h"
joshualitteb2a6762014-12-04 11:35:33 -080014#include "gl/builders/GrGLProgramBuilder.h"
sugoi24dcac22014-07-07 15:09:48 -070015
16namespace {
17
joshualittb0a8a372014-09-23 09:50:21 -070018class YUVtoRGBEffect : public GrFragmentProcessor {
sugoi24dcac22014-07-07 15:09:48 -070019public:
joshualittb0a8a372014-09-23 09:50:21 -070020 static GrFragmentProcessor* Create(GrTexture* yTexture, GrTexture* uTexture,
sugoi4ccce7e2015-02-13 13:57:09 -080021 GrTexture* vTexture, SkISize sizes[3],
22 SkYUVColorSpace colorSpace) {
23 SkScalar w[3], h[3];
24 w[0] = SkIntToScalar(sizes[0].fWidth) / SkIntToScalar(yTexture->width());
25 h[0] = SkIntToScalar(sizes[0].fHeight) / SkIntToScalar(yTexture->height());
26 w[1] = SkIntToScalar(sizes[1].fWidth) / SkIntToScalar(uTexture->width());
27 h[1] = SkIntToScalar(sizes[1].fHeight) / SkIntToScalar(uTexture->height());
28 w[2] = SkIntToScalar(sizes[2].fWidth) / SkIntToScalar(vTexture->width());
29 h[2] = SkIntToScalar(sizes[2].fHeight) / SkIntToScalar(vTexture->height());
30 SkMatrix yuvMatrix[3];
31 yuvMatrix[0] = GrCoordTransform::MakeDivByTextureWHMatrix(yTexture);
32 yuvMatrix[1] = yuvMatrix[0];
33 yuvMatrix[1].preScale(w[1] / w[0], h[1] / h[0]);
34 yuvMatrix[2] = yuvMatrix[0];
35 yuvMatrix[2].preScale(w[2] / w[0], h[2] / h[0]);
joshualittb8410602015-03-04 09:56:09 -080036 return SkNEW_ARGS(YUVtoRGBEffect, (yTexture, uTexture, vTexture, yuvMatrix, colorSpace));
sugoi24dcac22014-07-07 15:09:48 -070037 }
38
mtklein72c9faa2015-01-09 10:06:39 -080039 const char* name() const SK_OVERRIDE { return "YUV to RGB"; }
sugoi24dcac22014-07-07 15:09:48 -070040
rileyaabaef862014-09-12 17:45:58 -070041 SkYUVColorSpace getColorSpace() const {
42 return fColorSpace;
43 }
44
joshualittb0a8a372014-09-23 09:50:21 -070045 class GLProcessor : public GrGLFragmentProcessor {
sugoi24dcac22014-07-07 15:09:48 -070046 public:
rileyaabaef862014-09-12 17:45:58 -070047 static const GrGLfloat kJPEGConversionMatrix[16];
48 static const GrGLfloat kRec601ConversionMatrix[16];
49
sugoi24dcac22014-07-07 15:09:48 -070050 // this class always generates the same code.
joshualittb0a8a372014-09-23 09:50:21 -070051 static void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*) {}
sugoi24dcac22014-07-07 15:09:48 -070052
joshualitteb2a6762014-12-04 11:35:33 -080053 GLProcessor(const GrProcessor&) {}
sugoi24dcac22014-07-07 15:09:48 -070054
joshualitt15988992014-10-09 15:04:05 -070055 virtual void emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -070056 const GrFragmentProcessor&,
sugoi24dcac22014-07-07 15:09:48 -070057 const char* outputColor,
58 const char* inputColor,
59 const TransformedCoordsArray& coords,
60 const TextureSamplerArray& samplers) SK_OVERRIDE {
joshualitt15988992014-10-09 15:04:05 -070061 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
rileyaabaef862014-09-12 17:45:58 -070062
63 const char* yuvMatrix = NULL;
64 fMatrixUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -080065 kMat44f_GrSLType, kDefault_GrSLPrecision,
66 "YUVMatrix", &yuvMatrix);
joshualitt30ba4362014-08-21 20:18:45 -070067 fsBuilder->codeAppendf("\t%s = vec4(\n\t\t", outputColor);
joshualitt23e280d2014-09-18 12:26:38 -070068 fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0].getType());
joshualitt30ba4362014-08-21 20:18:45 -070069 fsBuilder->codeAppend(".r,\n\t\t");
sugoi4ccce7e2015-02-13 13:57:09 -080070 fsBuilder->appendTextureLookup(samplers[1], coords[1].c_str(), coords[1].getType());
joshualitt30ba4362014-08-21 20:18:45 -070071 fsBuilder->codeAppend(".r,\n\t\t");
sugoi4ccce7e2015-02-13 13:57:09 -080072 fsBuilder->appendTextureLookup(samplers[2], coords[2].c_str(), coords[2].getType());
joshualitt30ba4362014-08-21 20:18:45 -070073 fsBuilder->codeAppendf(".r,\n\t\t1.0) * %s;\n", yuvMatrix);
sugoi24dcac22014-07-07 15:09:48 -070074 }
75
rileyaabaef862014-09-12 17:45:58 -070076 virtual void setData(const GrGLProgramDataManager& pdman,
joshualittb0a8a372014-09-23 09:50:21 -070077 const GrProcessor& processor) SK_OVERRIDE {
78 const YUVtoRGBEffect& yuvEffect = processor.cast<YUVtoRGBEffect>();
rileyaabaef862014-09-12 17:45:58 -070079 switch (yuvEffect.getColorSpace()) {
80 case kJPEG_SkYUVColorSpace:
81 pdman.setMatrix4f(fMatrixUni, kJPEGConversionMatrix);
82 break;
83 case kRec601_SkYUVColorSpace:
84 pdman.setMatrix4f(fMatrixUni, kRec601ConversionMatrix);
85 break;
86 }
87 }
88
89 private:
90 GrGLProgramDataManager::UniformHandle fMatrixUni;
91
joshualittb0a8a372014-09-23 09:50:21 -070092 typedef GrGLFragmentProcessor INHERITED;
sugoi24dcac22014-07-07 15:09:48 -070093 };
94
joshualitteb2a6762014-12-04 11:35:33 -080095 virtual void getGLProcessorKey(const GrGLCaps& caps,
96 GrProcessorKeyBuilder* b) const SK_OVERRIDE {
97 GLProcessor::GenKey(*this, caps, b);
98 }
99
mtklein72c9faa2015-01-09 10:06:39 -0800100 GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE {
joshualitteb2a6762014-12-04 11:35:33 -0800101 return SkNEW_ARGS(GLProcessor, (*this));
102 }
103
sugoi24dcac22014-07-07 15:09:48 -0700104private:
rileyaabaef862014-09-12 17:45:58 -0700105 YUVtoRGBEffect(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vTexture,
joshualittb8410602015-03-04 09:56:09 -0800106 SkMatrix yuvMatrix[3], SkYUVColorSpace colorSpace)
sugoi4ccce7e2015-02-13 13:57:09 -0800107 : fYTransform(kLocal_GrCoordSet, yuvMatrix[0], yTexture, GrTextureParams::kNone_FilterMode)
sugoi24dcac22014-07-07 15:09:48 -0700108 , fYAccess(yTexture)
joshualittb8410602015-03-04 09:56:09 -0800109 , fUTransform(kLocal_GrCoordSet, yuvMatrix[1], uTexture, GrTextureParams::kNone_FilterMode)
110 , fUAccess(uTexture)
111 , fVTransform(kLocal_GrCoordSet, yuvMatrix[2], vTexture, GrTextureParams::kNone_FilterMode)
112 , fVAccess(vTexture)
rileyaabaef862014-09-12 17:45:58 -0700113 , fColorSpace(colorSpace) {
joshualitteb2a6762014-12-04 11:35:33 -0800114 this->initClassID<YUVtoRGBEffect>();
sugoi4ccce7e2015-02-13 13:57:09 -0800115 this->addCoordTransform(&fYTransform);
sugoi24dcac22014-07-07 15:09:48 -0700116 this->addTextureAccess(&fYAccess);
sugoi4ccce7e2015-02-13 13:57:09 -0800117 this->addCoordTransform(&fUTransform);
sugoi24dcac22014-07-07 15:09:48 -0700118 this->addTextureAccess(&fUAccess);
sugoi4ccce7e2015-02-13 13:57:09 -0800119 this->addCoordTransform(&fVTransform);
sugoi24dcac22014-07-07 15:09:48 -0700120 this->addTextureAccess(&fVAccess);
sugoi24dcac22014-07-07 15:09:48 -0700121 }
122
mtklein72c9faa2015-01-09 10:06:39 -0800123 bool onIsEqual(const GrFragmentProcessor& sBase) const SK_OVERRIDE {
joshualitt49586be2014-09-16 08:21:41 -0700124 const YUVtoRGBEffect& s = sBase.cast<YUVtoRGBEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700125 return fColorSpace == s.getColorSpace();
sugoi24dcac22014-07-07 15:09:48 -0700126 }
127
mtklein72c9faa2015-01-09 10:06:39 -0800128 void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE {
egdaniel1a8ecdf2014-10-03 06:24:12 -0700129 // YUV is opaque
egdaniel9e4d6d12014-10-15 13:49:02 -0700130 inout->setToOther(kA_GrColorComponentFlag, 0xFF << GrColor_SHIFT_A,
egdaniel605dd0f2014-11-12 08:35:25 -0800131 GrInvariantOutput::kWillNot_ReadInput);
egdaniel1a8ecdf2014-10-03 06:24:12 -0700132 }
133
sugoi4ccce7e2015-02-13 13:57:09 -0800134 GrCoordTransform fYTransform;
sugoi24dcac22014-07-07 15:09:48 -0700135 GrTextureAccess fYAccess;
sugoi4ccce7e2015-02-13 13:57:09 -0800136 GrCoordTransform fUTransform;
sugoi24dcac22014-07-07 15:09:48 -0700137 GrTextureAccess fUAccess;
sugoi4ccce7e2015-02-13 13:57:09 -0800138 GrCoordTransform fVTransform;
sugoi24dcac22014-07-07 15:09:48 -0700139 GrTextureAccess fVAccess;
rileyaabaef862014-09-12 17:45:58 -0700140 SkYUVColorSpace fColorSpace;
sugoi24dcac22014-07-07 15:09:48 -0700141
joshualittb0a8a372014-09-23 09:50:21 -0700142 typedef GrFragmentProcessor INHERITED;
sugoi24dcac22014-07-07 15:09:48 -0700143};
144
joshualittb0a8a372014-09-23 09:50:21 -0700145const GrGLfloat YUVtoRGBEffect::GLProcessor::kJPEGConversionMatrix[16] = {
rileyaabaef862014-09-12 17:45:58 -0700146 1.0f, 0.0f, 1.402f, -0.701f,
147 1.0f, -0.34414f, -0.71414f, 0.529f,
148 1.0f, 1.772f, 0.0f, -0.886f,
149 0.0f, 0.0f, 0.0f, 1.0};
joshualittb0a8a372014-09-23 09:50:21 -0700150const GrGLfloat YUVtoRGBEffect::GLProcessor::kRec601ConversionMatrix[16] = {
rileya223ba622014-09-16 06:23:50 -0700151 1.164f, 0.0f, 1.596f, -0.87075f,
152 1.164f, -0.391f, -0.813f, 0.52925f,
rileyaabaef862014-09-12 17:45:58 -0700153 1.164f, 2.018f, 0.0f, -1.08175f,
154 0.0f, 0.0f, 0.0f, 1.0};
sugoi24dcac22014-07-07 15:09:48 -0700155}
156
157//////////////////////////////////////////////////////////////////////////////
158
joshualittb0a8a372014-09-23 09:50:21 -0700159GrFragmentProcessor*
160GrYUVtoRGBEffect::Create(GrTexture* yTexture, GrTexture* uTexture, GrTexture* vTexture,
sugoi4ccce7e2015-02-13 13:57:09 -0800161 SkISize sizes[3], SkYUVColorSpace colorSpace) {
162 SkASSERT(yTexture && uTexture && vTexture && sizes);
163 return YUVtoRGBEffect::Create(yTexture, uTexture, vTexture, sizes, colorSpace);
sugoi24dcac22014-07-07 15:09:48 -0700164}