| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2016 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 #include "components/mus/gles2/gl_helper.h" | |
| 6 | |
| 7 #include "base/macros.h" | |
| 8 #include "third_party/skia/include/core/SkRect.h" | |
| 9 #include "third_party/skia/include/core/SkRegion.h" | |
| 10 | |
| 11 namespace mus { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 class ScopedGLuint { | |
| 16 public: | |
| 17 typedef void (gpu::gles2::GLES2Interface::*GenFunc)(GLsizei n, GLuint* ids); | |
| 18 typedef void (gpu::gles2::GLES2Interface::*DeleteFunc)(GLsizei n, | |
| 19 const GLuint* ids); | |
| 20 ScopedGLuint(gpu::gles2::GLES2Interface* gl, | |
| 21 GenFunc gen_func, | |
| 22 DeleteFunc delete_func) | |
| 23 : gl_(gl), id_(0u), delete_func_(delete_func) { | |
| 24 (gl_->*gen_func)(1, &id_); | |
| 25 } | |
| 26 | |
| 27 operator GLuint() const { return id_; } | |
| 28 | |
| 29 GLuint id() const { return id_; } | |
| 30 | |
| 31 ~ScopedGLuint() { | |
| 32 if (id_ != 0) { | |
| 33 (gl_->*delete_func_)(1, &id_); | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 private: | |
| 38 gpu::gles2::GLES2Interface* gl_; | |
| 39 GLuint id_; | |
| 40 DeleteFunc delete_func_; | |
| 41 | |
| 42 DISALLOW_COPY_AND_ASSIGN(ScopedGLuint); | |
| 43 }; | |
| 44 | |
| 45 class ScopedFramebuffer : public ScopedGLuint { | |
| 46 public: | |
| 47 explicit ScopedFramebuffer(gpu::gles2::GLES2Interface* gl) | |
| 48 : ScopedGLuint(gl, | |
| 49 &gpu::gles2::GLES2Interface::GenFramebuffers, | |
| 50 &gpu::gles2::GLES2Interface::DeleteFramebuffers) {} | |
| 51 }; | |
| 52 | |
| 53 template <GLenum Target> | |
| 54 class ScopedBinder { | |
| 55 public: | |
| 56 typedef void (gpu::gles2::GLES2Interface::*BindFunc)(GLenum target, | |
| 57 GLuint id); | |
| 58 ScopedBinder(gpu::gles2::GLES2Interface* gl, GLuint id, BindFunc bind_func) | |
| 59 : gl_(gl), bind_func_(bind_func) { | |
| 60 (gl_->*bind_func_)(Target, id); | |
| 61 } | |
| 62 | |
| 63 virtual ~ScopedBinder() { (gl_->*bind_func_)(Target, 0); } | |
| 64 | |
| 65 private: | |
| 66 gpu::gles2::GLES2Interface* gl_; | |
| 67 BindFunc bind_func_; | |
| 68 | |
| 69 DISALLOW_COPY_AND_ASSIGN(ScopedBinder); | |
| 70 }; | |
| 71 | |
| 72 template <GLenum Target> | |
| 73 class ScopedFramebufferBinder : ScopedBinder<Target> { | |
| 74 public: | |
| 75 ScopedFramebufferBinder(gpu::gles2::GLES2Interface* gl, GLuint id) | |
| 76 : ScopedBinder<Target>(gl, | |
| 77 id, | |
| 78 &gpu::gles2::GLES2Interface::BindFramebuffer) {} | |
| 79 }; | |
| 80 | |
| 81 } // namespace | |
| 82 | |
| 83 // Cribbed from content/common/gpu/client/gl_helper.cc | |
| 84 void GLCopySubBufferDamage(gpu::gles2::GLES2Interface* gl, | |
| 85 GLenum target, | |
| 86 GLuint texture, | |
| 87 GLuint previous_texture, | |
| 88 const SkRegion& new_damage, | |
| 89 const SkRegion& old_damage) { | |
| 90 SkRegion region(old_damage); | |
| 91 if (region.op(new_damage, SkRegion::kDifference_Op)) { | |
| 92 ScopedFramebuffer dst_framebuffer(gl); | |
| 93 ScopedFramebufferBinder<GL_FRAMEBUFFER> framebuffer_binder(gl, | |
| 94 dst_framebuffer); | |
| 95 gl->BindTexture(target, texture); | |
| 96 gl->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, | |
| 97 previous_texture, 0); | |
| 98 for (SkRegion::Iterator it(region); !it.done(); it.next()) { | |
| 99 const SkIRect& rect = it.rect(); | |
| 100 gl->CopyTexSubImage2D(target, 0, rect.x(), rect.y(), rect.x(), rect.y(), | |
| 101 rect.width(), rect.height()); | |
| 102 } | |
| 103 gl->BindTexture(target, 0); | |
| 104 gl->Flush(); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 } // namespace mus | |
| OLD | NEW |