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