OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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 "content/browser/compositor/buffered_output_surface.h" | |
6 | |
7 #include "content/common/gpu/client/context_provider_command_buffer.h" | |
8 #include "gpu/GLES2/gl2extchromium.h" | |
9 #include "gpu/command_buffer/client/gles2_interface.h" | |
10 | |
11 namespace content { | |
12 | |
13 BufferedOutputSurface::BufferedOutputSurface( | |
14 scoped_refptr<cc::ContextProvider> context_provider, | |
15 unsigned int internalformat) | |
16 : context_provider_(context_provider), | |
17 fbo_(0), | |
18 depth_rb_(0), | |
19 allocated_count_(0), | |
20 internalformat_(internalformat) { | |
21 Initialize(); | |
22 } | |
23 | |
24 BufferedOutputSurface::~BufferedOutputSurface() { | |
25 FreeAllSurfaces(); | |
26 | |
27 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); | |
28 if (fbo_) | |
29 gl->DeleteFramebuffers(1, &fbo_); | |
30 if (depth_rb_) | |
31 gl->DeleteRenderbuffers(1, &depth_rb_); | |
32 } | |
33 | |
34 void BufferedOutputSurface::Initialize() { | |
35 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); | |
36 gl->GenFramebuffers(1, &fbo_); | |
37 gl->GenRenderbuffers(1, &depth_rb_); | |
38 DCHECK(fbo_); | |
39 DCHECK(depth_rb_); | |
40 } | |
41 | |
42 void BufferedOutputSurface::BindFramebuffer() { | |
43 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); | |
44 gl->BindFramebuffer(GL_FRAMEBUFFER, fbo_); | |
45 | |
46 if (!current_surface_.first) { | |
47 current_surface_ = GetNextSurface(); | |
48 gl->FramebufferTexture2D(GL_FRAMEBUFFER, | |
49 GL_COLOR_ATTACHMENT0, | |
50 GL_TEXTURE_2D, | |
51 current_surface_.second, | |
52 0); | |
53 } | |
54 } | |
55 | |
56 void BufferedOutputSurface::SwapBuffers() { | |
57 if (last_surface_.first) | |
58 in_flight_surfaces_.push(last_surface_); | |
59 last_surface_ = current_surface_; | |
60 current_surface_.first = 0; | |
61 current_surface_.second = 0; | |
62 } | |
63 | |
64 void BufferedOutputSurface::Reshape(const gfx::Size& size, float scale_factor) { | |
65 if (size == size_) | |
66 return; | |
67 size_ = size; | |
68 FreeAllSurfaces(); | |
69 | |
70 if (size_.width() == 0 || size_.height() == 0) | |
71 return; // Nothing to do. | |
72 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); | |
73 gl->BindFramebuffer(GL_FRAMEBUFFER, fbo_); | |
74 gl->BindRenderbuffer(GL_RENDERBUFFER, depth_rb_); | |
75 gl->RenderbufferStorage( | |
76 GL_RENDERBUFFER, GL_DEPTH_COMPONENT24_OES, size_.width(), size_.height()); | |
77 gl->FramebufferRenderbuffer( | |
78 GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_rb_); | |
79 } | |
80 | |
81 void BufferedOutputSurface::PageFlipComplete() { | |
82 if (!in_flight_surfaces_.empty()) { | |
83 available_surfaces_.push_back(in_flight_surfaces_.front()); | |
84 in_flight_surfaces_.pop(); | |
85 } | |
86 } | |
87 | |
88 void BufferedOutputSurface::FreeAllSurfaces() { | |
89 FreeSurface(&last_surface_); | |
90 FreeSurface(¤t_surface_); | |
91 while (!in_flight_surfaces_.empty()) { | |
92 FreeSurface(&in_flight_surfaces_.front()); | |
93 in_flight_surfaces_.pop(); | |
94 } | |
95 for (size_t i = 0; i < available_surfaces_.size(); i++) | |
96 FreeSurface(&available_surfaces_[i]); | |
97 available_surfaces_.clear(); | |
98 } | |
99 | |
100 void BufferedOutputSurface::FreeSurface(AllocatedSurface* surface) { | |
101 if (surface->first) { | |
102 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); | |
103 gl->DeleteTextures(1, &surface->first); | |
alexst (slow to review)
2014/09/10 21:25:14
Use ReleaseTexImage2DCHROMIUM before deleting the
achaulk
2014/09/11 17:25:47
Done.
| |
104 gl->DestroyImageCHROMIUM(surface->second); | |
105 surface->first = 0; | |
106 surface->second = 0; | |
107 } | |
108 } | |
109 | |
110 BufferedOutputSurface::AllocatedSurface | |
111 BufferedOutputSurface::GetNextSurface() { | |
112 if (!available_surfaces_.empty()) { | |
113 AllocatedSurface id = available_surfaces_.back(); | |
114 available_surfaces_.pop_back(); | |
115 return id; | |
116 } | |
117 | |
118 unsigned int tex; | |
119 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); | |
120 gl->GenTextures(1, &tex); | |
121 | |
122 // We don't want to allow anything more than triple buffering. | |
123 DCHECK(allocated_count_ < 4); | |
alexst (slow to review)
2014/09/10 21:25:14
There is a DCHECK_LT, please use that instead.
achaulk
2014/09/11 17:25:48
Done.
| |
124 | |
125 unsigned int id = context_provider_->ContextGL()->CreateImageCHROMIUM( | |
126 size_.width(), | |
127 size_.height(), | |
128 internalformat_, | |
129 GL_IMAGE_SCANOUT_CHROMIUM); | |
130 DCHECK(id); | |
131 allocated_count_++; | |
132 gl->BindTexture(GL_TEXTURE_2D, tex); | |
133 gl->BindTexImage2DCHROMIUM(GL_TEXTURE_2D, id); | |
134 return AllocatedSurface(tex, id); | |
135 } | |
136 | |
137 } // namespace content | |
OLD | NEW |