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