Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: samplecode/SampleApp.cpp

Issue 894083003: add SkAnimTimer, SPACE = pause/resume, ESP = stop (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SampleApp.h" 8 #include "SampleApp.h"
9 9
10 #include "OverView.h" 10 #include "OverView.h"
11 #include "Resources.h" 11 #include "Resources.h"
12 #include "SampleCode.h" 12 #include "SampleCode.h"
13 #include "SamplePipeControllers.h" 13 #include "SamplePipeControllers.h"
14 #include "SkAnimTimer.h"
14 #include "SkCanvas.h" 15 #include "SkCanvas.h"
15 #include "SkCommandLineFlags.h" 16 #include "SkCommandLineFlags.h"
16 #include "SkData.h" 17 #include "SkData.h"
17 #include "SkDevice.h" 18 #include "SkDevice.h"
18 #include "SkDocument.h" 19 #include "SkDocument.h"
19 #include "SkGPipe.h" 20 #include "SkGPipe.h"
20 #include "SkGraphics.h" 21 #include "SkGraphics.h"
21 #include "SkImageEncoder.h" 22 #include "SkImageEncoder.h"
22 #include "SkOSFile.h" 23 #include "SkOSFile.h"
23 #include "SkPaint.h" 24 #include "SkPaint.h"
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 #define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER) 96 #define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER)
96 97
97 static SampleWindow* gSampleWindow; 98 static SampleWindow* gSampleWindow;
98 99
99 static bool gShowGMBounds; 100 static bool gShowGMBounds;
100 101
101 static void post_event_to_sink(SkEvent* evt, SkEventSink* sink) { 102 static void post_event_to_sink(SkEvent* evt, SkEventSink* sink) {
102 evt->setTargetID(sink->getSinkID())->post(); 103 evt->setTargetID(sink->getSinkID())->post();
103 } 104 }
104 105
106 static SkAnimTimer gAnimTimer;
107
105 /////////////////////////////////////////////////////////////////////////////// 108 ///////////////////////////////////////////////////////////////////////////////
106 109
107 static const char* skip_until(const char* str, const char* skip) { 110 static const char* skip_until(const char* str, const char* skip) {
108 if (!str) { 111 if (!str) {
109 return NULL; 112 return NULL;
110 } 113 }
111 return strstr(str, skip); 114 return strstr(str, skip);
112 } 115 }
113 116
114 static const char* skip_past(const char* str, const char* skip) { 117 static const char* skip_past(const char* str, const char* skip) {
(...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after
570 size[1] = height; 573 size[1] = height;
571 evt->setScalars(gPrefSizeEvtName, 2, size); 574 evt->setScalars(gPrefSizeEvtName, 2, size);
572 } 575 }
573 576
574 bool SampleCode::FastTextQ(const SkEvent& evt) { 577 bool SampleCode::FastTextQ(const SkEvent& evt) {
575 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1); 578 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
576 } 579 }
577 580
578 /////////////////////////////////////////////////////////////////////////////// 581 ///////////////////////////////////////////////////////////////////////////////
579 582
580 static SkMSec gAnimTime;
581 static SkMSec gAnimTimePrev;
582
583 SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
584 SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
585 SkScalar SampleCode::GetAnimSecondsDelta() {
586 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
587 }
588
589 SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
590 // since gAnimTime can be up to 32 bits, we can't convert it to a float
591 // or we'll lose the low bits. Hence we use doubles for the intermediate
592 // calculations
593 double seconds = (double)gAnimTime / 1000.0;
594 double value = SkScalarToDouble(speed) * seconds;
595 if (period) {
596 value = ::fmod(value, SkScalarToDouble(period));
597 }
598 return SkDoubleToScalar(value);
599 }
600
601 SkScalar SampleCode::GetAnimSinScalar(SkScalar amplitude,
602 SkScalar periodInSec,
603 SkScalar phaseInSec) {
604 if (!periodInSec) {
605 return 0;
606 }
607 double t = (double)gAnimTime / 1000.0 + phaseInSec;
608 t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec;
609 amplitude = SK_ScalarHalf * amplitude;
610 return SkScalarMul(amplitude, SkDoubleToScalar(sin(t))) + amplitude;
611 }
612
613 enum TilingMode { 583 enum TilingMode {
614 kNo_Tiling, 584 kNo_Tiling,
615 kAbs_128x128_Tiling, 585 kAbs_128x128_Tiling,
616 kAbs_256x256_Tiling, 586 kAbs_256x256_Tiling,
617 kRel_4x4_Tiling, 587 kRel_4x4_Tiling,
618 kRel_1x16_Tiling, 588 kRel_1x16_Tiling,
619 kRel_16x1_Tiling, 589 kRel_16x1_Tiling,
620 590
621 kLast_TilingMode_Enum 591 kLast_TilingMode_Enum
622 }; 592 };
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
658 title->set(evt.findString(gTitleEvtName)); 628 title->set(evt.findString(gTitleEvtName));
659 return true; 629 return true;
660 } 630 }
661 } 631 }
662 return false; 632 return false;
663 } 633 }
664 634
665 bool SampleWindow::sendAnimatePulse() { 635 bool SampleWindow::sendAnimatePulse() {
666 SkView* view = curr_view(this); 636 SkView* view = curr_view(this);
667 if (SampleView::IsSampleView(view)) { 637 if (SampleView::IsSampleView(view)) {
668 if (fDoAnimate) { 638 return ((SampleView*)view)->animate(gAnimTimer);
669 return ((SampleView*)view)->animatePulse(gAnimTime, gAnimTimePrev);
670 } else {
671 // 0 signals the view that we are no longer animating
672 ((SampleView*)view)->animatePulse(0, 0);
673 }
674 } 639 }
675 return false; 640 return false;
676 } 641 }
677 642
678 void SampleWindow::setZoomCenter(float x, float y) 643 void SampleWindow::setZoomCenter(float x, float y)
679 { 644 {
680 fZoomCenterX = x; 645 fZoomCenterX = x;
681 fZoomCenterY = y; 646 fZoomCenterY = y;
682 } 647 }
683 648
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
829 794
830 fDeviceType = kRaster_DeviceType; 795 fDeviceType = kRaster_DeviceType;
831 796
832 #if DEFAULT_TO_GPU 797 #if DEFAULT_TO_GPU
833 fDeviceType = kGPU_DeviceType; 798 fDeviceType = kGPU_DeviceType;
834 #endif 799 #endif
835 #if SK_ANGLE && DEFAULT_TO_ANGLE 800 #if SK_ANGLE && DEFAULT_TO_ANGLE
836 fDeviceType = kANGLE_DeviceType; 801 fDeviceType = kANGLE_DeviceType;
837 #endif 802 #endif
838 803
839 fDoAnimate = true;
840 fUseClip = false; 804 fUseClip = false;
841 fNClip = false; 805 fNClip = false;
842 fAnimating = false; 806 fAnimating = false;
843 fRotate = false; 807 fRotate = false;
844 fRotateAnimTime = 0;
845 fPerspAnim = false; 808 fPerspAnim = false;
846 fPerspAnimTime = 0;
847 fRequestGrabImage = false; 809 fRequestGrabImage = false;
848 fPipeState = SkOSMenu::kOffState; 810 fPipeState = SkOSMenu::kOffState;
849 fTilingMode = kNo_Tiling; 811 fTilingMode = kNo_Tiling;
850 fMeasureFPS = false; 812 fMeasureFPS = false;
851 fLCDState = SkOSMenu::kMixedState; 813 fLCDState = SkOSMenu::kMixedState;
852 fAAState = SkOSMenu::kMixedState; 814 fAAState = SkOSMenu::kMixedState;
853 fSubpixelState = SkOSMenu::kMixedState; 815 fSubpixelState = SkOSMenu::kMixedState;
854 fHintingState = 0; 816 fHintingState = 0;
855 fFilterLevelIndex = 0; 817 fFilterLevelIndex = 0;
856 fFlipAxis = 0; 818 fFlipAxis = 0;
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
967 // onSizeChange gets called. 929 // onSizeChange gets called.
968 if (this->height() && this->width()) { 930 if (this->height() && this->width()) {
969 this->onSizeChange(); 931 this->onSizeChange();
970 } 932 }
971 933
972 // can't call this synchronously, since it may require a subclass to 934 // can't call this synchronously, since it may require a subclass to
973 // to implement, or the caller may need us to have returned from the 935 // to implement, or the caller may need us to have returned from the
974 // constructor first. Hence we post an event to ourselves. 936 // constructor first. Hence we post an event to ourselves.
975 // this->updateTitle(); 937 // this->updateTitle();
976 post_event_to_sink(new SkEvent(gUpdateWindowTitleEvtName), this); 938 post_event_to_sink(new SkEvent(gUpdateWindowTitleEvtName), this);
939
940 gAnimTimer.run();
977 } 941 }
978 942
979 SampleWindow::~SampleWindow() { 943 SampleWindow::~SampleWindow() {
980 SkSafeUnref(fTypeface); 944 SkSafeUnref(fTypeface);
981 SkSafeUnref(fDevManager); 945 SkSafeUnref(fDevManager);
982 } 946 }
983 947
984 948
985 int SampleWindow::findByTitle(const char title[]) { 949 int SampleWindow::findByTitle(const char title[]) {
986 int i, count = fSamples.count(); 950 int i, count = fSamples.count();
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
1043 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint ); 1007 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint );
1044 } 1008 }
1045 paint.setColor(desiredColor); 1009 paint.setColor(desiredColor);
1046 canvas->drawText(c_str, size, left, top, paint); 1010 canvas->drawText(c_str, size, left, top, paint);
1047 } 1011 }
1048 1012
1049 #define XCLIP_N 8 1013 #define XCLIP_N 8
1050 #define YCLIP_N 8 1014 #define YCLIP_N 8
1051 1015
1052 void SampleWindow::draw(SkCanvas* canvas) { 1016 void SampleWindow::draw(SkCanvas* canvas) {
1053 // update the animation time 1017 gAnimTimer.updateTime();
1054 if (!gAnimTimePrev && !gAnimTime) {
1055 // first time make delta be 0
1056 gAnimTime = SkTime::GetMSecs();
1057 gAnimTimePrev = gAnimTime;
1058 } else {
1059 gAnimTimePrev = gAnimTime;
1060 gAnimTime = SkTime::GetMSecs();
1061 }
1062 1018
1063 if (fGesture.isActive()) { 1019 if (fGesture.isActive()) {
1064 this->updateMatrix(); 1020 this->updateMatrix();
1065 } 1021 }
1066 1022
1067 if (fMeasureFPS) { 1023 if (fMeasureFPS) {
1068 fMeasureFPS_Time = 0; 1024 fMeasureFPS_Time = 0;
1069 } 1025 }
1070 1026
1071 if (fNClip) { 1027 if (fNClip) {
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
1401 } 1357 }
1402 1358
1403 // Do this after presentGL and other finishing, rather than in afterChild 1359 // Do this after presentGL and other finishing, rather than in afterChild
1404 if (fMeasureFPS && fMeasureFPS_StartTime) { 1360 if (fMeasureFPS && fMeasureFPS_StartTime) {
1405 fMeasureFPS_Time += SkTime::GetMSecs() - fMeasureFPS_StartTime; 1361 fMeasureFPS_Time += SkTime::GetMSecs() - fMeasureFPS_StartTime;
1406 } 1362 }
1407 } 1363 }
1408 1364
1409 void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) { 1365 void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
1410 if (fRotate) { 1366 if (fRotate) {
1411 fRotateAnimTime += SampleCode::GetAnimSecondsDelta();
1412
1413 SkScalar cx = this->width() / 2; 1367 SkScalar cx = this->width() / 2;
1414 SkScalar cy = this->height() / 2; 1368 SkScalar cy = this->height() / 2;
1415 canvas->translate(cx, cy); 1369 canvas->translate(cx, cy);
1416 canvas->rotate(fRotateAnimTime * 10); 1370 canvas->rotate(gAnimTimer.scaled(10));
1417 canvas->translate(-cx, -cy); 1371 canvas->translate(-cx, -cy);
1418 } 1372 }
1419 1373
1420 if (fPerspAnim) { 1374 if (fPerspAnim) {
1421 fPerspAnimTime += SampleCode::GetAnimSecondsDelta(); 1375 SkScalar secs = gAnimTimer.scaled(1);
1422 1376
1423 static const SkScalar gAnimPeriod = 10 * SK_Scalar1; 1377 static const SkScalar gAnimPeriod = 10 * SK_Scalar1;
1424 static const SkScalar gAnimMag = SK_Scalar1 / 1000; 1378 static const SkScalar gAnimMag = SK_Scalar1 / 1000;
1425 SkScalar t = SkScalarMod(fPerspAnimTime, gAnimPeriod); 1379 SkScalar t = SkScalarMod(secs, gAnimPeriod);
1426 if (SkScalarFloorToInt(SkScalarDiv(fPerspAnimTime, gAnimPeriod)) & 0x1) { 1380 if (SkScalarFloorToInt(SkScalarDiv(secs, gAnimPeriod)) & 0x1) {
1427 t = gAnimPeriod - t; 1381 t = gAnimPeriod - t;
1428 } 1382 }
1429 t = 2 * t - gAnimPeriod; 1383 t = 2 * t - gAnimPeriod;
1430 t = SkScalarMul(SkScalarDiv(t, gAnimPeriod), gAnimMag); 1384 t = SkScalarMul(SkScalarDiv(t, gAnimPeriod), gAnimMag);
1431 SkMatrix m; 1385 SkMatrix m;
1432 m.reset(); 1386 m.reset();
1433 #if 1 1387 #if 1
1434 m.setPerspY(t); 1388 m.setPerspY(t);
1435 #else 1389 #else
1436 m.setPerspY(SK_Scalar1 / 1000); 1390 m.setPerspY(SK_Scalar1 / 1000);
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
1662 SkEvent evt(gFastTextEvtName); 1616 SkEvent evt(gFastTextEvtName);
1663 return curr_view(this)->doQuery(&evt); 1617 return curr_view(this)->doQuery(&evt);
1664 } 1618 }
1665 if (query->isType("ignore-window-bitmap")) { 1619 if (query->isType("ignore-window-bitmap")) {
1666 query->setFast32(this->getGrContext() != NULL); 1620 query->setFast32(this->getGrContext() != NULL);
1667 return true; 1621 return true;
1668 } 1622 }
1669 return this->INHERITED::onQuery(query); 1623 return this->INHERITED::onQuery(query);
1670 } 1624 }
1671 1625
1672 #if 0 // UNUSED
1673 static void cleanup_for_filename(SkString* name) {
1674 char* str = name->writable_str();
1675 for (size_t i = 0; i < name->size(); i++) {
1676 switch (str[i]) {
1677 case ':': str[i] = '-'; break;
1678 case '/': str[i] = '-'; break;
1679 case ' ': str[i] = '_'; break;
1680 default: break;
1681 }
1682 }
1683 }
1684 #endif
1685
1686 DECLARE_bool(portableFonts); 1626 DECLARE_bool(portableFonts);
1687 1627
1688 bool SampleWindow::onHandleChar(SkUnichar uni) { 1628 bool SampleWindow::onHandleChar(SkUnichar uni) {
1689 { 1629 {
1690 SkView* view = curr_view(this); 1630 SkView* view = curr_view(this);
1691 if (view) { 1631 if (view) {
1692 SkEvent evt(gCharEvtName); 1632 SkEvent evt(gCharEvtName);
1693 evt.setFast32(uni); 1633 evt.setFast32(uni);
1694 if (view->doQuery(&evt)) { 1634 if (view->doQuery(&evt)) {
1695 return true; 1635 return true;
(...skipping 24 matching lines...) Expand all
1720 fScrollTestX = fScrollTestY = 0; 1660 fScrollTestX = fScrollTestY = 0;
1721 } else { 1661 } else {
1722 fScrollTestX += dx; 1662 fScrollTestX += dx;
1723 fScrollTestY += dy; 1663 fScrollTestY += dy;
1724 } 1664 }
1725 this->inval(NULL); 1665 this->inval(NULL);
1726 return true; 1666 return true;
1727 } 1667 }
1728 1668
1729 switch (uni) { 1669 switch (uni) {
1730 case ' ': 1670 case 27: // ESC
1731 fDoAnimate = !fDoAnimate; 1671 gAnimTimer.stop();
1732 if (this->sendAnimatePulse()) { 1672 if (this->sendAnimatePulse()) {
1733 this->inval(NULL); 1673 this->inval(NULL);
1734 } 1674 }
1675 break;
1676 case ' ':
1677 gAnimTimer.togglePauseResume();
1678 if (this->sendAnimatePulse()) {
1679 this->inval(NULL);
1680 }
1735 break; 1681 break;
1736 case 'B': 1682 case 'B':
1737 post_event_to_sink(SkNEW_ARGS(SkEvent, ("PictFileView::toggleBBox")) , curr_view(this)); 1683 post_event_to_sink(SkNEW_ARGS(SkEvent, ("PictFileView::toggleBBox")) , curr_view(this));
1738 // Cannot call updateTitle() synchronously, because the toggleBBox e vent is still in 1684 // Cannot call updateTitle() synchronously, because the toggleBBox e vent is still in
1739 // the queue. 1685 // the queue.
1740 post_event_to_sink(SkNEW_ARGS(SkEvent, (gUpdateWindowTitleEvtName)), this); 1686 post_event_to_sink(SkNEW_ARGS(SkEvent, (gUpdateWindowTitleEvtName)), this);
1741 this->inval(NULL); 1687 this->inval(NULL);
1742 break; 1688 break;
1743 case 'D': 1689 case 'D':
1744 toggleDistanceFieldFonts(); 1690 toggleDistanceFieldFonts();
(...skipping 17 matching lines...) Expand all
1762 this->inval(NULL); 1708 this->inval(NULL);
1763 break; 1709 break;
1764 case 'i': 1710 case 'i':
1765 this->zoomIn(); 1711 this->zoomIn();
1766 break; 1712 break;
1767 case 'o': 1713 case 'o':
1768 this->zoomOut(); 1714 this->zoomOut();
1769 break; 1715 break;
1770 case 'r': 1716 case 'r':
1771 fRotate = !fRotate; 1717 fRotate = !fRotate;
1772 fRotateAnimTime = 0;
1773 this->inval(NULL); 1718 this->inval(NULL);
1774 this->updateTitle(); 1719 this->updateTitle();
1775 return true; 1720 return true;
1776 case 'k': 1721 case 'k':
1777 fPerspAnim = !fPerspAnim; 1722 fPerspAnim = !fPerspAnim;
1778 this->inval(NULL); 1723 this->inval(NULL);
1779 this->updateTitle(); 1724 this->updateTitle();
1780 return true; 1725 return true;
1781 #if SK_SUPPORT_GPU 1726 #if SK_SUPPORT_GPU
1782 case '\\': 1727 case '\\':
(...skipping 661 matching lines...) Expand 10 before | Expand all | Expand 10 after
2444 setenv("ANDROID_ROOT", "/android/device/data", 0); 2389 setenv("ANDROID_ROOT", "/android/device/data", 0);
2445 #endif 2390 #endif
2446 SkGraphics::Init(); 2391 SkGraphics::Init();
2447 SkEvent::Init(); 2392 SkEvent::Init();
2448 } 2393 }
2449 2394
2450 void application_term() { 2395 void application_term() {
2451 SkEvent::Term(); 2396 SkEvent::Term();
2452 SkGraphics::Term(); 2397 SkGraphics::Term();
2453 } 2398 }
OLDNEW
« gm/SkAnimTimer.h ('K') | « samplecode/SampleApp.h ('k') | samplecode/SampleArc.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698