yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 1 | #import "SkSampleUIView.h" |
| 2 | |
| 3 | //#define SKWIND_CONFIG SkBitmap::kRGB_565_Config |
| 4 | #define SKWIND_CONFIG SkBitmap::kARGB_8888_Config |
| 5 | #define SKGL_CONFIG kEAGLColorFormatRGB565 |
| 6 | //#define SKGL_CONFIG kEAGLColorFormatRGBA8 |
| 7 | |
| 8 | #define FORCE_REDRAW |
| 9 | |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkCGUtils.h" |
| 12 | #include "SampleApp.h" |
| 13 | |
| 14 | #if SK_SUPPORT_GPU |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 15 | //#define USE_GL_1 |
| 16 | #define USE_GL_2 |
| 17 | |
tomhudson@google.com | 02f90e8 | 2012-02-14 15:43:01 +0000 | [diff] [blame] | 18 | #include "gl/GrGLInterface.h" |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 19 | #include "GrContext.h" |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 20 | #include "SkGpuDevice.h" |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 21 | #endif |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 22 | |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 23 | class SkiOSDeviceManager : public SampleWindow::DeviceManager { |
| 24 | public: |
bsalomon@google.com | cca3c8f | 2012-09-28 16:56:28 +0000 | [diff] [blame] | 25 | SkiOSDeviceManager(GLint layerFBO) { |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 26 | #if SK_SUPPORT_GPU |
| 27 | fCurContext = NULL; |
| 28 | fCurIntf = NULL; |
| 29 | fCurRenderTarget = NULL; |
| 30 | fMSAASampleCount = 0; |
bsalomon@google.com | cca3c8f | 2012-09-28 16:56:28 +0000 | [diff] [blame] | 31 | fLayerFBO = layerFBO; |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 32 | #endif |
| 33 | fBackend = SkOSWindow::kNone_BackEndType; |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 34 | } |
| 35 | |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 36 | virtual ~SkiOSDeviceManager() { |
| 37 | #if SK_SUPPORT_GPU |
| 38 | SkSafeUnref(fCurContext); |
| 39 | SkSafeUnref(fCurIntf); |
| 40 | SkSafeUnref(fCurRenderTarget); |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 41 | #endif |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) SK_OVERRIDE { |
| 45 | SkASSERT(SkOSWindow::kNone_BackEndType == fBackend); |
| 46 | |
| 47 | fBackend = SkOSWindow::kNone_BackEndType; |
| 48 | |
| 49 | #if SK_SUPPORT_GPU |
| 50 | switch (win->getDeviceType()) { |
| 51 | // these two don't use GL |
| 52 | case SampleWindow::kRaster_DeviceType: |
| 53 | case SampleWindow::kPicture_DeviceType: |
| 54 | break; |
| 55 | // these guys use the native backend |
| 56 | case SampleWindow::kGPU_DeviceType: |
| 57 | case SampleWindow::kNullGPU_DeviceType: |
| 58 | fBackend = SkOSWindow::kNativeGL_BackEndType; |
| 59 | break; |
| 60 | default: |
| 61 | SkASSERT(false); |
| 62 | break; |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 63 | } |
| 64 | |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 65 | bool result = win->attach(fBackend, msaaSampleCount); |
| 66 | if (!result) { |
| 67 | SkDebugf("Failed to initialize GL"); |
| 68 | return; |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 69 | } |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 70 | fMSAASampleCount = msaaSampleCount; |
| 71 | |
| 72 | SkASSERT(NULL == fCurIntf); |
| 73 | switch (win->getDeviceType()) { |
| 74 | // these two don't use GL |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 75 | case SampleWindow::kRaster_DeviceType: |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 76 | case SampleWindow::kPicture_DeviceType: |
| 77 | fCurIntf = NULL; |
| 78 | break; |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 79 | case SampleWindow::kGPU_DeviceType: |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 80 | fCurIntf = GrGLCreateNativeInterface(); |
| 81 | break; |
| 82 | case SampleWindow::kNullGPU_DeviceType: |
| 83 | fCurIntf = GrGLCreateNullInterface(); |
| 84 | break; |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 85 | default: |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 86 | SkASSERT(false); |
| 87 | break; |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 88 | } |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 89 | |
| 90 | SkASSERT(NULL == fCurContext); |
| 91 | if (SkOSWindow::kNone_BackEndType != fBackend) { |
| 92 | fCurContext = GrContext::Create(kOpenGL_Shaders_GrEngine, |
| 93 | (GrPlatform3DContext) fCurIntf); |
| 94 | } |
| 95 | |
| 96 | if ((NULL == fCurContext || NULL == fCurIntf) && |
| 97 | SkOSWindow::kNone_BackEndType != fBackend) { |
| 98 | // We need some context and interface to see results if we're using a GL backend |
| 99 | SkSafeUnref(fCurContext); |
| 100 | SkSafeUnref(fCurIntf); |
| 101 | SkDebugf("Failed to setup 3D"); |
| 102 | win->detach(); |
| 103 | } |
| 104 | #endif // SK_SUPPORT_GPU |
| 105 | // call windowSizeChanged to create the render target |
| 106 | this->windowSizeChanged(win); |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 107 | } |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 108 | |
| 109 | virtual void tearDownBackend(SampleWindow *win) SK_OVERRIDE { |
| 110 | #if SK_SUPPORT_GPU |
| 111 | SkSafeUnref(fCurContext); |
| 112 | fCurContext = NULL; |
| 113 | |
| 114 | SkSafeUnref(fCurIntf); |
| 115 | fCurIntf = NULL; |
| 116 | |
| 117 | SkSafeUnref(fCurRenderTarget); |
| 118 | fCurRenderTarget = NULL; |
| 119 | #endif |
| 120 | win->detach(); |
| 121 | fBackend = SampleWindow::kNone_BackEndType; |
| 122 | } |
reed@google.com | 5957f47 | 2012-10-01 20:31:56 +0000 | [diff] [blame] | 123 | |
| 124 | virtual SkCanvas* createCanvas(SampleWindow::DeviceType dType, |
| 125 | SampleWindow* win) { |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 126 | switch (dType) { |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 127 | case SampleWindow::kRaster_DeviceType: |
reed@google.com | 5957f47 | 2012-10-01 20:31:56 +0000 | [diff] [blame] | 128 | // fallthrough |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 129 | case SampleWindow::kPicture_DeviceType: |
reed@google.com | 5957f47 | 2012-10-01 20:31:56 +0000 | [diff] [blame] | 130 | // fallthrough |
| 131 | #if SK_ANGLE |
| 132 | case SampleWindow::kANGLE_DeviceType: |
| 133 | #endif |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 134 | break; |
| 135 | #if SK_SUPPORT_GPU |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 136 | case SampleWindow::kGPU_DeviceType: |
| 137 | case SampleWindow::kNullGPU_DeviceType: |
reed@google.com | 5957f47 | 2012-10-01 20:31:56 +0000 | [diff] [blame] | 138 | if (fCurContext) { |
| 139 | SkAutoTUnref<SkDevice> device(new SkGpuDevice(fCurContext, |
| 140 | fCurRenderTarget)); |
| 141 | return new SkCanvas(device); |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 142 | } else { |
reed@google.com | 5957f47 | 2012-10-01 20:31:56 +0000 | [diff] [blame] | 143 | return NULL; |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 144 | } |
| 145 | break; |
| 146 | #endif |
| 147 | default: |
| 148 | SkASSERT(false); |
reed@google.com | 5957f47 | 2012-10-01 20:31:56 +0000 | [diff] [blame] | 149 | return NULL; |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 150 | } |
reed@google.com | 5957f47 | 2012-10-01 20:31:56 +0000 | [diff] [blame] | 151 | return NULL; |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 152 | } |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 153 | |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 154 | virtual void publishCanvas(SampleWindow::DeviceType dType, |
| 155 | SkCanvas* canvas, |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 156 | SampleWindow* win) SK_OVERRIDE { |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 157 | #if SK_SUPPORT_GPU |
bsalomon@google.com | cca3c8f | 2012-09-28 16:56:28 +0000 | [diff] [blame] | 158 | if (NULL != fCurContext) { |
| 159 | fCurContext->flush(); |
| 160 | } |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 161 | #endif |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 162 | win->present(); |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 163 | } |
| 164 | |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 165 | virtual void windowSizeChanged(SampleWindow* win) SK_OVERRIDE { |
| 166 | #if SK_SUPPORT_GPU |
bsalomon@google.com | cca3c8f | 2012-09-28 16:56:28 +0000 | [diff] [blame] | 167 | if (NULL != fCurContext) { |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 168 | win->attach(fBackend, fMSAASampleCount); |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 169 | |
bsalomon@google.com | cca3c8f | 2012-09-28 16:56:28 +0000 | [diff] [blame] | 170 | glBindFramebuffer(GL_FRAMEBUFFER, fLayerFBO); |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 171 | GrPlatformRenderTargetDesc desc; |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 172 | desc.fWidth = SkScalarRound(win->width()); |
| 173 | desc.fHeight = SkScalarRound(win->height()); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 174 | desc.fConfig = kSkia8888_PM_GrPixelConfig; |
bsalomon@google.com | cca3c8f | 2012-09-28 16:56:28 +0000 | [diff] [blame] | 175 | desc.fRenderTargetHandle = fLayerFBO; |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 176 | glGetIntegerv(GL_SAMPLES, &desc.fSampleCnt); |
| 177 | glGetIntegerv(GL_STENCIL_BITS, &desc.fStencilBits); |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 178 | |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 179 | SkSafeUnref(fCurRenderTarget); |
| 180 | fCurRenderTarget = fCurContext->createPlatformRenderTarget(desc); |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 181 | } |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 182 | #endif |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 183 | } |
| 184 | |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 185 | virtual GrContext* getGrContext() SK_OVERRIDE { |
| 186 | #if SK_SUPPORT_GPU |
| 187 | return fCurContext; |
| 188 | #else |
| 189 | return NULL; |
| 190 | #endif |
| 191 | } |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 192 | |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 193 | virtual GrRenderTarget* getGrRenderTarget() SK_OVERRIDE { |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 194 | #if SK_SUPPORT_GPU |
| 195 | return fCurRenderTarget; |
| 196 | #else |
| 197 | return NULL; |
| 198 | #endif |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 199 | } |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 200 | |
| 201 | bool isUsingGL() const { return SkOSWindow::kNone_BackEndType != fBackend; } |
| 202 | |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 203 | private: |
bsalomon@google.com | cca3c8f | 2012-09-28 16:56:28 +0000 | [diff] [blame] | 204 | |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 205 | #if SK_SUPPORT_GPU |
| 206 | GrContext* fCurContext; |
| 207 | const GrGLInterface* fCurIntf; |
| 208 | GrRenderTarget* fCurRenderTarget; |
bsalomon@google.com | cca3c8f | 2012-09-28 16:56:28 +0000 | [diff] [blame] | 209 | int fMSAASampleCount; |
| 210 | GLint fLayerFBO; |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 211 | #endif |
| 212 | |
| 213 | SkOSWindow::SkBackEndTypes fBackend; |
| 214 | |
| 215 | typedef SampleWindow::DeviceManager INHERITED; |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | //////////////////////////////////////////////////////////////////////////////// |
| 219 | @implementation SkSampleUIView |
| 220 | |
| 221 | @synthesize fTitle, fRasterLayer, fGLLayer; |
| 222 | |
| 223 | #include "SkApplication.h" |
| 224 | #include "SkEvent.h" |
| 225 | #include "SkWindow.h" |
| 226 | |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 227 | struct FPSState { |
| 228 | static const int FRAME_COUNT = 60; |
| 229 | |
| 230 | CFTimeInterval fNow0, fNow1; |
| 231 | CFTimeInterval fTime0, fTime1, fTotalTime; |
| 232 | int fFrameCounter; |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 233 | SkString str; |
| 234 | FPSState() { |
| 235 | fTime0 = fTime1 = fTotalTime = 0; |
| 236 | fFrameCounter = 0; |
| 237 | } |
| 238 | |
| 239 | void startDraw() { |
| 240 | fNow0 = CACurrentMediaTime(); |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void endDraw() { |
| 244 | fNow1 = CACurrentMediaTime(); |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void flush(SkOSWindow* hwnd) { |
| 248 | CFTimeInterval now2 = CACurrentMediaTime(); |
| 249 | |
| 250 | fTime0 += fNow1 - fNow0; |
| 251 | fTime1 += now2 - fNow1; |
| 252 | |
| 253 | if (++fFrameCounter == FRAME_COUNT) { |
| 254 | CFTimeInterval totalNow = CACurrentMediaTime(); |
| 255 | fTotalTime = totalNow - fTotalTime; |
| 256 | |
| 257 | //SkMSec ms0 = (int)(1000 * fTime0 / FRAME_COUNT); |
| 258 | //SkMSec msTotal = (int)(1000 * fTotalTime / FRAME_COUNT); |
| 259 | //str.printf(" ms: %d [%d], fps: %3.1f", msTotal, ms0, |
| 260 | // FRAME_COUNT / fTotalTime); |
| 261 | str.printf(" fps:%3.1f", FRAME_COUNT / fTotalTime); |
| 262 | hwnd->setTitle(NULL); |
| 263 | fTotalTime = totalNow; |
| 264 | fTime0 = fTime1 = 0; |
| 265 | fFrameCounter = 0; |
| 266 | } |
| 267 | } |
| 268 | }; |
| 269 | |
| 270 | static FPSState gFPS; |
| 271 | |
| 272 | #define FPS_StartDraw() gFPS.startDraw() |
| 273 | #define FPS_EndDraw() gFPS.endDraw() |
| 274 | #define FPS_Flush(wind) gFPS.flush(wind) |
| 275 | |
| 276 | /////////////////////////////////////////////////////////////////////////////// |
| 277 | |
| 278 | - (id)initWithDefaults { |
| 279 | if (self = [super initWithDefaults]) { |
| 280 | fRedrawRequestPending = false; |
| 281 | fFPSState = new FPSState; |
| 282 | |
| 283 | #ifdef USE_GL_1 |
| 284 | fGL.fContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1]; |
| 285 | #else |
| 286 | fGL.fContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; |
| 287 | #endif |
| 288 | |
| 289 | if (!fGL.fContext || ![EAGLContext setCurrentContext:fGL.fContext]) |
| 290 | { |
| 291 | [self release]; |
| 292 | return nil; |
| 293 | } |
| 294 | |
| 295 | // Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer |
| 296 | glGenFramebuffers(1, &fGL.fFramebuffer); |
| 297 | glBindFramebuffer(GL_FRAMEBUFFER, fGL.fFramebuffer); |
| 298 | |
| 299 | glGenRenderbuffers(1, &fGL.fRenderbuffer); |
| 300 | glGenRenderbuffers(1, &fGL.fStencilbuffer); |
| 301 | |
| 302 | glBindRenderbuffer(GL_RENDERBUFFER, fGL.fRenderbuffer); |
| 303 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, fGL.fRenderbuffer); |
| 304 | |
| 305 | glBindRenderbuffer(GL_RENDERBUFFER, fGL.fStencilbuffer); |
| 306 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fGL.fStencilbuffer); |
| 307 | |
| 308 | self.fGLLayer = [CAEAGLLayer layer]; |
| 309 | fGLLayer.bounds = self.bounds; |
| 310 | fGLLayer.anchorPoint = CGPointMake(0, 0); |
| 311 | fGLLayer.opaque = TRUE; |
| 312 | [self.layer addSublayer:fGLLayer]; |
| 313 | fGLLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: |
| 314 | [NSNumber numberWithBool:NO], |
| 315 | kEAGLDrawablePropertyRetainedBacking, |
| 316 | SKGL_CONFIG, |
| 317 | kEAGLDrawablePropertyColorFormat, |
| 318 | nil]; |
| 319 | |
| 320 | self.fRasterLayer = [CALayer layer]; |
| 321 | fRasterLayer.anchorPoint = CGPointMake(0, 0); |
| 322 | fRasterLayer.opaque = TRUE; |
| 323 | [self.layer addSublayer:fRasterLayer]; |
| 324 | |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 325 | NSMutableDictionary *newActions = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSNull null], @"onOrderIn", |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 326 | [NSNull null], @"onOrderOut", |
| 327 | [NSNull null], @"sublayers", |
| 328 | [NSNull null], @"contents", |
| 329 | [NSNull null], @"bounds", |
| 330 | nil]; |
| 331 | fGLLayer.actions = newActions; |
| 332 | fRasterLayer.actions = newActions; |
| 333 | [newActions release]; |
| 334 | |
bsalomon@google.com | cca3c8f | 2012-09-28 16:56:28 +0000 | [diff] [blame] | 335 | fDevManager = new SkiOSDeviceManager(fGL.fFramebuffer); |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 336 | static char* kDummyArgv = const_cast<char*>("dummyExecutableName"); |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 337 | fWind = new SampleWindow(self, 1, &kDummyArgv, fDevManager); |
bsalomon@google.com | cca3c8f | 2012-09-28 16:56:28 +0000 | [diff] [blame] | 338 | |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 339 | fWind->resize(self.frame.size.width, self.frame.size.height, SKWIND_CONFIG); |
| 340 | } |
| 341 | return self; |
| 342 | } |
| 343 | |
| 344 | - (void)dealloc { |
| 345 | delete fDevManager; |
| 346 | delete fFPSState; |
| 347 | self.fRasterLayer = nil; |
| 348 | self.fGLLayer = nil; |
| 349 | [fGL.fContext release]; |
| 350 | [super dealloc]; |
| 351 | } |
| 352 | |
| 353 | - (void)layoutSubviews { |
| 354 | int W, H; |
| 355 | |
| 356 | // Allocate color buffer backing based on the current layer size |
| 357 | glBindRenderbuffer(GL_RENDERBUFFER, fGL.fRenderbuffer); |
| 358 | [fGL.fContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:fGLLayer]; |
| 359 | |
| 360 | glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &fGL.fWidth); |
| 361 | glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &fGL.fHeight); |
| 362 | |
| 363 | glBindRenderbuffer(GL_RENDERBUFFER, fGL.fStencilbuffer); |
| 364 | glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, fGL.fWidth, fGL.fHeight); |
| 365 | |
| 366 | if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { |
| 367 | NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER)); |
| 368 | } |
| 369 | |
| 370 | if (fDevManager->isUsingGL()) { |
| 371 | W = fGL.fWidth; |
| 372 | H = fGL.fHeight; |
| 373 | CGRect rect = CGRectMake(0, 0, W, H); |
| 374 | fGLLayer.bounds = rect; |
| 375 | } |
| 376 | else { |
| 377 | CGRect rect = self.bounds; |
| 378 | W = (int)CGRectGetWidth(rect); |
| 379 | H = (int)CGRectGetHeight(rect); |
| 380 | fRasterLayer.bounds = rect; |
| 381 | } |
| 382 | |
| 383 | printf("---- layoutSubviews %d %d\n", W, H); |
| 384 | fWind->resize(W, H); |
| 385 | fWind->inval(NULL); |
| 386 | } |
| 387 | |
| 388 | /////////////////////////////////////////////////////////////////////////////// |
| 389 | |
| 390 | - (void)drawWithCanvas:(SkCanvas*)canvas { |
| 391 | fRedrawRequestPending = false; |
| 392 | fFPSState->startDraw(); |
| 393 | fWind->draw(canvas); |
| 394 | fFPSState->endDraw(); |
| 395 | #ifdef FORCE_REDRAW |
| 396 | fWind->inval(NULL); |
| 397 | #endif |
| 398 | fFPSState->flush(fWind); |
| 399 | } |
| 400 | |
| 401 | - (void)drawInGL { |
| 402 | // This application only creates a single context which is already set current at this point. |
| 403 | // This call is redundant, but needed if dealing with multiple contexts. |
| 404 | [EAGLContext setCurrentContext:fGL.fContext]; |
| 405 | |
| 406 | // This application only creates a single default framebuffer which is already bound at this point. |
| 407 | // This call is redundant, but needed if dealing with multiple framebuffers. |
| 408 | glBindFramebuffer(GL_FRAMEBUFFER, fGL.fFramebuffer); |
| 409 | |
| 410 | GLint scissorEnable; |
| 411 | glGetIntegerv(GL_SCISSOR_TEST, &scissorEnable); |
| 412 | glDisable(GL_SCISSOR_TEST); |
| 413 | glClearColor(0,0,0,0); |
| 414 | glClear(GL_COLOR_BUFFER_BIT); |
| 415 | if (scissorEnable) { |
| 416 | glEnable(GL_SCISSOR_TEST); |
| 417 | } |
| 418 | glViewport(0, 0, fGL.fWidth, fGL.fHeight); |
| 419 | |
bsalomon@google.com | cca3c8f | 2012-09-28 16:56:28 +0000 | [diff] [blame] | 420 | |
reed@google.com | 5957f47 | 2012-10-01 20:31:56 +0000 | [diff] [blame] | 421 | SkAutoTUnref<SkCanvas> canvas(fWind->createCanvas()); |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 422 | // if we're not "retained", then we have to always redraw everything. |
| 423 | // This call forces us to ignore the fDirtyRgn, and draw everywhere. |
| 424 | // If we are "retained", we can skip this call (as the raster case does) |
| 425 | fWind->forceInvalAll(); |
bsalomon@google.com | cca3c8f | 2012-09-28 16:56:28 +0000 | [diff] [blame] | 426 | |
reed@google.com | 5957f47 | 2012-10-01 20:31:56 +0000 | [diff] [blame] | 427 | [self drawWithCanvas:canvas]; |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 428 | |
| 429 | // This application only creates a single color renderbuffer which is already bound at this point. |
| 430 | // This call is redundant, but needed if dealing with multiple renderbuffers. |
| 431 | glBindRenderbuffer(GL_RENDERBUFFER, fGL.fRenderbuffer); |
| 432 | [fGL.fContext presentRenderbuffer:GL_RENDERBUFFER]; |
| 433 | |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | - (void)drawInRaster { |
reed@google.com | 5957f47 | 2012-10-01 20:31:56 +0000 | [diff] [blame] | 437 | SkAutoTUnref<SkCanvas> canvas(fWind->createCanvas()); |
| 438 | [self drawWithCanvas:canvas]; |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 439 | CGImageRef cgimage = SkCreateCGImageRef(fWind->getBitmap()); |
| 440 | fRasterLayer.contents = (id)cgimage; |
| 441 | CGImageRelease(cgimage); |
| 442 | } |
| 443 | |
| 444 | - (void)forceRedraw { |
| 445 | if (fDevManager->isUsingGL()) |
| 446 | [self drawInGL]; |
| 447 | else |
| 448 | [self drawInRaster]; |
| 449 | } |
| 450 | |
| 451 | /////////////////////////////////////////////////////////////////////////////// |
| 452 | |
| 453 | - (void)setSkTitle:(const char *)title { |
| 454 | NSString* text = [NSString stringWithUTF8String:title]; |
| 455 | if ([text length] > 0) |
| 456 | self.fTitle = text; |
| 457 | |
| 458 | if (fTitleItem && fTitle) { |
| 459 | fTitleItem.title = [NSString stringWithFormat:@"%@%@", fTitle, |
| 460 | [NSString stringWithUTF8String:fFPSState->str.c_str()]]; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | - (void)postInvalWithRect:(const SkIRect*)r { |
| 465 | if (!fRedrawRequestPending) { |
| 466 | fRedrawRequestPending = true; |
| 467 | bool gl = fDevManager->isUsingGL(); |
| 468 | [CATransaction begin]; |
| 469 | [CATransaction setAnimationDuration:0]; |
| 470 | fRasterLayer.hidden = gl; |
| 471 | fGLLayer.hidden = !gl; |
| 472 | [CATransaction commit]; |
| 473 | if (gl) { |
| 474 | [self performSelector:@selector(drawInGL) withObject:nil afterDelay:0]; |
| 475 | } |
| 476 | else { |
| 477 | [self performSelector:@selector(drawInRaster) withObject:nil afterDelay:0]; |
| 478 | [self setNeedsDisplay]; |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | @end |