gl_helper.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
1// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#ifndef CONTENT_COMMON_GPU_CLIENT_GL_HELPER_H_ 6#define CONTENT_COMMON_GPU_CLIENT_GL_HELPER_H_ 7 8#include "base/atomicops.h" 9#include "base/basictypes.h" 10#include "base/callback.h" 11#include "base/memory/scoped_ptr.h" 12#include "content/common/content_export.h" 13#include "gpu/command_buffer/client/gles2_interface.h" 14#include "gpu/command_buffer/common/mailbox_holder.h" 15#include "third_party/skia/include/core/SkBitmap.h" 16 17namespace gfx { 18class Rect; 19class Size; 20} 21 22namespace gpu { 23class ContextSupport; 24struct Mailbox; 25} 26 27namespace media { 28class VideoFrame; 29}; 30 31class SkRegion; 32 33namespace content { 34 35class GLHelperScaling; 36 37class ScopedGLuint { 38 public: 39 typedef void (gpu::gles2::GLES2Interface::*GenFunc)(GLsizei n, GLuint* ids); 40 typedef void (gpu::gles2::GLES2Interface::*DeleteFunc)(GLsizei n, 41 const GLuint* ids); 42 ScopedGLuint(gpu::gles2::GLES2Interface* gl, 43 GenFunc gen_func, 44 DeleteFunc delete_func) 45 : gl_(gl), id_(0u), delete_func_(delete_func) { 46 (gl_->*gen_func)(1, &id_); 47 } 48 49 operator GLuint() const { return id_; } 50 51 GLuint id() const { return id_; } 52 53 ~ScopedGLuint() { 54 if (id_ != 0) { 55 (gl_->*delete_func_)(1, &id_); 56 } 57 } 58 59 private: 60 gpu::gles2::GLES2Interface* gl_; 61 GLuint id_; 62 DeleteFunc delete_func_; 63 64 DISALLOW_COPY_AND_ASSIGN(ScopedGLuint); 65}; 66 67class ScopedBuffer : public ScopedGLuint { 68 public: 69 explicit ScopedBuffer(gpu::gles2::GLES2Interface* gl) 70 : ScopedGLuint(gl, 71 &gpu::gles2::GLES2Interface::GenBuffers, 72 &gpu::gles2::GLES2Interface::DeleteBuffers) {} 73}; 74 75class ScopedFramebuffer : public ScopedGLuint { 76 public: 77 explicit ScopedFramebuffer(gpu::gles2::GLES2Interface* gl) 78 : ScopedGLuint(gl, 79 &gpu::gles2::GLES2Interface::GenFramebuffers, 80 &gpu::gles2::GLES2Interface::DeleteFramebuffers) {} 81}; 82 83class ScopedTexture : public ScopedGLuint { 84 public: 85 explicit ScopedTexture(gpu::gles2::GLES2Interface* gl) 86 : ScopedGLuint(gl, 87 &gpu::gles2::GLES2Interface::GenTextures, 88 &gpu::gles2::GLES2Interface::DeleteTextures) {} 89}; 90 91template <GLenum Target> 92class ScopedBinder { 93 public: 94 typedef void (gpu::gles2::GLES2Interface::*BindFunc)(GLenum target, 95 GLuint id); 96 ScopedBinder(gpu::gles2::GLES2Interface* gl, GLuint id, BindFunc bind_func) 97 : gl_(gl), bind_func_(bind_func) { 98 (gl_->*bind_func_)(Target, id); 99 } 100 101 virtual ~ScopedBinder() { (gl_->*bind_func_)(Target, 0); } 102 103 private: 104 gpu::gles2::GLES2Interface* gl_; 105 BindFunc bind_func_; 106 107 DISALLOW_COPY_AND_ASSIGN(ScopedBinder); 108}; 109 110template <GLenum Target> 111class ScopedBufferBinder : ScopedBinder<Target> { 112 public: 113 ScopedBufferBinder(gpu::gles2::GLES2Interface* gl, GLuint id) 114 : ScopedBinder<Target>(gl, id, &gpu::gles2::GLES2Interface::BindBuffer) {} 115}; 116 117template <GLenum Target> 118class ScopedFramebufferBinder : ScopedBinder<Target> { 119 public: 120 ScopedFramebufferBinder(gpu::gles2::GLES2Interface* gl, GLuint id) 121 : ScopedBinder<Target>(gl, 122 id, 123 &gpu::gles2::GLES2Interface::BindFramebuffer) {} 124}; 125 126template <GLenum Target> 127class ScopedTextureBinder : ScopedBinder<Target> { 128 public: 129 ScopedTextureBinder(gpu::gles2::GLES2Interface* gl, GLuint id) 130 : ScopedBinder<Target>(gl, id, &gpu::gles2::GLES2Interface::BindTexture) { 131 } 132}; 133 134class ReadbackYUVInterface; 135class GLHelperReadbackSupport; 136 137// Provides higher level operations on top of the gpu::gles2::GLES2Interface 138// interfaces. 139class CONTENT_EXPORT GLHelper { 140 public: 141 GLHelper(gpu::gles2::GLES2Interface* gl, 142 gpu::ContextSupport* context_support); 143 ~GLHelper(); 144 145 enum ScalerQuality { 146 // Bilinear single pass, fastest possible. 147 SCALER_QUALITY_FAST = 1, 148 149 // Bilinear upscale + N * 50% bilinear downscales. 150 // This is still fast enough for most purposes and 151 // Image quality is nearly as good as the BEST option. 152 SCALER_QUALITY_GOOD = 2, 153 154 // Bicubic upscale + N * 50% bicubic downscales. 155 // Produces very good quality scaled images, but it's 156 // 2-8x slower than the "GOOD" quality, so it's not always 157 // worth it. 158 SCALER_QUALITY_BEST = 3, 159 }; 160 161 // Copies the block of pixels specified with |src_subrect| from |src_texture|, 162 // scales it to |dst_size|, and writes it into |out|. 163 // |src_size| is the size of |src_texture|. The result is of format GL_BGRA 164 // and is potentially flipped vertically to make it a correct image 165 // representation. |callback| is invoked with the copy result when the copy 166 // operation has completed. 167 // Note that the src_texture will have the min/mag filter set to GL_LINEAR 168 // and wrap_s/t set to CLAMP_TO_EDGE in this call. 169 void CropScaleReadbackAndCleanTexture( 170 GLuint src_texture, 171 const gfx::Size& src_size, 172 const gfx::Rect& src_subrect, 173 const gfx::Size& dst_size, 174 unsigned char* out, 175 const SkBitmap::Config config, 176 const base::Callback<void(bool)>& callback); 177 178 // Copies the block of pixels specified with |src_subrect| from |src_mailbox|, 179 // scales it to |dst_size|, and writes it into |out|. 180 // |src_size| is the size of |src_mailbox|. The result is of format GL_BGRA 181 // and is potentially flipped vertically to make it a correct image 182 // representation. |callback| is invoked with the copy result when the copy 183 // operation has completed. 184 // Note that the texture bound to src_mailbox will have the min/mag filter set 185 // to GL_LINEAR and wrap_s/t set to CLAMP_TO_EDGE in this call. src_mailbox is 186 // assumed to be GL_TEXTURE_2D. 187 void CropScaleReadbackAndCleanMailbox( 188 const gpu::Mailbox& src_mailbox, 189 uint32 sync_point, 190 const gfx::Size& src_size, 191 const gfx::Rect& src_subrect, 192 const gfx::Size& dst_size, 193 unsigned char* out, 194 const SkBitmap::Config config, 195 const base::Callback<void(bool)>& callback); 196 197 // Copies the texture data out of |texture| into |out|. |size| is the 198 // size of the texture. No post processing is applied to the pixels. The 199 // texture is assumed to have a format of GL_RGBA with a pixel type of 200 // GL_UNSIGNED_BYTE. This is a blocking call that calls glReadPixels on the 201 // current OpenGL context. 202 void ReadbackTextureSync(GLuint texture, 203 const gfx::Rect& src_rect, 204 unsigned char* out, 205 SkBitmap::Config format); 206 207 void ReadbackTextureAsync(GLuint texture, 208 const gfx::Size& dst_size, 209 unsigned char* out, 210 SkBitmap::Config config, 211 const base::Callback<void(bool)>& callback); 212 213 // Creates a copy of the specified texture. |size| is the size of the texture. 214 // Note that the src_texture will have the min/mag filter set to GL_LINEAR 215 // and wrap_s/t set to CLAMP_TO_EDGE in this call. 216 GLuint CopyTexture(GLuint texture, const gfx::Size& size); 217 218 // Creates a scaled copy of the specified texture. |src_size| is the size of 219 // the texture and |dst_size| is the size of the resulting copy. 220 // Note that the src_texture will have the min/mag filter set to GL_LINEAR 221 // and wrap_s/t set to CLAMP_TO_EDGE in this call. 222 GLuint CopyAndScaleTexture(GLuint texture, 223 const gfx::Size& src_size, 224 const gfx::Size& dst_size, 225 bool vertically_flip_texture, 226 ScalerQuality quality); 227 228 // Returns the shader compiled from the source. 229 GLuint CompileShaderFromSource(const GLchar* source, GLenum type); 230 231 // Copies all pixels from |previous_texture| into |texture| that are 232 // inside the region covered by |old_damage| but not part of |new_damage|. 233 void CopySubBufferDamage(GLuint texture, 234 GLuint previous_texture, 235 const SkRegion& new_damage, 236 const SkRegion& old_damage); 237 238 // Simply creates a texture. 239 GLuint CreateTexture(); 240 // Deletes a texture. 241 void DeleteTexture(GLuint texture_id); 242 243 // Insert a sync point into the GL command buffer. 244 uint32 InsertSyncPoint(); 245 // Wait for the sync point before executing further GL commands. 246 void WaitSyncPoint(uint32 sync_point); 247 248 // Creates a mailbox holder that is attached to the given texture id, with a 249 // sync point to wait on before using the mailbox. Returns a holder with an 250 // empty mailbox on failure. 251 // Note the texture is assumed to be GL_TEXTURE_2D. 252 gpu::MailboxHolder ProduceMailboxHolderFromTexture(GLuint texture_id); 253 254 // Creates a texture and consumes a mailbox into it. Returns 0 on failure. 255 // Note the mailbox is assumed to be GL_TEXTURE_2D. 256 GLuint ConsumeMailboxToTexture(const gpu::Mailbox& mailbox, 257 uint32 sync_point); 258 259 // Resizes the texture's size to |size|. 260 void ResizeTexture(GLuint texture, const gfx::Size& size); 261 262 // Copies the framebuffer data given in |rect| to |texture|. 263 void CopyTextureSubImage(GLuint texture, const gfx::Rect& rect); 264 265 // Copies the all framebuffer data to |texture|. |size| specifies the 266 // size of the framebuffer. 267 void CopyTextureFullImage(GLuint texture, const gfx::Size& size); 268 269 // Flushes GL commands. 270 void Flush(); 271 272 273 // A scaler will cache all intermediate textures and programs 274 // needed to scale from a specified size to a destination size. 275 // If the source or destination sizes changes, you must create 276 // a new scaler. 277 class CONTENT_EXPORT ScalerInterface { 278 public: 279 ScalerInterface() {} 280 virtual ~ScalerInterface() {} 281 282 // Note that the src_texture will have the min/mag filter set to GL_LINEAR 283 // and wrap_s/t set to CLAMP_TO_EDGE in this call. 284 virtual void Scale(GLuint source_texture, GLuint dest_texture) = 0; 285 virtual const gfx::Size& SrcSize() = 0; 286 virtual const gfx::Rect& SrcSubrect() = 0; 287 virtual const gfx::Size& DstSize() = 0; 288 }; 289 290 // Note that the quality may be adjusted down if texture 291 // allocations fail or hardware doesn't support the requtested 292 // quality. Note that ScalerQuality enum is arranged in 293 // numerical order for simplicity. 294 ScalerInterface* CreateScaler(ScalerQuality quality, 295 const gfx::Size& src_size, 296 const gfx::Rect& src_subrect, 297 const gfx::Size& dst_size, 298 bool vertically_flip_texture, 299 bool swizzle); 300 301 // Create a readback pipeline that will scale a subsection of the source 302 // texture, then convert it to YUV422 planar form and then read back that. 303 // This reduces the amount of memory read from GPU to CPU memory by a factor 304 // 2.6, which can be quite handy since readbacks have very limited speed 305 // on some platforms. All values in |dst_size| and |dst_subrect| must be 306 // a multiple of two. If |use_mrt| is true, the pipeline will try to optimize 307 // the YUV conversion using the multi-render-target extension. |use_mrt| 308 // should only be set to false for testing. 309 ReadbackYUVInterface* CreateReadbackPipelineYUV(ScalerQuality quality, 310 const gfx::Size& src_size, 311 const gfx::Rect& src_subrect, 312 const gfx::Size& dst_size, 313 const gfx::Rect& dst_subrect, 314 bool flip_vertically, 315 bool use_mrt); 316 317 // Returns the maximum number of draw buffers available, 318 // 0 if GL_EXT_draw_buffers is not available. 319 GLint MaxDrawBuffers(); 320 321 // Checks whether the readbback is supported for texture with the 322 // matching config. This doesnt check for cross format readbacks. 323 bool IsReadbackConfigSupported(SkBitmap::Config texture_format); 324 325 protected: 326 class CopyTextureToImpl; 327 328 // Creates |copy_texture_to_impl_| if NULL. 329 void InitCopyTextToImpl(); 330 // Creates |scaler_impl_| if NULL. 331 void InitScalerImpl(); 332 333 gpu::gles2::GLES2Interface* gl_; 334 gpu::ContextSupport* context_support_; 335 scoped_ptr<CopyTextureToImpl> copy_texture_to_impl_; 336 scoped_ptr<GLHelperScaling> scaler_impl_; 337 scoped_ptr<GLHelperReadbackSupport> readback_support_; 338 339 DISALLOW_COPY_AND_ASSIGN(GLHelper); 340}; 341 342// Similar to a ScalerInterface, a yuv readback pipeline will 343// cache a scaler and all intermediate textures and frame buffers 344// needed to scale, crop, letterbox and read back a texture from 345// the GPU into CPU-accessible RAM. A single readback pipeline 346// can handle multiple outstanding readbacks at the same time, but 347// if the source or destination sizes change, you'll need to create 348// a new readback pipeline. 349class CONTENT_EXPORT ReadbackYUVInterface { 350 public: 351 ReadbackYUVInterface() {} 352 virtual ~ReadbackYUVInterface() {} 353 354 // Note that |target| must use YV12 format. 355 virtual void ReadbackYUV(const gpu::Mailbox& mailbox, 356 uint32 sync_point, 357 const scoped_refptr<media::VideoFrame>& target, 358 const base::Callback<void(bool)>& callback) = 0; 359 virtual GLHelper::ScalerInterface* scaler() = 0; 360}; 361 362} // namespace content 363 364#endif // CONTENT_COMMON_GPU_CLIENT_GL_HELPER_H_ 365