1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <GLES2/gl2.h> 20 #include <GLES2/gl2ext.h> 21 22 #include "FrameworkFormats.h" 23 24 // The purpose of YUVConverter is to use 25 // OpenGL shaders to convert YUV images to RGB 26 // images that can be displayed on screen. 27 // Doing this on the GPU can be much faster than 28 // on the CPU. 29 30 // Usage: 31 // 0. Have a current OpenGL context working. 32 // 1. Constructing the YUVConverter object will allocate 33 // OpenGL resources needed to convert, given the desired 34 // |width| and |height| of the buffer. 35 // 2. To convert a given YUV buffer of |pixels|, call 36 // the drawConvert method (with x, y, width, and height 37 // arguments depicting the region to convert). 38 // The RGB version of the YUV buffer will be drawn 39 // to the current framebuffer. To retrieve 40 // the result, if the user of the result is an OpenGL texture, 41 // it suffices to have that texture be the color attachment 42 // of the framebuffer object. Or, if you want the results 43 // on the CPU, call glReadPixels() after the call to drawConvert(). 44 class YUVConverter { 45 public: 46 // call ctor when creating a gralloc buffer 47 // with YUV format 48 YUVConverter(int width, int height, FrameworkFormat format); 49 // destroy when ColorBuffer is destroyed 50 ~YUVConverter(); 51 // call when gralloc_unlock updates 52 // the host color buffer 53 // (rcUpdateColorBuffer) 54 void drawConvert(int x, int y, int width, int height, char* pixels); 55 private: 56 // For dealing with n-pixel-aligned buffers 57 void updateCutoffs(float width, float ywidth, float halfwidth, float cwidth); 58 FrameworkFormat mFormat; 59 // We need the following GL objects: 60 GLuint mVshader = 0; 61 GLuint mFshader = 0; // Fragment shader (does actual conversion) 62 GLuint mProgram = 0; 63 GLuint mVbuf = 0; 64 GLuint mIbuf = 0; 65 GLuint mYtex = 0; 66 GLuint mUtex = 0; 67 GLuint mVtex = 0; 68 69 // shader uniform locations 70 GLint mYWidthCutoffLoc = -1; 71 GLint mCWidthCutoffLoc = -1; 72 GLint mYSamplerLoc = -1; 73 GLint mUSamplerLoc = -1; 74 GLint mVSamplerLoc = -1; 75 float mYWidthCutoff = 1.0; 76 float mCWidthCutoff = 1.0; 77 }; 78