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/common/gpu/image_transport_surface_calayer_mac.h" |
| 6 |
| 7 #include "base/mac/sdk_forward_declarations.h" |
| 8 #include "content/common/gpu/surface_handle_types_mac.h" |
| 9 #include "ui/base/cocoa/animation_utils.h" |
| 10 #include "ui/gfx/geometry/size_conversions.h" |
| 11 |
| 12 @interface ImageTransportLayer (Private) { |
| 13 } |
| 14 @end |
| 15 |
| 16 @implementation ImageTransportLayer |
| 17 |
| 18 - (id)initWithContext:(CGLContextObj)context |
| 19 withTexture:(GLuint)texture |
| 20 withPixelSize:(gfx::Size)pixelSize |
| 21 withScaleFactor:(float)scaleFactor { |
| 22 if (self = [super init]) { |
| 23 shareContext_.reset(CGLRetainContext(context)); |
| 24 texture_ = texture; |
| 25 pixelSize_ = pixelSize; |
| 26 |
| 27 gfx::Size dipSize(gfx::ToFlooredSize(gfx::ScaleSize( |
| 28 pixelSize_, 1.0f / scaleFactor))); |
| 29 [self setContentsScale:scaleFactor]; |
| 30 [self setFrame:CGRectMake(0, 0, dipSize.width(), dipSize.height())]; |
| 31 } |
| 32 return self; |
| 33 } |
| 34 |
| 35 - (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask { |
| 36 return CGLRetainPixelFormat(CGLGetPixelFormat(shareContext_)); |
| 37 } |
| 38 |
| 39 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat { |
| 40 CGLContextObj context = NULL; |
| 41 CGLError error = CGLCreateContext(pixelFormat, shareContext_, &context); |
| 42 if (error != kCGLNoError) |
| 43 DLOG(ERROR) << "CGLCreateContext failed with CGL error: " << error; |
| 44 return context; |
| 45 } |
| 46 |
| 47 - (BOOL)canDrawInCGLContext:(CGLContextObj)glContext |
| 48 pixelFormat:(CGLPixelFormatObj)pixelFormat |
| 49 forLayerTime:(CFTimeInterval)timeInterval |
| 50 displayTime:(const CVTimeStamp*)timeStamp { |
| 51 return YES; |
| 52 } |
| 53 |
| 54 - (void)drawInCGLContext:(CGLContextObj)glContext |
| 55 pixelFormat:(CGLPixelFormatObj)pixelFormat |
| 56 forLayerTime:(CFTimeInterval)timeInterval |
| 57 displayTime:(const CVTimeStamp*)timeStamp { |
| 58 glClearColor(1, 0, 1, 1); |
| 59 glClear(GL_COLOR_BUFFER_BIT); |
| 60 |
| 61 GLint viewport[4] = {0, 0, 0, 0}; |
| 62 glGetIntegerv(GL_VIEWPORT, viewport); |
| 63 gfx::Size viewportSize(viewport[2], viewport[3]); |
| 64 |
| 65 // Set the coordinate system to be one-to-one with pixels. |
| 66 glMatrixMode(GL_PROJECTION); |
| 67 glLoadIdentity(); |
| 68 glOrtho(0, viewportSize.width(), 0, viewportSize.height(), -1, 1); |
| 69 glMatrixMode(GL_MODELVIEW); |
| 70 glLoadIdentity(); |
| 71 |
| 72 // Draw a fullscreen quad. |
| 73 glColor4f(1, 1, 1, 1); |
| 74 glEnable(GL_TEXTURE_RECTANGLE_ARB); |
| 75 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture_); |
| 76 glBegin(GL_QUADS); |
| 77 { |
| 78 glTexCoord2f(0, 0); |
| 79 glVertex2f(0, 0); |
| 80 |
| 81 glTexCoord2f(0, pixelSize_.height()); |
| 82 glVertex2f(0, pixelSize_.height()); |
| 83 |
| 84 glTexCoord2f(pixelSize_.width(), pixelSize_.height()); |
| 85 glVertex2f(pixelSize_.width(), pixelSize_.height()); |
| 86 |
| 87 glTexCoord2f(pixelSize_.width(), 0); |
| 88 glVertex2f(pixelSize_.width(), 0); |
| 89 } |
| 90 glEnd(); |
| 91 glBindTexture(0, texture_); |
| 92 glDisable(GL_TEXTURE_RECTANGLE_ARB); |
| 93 |
| 94 [super drawInCGLContext:glContext |
| 95 pixelFormat:pixelFormat |
| 96 forLayerTime:timeInterval |
| 97 displayTime:timeStamp]; |
| 98 } |
| 99 |
| 100 @end |
| 101 |
| 102 namespace content { |
| 103 |
| 104 CALayerStorageProvider::CALayerStorageProvider() { |
| 105 base::scoped_nsobject<NSDictionary> dict([[NSDictionary alloc] init]); |
| 106 CGSConnectionID connection_id = CGSMainConnectionID(); |
| 107 context_.reset([CAContext contextWithCGSConnection:connection_id |
| 108 options:dict]); |
| 109 [context_ retain]; |
| 110 } |
| 111 |
| 112 CALayerStorageProvider::~CALayerStorageProvider() { |
| 113 } |
| 114 |
| 115 gfx::Size CALayerStorageProvider::GetRoundedSize(gfx::Size size) { |
| 116 return size; |
| 117 } |
| 118 |
| 119 bool CALayerStorageProvider::AllocateColorBufferStorage( |
| 120 CGLContextObj context, GLuint texture, |
| 121 gfx::Size pixel_size, float scale_factor) { |
| 122 // Allocate an ordinary OpenGL texture to back the FBO. |
| 123 GLenum error; |
| 124 while ((error = glGetError()) != GL_NO_ERROR) { |
| 125 DLOG(ERROR) << "Error found (and ignored) before allocating buffer " |
| 126 << "storage: " << error; |
| 127 } |
| 128 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, |
| 129 0, |
| 130 GL_RGBA, |
| 131 pixel_size.width(), |
| 132 pixel_size.height(), |
| 133 0, |
| 134 GL_RGBA, |
| 135 GL_UNSIGNED_BYTE, |
| 136 NULL); |
| 137 error = glGetError(); |
| 138 if (error != GL_NO_ERROR) { |
| 139 DLOG(ERROR) << "glTexImage failed with GL error: " << error; |
| 140 return false; |
| 141 } |
| 142 glFlush(); |
| 143 |
| 144 // Disable the fade-in animation as the layer is changed. |
| 145 ScopedCAActionDisabler disabler; |
| 146 |
| 147 // Resize the CAOpenGLLayer to match the size needed, and change it to be the |
| 148 // hosted layer. |
| 149 layer_.reset([[ImageTransportLayer alloc] initWithContext:context |
| 150 withTexture:texture |
| 151 withPixelSize:pixel_size |
| 152 withScaleFactor:scale_factor]); |
| 153 return true; |
| 154 } |
| 155 |
| 156 void CALayerStorageProvider::FreeColorBufferStorage() { |
| 157 [context_ setLayer:nil]; |
| 158 layer_.reset(); |
| 159 } |
| 160 |
| 161 uint64 CALayerStorageProvider::GetSurfaceHandle() const { |
| 162 return SurfaceHandleFromCAContextID([context_ contextId]); |
| 163 } |
| 164 |
| 165 void CALayerStorageProvider::WillSwapBuffers() { |
| 166 // Don't add the layer to the CAContext until a SwapBuffers is going to be |
| 167 // called, because the texture does not have any content until the |
| 168 // SwapBuffers call is about to be made. |
| 169 if ([context_ layer] != layer_.get()) |
| 170 [context_ setLayer:layer_]; |
| 171 |
| 172 // TODO(ccameron): Use the isAsynchronous property to ensure smooth |
| 173 // animation. |
| 174 [layer_ setNeedsDisplay]; |
| 175 } |
| 176 |
| 177 } // namespace content |
OLD | NEW |