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 | } |
bsalomon@google.com | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 64 | SkOSWindow::AttachmentInfo info; |
| 65 | bool result = win->attach(fBackend, msaaSampleCount, &info); |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 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) { |
bsalomon@google.com | 365d787 | 2013-02-07 17:01:39 +0000 | [diff] [blame] | 92 | fCurContext = GrContext::Create(kOpenGL_GrBackend, |
| 93 | (GrBackendContext) fCurIntf); |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 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) { |
robertphillips@google.com | 9b051a3 | 2013-08-20 20:06:40 +0000 | [diff] [blame^] | 139 | SkAutoTUnref<SkDevice> device(new SkGpuDevice(fCurContext, |
| 140 | fCurRenderTarget)); |
reed@google.com | 5957f47 | 2012-10-01 20:31:56 +0000 | [diff] [blame] | 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 | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 168 | SkOSWindow::AttachmentInfo info; |
| 169 | |
| 170 | win->attach(fBackend, fMSAASampleCount, &info); |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 171 | |
bsalomon@google.com | cca3c8f | 2012-09-28 16:56:28 +0000 | [diff] [blame] | 172 | glBindFramebuffer(GL_FRAMEBUFFER, fLayerFBO); |
bsalomon@google.com | f67bd9d | 2013-02-07 16:23:58 +0000 | [diff] [blame] | 173 | GrBackendRenderTargetDesc desc; |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 174 | desc.fWidth = SkScalarRound(win->width()); |
| 175 | desc.fHeight = SkScalarRound(win->height()); |
bsalomon@google.com | f67bd9d | 2013-02-07 16:23:58 +0000 | [diff] [blame] | 176 | desc.fConfig = kSkia8888_GrPixelConfig; |
bsalomon@google.com | cca3c8f | 2012-09-28 16:56:28 +0000 | [diff] [blame] | 177 | desc.fRenderTargetHandle = fLayerFBO; |
bsalomon@google.com | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 178 | desc.fSampleCnt = info.fSampleCount; |
| 179 | desc.fStencilBits = info.fStencilBits; |
| 180 | |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 181 | SkSafeUnref(fCurRenderTarget); |
bsalomon@google.com | f67bd9d | 2013-02-07 16:23:58 +0000 | [diff] [blame] | 182 | fCurRenderTarget = fCurContext->wrapBackendRenderTarget(desc); |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 183 | } |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 184 | #endif |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 185 | } |
| 186 | |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 187 | virtual GrContext* getGrContext() SK_OVERRIDE { |
| 188 | #if SK_SUPPORT_GPU |
| 189 | return fCurContext; |
| 190 | #else |
| 191 | return NULL; |
| 192 | #endif |
| 193 | } |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 194 | |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 195 | virtual GrRenderTarget* getGrRenderTarget() SK_OVERRIDE { |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 196 | #if SK_SUPPORT_GPU |
| 197 | return fCurRenderTarget; |
| 198 | #else |
| 199 | return NULL; |
| 200 | #endif |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 201 | } |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 202 | |
| 203 | bool isUsingGL() const { return SkOSWindow::kNone_BackEndType != fBackend; } |
| 204 | |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 205 | private: |
bsalomon@google.com | cca3c8f | 2012-09-28 16:56:28 +0000 | [diff] [blame] | 206 | |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 207 | #if SK_SUPPORT_GPU |
| 208 | GrContext* fCurContext; |
| 209 | const GrGLInterface* fCurIntf; |
| 210 | GrRenderTarget* fCurRenderTarget; |
bsalomon@google.com | cca3c8f | 2012-09-28 16:56:28 +0000 | [diff] [blame] | 211 | int fMSAASampleCount; |
| 212 | GLint fLayerFBO; |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 213 | #endif |
| 214 | |
| 215 | SkOSWindow::SkBackEndTypes fBackend; |
| 216 | |
| 217 | typedef SampleWindow::DeviceManager INHERITED; |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | //////////////////////////////////////////////////////////////////////////////// |
| 221 | @implementation SkSampleUIView |
| 222 | |
| 223 | @synthesize fTitle, fRasterLayer, fGLLayer; |
| 224 | |
| 225 | #include "SkApplication.h" |
| 226 | #include "SkEvent.h" |
| 227 | #include "SkWindow.h" |
| 228 | |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 229 | struct FPSState { |
| 230 | static const int FRAME_COUNT = 60; |
| 231 | |
| 232 | CFTimeInterval fNow0, fNow1; |
| 233 | CFTimeInterval fTime0, fTime1, fTotalTime; |
| 234 | int fFrameCounter; |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 235 | SkString str; |
| 236 | FPSState() { |
| 237 | fTime0 = fTime1 = fTotalTime = 0; |
| 238 | fFrameCounter = 0; |
| 239 | } |
| 240 | |
| 241 | void startDraw() { |
| 242 | fNow0 = CACurrentMediaTime(); |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | void endDraw() { |
| 246 | fNow1 = CACurrentMediaTime(); |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void flush(SkOSWindow* hwnd) { |
| 250 | CFTimeInterval now2 = CACurrentMediaTime(); |
| 251 | |
| 252 | fTime0 += fNow1 - fNow0; |
| 253 | fTime1 += now2 - fNow1; |
| 254 | |
| 255 | if (++fFrameCounter == FRAME_COUNT) { |
| 256 | CFTimeInterval totalNow = CACurrentMediaTime(); |
| 257 | fTotalTime = totalNow - fTotalTime; |
| 258 | |
| 259 | //SkMSec ms0 = (int)(1000 * fTime0 / FRAME_COUNT); |
| 260 | //SkMSec msTotal = (int)(1000 * fTotalTime / FRAME_COUNT); |
| 261 | //str.printf(" ms: %d [%d], fps: %3.1f", msTotal, ms0, |
| 262 | // FRAME_COUNT / fTotalTime); |
| 263 | str.printf(" fps:%3.1f", FRAME_COUNT / fTotalTime); |
| 264 | hwnd->setTitle(NULL); |
| 265 | fTotalTime = totalNow; |
| 266 | fTime0 = fTime1 = 0; |
| 267 | fFrameCounter = 0; |
| 268 | } |
| 269 | } |
| 270 | }; |
| 271 | |
| 272 | static FPSState gFPS; |
| 273 | |
| 274 | #define FPS_StartDraw() gFPS.startDraw() |
| 275 | #define FPS_EndDraw() gFPS.endDraw() |
| 276 | #define FPS_Flush(wind) gFPS.flush(wind) |
| 277 | |
| 278 | /////////////////////////////////////////////////////////////////////////////// |
| 279 | |
| 280 | - (id)initWithDefaults { |
| 281 | if (self = [super initWithDefaults]) { |
| 282 | fRedrawRequestPending = false; |
| 283 | fFPSState = new FPSState; |
| 284 | |
| 285 | #ifdef USE_GL_1 |
| 286 | fGL.fContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1]; |
| 287 | #else |
| 288 | fGL.fContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; |
| 289 | #endif |
| 290 | |
| 291 | if (!fGL.fContext || ![EAGLContext setCurrentContext:fGL.fContext]) |
| 292 | { |
| 293 | [self release]; |
| 294 | return nil; |
| 295 | } |
| 296 | |
| 297 | // Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer |
| 298 | glGenFramebuffers(1, &fGL.fFramebuffer); |
| 299 | glBindFramebuffer(GL_FRAMEBUFFER, fGL.fFramebuffer); |
| 300 | |
| 301 | glGenRenderbuffers(1, &fGL.fRenderbuffer); |
| 302 | glGenRenderbuffers(1, &fGL.fStencilbuffer); |
| 303 | |
| 304 | glBindRenderbuffer(GL_RENDERBUFFER, fGL.fRenderbuffer); |
| 305 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, fGL.fRenderbuffer); |
| 306 | |
| 307 | glBindRenderbuffer(GL_RENDERBUFFER, fGL.fStencilbuffer); |
| 308 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fGL.fStencilbuffer); |
| 309 | |
| 310 | self.fGLLayer = [CAEAGLLayer layer]; |
| 311 | fGLLayer.bounds = self.bounds; |
| 312 | fGLLayer.anchorPoint = CGPointMake(0, 0); |
| 313 | fGLLayer.opaque = TRUE; |
| 314 | [self.layer addSublayer:fGLLayer]; |
| 315 | fGLLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: |
| 316 | [NSNumber numberWithBool:NO], |
| 317 | kEAGLDrawablePropertyRetainedBacking, |
| 318 | SKGL_CONFIG, |
| 319 | kEAGLDrawablePropertyColorFormat, |
| 320 | nil]; |
| 321 | |
| 322 | self.fRasterLayer = [CALayer layer]; |
| 323 | fRasterLayer.anchorPoint = CGPointMake(0, 0); |
| 324 | fRasterLayer.opaque = TRUE; |
| 325 | [self.layer addSublayer:fRasterLayer]; |
| 326 | |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 327 | NSMutableDictionary *newActions = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSNull null], @"onOrderIn", |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 328 | [NSNull null], @"onOrderOut", |
| 329 | [NSNull null], @"sublayers", |
| 330 | [NSNull null], @"contents", |
| 331 | [NSNull null], @"bounds", |
| 332 | nil]; |
| 333 | fGLLayer.actions = newActions; |
| 334 | fRasterLayer.actions = newActions; |
| 335 | [newActions release]; |
| 336 | |
bsalomon@google.com | cca3c8f | 2012-09-28 16:56:28 +0000 | [diff] [blame] | 337 | fDevManager = new SkiOSDeviceManager(fGL.fFramebuffer); |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 338 | static char* kDummyArgv = const_cast<char*>("dummyExecutableName"); |
bsalomon@google.com | 230504d | 2012-09-27 16:04:54 +0000 | [diff] [blame] | 339 | fWind = new SampleWindow(self, 1, &kDummyArgv, fDevManager); |
bsalomon@google.com | cca3c8f | 2012-09-28 16:56:28 +0000 | [diff] [blame] | 340 | |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 341 | fWind->resize(self.frame.size.width, self.frame.size.height, SKWIND_CONFIG); |
| 342 | } |
| 343 | return self; |
| 344 | } |
| 345 | |
| 346 | - (void)dealloc { |
| 347 | delete fDevManager; |
| 348 | delete fFPSState; |
| 349 | self.fRasterLayer = nil; |
| 350 | self.fGLLayer = nil; |
| 351 | [fGL.fContext release]; |
| 352 | [super dealloc]; |
| 353 | } |
| 354 | |
| 355 | - (void)layoutSubviews { |
| 356 | int W, H; |
| 357 | |
| 358 | // Allocate color buffer backing based on the current layer size |
| 359 | glBindRenderbuffer(GL_RENDERBUFFER, fGL.fRenderbuffer); |
| 360 | [fGL.fContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:fGLLayer]; |
| 361 | |
| 362 | glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &fGL.fWidth); |
| 363 | glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &fGL.fHeight); |
| 364 | |
| 365 | glBindRenderbuffer(GL_RENDERBUFFER, fGL.fStencilbuffer); |
| 366 | glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, fGL.fWidth, fGL.fHeight); |
| 367 | |
| 368 | if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { |
| 369 | NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER)); |
| 370 | } |
| 371 | |
| 372 | if (fDevManager->isUsingGL()) { |
| 373 | W = fGL.fWidth; |
| 374 | H = fGL.fHeight; |
| 375 | CGRect rect = CGRectMake(0, 0, W, H); |
| 376 | fGLLayer.bounds = rect; |
| 377 | } |
| 378 | else { |
| 379 | CGRect rect = self.bounds; |
| 380 | W = (int)CGRectGetWidth(rect); |
| 381 | H = (int)CGRectGetHeight(rect); |
| 382 | fRasterLayer.bounds = rect; |
| 383 | } |
| 384 | |
| 385 | printf("---- layoutSubviews %d %d\n", W, H); |
| 386 | fWind->resize(W, H); |
| 387 | fWind->inval(NULL); |
| 388 | } |
| 389 | |
| 390 | /////////////////////////////////////////////////////////////////////////////// |
| 391 | |
| 392 | - (void)drawWithCanvas:(SkCanvas*)canvas { |
| 393 | fRedrawRequestPending = false; |
| 394 | fFPSState->startDraw(); |
| 395 | fWind->draw(canvas); |
| 396 | fFPSState->endDraw(); |
| 397 | #ifdef FORCE_REDRAW |
| 398 | fWind->inval(NULL); |
| 399 | #endif |
| 400 | fFPSState->flush(fWind); |
| 401 | } |
| 402 | |
| 403 | - (void)drawInGL { |
| 404 | // This application only creates a single context which is already set current at this point. |
| 405 | // This call is redundant, but needed if dealing with multiple contexts. |
| 406 | [EAGLContext setCurrentContext:fGL.fContext]; |
| 407 | |
| 408 | // This application only creates a single default framebuffer which is already bound at this point. |
| 409 | // This call is redundant, but needed if dealing with multiple framebuffers. |
| 410 | glBindFramebuffer(GL_FRAMEBUFFER, fGL.fFramebuffer); |
| 411 | |
| 412 | GLint scissorEnable; |
| 413 | glGetIntegerv(GL_SCISSOR_TEST, &scissorEnable); |
| 414 | glDisable(GL_SCISSOR_TEST); |
| 415 | glClearColor(0,0,0,0); |
| 416 | glClear(GL_COLOR_BUFFER_BIT); |
| 417 | if (scissorEnable) { |
| 418 | glEnable(GL_SCISSOR_TEST); |
| 419 | } |
| 420 | glViewport(0, 0, fGL.fWidth, fGL.fHeight); |
| 421 | |
bsalomon@google.com | cca3c8f | 2012-09-28 16:56:28 +0000 | [diff] [blame] | 422 | |
reed@google.com | 5957f47 | 2012-10-01 20:31:56 +0000 | [diff] [blame] | 423 | SkAutoTUnref<SkCanvas> canvas(fWind->createCanvas()); |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 424 | // if we're not "retained", then we have to always redraw everything. |
| 425 | // This call forces us to ignore the fDirtyRgn, and draw everywhere. |
| 426 | // If we are "retained", we can skip this call (as the raster case does) |
| 427 | fWind->forceInvalAll(); |
bsalomon@google.com | cca3c8f | 2012-09-28 16:56:28 +0000 | [diff] [blame] | 428 | |
reed@google.com | 5957f47 | 2012-10-01 20:31:56 +0000 | [diff] [blame] | 429 | [self drawWithCanvas:canvas]; |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 430 | |
| 431 | // This application only creates a single color renderbuffer which is already bound at this point. |
| 432 | // This call is redundant, but needed if dealing with multiple renderbuffers. |
| 433 | glBindRenderbuffer(GL_RENDERBUFFER, fGL.fRenderbuffer); |
| 434 | [fGL.fContext presentRenderbuffer:GL_RENDERBUFFER]; |
| 435 | |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | - (void)drawInRaster { |
reed@google.com | 5957f47 | 2012-10-01 20:31:56 +0000 | [diff] [blame] | 439 | SkAutoTUnref<SkCanvas> canvas(fWind->createCanvas()); |
| 440 | [self drawWithCanvas:canvas]; |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 441 | CGImageRef cgimage = SkCreateCGImageRef(fWind->getBitmap()); |
| 442 | fRasterLayer.contents = (id)cgimage; |
| 443 | CGImageRelease(cgimage); |
| 444 | } |
| 445 | |
| 446 | - (void)forceRedraw { |
| 447 | if (fDevManager->isUsingGL()) |
| 448 | [self drawInGL]; |
| 449 | else |
| 450 | [self drawInRaster]; |
| 451 | } |
| 452 | |
| 453 | /////////////////////////////////////////////////////////////////////////////// |
| 454 | |
| 455 | - (void)setSkTitle:(const char *)title { |
| 456 | NSString* text = [NSString stringWithUTF8String:title]; |
| 457 | if ([text length] > 0) |
| 458 | self.fTitle = text; |
| 459 | |
| 460 | if (fTitleItem && fTitle) { |
| 461 | fTitleItem.title = [NSString stringWithFormat:@"%@%@", fTitle, |
| 462 | [NSString stringWithUTF8String:fFPSState->str.c_str()]]; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | - (void)postInvalWithRect:(const SkIRect*)r { |
| 467 | if (!fRedrawRequestPending) { |
| 468 | fRedrawRequestPending = true; |
| 469 | bool gl = fDevManager->isUsingGL(); |
| 470 | [CATransaction begin]; |
| 471 | [CATransaction setAnimationDuration:0]; |
| 472 | fRasterLayer.hidden = gl; |
| 473 | fGLLayer.hidden = !gl; |
| 474 | [CATransaction commit]; |
| 475 | if (gl) { |
| 476 | [self performSelector:@selector(drawInGL) withObject:nil afterDelay:0]; |
| 477 | } |
| 478 | else { |
| 479 | [self performSelector:@selector(drawInRaster) withObject:nil afterDelay:0]; |
| 480 | [self setNeedsDisplay]; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
bsalomon@google.com | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 485 | - (void)getAttachmentInfo:(SkOSWindow::AttachmentInfo*)info { |
| 486 | glBindRenderbuffer(GL_RENDERBUFFER, fGL.fRenderbuffer); |
| 487 | glGetRenderbufferParameteriv(GL_RENDERBUFFER, |
| 488 | GL_RENDERBUFFER_STENCIL_SIZE, |
| 489 | &info->fStencilBits); |
| 490 | glGetRenderbufferParameteriv(GL_RENDERBUFFER, |
| 491 | GL_RENDERBUFFER_SAMPLES_APPLE, |
| 492 | &info->fSampleCount); |
| 493 | } |
| 494 | |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 495 | @end |