Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1 | #include "SampleApp.h" |
| 2 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | #include "SkCanvas.h" |
| 4 | #include "SkDevice.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 5 | #include "SkGpuCanvas.h" |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 6 | #include "SkGpuDevice.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | #include "SkGraphics.h" |
reed@android.com | b08eb2b | 2009-01-06 20:16:26 +0000 | [diff] [blame] | 8 | #include "SkImageEncoder.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #include "SkPaint.h" |
| 10 | #include "SkPicture.h" |
| 11 | #include "SkStream.h" |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 12 | #include "SkTime.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkWindow.h" |
| 14 | |
| 15 | #include "SampleCode.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 16 | #include "GrContext.h" |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 17 | #include "SkTypeface.h" |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 18 | |
Scroggo | 3e7ff9f | 2011-06-16 15:31:26 +0000 | [diff] [blame] | 19 | #include "GrGLInterface.h" |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 20 | |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 21 | #include "SkPDFDevice.h" |
| 22 | #include "SkPDFDocument.h" |
| 23 | #include "SkStream.h" |
| 24 | |
yangsu@google.com | 1f39421 | 2011-06-01 18:03:34 +0000 | [diff] [blame] | 25 | #define TEST_GPIPEx |
| 26 | |
| 27 | #ifdef TEST_GPIPE |
| 28 | #define PIPE_FILE |
| 29 | #define FILE_PATH "/path/to/drawing.data" |
| 30 | #endif |
| 31 | |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 32 | #define USE_ARROWS_FOR_ZOOM true |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 33 | //#define DEFAULT_TO_GPU |
| 34 | |
reed@android.com | e191b16 | 2009-12-18 21:33:39 +0000 | [diff] [blame] | 35 | extern SkView* create_overview(int, const SkViewFactory[]); |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 36 | |
reed@android.com | cb34235 | 2010-07-22 18:27:53 +0000 | [diff] [blame] | 37 | #define SK_SUPPORT_GL |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | |
| 39 | #define ANIMATING_EVENTTYPE "nextSample" |
| 40 | #define ANIMATING_DELAY 750 |
| 41 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 42 | #ifdef SK_DEBUG |
reed@google.com | bad8c87 | 2011-05-18 20:10:31 +0000 | [diff] [blame] | 43 | #define FPS_REPEAT_MULTIPLIER 1 |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 44 | #else |
reed@google.com | bad8c87 | 2011-05-18 20:10:31 +0000 | [diff] [blame] | 45 | #define FPS_REPEAT_MULTIPLIER 10 |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 46 | #endif |
reed@google.com | bad8c87 | 2011-05-18 20:10:31 +0000 | [diff] [blame] | 47 | #define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER) |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 48 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 49 | #ifdef SK_SUPPORT_GL |
| 50 | #include "GrGLConfig.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 51 | #endif |
| 52 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 53 | /////////////// |
| 54 | static const char view_inval_msg[] = "view-inval-msg"; |
| 55 | |
Scroggo | 62b65b0 | 2011-06-21 16:01:26 +0000 | [diff] [blame] | 56 | void SampleWindow::postInvalDelay() { |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 57 | SkEvent* evt = new SkEvent(view_inval_msg); |
Scroggo | 62b65b0 | 2011-06-21 16:01:26 +0000 | [diff] [blame] | 58 | evt->post(this->getSinkID(), 1); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static bool isInvalEvent(const SkEvent& evt) { |
| 62 | return evt.isType(view_inval_msg); |
| 63 | } |
| 64 | ////////////////// |
| 65 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | SkViewRegister* SkViewRegister::gHead; |
| 67 | SkViewRegister::SkViewRegister(SkViewFactory fact) : fFact(fact) { |
| 68 | static bool gOnce; |
| 69 | if (!gOnce) { |
| 70 | gHead = NULL; |
| 71 | gOnce = true; |
| 72 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 73 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | fChain = gHead; |
| 75 | gHead = this; |
| 76 | } |
| 77 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 78 | #if defined(SK_SUPPORT_GL) |
| 79 | #define SK_USE_SHADERS |
| 80 | #endif |
| 81 | |
twiz@google.com | 06c3b6b | 2011-03-14 16:58:39 +0000 | [diff] [blame] | 82 | #ifdef SK_BUILD_FOR_MAC |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 83 | #include <CoreFoundation/CoreFoundation.h> |
| 84 | #include <CoreFoundation/CFURLAccess.h> |
| 85 | |
| 86 | static void testpdf() { |
| 87 | CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf", |
| 88 | kCFStringEncodingUTF8); |
| 89 | CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path, |
| 90 | kCFURLPOSIXPathStyle, |
| 91 | false); |
| 92 | CFRelease(path); |
| 93 | CGRect box = CGRectMake(0, 0, 8*72, 10*72); |
| 94 | CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL); |
| 95 | CFRelease(url); |
| 96 | |
| 97 | CGContextBeginPage(cg, &box); |
| 98 | CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5); |
| 99 | CGContextFillEllipseInRect(cg, r); |
| 100 | CGContextEndPage(cg); |
| 101 | CGContextRelease(cg); |
| 102 | |
| 103 | if (false) { |
| 104 | SkBitmap bm; |
| 105 | bm.setConfig(SkBitmap::kA8_Config, 64, 64); |
| 106 | bm.allocPixels(); |
| 107 | bm.eraseColor(0); |
| 108 | |
| 109 | SkCanvas canvas(bm); |
| 110 | |
| 111 | } |
| 112 | } |
| 113 | #endif |
| 114 | |
| 115 | ////////////////////////////////////////////////////////////////////////////// |
| 116 | |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 117 | enum FlipAxisEnum { |
| 118 | kFlipAxis_X = (1 << 0), |
| 119 | kFlipAxis_Y = (1 << 1) |
| 120 | }; |
| 121 | |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 122 | static SkTriState cycle_tristate(SkTriState state) { |
| 123 | static const SkTriState gCycle[] = { |
| 124 | /* kFalse_SkTriState -> */ kUnknown_SkTriState, |
| 125 | /* kTrue_SkTriState -> */ kFalse_SkTriState, |
| 126 | /* kUnknown_SkTriState -> */ kTrue_SkTriState, |
| 127 | }; |
| 128 | return gCycle[state]; |
| 129 | } |
| 130 | |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 131 | #include "SkDrawFilter.h" |
| 132 | |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 133 | class FlagsDrawFilter : public SkDrawFilter { |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 134 | public: |
reed@google.com | 09e3baa | 2011-05-18 12:04:31 +0000 | [diff] [blame] | 135 | FlagsDrawFilter(SkTriState lcd, SkTriState aa, SkTriState filter, |
| 136 | SkTriState hinting) : |
| 137 | fLCDState(lcd), fAAState(aa), fFilterState(filter), fHintingState(hinting) {} |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 138 | |
mike@reedtribe.org | 3ce59dc | 2011-04-08 00:38:05 +0000 | [diff] [blame] | 139 | virtual void filter(SkPaint* paint, Type t) { |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 140 | if (kText_Type == t && kUnknown_SkTriState != fLCDState) { |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 141 | paint->setLCDRenderText(kTrue_SkTriState == fLCDState); |
| 142 | } |
| 143 | if (kUnknown_SkTriState != fAAState) { |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 144 | paint->setAntiAlias(kTrue_SkTriState == fAAState); |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 145 | } |
reed@google.com | 176753a | 2011-05-17 15:32:04 +0000 | [diff] [blame] | 146 | if (kUnknown_SkTriState != fFilterState) { |
| 147 | paint->setFilterBitmap(kTrue_SkTriState == fFilterState); |
| 148 | } |
reed@google.com | 09e3baa | 2011-05-18 12:04:31 +0000 | [diff] [blame] | 149 | if (kUnknown_SkTriState != fHintingState) { |
| 150 | paint->setHinting(kTrue_SkTriState == fHintingState ? |
| 151 | SkPaint::kNormal_Hinting : |
| 152 | SkPaint::kSlight_Hinting); |
| 153 | } |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | private: |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 157 | SkTriState fLCDState; |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 158 | SkTriState fAAState; |
reed@google.com | 176753a | 2011-05-17 15:32:04 +0000 | [diff] [blame] | 159 | SkTriState fFilterState; |
reed@google.com | 09e3baa | 2011-05-18 12:04:31 +0000 | [diff] [blame] | 160 | SkTriState fHintingState; |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 163 | ////////////////////////////////////////////////////////////////////////////// |
| 164 | |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 165 | #define MAX_ZOOM_LEVEL 8 |
| 166 | #define MIN_ZOOM_LEVEL -8 |
| 167 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 168 | static const char gCharEvtName[] = "SampleCode_Char_Event"; |
| 169 | static const char gKeyEvtName[] = "SampleCode_Key_Event"; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 170 | static const char gTitleEvtName[] = "SampleCode_Title_Event"; |
| 171 | static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event"; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 172 | static const char gFastTextEvtName[] = "SampleCode_FastText_Event"; |
| 173 | |
| 174 | bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) { |
| 175 | if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) { |
| 176 | if (outUni) { |
| 177 | *outUni = evt.getFast32(); |
| 178 | } |
| 179 | return true; |
| 180 | } |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) { |
| 185 | if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) { |
| 186 | if (outKey) { |
| 187 | *outKey = (SkKey)evt.getFast32(); |
| 188 | } |
| 189 | return true; |
| 190 | } |
| 191 | return false; |
| 192 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 193 | |
| 194 | bool SampleCode::TitleQ(const SkEvent& evt) { |
| 195 | return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1); |
| 196 | } |
| 197 | |
| 198 | void SampleCode::TitleR(SkEvent* evt, const char title[]) { |
| 199 | SkASSERT(evt && TitleQ(*evt)); |
| 200 | evt->setString(gTitleEvtName, title); |
| 201 | } |
| 202 | |
| 203 | bool SampleCode::PrefSizeQ(const SkEvent& evt) { |
| 204 | return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1); |
| 205 | } |
| 206 | |
| 207 | void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) { |
| 208 | SkASSERT(evt && PrefSizeQ(*evt)); |
| 209 | SkScalar size[2]; |
| 210 | size[0] = width; |
| 211 | size[1] = height; |
| 212 | evt->setScalars(gPrefSizeEvtName, 2, size); |
| 213 | } |
| 214 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 215 | bool SampleCode::FastTextQ(const SkEvent& evt) { |
| 216 | return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1); |
| 217 | } |
| 218 | |
| 219 | /////////////////////////////////////////////////////////////////////////////// |
| 220 | |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 221 | static SkMSec gAnimTime; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 222 | static SkMSec gAnimTimePrev; |
| 223 | |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 224 | SkMSec SampleCode::GetAnimTime() { return gAnimTime; } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 225 | SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; } |
| 226 | SkScalar SampleCode::GetAnimSecondsDelta() { |
| 227 | return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0); |
| 228 | } |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 229 | |
| 230 | SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 231 | // since gAnimTime can be up to 32 bits, we can't convert it to a float |
| 232 | // or we'll lose the low bits. Hence we use doubles for the intermediate |
| 233 | // calculations |
| 234 | double seconds = (double)gAnimTime / 1000.0; |
| 235 | double value = SkScalarToDouble(speed) * seconds; |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 236 | if (period) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 237 | value = ::fmod(value, SkScalarToDouble(period)); |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 238 | } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 239 | return SkDoubleToScalar(value); |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 240 | } |
| 241 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 242 | ////////////////////////////////////////////////////////////////////////////// |
| 243 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 244 | static SkView* curr_view(SkWindow* wind) { |
| 245 | SkView::F2BIter iter(wind); |
| 246 | return iter.next(); |
| 247 | } |
| 248 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 249 | void SampleWindow::setZoomCenter(float x, float y) |
| 250 | { |
| 251 | fZoomCenterX = SkFloatToScalar(x); |
| 252 | fZoomCenterY = SkFloatToScalar(y); |
| 253 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 254 | |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 255 | bool SampleWindow::setGrContext(GrContext* context) |
| 256 | { |
| 257 | if (fGrContext) { |
| 258 | fGrContext->unref(); |
| 259 | } |
| 260 | fGrContext = context; |
| 261 | fGrContext->ref(); |
| 262 | return true; |
| 263 | } |
| 264 | |
| 265 | GrContext* SampleWindow::getGrContext() |
| 266 | { |
| 267 | return fGrContext; |
| 268 | } |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 269 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 270 | bool SampleWindow::zoomIn() |
| 271 | { |
| 272 | // Arbitrarily decided |
| 273 | if (fFatBitsScale == 25) return false; |
| 274 | fFatBitsScale++; |
| 275 | this->inval(NULL); |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | bool SampleWindow::zoomOut() |
| 280 | { |
| 281 | if (fFatBitsScale == 1) return false; |
| 282 | fFatBitsScale--; |
| 283 | this->inval(NULL); |
| 284 | return true; |
| 285 | } |
| 286 | |
| 287 | void SampleWindow::toggleZoomer() |
| 288 | { |
| 289 | fShowZoomer = !fShowZoomer; |
| 290 | this->inval(NULL); |
| 291 | } |
| 292 | |
| 293 | void SampleWindow::updatePointer(int x, int y) |
| 294 | { |
| 295 | fMouseX = x; |
| 296 | fMouseY = y; |
| 297 | if (fShowZoomer) { |
| 298 | this->inval(NULL); |
| 299 | } |
| 300 | } |
| 301 | |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 302 | bool SampleWindow::make3DReady() { |
| 303 | |
| 304 | #if defined(SK_SUPPORT_GL) |
bsalomon@google.com | 498a623 | 2011-03-10 18:24:15 +0000 | [diff] [blame] | 305 | if (attachGL()) { |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 306 | if (NULL != fGrContext) { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 307 | // various gr lifecycle tests |
| 308 | #if 0 |
| 309 | fGrContext->freeGpuResources(); |
| 310 | #elif 0 |
| 311 | // this will leak resources. |
| 312 | fGrContext->contextLost(); |
| 313 | #elif 0 |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 314 | GrAssert(1 == fGrContext->refcnt()); |
| 315 | fGrContext->unref(); |
| 316 | fGrContext = NULL; |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 317 | #endif |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 318 | } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 319 | |
bsalomon@google.com | 498a623 | 2011-03-10 18:24:15 +0000 | [diff] [blame] | 320 | if (NULL == fGrContext) { |
| 321 | #if defined(SK_USE_SHADERS) |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 322 | fGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, NULL); |
bsalomon@google.com | 498a623 | 2011-03-10 18:24:15 +0000 | [diff] [blame] | 323 | #else |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 324 | fGrContext = GrContext::Create(kOpenGL_Fixed_GrEngine, NULL); |
bsalomon@google.com | 498a623 | 2011-03-10 18:24:15 +0000 | [diff] [blame] | 325 | #endif |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 326 | SkDebugf("---- constructor\n"); |
bsalomon@google.com | 498a623 | 2011-03-10 18:24:15 +0000 | [diff] [blame] | 327 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 328 | |
bsalomon@google.com | 498a623 | 2011-03-10 18:24:15 +0000 | [diff] [blame] | 329 | if (NULL != fGrContext) { |
| 330 | return true; |
| 331 | } else { |
| 332 | detachGL(); |
| 333 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 334 | } |
| 335 | #endif |
| 336 | SkDebugf("Failed to setup 3D"); |
| 337 | return false; |
| 338 | } |
| 339 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 340 | SampleWindow::CanvasType SampleWindow::cycle_canvastype(CanvasType ct) { |
| 341 | static const CanvasType gCT[] = { |
| 342 | kPicture_CanvasType, |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 343 | kGPU_CanvasType, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 344 | kRaster_CanvasType |
| 345 | }; |
| 346 | return gCT[ct]; |
| 347 | } |
| 348 | |
| 349 | SampleWindow::SampleWindow(void* hwnd) : INHERITED(hwnd) { |
yangsu@google.com | 1f39421 | 2011-06-01 18:03:34 +0000 | [diff] [blame] | 350 | #ifdef PIPE_FILE |
| 351 | //Clear existing file or create file if it doesn't exist |
| 352 | FILE* f = fopen(FILE_PATH, "wb"); |
| 353 | fclose(f); |
| 354 | #endif |
| 355 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 356 | fPicture = NULL; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 357 | fGpuCanvas = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 358 | |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 359 | fGrContext = NULL; |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 360 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 361 | #ifdef DEFAULT_TO_GPU |
| 362 | fCanvasType = kGPU_CanvasType; |
| 363 | #else |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 364 | fCanvasType = kRaster_CanvasType; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 365 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 366 | fUseClip = false; |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 367 | fNClip = false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 368 | fRepeatDrawing = false; |
| 369 | fAnimating = false; |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 370 | fRotate = false; |
| 371 | fScale = false; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 372 | fRequestGrabImage = false; |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 373 | fUsePipe = false; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 374 | fMeasureFPS = false; |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 375 | fLCDState = kUnknown_SkTriState; |
| 376 | fAAState = kUnknown_SkTriState; |
reed@google.com | 66f22fd | 2011-05-17 15:33:45 +0000 | [diff] [blame] | 377 | fFilterState = kUnknown_SkTriState; |
reed@google.com | 09e3baa | 2011-05-18 12:04:31 +0000 | [diff] [blame] | 378 | fHintingState = kUnknown_SkTriState; |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 379 | fFlipAxis = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 380 | fScrollTestX = fScrollTestY = 0; |
| 381 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 382 | fMouseX = fMouseY = 0; |
mike@reedtribe.org | 3ce59dc | 2011-04-08 00:38:05 +0000 | [diff] [blame] | 383 | fFatBitsScale = 8; |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 384 | fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold); |
| 385 | fShowZoomer = false; |
| 386 | |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 387 | fZoomLevel = 0; |
| 388 | fZoomScale = SK_Scalar1; |
| 389 | |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 390 | fSaveToPdf = false; |
| 391 | fPdfCanvas = NULL; |
| 392 | |
bsalomon@google.com | 2fbc7fa | 2011-01-05 16:34:41 +0000 | [diff] [blame] | 393 | // this->setConfig(SkBitmap::kRGB_565_Config); |
| 394 | this->setConfig(SkBitmap::kARGB_8888_Config); |
| 395 | this->setVisibleP(true); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 396 | this->setClipToBounds(false); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 397 | |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 398 | { |
| 399 | const SkViewRegister* reg = SkViewRegister::Head(); |
| 400 | while (reg) { |
| 401 | *fSamples.append() = reg->factory(); |
| 402 | reg = reg->next(); |
| 403 | } |
| 404 | } |
| 405 | fCurrIndex = 0; |
reed@android.com | e0f13ee | 2009-11-04 19:40:25 +0000 | [diff] [blame] | 406 | this->loadView(fSamples[fCurrIndex]()); |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 407 | |
Scroggo | b4490c7 | 2011-06-17 13:53:05 +0000 | [diff] [blame] | 408 | // If another constructor set our dimensions, ensure that our |
| 409 | // onSizeChange gets called. |
| 410 | if (this->height() && this->width()) { |
| 411 | this->onSizeChange(); |
| 412 | } |
yangsu@google.com | 501775e | 2011-06-24 16:04:50 +0000 | [diff] [blame^] | 413 | |
| 414 | fPDFData = NULL; |
twiz@google.com | 06c3b6b | 2011-03-14 16:58:39 +0000 | [diff] [blame] | 415 | #ifdef SK_BUILD_FOR_MAC |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 416 | testpdf(); |
twiz@google.com | 06c3b6b | 2011-03-14 16:58:39 +0000 | [diff] [blame] | 417 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | SampleWindow::~SampleWindow() { |
| 421 | delete fPicture; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 422 | delete fGpuCanvas; |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 423 | delete fPdfCanvas; |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 424 | if (NULL != fGrContext) { |
| 425 | fGrContext->unref(); |
| 426 | } |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 427 | fTypeface->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 428 | } |
| 429 | |
reed@android.com | 55e76b2 | 2009-11-23 21:46:47 +0000 | [diff] [blame] | 430 | static SkBitmap capture_bitmap(SkCanvas* canvas) { |
| 431 | SkBitmap bm; |
| 432 | const SkBitmap& src = canvas->getDevice()->accessBitmap(false); |
| 433 | src.copyTo(&bm, src.config()); |
| 434 | return bm; |
| 435 | } |
| 436 | |
| 437 | static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig, |
| 438 | SkBitmap* diff) { |
| 439 | const SkBitmap& src = canvas->getDevice()->accessBitmap(false); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 440 | |
reed@android.com | 55e76b2 | 2009-11-23 21:46:47 +0000 | [diff] [blame] | 441 | SkAutoLockPixels alp0(src); |
| 442 | SkAutoLockPixels alp1(orig); |
| 443 | for (int y = 0; y < src.height(); y++) { |
| 444 | const void* srcP = src.getAddr(0, y); |
| 445 | const void* origP = orig.getAddr(0, y); |
| 446 | size_t bytes = src.width() * src.bytesPerPixel(); |
| 447 | if (memcmp(srcP, origP, bytes)) { |
| 448 | SkDebugf("---------- difference on line %d\n", y); |
| 449 | return true; |
| 450 | } |
| 451 | } |
| 452 | return false; |
| 453 | } |
| 454 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 455 | static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint) |
| 456 | { |
| 457 | SkColor desiredColor = paint.getColor(); |
| 458 | paint.setColor(SK_ColorWHITE); |
| 459 | const char* c_str = string.c_str(); |
| 460 | size_t size = string.size(); |
| 461 | SkRect bounds; |
| 462 | paint.measureText(c_str, size, &bounds); |
| 463 | bounds.offset(left, top); |
| 464 | SkScalar inset = SkIntToScalar(-2); |
| 465 | bounds.inset(inset, inset); |
| 466 | canvas->drawRect(bounds, paint); |
| 467 | if (desiredColor != SK_ColorBLACK) { |
| 468 | paint.setColor(SK_ColorBLACK); |
| 469 | canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint); |
| 470 | } |
| 471 | paint.setColor(desiredColor); |
| 472 | canvas->drawText(c_str, size, left, top, paint); |
| 473 | } |
| 474 | |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 475 | #define XCLIP_N 8 |
| 476 | #define YCLIP_N 8 |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 477 | |
| 478 | void SampleWindow::draw(SkCanvas* canvas) { |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 479 | // update the animation time |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 480 | gAnimTimePrev = gAnimTime; |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 481 | gAnimTime = SkTime::GetMSecs(); |
| 482 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 483 | SkScalar cx = fZoomCenterX; |
| 484 | SkScalar cy = fZoomCenterY; |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 485 | |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 486 | if (fZoomLevel) { |
| 487 | SkMatrix m; |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 488 | SkPoint center; |
| 489 | m = canvas->getTotalMatrix();//.invert(&m); |
| 490 | m.mapXY(cx, cy, ¢er); |
| 491 | cx = center.fX; |
| 492 | cy = center.fY; |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 493 | |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 494 | m.setTranslate(-cx, -cy); |
| 495 | m.postScale(fZoomScale, fZoomScale); |
| 496 | m.postTranslate(cx, cy); |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 497 | |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 498 | canvas->concat(m); |
| 499 | } |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 500 | |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 501 | if (fFlipAxis) { |
| 502 | SkMatrix m; |
| 503 | m.setTranslate(cx, cy); |
| 504 | if (fFlipAxis & kFlipAxis_X) { |
| 505 | m.preScale(-SK_Scalar1, SK_Scalar1); |
| 506 | } |
| 507 | if (fFlipAxis & kFlipAxis_Y) { |
| 508 | m.preScale(SK_Scalar1, -SK_Scalar1); |
| 509 | } |
| 510 | m.preTranslate(-cx, -cy); |
| 511 | canvas->concat(m); |
| 512 | } |
| 513 | |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 514 | // Apply any gesture matrix |
| 515 | if (true) { |
| 516 | const SkMatrix& localM = fGesture.localM(); |
| 517 | if (localM.getType() & SkMatrix::kScale_Mask) { |
| 518 | canvas->setExternalMatrix(&localM); |
| 519 | } |
| 520 | canvas->concat(localM); |
| 521 | canvas->concat(fGesture.globalM()); |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 522 | |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 523 | if (fGesture.isActive()) { |
| 524 | this->inval(NULL); |
| 525 | } |
| 526 | } |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 527 | |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 528 | if (fNClip) { |
reed@android.com | 55e76b2 | 2009-11-23 21:46:47 +0000 | [diff] [blame] | 529 | this->INHERITED::draw(canvas); |
| 530 | SkBitmap orig = capture_bitmap(canvas); |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 531 | |
| 532 | const SkScalar w = this->width(); |
| 533 | const SkScalar h = this->height(); |
| 534 | const SkScalar cw = w / XCLIP_N; |
| 535 | const SkScalar ch = h / YCLIP_N; |
| 536 | for (int y = 0; y < YCLIP_N; y++) { |
reed@android.com | 55e76b2 | 2009-11-23 21:46:47 +0000 | [diff] [blame] | 537 | SkRect r; |
| 538 | r.fTop = y * ch; |
| 539 | r.fBottom = (y + 1) * ch; |
| 540 | if (y == YCLIP_N - 1) { |
| 541 | r.fBottom = h; |
| 542 | } |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 543 | for (int x = 0; x < XCLIP_N; x++) { |
| 544 | SkAutoCanvasRestore acr(canvas, true); |
reed@android.com | 55e76b2 | 2009-11-23 21:46:47 +0000 | [diff] [blame] | 545 | r.fLeft = x * cw; |
| 546 | r.fRight = (x + 1) * cw; |
| 547 | if (x == XCLIP_N - 1) { |
| 548 | r.fRight = w; |
| 549 | } |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 550 | canvas->clipRect(r); |
| 551 | this->INHERITED::draw(canvas); |
| 552 | } |
| 553 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 554 | |
reed@android.com | 55e76b2 | 2009-11-23 21:46:47 +0000 | [diff] [blame] | 555 | SkBitmap diff; |
| 556 | if (bitmap_diff(canvas, orig, &diff)) { |
| 557 | } |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 558 | } else { |
| 559 | this->INHERITED::draw(canvas); |
| 560 | } |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 561 | if (fShowZoomer && fCanvasType != kGPU_CanvasType && !fSaveToPdf) { |
Scroggo | 3272ba8 | 2011-05-25 20:50:42 +0000 | [diff] [blame] | 562 | // In the GPU case, INHERITED::draw calls beforeChildren, which |
| 563 | // creates an SkGpuCanvas. All further draw calls are directed |
| 564 | // at that canvas, which is deleted in afterChildren (which is |
| 565 | // also called by draw), so we cannot show the zoomer here. |
| 566 | // Instead, we call it inside afterChildren. |
| 567 | showZoomer(canvas); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | void SampleWindow::showZoomer(SkCanvas* canvas) { |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 572 | int count = canvas->save(); |
| 573 | canvas->resetMatrix(); |
| 574 | // Ensure the mouse position is on screen. |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 575 | int width = SkScalarRound(this->width()); |
| 576 | int height = SkScalarRound(this->height()); |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 577 | if (fMouseX >= width) fMouseX = width - 1; |
| 578 | else if (fMouseX < 0) fMouseX = 0; |
| 579 | if (fMouseY >= height) fMouseY = height - 1; |
| 580 | else if (fMouseY < 0) fMouseY = 0; |
reed@google.com | b36334d | 2011-05-18 15:07:20 +0000 | [diff] [blame] | 581 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 582 | SkBitmap bitmap = capture_bitmap(canvas); |
reed@google.com | b36334d | 2011-05-18 15:07:20 +0000 | [diff] [blame] | 583 | bitmap.lockPixels(); |
| 584 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 585 | // Find the size of the zoomed in view, forced to be odd, so the examined pixel is in the middle. |
mike@reedtribe.org | 3ce59dc | 2011-04-08 00:38:05 +0000 | [diff] [blame] | 586 | int zoomedWidth = (width >> 1) | 1; |
| 587 | int zoomedHeight = (height >> 1) | 1; |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 588 | SkIRect src; |
| 589 | src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale); |
| 590 | src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1)); |
| 591 | SkRect dest; |
| 592 | dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight)); |
| 593 | dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight)); |
| 594 | SkPaint paint; |
| 595 | // Clear the background behind our zoomed in view |
| 596 | paint.setColor(SK_ColorWHITE); |
| 597 | canvas->drawRect(dest, paint); |
| 598 | canvas->drawBitmapRect(bitmap, &src, dest); |
| 599 | paint.setColor(SK_ColorBLACK); |
| 600 | paint.setStyle(SkPaint::kStroke_Style); |
| 601 | // Draw a border around the pixel in the middle |
| 602 | SkRect originalPixel; |
| 603 | originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1)); |
| 604 | SkMatrix matrix; |
| 605 | SkRect scalarSrc; |
| 606 | scalarSrc.set(src); |
| 607 | SkColor color = bitmap.getColor(fMouseX, fMouseY); |
| 608 | if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) { |
| 609 | SkRect pixel; |
| 610 | matrix.mapRect(&pixel, originalPixel); |
| 611 | // TODO Perhaps measure the values and make the outline white if it's "dark" |
| 612 | if (color == SK_ColorBLACK) { |
| 613 | paint.setColor(SK_ColorWHITE); |
| 614 | } |
| 615 | canvas->drawRect(pixel, paint); |
| 616 | } |
| 617 | paint.setColor(SK_ColorBLACK); |
| 618 | // Draw a border around the destination rectangle |
| 619 | canvas->drawRect(dest, paint); |
| 620 | paint.setStyle(SkPaint::kStrokeAndFill_Style); |
| 621 | // Identify the pixel and its color on screen |
| 622 | paint.setTypeface(fTypeface); |
| 623 | paint.setAntiAlias(true); |
| 624 | SkScalar lineHeight = paint.getFontMetrics(NULL); |
| 625 | SkString string; |
| 626 | string.appendf("(%i, %i)", fMouseX, fMouseY); |
| 627 | SkScalar left = dest.fLeft + SkIntToScalar(3); |
| 628 | SkScalar i = SK_Scalar1; |
| 629 | drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint); |
| 630 | // Alpha |
| 631 | i += SK_Scalar1; |
| 632 | string.reset(); |
| 633 | string.appendf("A: %X", SkColorGetA(color)); |
| 634 | drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint); |
| 635 | // Red |
| 636 | i += SK_Scalar1; |
| 637 | string.reset(); |
| 638 | string.appendf("R: %X", SkColorGetR(color)); |
| 639 | paint.setColor(SK_ColorRED); |
| 640 | drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint); |
| 641 | // Green |
| 642 | i += SK_Scalar1; |
| 643 | string.reset(); |
| 644 | string.appendf("G: %X", SkColorGetG(color)); |
| 645 | paint.setColor(SK_ColorGREEN); |
| 646 | drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint); |
| 647 | // Blue |
| 648 | i += SK_Scalar1; |
| 649 | string.reset(); |
| 650 | string.appendf("B: %X", SkColorGetB(color)); |
| 651 | paint.setColor(SK_ColorBLUE); |
| 652 | drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint); |
| 653 | canvas->restoreToCount(count); |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 654 | } |
| 655 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 656 | void SampleWindow::onDraw(SkCanvas* canvas) { |
| 657 | if (fRepeatDrawing) { |
| 658 | this->inval(NULL); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | #include "SkColorPriv.h" |
| 663 | |
| 664 | static void reverseRedAndBlue(const SkBitmap& bm) { |
| 665 | SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config); |
| 666 | uint8_t* p = (uint8_t*)bm.getPixels(); |
| 667 | uint8_t* stop = p + bm.getSize(); |
| 668 | while (p < stop) { |
| 669 | // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply |
| 670 | unsigned scale = SkAlpha255To256(p[3]); |
| 671 | unsigned r = p[2]; |
| 672 | unsigned b = p[0]; |
| 673 | p[0] = SkAlphaMul(r, scale); |
| 674 | p[1] = SkAlphaMul(p[1], scale); |
| 675 | p[2] = SkAlphaMul(b, scale); |
| 676 | p += 4; |
| 677 | } |
| 678 | } |
| 679 | |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 680 | void SampleWindow::saveToPdf() |
| 681 | { |
| 682 | fSaveToPdf = true; |
| 683 | this->inval(NULL); |
| 684 | } |
| 685 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 686 | SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 687 | if (kGPU_CanvasType != fCanvasType) { |
reed@android.com | 6efdc47 | 2008-12-19 18:24:35 +0000 | [diff] [blame] | 688 | #ifdef SK_SUPPORT_GL |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 689 | detachGL(); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 690 | #endif |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 691 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 692 | |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 693 | if (fSaveToPdf) { |
| 694 | const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false); |
| 695 | SkISize size = SkISize::Make(bmp.width(), bmp.height()); |
| 696 | SkPDFDevice* pdfDevice = new SkPDFDevice(size, size, |
| 697 | canvas->getTotalMatrix()); |
| 698 | fPdfCanvas = new SkCanvas(pdfDevice); |
| 699 | pdfDevice->unref(); |
| 700 | canvas = fPdfCanvas; |
| 701 | } else { |
| 702 | switch (fCanvasType) { |
| 703 | case kRaster_CanvasType: |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 704 | canvas = this->INHERITED::beforeChildren(canvas); |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 705 | break; |
| 706 | case kPicture_CanvasType: |
| 707 | fPicture = new SkPicture; |
| 708 | canvas = fPicture->beginRecording(9999, 9999); |
| 709 | break; |
| 710 | case kGPU_CanvasType: { |
| 711 | if (make3DReady()) { |
| 712 | SkDevice* device = canvas->getDevice(); |
| 713 | const SkBitmap& bitmap = device->accessBitmap(true); |
| 714 | |
| 715 | GrRenderTarget* renderTarget; |
| 716 | |
| 717 | GrPlatformSurfaceDesc desc; |
| 718 | desc.reset(); |
| 719 | desc.fSurfaceType = kRenderTarget_GrPlatformSurfaceType; |
| 720 | desc.fWidth = bitmap.width(); |
| 721 | desc.fHeight = bitmap.height(); |
| 722 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 723 | desc.fStencilBits = 8; |
| 724 | GrGLint buffer; |
| 725 | GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer); |
| 726 | desc.fPlatformRenderTarget = buffer; |
| 727 | |
| 728 | renderTarget = static_cast<GrRenderTarget*>( |
| 729 | fGrContext->createPlatformSurface(desc)); |
| 730 | fGpuCanvas = new SkGpuCanvas(fGrContext, renderTarget); |
| 731 | renderTarget->unref(); |
| 732 | |
| 733 | device = new SkGpuDevice(fGrContext, renderTarget); |
| 734 | fGpuCanvas->setDevice(device)->unref(); |
| 735 | |
| 736 | fGpuCanvas->concat(canvas->getTotalMatrix()); |
| 737 | canvas = fGpuCanvas; |
| 738 | |
| 739 | } else { |
| 740 | canvas = this->INHERITED::beforeChildren(canvas); |
| 741 | } |
| 742 | break; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 743 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 744 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | if (fUseClip) { |
| 748 | canvas->drawColor(0xFFFF88FF); |
| 749 | canvas->clipPath(fClipPath); |
| 750 | } |
| 751 | |
| 752 | return canvas; |
| 753 | } |
| 754 | |
| 755 | static void paint_rgn(const SkBitmap& bm, const SkIRect& r, |
| 756 | const SkRegion& rgn) { |
| 757 | SkCanvas canvas(bm); |
| 758 | SkRegion inval(rgn); |
| 759 | |
| 760 | inval.translate(r.fLeft, r.fTop); |
| 761 | canvas.clipRegion(inval); |
| 762 | canvas.drawColor(0xFFFF8080); |
| 763 | } |
yangsu@google.com | 501775e | 2011-06-24 16:04:50 +0000 | [diff] [blame^] | 764 | #include "SkData.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 765 | void SampleWindow::afterChildren(SkCanvas* orig) { |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 766 | if (fSaveToPdf) { |
| 767 | fSaveToPdf = false; |
| 768 | if (fShowZoomer) { |
| 769 | showZoomer(fPdfCanvas); |
| 770 | } |
| 771 | SkString name; |
| 772 | name.printf("%s.pdf", this->getTitle()); |
| 773 | SkPDFDocument doc; |
| 774 | SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice()); |
| 775 | doc.appendPage(device); |
| 776 | #ifdef ANDROID |
| 777 | name.prepend("/sdcard/"); |
| 778 | #endif |
yangsu@google.com | 501775e | 2011-06-24 16:04:50 +0000 | [diff] [blame^] | 779 | |
| 780 | #ifdef SK_BUILD_FOR_IOS |
| 781 | SkDynamicMemoryWStream mstream; |
| 782 | doc.emitPDF(&mstream); |
| 783 | fPDFData = SkData::NewWithCopy(mstream.getStream(),mstream.getOffset()); |
| 784 | #endif |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 785 | SkFILEWStream stream(name.c_str()); |
| 786 | if (stream.isValid()) { |
| 787 | doc.emitPDF(&stream); |
| 788 | const char* desc = "File saved from Skia SampleApp"; |
| 789 | this->onPDFSaved(this->getTitle(), desc, name.c_str()); |
| 790 | } |
yangsu@google.com | 501775e | 2011-06-24 16:04:50 +0000 | [diff] [blame^] | 791 | |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 792 | delete fPdfCanvas; |
| 793 | fPdfCanvas = NULL; |
| 794 | |
| 795 | // We took over the draw calls in order to create the PDF, so we need |
| 796 | // to redraw. |
| 797 | this->inval(NULL); |
| 798 | return; |
| 799 | } |
| 800 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 801 | if (fRequestGrabImage) { |
| 802 | fRequestGrabImage = false; |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 803 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 804 | SkCanvas* canvas = fGpuCanvas ? fGpuCanvas : orig; |
| 805 | SkDevice* device = canvas->getDevice(); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 806 | SkBitmap bmp; |
| 807 | if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 808 | static int gSampleGrabCounter; |
| 809 | SkString name; |
| 810 | name.printf("sample_grab_%d", gSampleGrabCounter++); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 811 | SkImageEncoder::EncodeFile(name.c_str(), bmp, |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 812 | SkImageEncoder::kPNG_Type, 100); |
| 813 | } |
| 814 | } |
| 815 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 816 | switch (fCanvasType) { |
| 817 | case kRaster_CanvasType: |
| 818 | break; |
| 819 | case kPicture_CanvasType: |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 820 | if (true) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 821 | SkPicture* pict = new SkPicture(*fPicture); |
| 822 | fPicture->unref(); |
| 823 | orig->drawPicture(*pict); |
| 824 | pict->unref(); |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 825 | } else if (true) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 826 | SkDynamicMemoryWStream ostream; |
| 827 | fPicture->serialize(&ostream); |
| 828 | fPicture->unref(); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 829 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 830 | SkMemoryStream istream(ostream.getStream(), ostream.getOffset()); |
| 831 | SkPicture pict(&istream); |
| 832 | orig->drawPicture(pict); |
| 833 | } else { |
| 834 | fPicture->draw(orig); |
| 835 | fPicture->unref(); |
| 836 | } |
| 837 | fPicture = NULL; |
| 838 | break; |
reed@android.com | 6efdc47 | 2008-12-19 18:24:35 +0000 | [diff] [blame] | 839 | #ifdef SK_SUPPORT_GL |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 840 | case kGPU_CanvasType: |
Scroggo | aed68d9 | 2011-06-08 14:26:00 +0000 | [diff] [blame] | 841 | if (fShowZoomer && fGpuCanvas) { |
Scroggo | 3272ba8 | 2011-05-25 20:50:42 +0000 | [diff] [blame] | 842 | this->showZoomer(fGpuCanvas); |
| 843 | } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 844 | delete fGpuCanvas; |
| 845 | fGpuCanvas = NULL; |
| 846 | presentGL(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 847 | break; |
reed@android.com | 6efdc47 | 2008-12-19 18:24:35 +0000 | [diff] [blame] | 848 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 849 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 850 | |
reed@google.com | 17d7aec | 2011-04-25 14:31:44 +0000 | [diff] [blame] | 851 | // Do this after presentGL and other finishing, rather than in afterChild |
| 852 | if (fMeasureFPS && fMeasureFPS_Time) { |
| 853 | fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time; |
| 854 | this->updateTitle(); |
Scroggo | 62b65b0 | 2011-06-21 16:01:26 +0000 | [diff] [blame] | 855 | this->postInvalDelay(); |
reed@google.com | 17d7aec | 2011-04-25 14:31:44 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | // if ((fScrollTestX | fScrollTestY) != 0) |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 859 | if (false) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 860 | const SkBitmap& bm = orig->getDevice()->accessBitmap(true); |
| 861 | int dx = fScrollTestX * 7; |
| 862 | int dy = fScrollTestY * 7; |
| 863 | SkIRect r; |
| 864 | SkRegion inval; |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 865 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 866 | r.set(50, 50, 50+100, 50+100); |
| 867 | bm.scrollRect(&r, dx, dy, &inval); |
| 868 | paint_rgn(bm, r, inval); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 869 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 870 | } |
| 871 | |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 872 | void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) { |
| 873 | if (fScale) { |
| 874 | SkScalar scale = SK_Scalar1 * 7 / 10; |
| 875 | SkScalar cx = this->width() / 2; |
| 876 | SkScalar cy = this->height() / 2; |
| 877 | canvas->translate(cx, cy); |
| 878 | canvas->scale(scale, scale); |
| 879 | canvas->translate(-cx, -cy); |
| 880 | } |
| 881 | if (fRotate) { |
| 882 | SkScalar cx = this->width() / 2; |
| 883 | SkScalar cy = this->height() / 2; |
| 884 | canvas->translate(cx, cy); |
| 885 | canvas->rotate(SkIntToScalar(30)); |
| 886 | canvas->translate(-cx, -cy); |
| 887 | } |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 888 | |
reed@google.com | 09e3baa | 2011-05-18 12:04:31 +0000 | [diff] [blame] | 889 | canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState, |
| 890 | fFilterState, fHintingState))->unref(); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 891 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 892 | if (fMeasureFPS) { |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 893 | fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw |
| 894 | if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) { |
| 895 | fMeasureFPS_Time = SkTime::GetMSecs(); |
| 896 | } |
| 897 | } else { |
| 898 | (void)SampleView::SetRepeatDraw(child, 1); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 899 | } |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 900 | (void)SampleView::SetUsePipe(child, fUsePipe); |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) { |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 904 | canvas->setDrawFilter(NULL); |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 905 | } |
| 906 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 907 | static SkBitmap::Config gConfigCycle[] = { |
| 908 | SkBitmap::kNo_Config, // none -> none |
| 909 | SkBitmap::kNo_Config, // a1 -> none |
| 910 | SkBitmap::kNo_Config, // a8 -> none |
| 911 | SkBitmap::kNo_Config, // index8 -> none |
| 912 | SkBitmap::kARGB_4444_Config, // 565 -> 4444 |
| 913 | SkBitmap::kARGB_8888_Config, // 4444 -> 8888 |
| 914 | SkBitmap::kRGB_565_Config // 8888 -> 565 |
| 915 | }; |
| 916 | |
| 917 | static SkBitmap::Config cycle_configs(SkBitmap::Config c) { |
| 918 | return gConfigCycle[c]; |
| 919 | } |
| 920 | |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 921 | void SampleWindow::changeZoomLevel(float delta) { |
| 922 | fZoomLevel += SkFloatToScalar(delta); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 923 | if (fZoomLevel > 0) { |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 924 | fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL); |
| 925 | fZoomScale = fZoomLevel + SK_Scalar1; |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 926 | } else if (fZoomLevel < 0) { |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 927 | fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL); |
| 928 | fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 929 | } else { |
| 930 | fZoomScale = SK_Scalar1; |
| 931 | } |
| 932 | |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 933 | this->updateTitle(); |
| 934 | |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 935 | this->inval(NULL); |
| 936 | } |
| 937 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 938 | bool SampleWindow::previousSample() { |
Scroggo | 62b65b0 | 2011-06-21 16:01:26 +0000 | [diff] [blame] | 939 | fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count(); |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 940 | this->loadView(fSamples[fCurrIndex]()); |
| 941 | return true; |
| 942 | } |
| 943 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 944 | bool SampleWindow::nextSample() { |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 945 | fCurrIndex = (fCurrIndex + 1) % fSamples.count(); |
| 946 | this->loadView(fSamples[fCurrIndex]()); |
| 947 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 948 | } |
| 949 | |
yangsu@google.com | 501775e | 2011-06-24 16:04:50 +0000 | [diff] [blame^] | 950 | bool SampleWindow::goToSample(int i) { |
| 951 | fCurrIndex = (i) % fSamples.count(); |
| 952 | this->loadView(fSamples[fCurrIndex]()); |
| 953 | return true; |
| 954 | } |
| 955 | |
| 956 | SkString SampleWindow::getSampleTitle(int i) { |
| 957 | SkView* view = fSamples[i](); |
| 958 | SkString title; |
| 959 | SkEvent evt(gTitleEvtName); |
| 960 | if (view->doQuery(&evt)) { |
| 961 | title.set(evt.findString(gTitleEvtName)); |
| 962 | } |
| 963 | view->unref(); |
| 964 | return title; |
| 965 | } |
| 966 | |
| 967 | int SampleWindow::sampleCount() { |
| 968 | return fSamples.count(); |
| 969 | } |
| 970 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 971 | void SampleWindow::postAnimatingEvent() { |
| 972 | if (fAnimating) { |
| 973 | SkEvent* evt = new SkEvent(ANIMATING_EVENTTYPE); |
| 974 | evt->post(this->getSinkID(), ANIMATING_DELAY); |
| 975 | } |
| 976 | } |
| 977 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 978 | bool SampleWindow::onEvent(const SkEvent& evt) { |
| 979 | if (evt.isType(ANIMATING_EVENTTYPE)) { |
| 980 | if (fAnimating) { |
| 981 | this->nextSample(); |
| 982 | this->postAnimatingEvent(); |
| 983 | } |
| 984 | return true; |
| 985 | } |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 986 | if (evt.isType("set-curr-index")) { |
| 987 | fCurrIndex = evt.getFast32() % fSamples.count(); |
| 988 | this->loadView(fSamples[fCurrIndex]()); |
| 989 | return true; |
| 990 | } |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 991 | if (isInvalEvent(evt)) { |
| 992 | this->inval(NULL); |
| 993 | return true; |
| 994 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 995 | return this->INHERITED::onEvent(evt); |
| 996 | } |
| 997 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 998 | bool SampleWindow::onQuery(SkEvent* query) { |
| 999 | if (query->isType("get-slide-count")) { |
| 1000 | query->setFast32(fSamples.count()); |
| 1001 | return true; |
| 1002 | } |
| 1003 | if (query->isType("get-slide-title")) { |
| 1004 | SkView* view = fSamples[query->getFast32()](); |
| 1005 | SkEvent evt(gTitleEvtName); |
| 1006 | if (view->doQuery(&evt)) { |
| 1007 | query->setString("title", evt.findString(gTitleEvtName)); |
| 1008 | } |
| 1009 | SkSafeUnref(view); |
| 1010 | return true; |
| 1011 | } |
| 1012 | if (query->isType("use-fast-text")) { |
| 1013 | SkEvent evt(gFastTextEvtName); |
| 1014 | return curr_view(this)->doQuery(&evt); |
| 1015 | } |
| 1016 | return this->INHERITED::onQuery(query); |
| 1017 | } |
| 1018 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 1019 | static void cleanup_for_filename(SkString* name) { |
| 1020 | char* str = name->writable_str(); |
reed@android.com | e191b16 | 2009-12-18 21:33:39 +0000 | [diff] [blame] | 1021 | for (size_t i = 0; i < name->size(); i++) { |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 1022 | switch (str[i]) { |
| 1023 | case ':': str[i] = '-'; break; |
| 1024 | case '/': str[i] = '-'; break; |
| 1025 | case ' ': str[i] = '_'; break; |
| 1026 | default: break; |
| 1027 | } |
| 1028 | } |
| 1029 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1030 | |
| 1031 | bool SampleWindow::onHandleChar(SkUnichar uni) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1032 | { |
| 1033 | SkView* view = curr_view(this); |
| 1034 | if (view) { |
| 1035 | SkEvent evt(gCharEvtName); |
| 1036 | evt.setFast32(uni); |
| 1037 | if (view->doQuery(&evt)) { |
| 1038 | return true; |
| 1039 | } |
| 1040 | } |
| 1041 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1042 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1043 | int dx = 0xFF; |
| 1044 | int dy = 0xFF; |
| 1045 | |
| 1046 | switch (uni) { |
| 1047 | case '5': dx = 0; dy = 0; break; |
| 1048 | case '8': dx = 0; dy = -1; break; |
| 1049 | case '6': dx = 1; dy = 0; break; |
| 1050 | case '2': dx = 0; dy = 1; break; |
| 1051 | case '4': dx = -1; dy = 0; break; |
| 1052 | case '7': dx = -1; dy = -1; break; |
| 1053 | case '9': dx = 1; dy = -1; break; |
| 1054 | case '3': dx = 1; dy = 1; break; |
| 1055 | case '1': dx = -1; dy = 1; break; |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1056 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1057 | default: |
| 1058 | break; |
| 1059 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1060 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1061 | if (0xFF != dx && 0xFF != dy) { |
| 1062 | if ((dx | dy) == 0) { |
| 1063 | fScrollTestX = fScrollTestY = 0; |
| 1064 | } else { |
| 1065 | fScrollTestX += dx; |
| 1066 | fScrollTestY += dy; |
| 1067 | } |
| 1068 | this->inval(NULL); |
| 1069 | return true; |
| 1070 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1071 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 1072 | switch (uni) { |
| 1073 | case 'a': |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1074 | this->toggleSlideshow(); |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 1075 | return true; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1076 | case 'b': |
| 1077 | fAAState = cycle_tristate(fAAState); |
| 1078 | this->updateTitle(); |
| 1079 | this->inval(NULL); |
| 1080 | break; |
| 1081 | case 'c': |
| 1082 | fUseClip = !fUseClip; |
| 1083 | this->inval(NULL); |
| 1084 | this->updateTitle(); |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 1085 | return true; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1086 | case 'd': |
| 1087 | SkGraphics::SetFontCacheUsed(0); |
| 1088 | return true; |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 1089 | case 'e': |
| 1090 | this->saveToPdf(); |
| 1091 | break; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1092 | case 'f': |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1093 | this->toggleFPS(); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1094 | break; |
| 1095 | case 'g': |
| 1096 | fRequestGrabImage = true; |
| 1097 | this->inval(NULL); |
| 1098 | break; |
reed@google.com | 09e3baa | 2011-05-18 12:04:31 +0000 | [diff] [blame] | 1099 | case 'h': |
| 1100 | fHintingState = cycle_tristate(fHintingState); |
| 1101 | this->updateTitle(); |
| 1102 | this->inval(NULL); |
| 1103 | break; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1104 | case 'i': |
| 1105 | this->zoomIn(); |
| 1106 | break; |
| 1107 | case 'l': |
| 1108 | fLCDState = cycle_tristate(fLCDState); |
| 1109 | this->updateTitle(); |
| 1110 | this->inval(NULL); |
| 1111 | break; |
reed@google.com | 176753a | 2011-05-17 15:32:04 +0000 | [diff] [blame] | 1112 | case 'n': |
| 1113 | fFilterState = cycle_tristate(fFilterState); |
| 1114 | this->updateTitle(); |
| 1115 | this->inval(NULL); |
| 1116 | break; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1117 | case 'o': |
| 1118 | this->zoomOut(); |
| 1119 | break; |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 1120 | case 'p': |
| 1121 | fUsePipe = !fUsePipe; |
reed@google.com | a6ff4dc | 2011-05-12 22:08:24 +0000 | [diff] [blame] | 1122 | this->updateTitle(); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 1123 | this->inval(NULL); |
| 1124 | break; |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 1125 | case 'r': |
| 1126 | fRotate = !fRotate; |
| 1127 | this->inval(NULL); |
| 1128 | this->updateTitle(); |
| 1129 | return true; |
| 1130 | case 's': |
| 1131 | fScale = !fScale; |
| 1132 | this->inval(NULL); |
| 1133 | this->updateTitle(); |
| 1134 | return true; |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 1135 | case 'x': |
| 1136 | fFlipAxis ^= kFlipAxis_X; |
| 1137 | this->updateTitle(); |
| 1138 | this->inval(NULL); |
| 1139 | break; |
| 1140 | case 'y': |
| 1141 | fFlipAxis ^= kFlipAxis_Y; |
| 1142 | this->updateTitle(); |
| 1143 | this->inval(NULL); |
| 1144 | break; |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 1145 | case 'z': |
| 1146 | this->toggleZoomer(); |
| 1147 | break; |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 1148 | default: |
| 1149 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1150 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1151 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1152 | return this->INHERITED::onHandleChar(uni); |
| 1153 | } |
| 1154 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1155 | void SampleWindow::toggleFPS() { |
| 1156 | fMeasureFPS = !fMeasureFPS; |
| 1157 | this->inval(NULL); |
| 1158 | this->updateTitle(); |
| 1159 | } |
| 1160 | |
| 1161 | void SampleWindow::toggleSlideshow() { |
| 1162 | fAnimating = !fAnimating; |
| 1163 | this->postAnimatingEvent(); |
| 1164 | this->updateTitle(); |
| 1165 | } |
| 1166 | |
| 1167 | void SampleWindow::toggleRendering() { |
| 1168 | fCanvasType = cycle_canvastype(fCanvasType); |
| 1169 | this->updateTitle(); |
| 1170 | this->inval(NULL); |
| 1171 | } |
| 1172 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1173 | #include "SkDumpCanvas.h" |
| 1174 | |
| 1175 | bool SampleWindow::onHandleKey(SkKey key) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1176 | { |
| 1177 | SkView* view = curr_view(this); |
| 1178 | if (view) { |
| 1179 | SkEvent evt(gKeyEvtName); |
| 1180 | evt.setFast32(key); |
| 1181 | if (view->doQuery(&evt)) { |
| 1182 | return true; |
| 1183 | } |
| 1184 | } |
| 1185 | } |
| 1186 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1187 | switch (key) { |
| 1188 | case kRight_SkKey: |
| 1189 | if (this->nextSample()) { |
| 1190 | return true; |
| 1191 | } |
| 1192 | break; |
| 1193 | case kLeft_SkKey: |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1194 | toggleRendering(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1195 | return true; |
| 1196 | case kUp_SkKey: |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1197 | if (USE_ARROWS_FOR_ZOOM) { |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 1198 | this->changeZoomLevel(1.f); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1199 | } else { |
| 1200 | fNClip = !fNClip; |
| 1201 | this->inval(NULL); |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 1202 | this->updateTitle(); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1203 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1204 | return true; |
| 1205 | case kDown_SkKey: |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1206 | if (USE_ARROWS_FOR_ZOOM) { |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 1207 | this->changeZoomLevel(-1.f); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1208 | } else { |
| 1209 | this->setConfig(cycle_configs(this->getBitmap().config())); |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 1210 | this->updateTitle(); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1211 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1212 | return true; |
| 1213 | case kOK_SkKey: |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1214 | if (false) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1215 | SkDebugfDumper dumper; |
| 1216 | SkDumpCanvas dc(&dumper); |
| 1217 | this->draw(&dc); |
| 1218 | } else { |
| 1219 | fRepeatDrawing = !fRepeatDrawing; |
| 1220 | if (fRepeatDrawing) { |
| 1221 | this->inval(NULL); |
| 1222 | } |
| 1223 | } |
| 1224 | return true; |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 1225 | case kBack_SkKey: |
| 1226 | this->loadView(NULL); |
| 1227 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1228 | default: |
| 1229 | break; |
| 1230 | } |
| 1231 | return this->INHERITED::onHandleKey(key); |
| 1232 | } |
| 1233 | |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 1234 | /////////////////////////////////////////////////////////////////////////////// |
| 1235 | |
| 1236 | static const char gGestureClickType[] = "GestureClickType"; |
| 1237 | |
Scroggo | d3aed39 | 2011-06-22 13:26:56 +0000 | [diff] [blame] | 1238 | bool SampleWindow::onDispatchClick(int x, int y, Click::State state, |
| 1239 | void* owner) { |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 1240 | if (Click::kMoved_State == state) { |
| 1241 | updatePointer(x, y); |
| 1242 | } |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1243 | int w = SkScalarRound(this->width()); |
| 1244 | int h = SkScalarRound(this->height()); |
| 1245 | |
| 1246 | // check for the resize-box |
| 1247 | if (w - x < 16 && h - y < 16) { |
| 1248 | return false; // let the OS handle the click |
| 1249 | } else { |
Scroggo | d3aed39 | 2011-06-22 13:26:56 +0000 | [diff] [blame] | 1250 | return this->INHERITED::onDispatchClick(x, y, state, owner); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1251 | } |
| 1252 | } |
| 1253 | |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 1254 | class GestureClick : public SkView::Click { |
| 1255 | public: |
| 1256 | GestureClick(SkView* target) : SkView::Click(target) { |
| 1257 | this->setType(gGestureClickType); |
| 1258 | } |
| 1259 | |
| 1260 | static bool IsGesture(Click* click) { |
| 1261 | return click->isType(gGestureClickType); |
| 1262 | } |
| 1263 | }; |
| 1264 | |
| 1265 | SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) { |
| 1266 | return new GestureClick(this); |
| 1267 | } |
| 1268 | |
| 1269 | bool SampleWindow::onClick(Click* click) { |
| 1270 | if (GestureClick::IsGesture(click)) { |
| 1271 | float x = SkScalarToFloat(click->fCurr.fX); |
| 1272 | float y = SkScalarToFloat(click->fCurr.fY); |
| 1273 | switch (click->fState) { |
| 1274 | case SkView::Click::kDown_State: |
Scroggo | d3aed39 | 2011-06-22 13:26:56 +0000 | [diff] [blame] | 1275 | fGesture.touchBegin(click->fOwner, x, y); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 1276 | break; |
| 1277 | case SkView::Click::kMoved_State: |
Scroggo | d3aed39 | 2011-06-22 13:26:56 +0000 | [diff] [blame] | 1278 | fGesture.touchMoved(click->fOwner, x, y); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 1279 | this->inval(NULL); |
| 1280 | break; |
| 1281 | case SkView::Click::kUp_State: |
Scroggo | d3aed39 | 2011-06-22 13:26:56 +0000 | [diff] [blame] | 1282 | fGesture.touchEnd(click->fOwner); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 1283 | this->inval(NULL); |
| 1284 | break; |
| 1285 | } |
| 1286 | return true; |
| 1287 | } |
| 1288 | return false; |
| 1289 | } |
| 1290 | |
| 1291 | /////////////////////////////////////////////////////////////////////////////// |
| 1292 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1293 | void SampleWindow::loadView(SkView* view) { |
| 1294 | SkView::F2BIter iter(this); |
| 1295 | SkView* prev = iter.next(); |
| 1296 | if (prev) { |
| 1297 | prev->detachFromParent(); |
| 1298 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1299 | |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 1300 | if (NULL == view) { |
| 1301 | view = create_overview(fSamples.count(), fSamples.begin()); |
| 1302 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1303 | view->setVisibleP(true); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1304 | view->setClipToBounds(false); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1305 | this->attachChildToFront(view)->unref(); |
| 1306 | view->setSize(this->width(), this->height()); |
| 1307 | |
| 1308 | this->updateTitle(); |
| 1309 | } |
| 1310 | |
| 1311 | static const char* gConfigNames[] = { |
| 1312 | "unknown config", |
| 1313 | "A1", |
| 1314 | "A8", |
| 1315 | "Index8", |
| 1316 | "565", |
| 1317 | "4444", |
| 1318 | "8888" |
| 1319 | }; |
| 1320 | |
| 1321 | static const char* configToString(SkBitmap::Config c) { |
| 1322 | return gConfigNames[c]; |
| 1323 | } |
| 1324 | |
| 1325 | static const char* gCanvasTypePrefix[] = { |
| 1326 | "raster: ", |
| 1327 | "picture: ", |
| 1328 | "opengl: " |
| 1329 | }; |
| 1330 | |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 1331 | static const char* trystate_str(SkTriState state, |
| 1332 | const char trueStr[], const char falseStr[]) { |
| 1333 | if (kTrue_SkTriState == state) { |
| 1334 | return trueStr; |
| 1335 | } else if (kFalse_SkTriState == state) { |
| 1336 | return falseStr; |
| 1337 | } |
| 1338 | return NULL; |
| 1339 | } |
| 1340 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1341 | void SampleWindow::updateTitle() { |
| 1342 | SkString title; |
| 1343 | |
| 1344 | SkView::F2BIter iter(this); |
| 1345 | SkView* view = iter.next(); |
| 1346 | SkEvent evt(gTitleEvtName); |
| 1347 | if (view->doQuery(&evt)) { |
| 1348 | title.set(evt.findString(gTitleEvtName)); |
| 1349 | } |
| 1350 | if (title.size() == 0) { |
| 1351 | title.set("<unknown>"); |
| 1352 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1353 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1354 | title.prepend(gCanvasTypePrefix[fCanvasType]); |
| 1355 | |
| 1356 | title.prepend(" "); |
| 1357 | title.prepend(configToString(this->getBitmap().config())); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1358 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1359 | if (fAnimating) { |
| 1360 | title.prepend("<A> "); |
| 1361 | } |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 1362 | if (fScale) { |
| 1363 | title.prepend("<S> "); |
| 1364 | } |
| 1365 | if (fRotate) { |
| 1366 | title.prepend("<R> "); |
| 1367 | } |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 1368 | if (fNClip) { |
| 1369 | title.prepend("<C> "); |
| 1370 | } |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 1371 | |
| 1372 | title.prepend(trystate_str(fLCDState, "LCD ", "lcd ")); |
| 1373 | title.prepend(trystate_str(fAAState, "AA ", "aa ")); |
reed@google.com | 09e3baa | 2011-05-18 12:04:31 +0000 | [diff] [blame] | 1374 | title.prepend(trystate_str(fFilterState, "H ", "h ")); |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 1375 | title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL); |
| 1376 | title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1377 | |
| 1378 | if (fZoomLevel) { |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 1379 | title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel)); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1380 | } |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1381 | |
| 1382 | if (fMeasureFPS) { |
reed@google.com | bad8c87 | 2011-05-18 20:10:31 +0000 | [diff] [blame] | 1383 | title.appendf(" %6.1f ms", fMeasureFPS_Time / (float)FPS_REPEAT_MULTIPLIER); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1384 | } |
reed@google.com | a6ff4dc | 2011-05-12 22:08:24 +0000 | [diff] [blame] | 1385 | if (fUsePipe && SampleView::IsSampleView(view)) { |
| 1386 | title.prepend("<P> "); |
| 1387 | } |
| 1388 | if (SampleView::IsSampleView(view)) { |
| 1389 | title.prepend("! "); |
| 1390 | } |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1391 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1392 | this->setTitle(title.c_str()); |
| 1393 | } |
| 1394 | |
| 1395 | void SampleWindow::onSizeChange() { |
| 1396 | this->INHERITED::onSizeChange(); |
| 1397 | |
| 1398 | SkView::F2BIter iter(this); |
| 1399 | SkView* view = iter.next(); |
| 1400 | view->setSize(this->width(), this->height()); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1401 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1402 | // rebuild our clippath |
| 1403 | { |
| 1404 | const SkScalar W = this->width(); |
| 1405 | const SkScalar H = this->height(); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1406 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1407 | fClipPath.reset(); |
| 1408 | #if 0 |
| 1409 | for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) { |
| 1410 | SkRect r; |
| 1411 | r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30)); |
| 1412 | for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0)) |
| 1413 | fClipPath.addRect(r); |
| 1414 | } |
| 1415 | #else |
| 1416 | SkRect r; |
| 1417 | r.set(0, 0, W, H); |
| 1418 | fClipPath.addRect(r, SkPath::kCCW_Direction); |
| 1419 | r.set(W/4, H/4, W*3/4, H*3/4); |
| 1420 | fClipPath.addRect(r, SkPath::kCW_Direction); |
| 1421 | #endif |
| 1422 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1423 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1424 | fZoomCenterX = SkScalarHalf(this->width()); |
| 1425 | fZoomCenterY = SkScalarHalf(this->height()); |
| 1426 | |
Scroggo | 3e7ff9f | 2011-06-16 15:31:26 +0000 | [diff] [blame] | 1427 | #ifdef ANDROID |
Scroggo | b4490c7 | 2011-06-17 13:53:05 +0000 | [diff] [blame] | 1428 | // FIXME: The first draw after a size change does not work on Android, so |
| 1429 | // we post an invalidate. |
Scroggo | 62b65b0 | 2011-06-21 16:01:26 +0000 | [diff] [blame] | 1430 | this->postInvalDelay(); |
Scroggo | 716a038 | 2011-06-16 14:00:15 +0000 | [diff] [blame] | 1431 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1432 | this->updateTitle(); // to refresh our config |
| 1433 | } |
| 1434 | |
| 1435 | /////////////////////////////////////////////////////////////////////////////// |
| 1436 | |
reed@google.com | a6ff4dc | 2011-05-12 22:08:24 +0000 | [diff] [blame] | 1437 | static const char is_sample_view_tag[] = "sample-is-sample-view"; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1438 | static const char repeat_count_tag[] = "sample-set-repeat-count"; |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 1439 | static const char set_use_pipe_tag[] = "sample-set-use-pipe"; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1440 | |
reed@google.com | a6ff4dc | 2011-05-12 22:08:24 +0000 | [diff] [blame] | 1441 | bool SampleView::IsSampleView(SkView* view) { |
| 1442 | SkEvent evt(is_sample_view_tag); |
| 1443 | return view->doQuery(&evt); |
| 1444 | } |
| 1445 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 1446 | bool SampleView::SetRepeatDraw(SkView* view, int count) { |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1447 | SkEvent evt(repeat_count_tag); |
| 1448 | evt.setFast32(count); |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 1449 | return view->doEvent(evt); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 1452 | bool SampleView::SetUsePipe(SkView* view, bool pred) { |
| 1453 | SkEvent evt(set_use_pipe_tag); |
| 1454 | evt.setFast32(pred); |
| 1455 | return view->doEvent(evt); |
| 1456 | } |
| 1457 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1458 | bool SampleView::onEvent(const SkEvent& evt) { |
| 1459 | if (evt.isType(repeat_count_tag)) { |
| 1460 | fRepeatCount = evt.getFast32(); |
| 1461 | return true; |
| 1462 | } |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 1463 | if (evt.isType(set_use_pipe_tag)) { |
| 1464 | fUsePipe = !!evt.getFast32(); |
| 1465 | return true; |
| 1466 | } |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1467 | return this->INHERITED::onEvent(evt); |
| 1468 | } |
| 1469 | |
| 1470 | bool SampleView::onQuery(SkEvent* evt) { |
reed@google.com | a6ff4dc | 2011-05-12 22:08:24 +0000 | [diff] [blame] | 1471 | if (evt->isType(is_sample_view_tag)) { |
| 1472 | return true; |
| 1473 | } |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1474 | return this->INHERITED::onQuery(evt); |
| 1475 | } |
| 1476 | |
reed@google.com | 68f456d | 2011-05-02 18:55:39 +0000 | [diff] [blame] | 1477 | #ifdef TEST_GPIPE |
| 1478 | #include "SkGPipe.h" |
reed@google.com | 64e3eb2 | 2011-05-04 14:32:04 +0000 | [diff] [blame] | 1479 | |
| 1480 | class SimplePC : public SkGPipeController { |
| 1481 | public: |
| 1482 | SimplePC(SkCanvas* target); |
| 1483 | ~SimplePC(); |
| 1484 | |
| 1485 | virtual void* requestBlock(size_t minRequest, size_t* actual); |
| 1486 | virtual void notifyWritten(size_t bytes); |
| 1487 | |
| 1488 | private: |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 1489 | SkGPipeReader fReader; |
| 1490 | void* fBlock; |
| 1491 | size_t fBlockSize; |
| 1492 | size_t fBytesWritten; |
| 1493 | int fAtomsWritten; |
reed@google.com | 64e3eb2 | 2011-05-04 14:32:04 +0000 | [diff] [blame] | 1494 | SkGPipeReader::Status fStatus; |
| 1495 | |
| 1496 | size_t fTotalWritten; |
| 1497 | }; |
| 1498 | |
| 1499 | SimplePC::SimplePC(SkCanvas* target) : fReader(target) { |
| 1500 | fBlock = NULL; |
| 1501 | fBlockSize = fBytesWritten = 0; |
| 1502 | fStatus = SkGPipeReader::kDone_Status; |
| 1503 | fTotalWritten = 0; |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 1504 | fAtomsWritten = 0; |
reed@google.com | 64e3eb2 | 2011-05-04 14:32:04 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | SimplePC::~SimplePC() { |
| 1508 | // SkASSERT(SkGPipeReader::kDone_Status == fStatus); |
| 1509 | sk_free(fBlock); |
| 1510 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 1511 | if (fTotalWritten) { |
| 1512 | SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten, |
| 1513 | fAtomsWritten, fStatus); |
| 1514 | } |
reed@google.com | 64e3eb2 | 2011-05-04 14:32:04 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | void* SimplePC::requestBlock(size_t minRequest, size_t* actual) { |
| 1518 | sk_free(fBlock); |
| 1519 | |
| 1520 | fBlockSize = minRequest * 4; |
| 1521 | fBlock = sk_malloc_throw(fBlockSize); |
| 1522 | fBytesWritten = 0; |
| 1523 | *actual = fBlockSize; |
| 1524 | return fBlock; |
| 1525 | } |
| 1526 | |
| 1527 | void SimplePC::notifyWritten(size_t bytes) { |
| 1528 | SkASSERT(fBytesWritten + bytes <= fBlockSize); |
yangsu@google.com | 1f39421 | 2011-06-01 18:03:34 +0000 | [diff] [blame] | 1529 | |
| 1530 | #ifdef PIPE_FILE |
| 1531 | //File is open in append mode |
| 1532 | FILE* f = fopen(FILE_PATH, "ab"); |
| 1533 | SkASSERT(f != NULL); |
| 1534 | fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f); |
| 1535 | fclose(f); |
| 1536 | #endif |
| 1537 | |
reed@google.com | 64e3eb2 | 2011-05-04 14:32:04 +0000 | [diff] [blame] | 1538 | fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes); |
| 1539 | SkASSERT(SkGPipeReader::kError_Status != fStatus); |
| 1540 | fBytesWritten += bytes; |
| 1541 | fTotalWritten += bytes; |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 1542 | |
| 1543 | fAtomsWritten += 1; |
reed@google.com | 64e3eb2 | 2011-05-04 14:32:04 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
reed@google.com | 68f456d | 2011-05-02 18:55:39 +0000 | [diff] [blame] | 1546 | #endif |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 1547 | |
reed@google.com | 64e3eb2 | 2011-05-04 14:32:04 +0000 | [diff] [blame] | 1548 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1549 | void SampleView::onDraw(SkCanvas* canvas) { |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 1550 | #ifdef TEST_GPIPE |
reed@google.com | 64e3eb2 | 2011-05-04 14:32:04 +0000 | [diff] [blame] | 1551 | SimplePC controller(canvas); |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 1552 | SkGPipeWriter writer; |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 1553 | if (fUsePipe) { |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 1554 | uint32_t flags = SkGPipeWriter::kCrossProcess_Flag; |
| 1555 | // flags = 0; |
| 1556 | canvas = writer.startRecording(&controller, flags); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 1557 | } |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 1558 | #endif |
| 1559 | |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 1560 | this->onDrawBackground(canvas); |
| 1561 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1562 | for (int i = 0; i < fRepeatCount; i++) { |
| 1563 | SkAutoCanvasRestore acr(canvas, true); |
| 1564 | this->onDrawContent(canvas); |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | void SampleView::onDrawBackground(SkCanvas* canvas) { |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 1569 | canvas->drawColor(fBGColor); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | /////////////////////////////////////////////////////////////////////////////// |
| 1573 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1574 | template <typename T> void SkTBSort(T array[], int count) { |
| 1575 | for (int i = 1; i < count - 1; i++) { |
| 1576 | bool didSwap = false; |
| 1577 | for (int j = count - 1; j > i; --j) { |
| 1578 | if (array[j] < array[j-1]) { |
| 1579 | T tmp(array[j-1]); |
| 1580 | array[j-1] = array[j]; |
| 1581 | array[j] = tmp; |
| 1582 | didSwap = true; |
| 1583 | } |
| 1584 | } |
| 1585 | if (!didSwap) { |
| 1586 | break; |
| 1587 | } |
| 1588 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1589 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1590 | for (int k = 0; k < count - 1; k++) { |
| 1591 | SkASSERT(!(array[k+1] < array[k])); |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | #include "SkRandom.h" |
| 1596 | |
| 1597 | static void rand_rect(SkIRect* rect, SkRandom& rand) { |
| 1598 | int bits = 8; |
| 1599 | int shift = 32 - bits; |
| 1600 | rect->set(rand.nextU() >> shift, rand.nextU() >> shift, |
| 1601 | rand.nextU() >> shift, rand.nextU() >> shift); |
| 1602 | rect->sort(); |
| 1603 | } |
| 1604 | |
| 1605 | static void dumpRect(const SkIRect& r) { |
| 1606 | SkDebugf(" { %d, %d, %d, %d },\n", |
| 1607 | r.fLeft, r.fTop, |
| 1608 | r.fRight, r.fBottom); |
| 1609 | } |
| 1610 | |
| 1611 | static void test_rects(const SkIRect rect[], int count) { |
| 1612 | SkRegion rgn0, rgn1; |
| 1613 | |
| 1614 | for (int i = 0; i < count; i++) { |
| 1615 | rgn0.op(rect[i], SkRegion::kUnion_Op); |
| 1616 | // dumpRect(rect[i]); |
| 1617 | } |
| 1618 | rgn1.setRects(rect, count); |
| 1619 | |
| 1620 | if (rgn0 != rgn1) { |
| 1621 | SkDebugf("\n"); |
| 1622 | for (int i = 0; i < count; i++) { |
| 1623 | dumpRect(rect[i]); |
| 1624 | } |
| 1625 | SkDebugf("\n"); |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | static void test() { |
| 1630 | size_t i; |
| 1631 | |
| 1632 | const SkIRect r0[] = { |
| 1633 | { 0, 0, 1, 1 }, |
| 1634 | { 2, 2, 3, 3 }, |
| 1635 | }; |
| 1636 | const SkIRect r1[] = { |
| 1637 | { 0, 0, 1, 3 }, |
| 1638 | { 1, 1, 2, 2 }, |
| 1639 | { 2, 0, 3, 3 }, |
| 1640 | }; |
| 1641 | const SkIRect r2[] = { |
| 1642 | { 0, 0, 1, 2 }, |
| 1643 | { 2, 1, 3, 3 }, |
| 1644 | { 4, 0, 5, 1 }, |
| 1645 | { 6, 0, 7, 4 }, |
| 1646 | }; |
| 1647 | |
| 1648 | static const struct { |
| 1649 | const SkIRect* fRects; |
| 1650 | int fCount; |
| 1651 | } gRecs[] = { |
| 1652 | { r0, SK_ARRAY_COUNT(r0) }, |
| 1653 | { r1, SK_ARRAY_COUNT(r1) }, |
| 1654 | { r2, SK_ARRAY_COUNT(r2) }, |
| 1655 | }; |
| 1656 | |
| 1657 | for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) { |
| 1658 | test_rects(gRecs[i].fRects, gRecs[i].fCount); |
| 1659 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1660 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1661 | SkRandom rand; |
| 1662 | for (i = 0; i < 10000; i++) { |
| 1663 | SkRegion rgn0, rgn1; |
| 1664 | |
| 1665 | const int N = 8; |
| 1666 | SkIRect rect[N]; |
| 1667 | for (int j = 0; j < N; j++) { |
| 1668 | rand_rect(&rect[j], rand); |
| 1669 | } |
| 1670 | test_rects(rect, N); |
| 1671 | } |
| 1672 | } |
| 1673 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1674 | SkOSWindow* create_sk_window(void* hwnd) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1675 | // test(); |
bsalomon@google.com | 2fbc7fa | 2011-01-05 16:34:41 +0000 | [diff] [blame] | 1676 | return new SampleWindow(hwnd); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | void get_preferred_size(int* x, int* y, int* width, int* height) { |
| 1680 | *x = 10; |
| 1681 | *y = 50; |
| 1682 | *width = 640; |
| 1683 | *height = 480; |
| 1684 | } |
| 1685 | |
| 1686 | void application_init() { |
| 1687 | // setenv("ANDROID_ROOT", "../../../data", 0); |
reed@android.com | e191b16 | 2009-12-18 21:33:39 +0000 | [diff] [blame] | 1688 | #ifdef SK_BUILD_FOR_MAC |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1689 | setenv("ANDROID_ROOT", "/android/device/data", 0); |
reed@android.com | e191b16 | 2009-12-18 21:33:39 +0000 | [diff] [blame] | 1690 | #endif |
bsalomon@google.com | 2fbc7fa | 2011-01-05 16:34:41 +0000 | [diff] [blame] | 1691 | SkGraphics::Init(); |
| 1692 | SkEvent::Init(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | void application_term() { |
bsalomon@google.com | 2fbc7fa | 2011-01-05 16:34:41 +0000 | [diff] [blame] | 1696 | SkEvent::Term(); |
| 1697 | SkGraphics::Term(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1698 | } |