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.
5 #include "ui/accelerated_widget_mac/io_surface_texture.h"
7 #include <OpenGL/CGLIOSurface.h>
8 #include <OpenGL/CGLRenderers.h>
10 #include <OpenGL/OpenGL.h>
12 #include "base/bind.h"
13 #include "base/bind_helpers.h"
14 #include "base/callback_helpers.h"
15 #include "base/logging.h"
16 #include "base/mac/bind_objc_block.h"
17 #include "base/message_loop/message_loop.h"
18 #include "base/threading/platform_thread.h"
19 #include "base/trace_event/trace_event.h"
20 #include "third_party/skia/include/core/SkBitmap.h"
21 #include "ui/accelerated_widget_mac/io_surface_context.h"
22 #include "ui/gfx/geometry/rect.h"
23 #include "ui/gfx/geometry/size_conversions.h"
24 #include "ui/gl/gl_context.h"
29 scoped_refptr<IOSurfaceTexture> IOSurfaceTexture::Create(
30 bool needs_gl_finish_workaround) {
31 scoped_refptr<IOSurfaceContext> offscreen_context =
32 IOSurfaceContext::Get(
33 IOSurfaceContext::kOffscreenContext);
34 if (!offscreen_context.get()) {
35 LOG(ERROR) << "Failed to create context for offscreen operations";
39 return new IOSurfaceTexture(offscreen_context, needs_gl_finish_workaround);
42 IOSurfaceTexture::IOSurfaceTexture(
43 const scoped_refptr<IOSurfaceContext>& offscreen_context,
44 bool needs_gl_finish_workaround)
45 : offscreen_context_(offscreen_context),
47 gl_error_(GL_NO_ERROR),
48 eviction_queue_iterator_(eviction_queue_.Get().end()),
49 eviction_has_been_drawn_since_updated_(false),
50 needs_gl_finish_workaround_(needs_gl_finish_workaround) {
51 CHECK(offscreen_context_.get());
54 IOSurfaceTexture::~IOSurfaceTexture() {
55 ReleaseIOSurfaceAndTexture();
56 offscreen_context_ = NULL;
57 DCHECK(eviction_queue_iterator_ == eviction_queue_.Get().end());
60 bool IOSurfaceTexture::DrawIOSurface() {
61 TRACE_EVENT0("browser", "IOSurfaceTexture::DrawIOSurface");
63 // If we have release the IOSurface, clear the screen to light grey and
66 glClearColor(0.9, 0.9, 0.9, 1);
67 glClear(GL_COLOR_BUFFER_BIT);
71 // The viewport is the size of the CALayer, which should always match the
72 // IOSurface pixel size.
74 glGetIntegerv(GL_VIEWPORT, viewport);
75 gfx::Rect viewport_rect(viewport[0], viewport[1], viewport[2], viewport[3]);
76 DCHECK_EQ(pixel_size_.ToString(), viewport_rect.size().ToString());
78 // Set the projection matrix to match 1 unit to 1 pixel.
79 glMatrixMode(GL_PROJECTION);
81 glOrtho(0, viewport_rect.width(), 0, viewport_rect.height(), -1, 1);
82 glMatrixMode(GL_MODELVIEW);
85 // Draw a quad the size of the IOSurface. This should cover the full viewport.
86 glColor4f(1, 1, 1, 1);
87 glEnable(GL_TEXTURE_RECTANGLE_ARB);
88 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture_);
92 glTexCoord2f(pixel_size_.width(), 0);
93 glVertex2f(pixel_size_.width(), 0);
94 glTexCoord2f(pixel_size_.width(), pixel_size_.height());
95 glVertex2f(pixel_size_.width(), pixel_size_.height());
96 glTexCoord2f(0, pixel_size_.height());
97 glVertex2f(0, pixel_size_.height());
99 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
100 glDisable(GL_TEXTURE_RECTANGLE_ARB);
102 // Workaround for issue 158469. Issue a dummy draw call with texture_ not
103 // bound to a texture, in order to shake all references to the IOSurface out
105 glBegin(GL_TRIANGLES);
108 if (needs_gl_finish_workaround_) {
109 TRACE_EVENT0("gpu", "glFinish");
113 // Check if any of the drawing calls result in an error.
116 if (gl_error_ != GL_NO_ERROR) {
117 LOG(ERROR) << "GL error in DrawIOSurface: " << gl_error_;
119 // If there was an error, clear the screen to a light grey to avoid
120 // rendering artifacts.
121 glClearColor(0.8, 0.8, 0.8, 1.0);
122 glClear(GL_COLOR_BUFFER_BIT);
125 eviction_has_been_drawn_since_updated_ = true;
129 bool IOSurfaceTexture::SetIOSurface(
130 IOSurfaceID io_surface_id,
131 const gfx::Size& pixel_size) {
132 TRACE_EVENT0("browser", "IOSurfaceTexture::MapIOSurfaceToTexture");
134 // Destroy the old IOSurface and texture if it is no longer needed.
135 bool needs_new_iosurface =
136 !io_surface_ || io_surface_id != IOSurfaceGetID(io_surface_);
137 if (needs_new_iosurface)
138 ReleaseIOSurfaceAndTexture();
140 // Note that because IOSurface sizes are rounded, the same IOSurface may have
141 // two different sizes associated with it, so update the sizes before the
143 pixel_size_ = pixel_size;
145 // Early-out if the IOSurface has not changed.
146 if (!needs_new_iosurface)
149 // If we early-out at any point from now on, it's because of an error, and we
150 // should destroy the texture and release the IOSurface.
151 base::ScopedClosureRunner error_runner(base::BindBlock(^{
152 ReleaseIOSurfaceAndTexture();
155 // Open the IOSurface handle.
156 io_surface_.reset(IOSurfaceLookup(io_surface_id));
160 // Actual IOSurface size is rounded up to reduce reallocations during window
161 // resize. Get the actual size to properly map the texture.
162 gfx::Size rounded_size(IOSurfaceGetWidth(io_surface_),
163 IOSurfaceGetHeight(io_surface_));
165 // Create the GL texture and set it to be backed by the IOSurface.
166 CGLError cgl_error = kCGLNoError;
168 gfx::ScopedCGLSetCurrentContext scoped_set_current_context(
169 offscreen_context_->cgl_context());
170 glGenTextures(1, &texture_);
171 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture_);
173 GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
175 GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
176 cgl_error = CGLTexImageIOSurface2D(
177 offscreen_context_->cgl_context(),
178 GL_TEXTURE_RECTANGLE_ARB,
180 rounded_size.width(),
181 rounded_size.height(),
183 GL_UNSIGNED_INT_8_8_8_8_REV,
186 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
190 // Return failure if an error was encountered by CGL or GL.
191 if (cgl_error != kCGLNoError) {
192 LOG(ERROR) << "CGLTexImageIOSurface2D failed with CGL error: " << cgl_error;
195 if (gl_error_ != GL_NO_ERROR) {
196 LOG(ERROR) << "Hit GL error in SetIOSurface: " << gl_error_;
200 ignore_result(error_runner.Release());
204 void IOSurfaceTexture::ReleaseIOSurfaceAndTexture() {
205 gfx::ScopedCGLSetCurrentContext scoped_set_current_context(
206 offscreen_context_->cgl_context());
209 glDeleteTextures(1, &texture_);
212 pixel_size_ = gfx::Size();
215 EvictionMarkEvicted();
218 bool IOSurfaceTexture::HasBeenPoisoned() const {
219 return offscreen_context_->HasBeenPoisoned();
222 GLenum IOSurfaceTexture::GetAndSaveGLError() {
223 GLenum gl_error = glGetError();
224 if (gl_error_ == GL_NO_ERROR)
225 gl_error_ = gl_error;
229 void IOSurfaceTexture::EvictionMarkUpdated() {
230 EvictionMarkEvicted();
231 eviction_queue_.Get().push_back(this);
232 eviction_queue_iterator_ = --eviction_queue_.Get().end();
233 eviction_has_been_drawn_since_updated_ = false;
234 EvictionScheduleDoEvict();
237 void IOSurfaceTexture::EvictionMarkEvicted() {
238 if (eviction_queue_iterator_ == eviction_queue_.Get().end())
240 eviction_queue_.Get().erase(eviction_queue_iterator_);
241 eviction_queue_iterator_ = eviction_queue_.Get().end();
242 eviction_has_been_drawn_since_updated_ = false;
246 void IOSurfaceTexture::EvictionScheduleDoEvict() {
247 if (eviction_scheduled_)
249 if (eviction_queue_.Get().size() <= kMaximumUnevictedSurfaces)
252 eviction_scheduled_ = true;
253 base::MessageLoop::current()->PostTask(
255 base::Bind(&IOSurfaceTexture::EvictionDoEvict));
259 void IOSurfaceTexture::EvictionDoEvict() {
260 eviction_scheduled_ = false;
261 // Walk the list of allocated surfaces from least recently used to most
263 for (EvictionQueue::iterator it = eviction_queue_.Get().begin();
264 it != eviction_queue_.Get().end();) {
265 IOSurfaceTexture* surface = *it;
268 // If the number of IOSurfaces allocated is less than the threshold,
269 // stop walking the list of surfaces.
270 if (eviction_queue_.Get().size() <= kMaximumUnevictedSurfaces)
273 // Don't evict anything that has not yet been drawn.
274 if (!surface->eviction_has_been_drawn_since_updated_)
277 // Evict the surface.
278 surface->ReleaseIOSurfaceAndTexture();
283 base::LazyInstance<IOSurfaceTexture::EvictionQueue>
284 IOSurfaceTexture::eviction_queue_;
287 bool IOSurfaceTexture::eviction_scheduled_ = false;