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, gfx::Size pixel_size, float scale_factor) { | |
121 // Allocate an ordinary OpenGL texture to back the FBO. | |
122 GLenum error = glGetError(); | |
piman
2014/06/19 21:51:02
note: there can be a stack of GL errors. If you wa
ccameron
2014/06/19 23:56:41
Done.
| |
123 if (error != GL_NO_ERROR) { | |
124 DLOG(ERROR) << "Error found (and ignored) before allocating buffer " | |
125 << "storage: " << error; | |
126 } | |
127 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, | |
128 0, | |
129 GL_RGBA, | |
130 pixel_size.width(), | |
131 pixel_size.height(), | |
132 0, | |
133 GL_RGBA, | |
134 GL_UNSIGNED_BYTE, | |
135 NULL); | |
136 error = glGetError(); | |
137 if (error != GL_NO_ERROR) { | |
138 DLOG(ERROR) << "glTexImage failed with GL error: " << error; | |
139 return false; | |
140 } | |
141 glFlush(); | |
142 | |
143 GLuint texture = 0; | |
144 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, | |
145 reinterpret_cast<GLint*>(&texture)); | |
piman
2014/06/19 21:51:02
How about passing the texture id to the function?
ccameron
2014/06/19 23:56:41
Umm, that's a good point. Somehow that eluded me.
| |
146 | |
147 // Disable the fade-in animation as the layer is changed. | |
148 ScopedCAActionDisabler disabler; | |
149 | |
150 // Resize the CAOpenGLLayer to match the size needed, and change it to be the | |
151 // hosted layer. | |
152 layer_.reset([[ImageTransportLayer alloc] initWithContext:context | |
153 withTexture:texture | |
154 withPixelSize:pixel_size | |
155 withScaleFactor:scale_factor]); | |
156 return true; | |
157 } | |
158 | |
159 void CALayerStorageProvider::FreeColorBufferStorage() { | |
160 [context_ setLayer:nil]; | |
161 layer_.reset(); | |
162 } | |
163 | |
164 uint64 CALayerStorageProvider::GetSurfaceHandle() const { | |
165 return SurfaceHandleFromCAContextID([context_ contextId]); | |
166 } | |
167 | |
168 void CALayerStorageProvider::WillSwapBuffers() { | |
169 // Don't add the layer to the CAContext until a SwapBuffers is going to be | |
170 // called, because the texture does not have any content until the | |
171 // SwapBuffers call is about to be made. | |
172 if ([context_ layer] != layer_.get()) | |
173 [context_ setLayer:layer_]; | |
174 | |
175 // TODO(ccameron): Use the isAsynchronous property to ensure smooth | |
176 // animation. | |
177 [layer_ setNeedsDisplay]; | |
178 } | |
179 | |
180 } // namespace content | |
OLD | NEW |