2008-12-17 15:59:43 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkDevice.h"
|
2010-12-22 21:39:39 +00:00
|
|
|
#include "SkGpuCanvas.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkGraphics.h"
|
2009-01-06 20:16:26 +00:00
|
|
|
#include "SkImageEncoder.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkPicture.h"
|
|
|
|
#include "SkStream.h"
|
2009-11-23 21:07:51 +00:00
|
|
|
#include "SkTime.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkWindow.h"
|
|
|
|
|
|
|
|
#include "SampleCode.h"
|
2010-12-22 21:39:39 +00:00
|
|
|
#include "GrContext.h"
|
2011-03-16 12:10:02 +00:00
|
|
|
#include "SkTouchGesture.h"
|
2010-12-20 18:26:13 +00:00
|
|
|
|
2011-03-21 00:53:39 +00:00
|
|
|
#define USE_ARROWS_FOR_ZOOM true
|
2010-12-20 18:26:13 +00:00
|
|
|
//#define DEFAULT_TO_GPU
|
|
|
|
|
2009-12-18 21:33:39 +00:00
|
|
|
extern SkView* create_overview(int, const SkViewFactory[]);
|
2009-11-03 04:00:48 +00:00
|
|
|
|
2010-07-22 18:27:53 +00:00
|
|
|
#define SK_SUPPORT_GL
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
#define ANIMATING_EVENTTYPE "nextSample"
|
|
|
|
#define ANIMATING_DELAY 750
|
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
#ifdef SK_SUPPORT_GL
|
|
|
|
#include "GrGLConfig.h"
|
|
|
|
#endif
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkViewRegister* SkViewRegister::gHead;
|
|
|
|
SkViewRegister::SkViewRegister(SkViewFactory fact) : fFact(fact) {
|
|
|
|
static bool gOnce;
|
|
|
|
if (!gOnce) {
|
|
|
|
gHead = NULL;
|
|
|
|
gOnce = true;
|
|
|
|
}
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
fChain = gHead;
|
|
|
|
gHead = this;
|
|
|
|
}
|
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
#if defined(SK_SUPPORT_GL)
|
|
|
|
#define SK_USE_SHADERS
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|
|
|
|
|
2011-03-14 16:58:39 +00:00
|
|
|
#ifdef SK_BUILD_FOR_MAC
|
2011-03-11 20:08:25 +00:00
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
#include <CoreFoundation/CFURLAccess.h>
|
|
|
|
|
|
|
|
static void testpdf() {
|
|
|
|
CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf",
|
|
|
|
kCFStringEncodingUTF8);
|
|
|
|
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path,
|
|
|
|
kCFURLPOSIXPathStyle,
|
|
|
|
false);
|
|
|
|
CFRelease(path);
|
|
|
|
CGRect box = CGRectMake(0, 0, 8*72, 10*72);
|
|
|
|
CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL);
|
|
|
|
CFRelease(url);
|
|
|
|
|
|
|
|
CGContextBeginPage(cg, &box);
|
|
|
|
CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5);
|
|
|
|
CGContextFillEllipseInRect(cg, r);
|
|
|
|
CGContextEndPage(cg);
|
|
|
|
CGContextRelease(cg);
|
|
|
|
|
|
|
|
if (false) {
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.setConfig(SkBitmap::kA8_Config, 64, 64);
|
|
|
|
bm.allocPixels();
|
|
|
|
bm.eraseColor(0);
|
|
|
|
|
|
|
|
SkCanvas canvas(bm);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "SkDrawFilter.h"
|
|
|
|
|
|
|
|
class LCDTextDrawFilter : public SkDrawFilter {
|
|
|
|
public:
|
|
|
|
enum Mode {
|
|
|
|
kNeutral_Mode,
|
|
|
|
kForceOn_Mode,
|
|
|
|
kForceOff_Mode
|
|
|
|
};
|
|
|
|
|
|
|
|
LCDTextDrawFilter(Mode mode) : fMode(mode) {}
|
|
|
|
|
|
|
|
virtual bool filter(SkCanvas*, SkPaint* paint, Type t) {
|
|
|
|
if (kText_Type == t && kNeutral_Mode != fMode) {
|
|
|
|
fPrevLCD = paint->isLCDRenderText();
|
|
|
|
paint->setLCDRenderText(kForceOn_Mode == fMode);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** If filter() returned true, then restore() will be called to restore the
|
|
|
|
canvas/paint to their previous states
|
|
|
|
*/
|
|
|
|
virtual void restore(SkCanvas*, SkPaint* paint, Type t) {
|
|
|
|
if (kText_Type == t && kNeutral_Mode != fMode) {
|
|
|
|
paint->setLCDRenderText(fPrevLCD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Mode fMode;
|
|
|
|
bool fPrevLCD;
|
|
|
|
};
|
|
|
|
|
|
|
|
LCDTextDrawFilter::Mode cycle_lcdmode(LCDTextDrawFilter::Mode mode) {
|
|
|
|
static const LCDTextDrawFilter::Mode gCycle[] = {
|
|
|
|
/* kNeutral_Mode -> */ LCDTextDrawFilter::kForceOn_Mode,
|
|
|
|
/* kForceOn_Mode -> */ LCDTextDrawFilter::kForceOff_Mode,
|
|
|
|
/* kForceOff_Mode -> */ LCDTextDrawFilter::kNeutral_Mode
|
|
|
|
};
|
|
|
|
return gCycle[mode];
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-03-21 00:53:39 +00:00
|
|
|
#define MAX_ZOOM_LEVEL 8
|
|
|
|
#define MIN_ZOOM_LEVEL -8
|
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
static const char gCharEvtName[] = "SampleCode_Char_Event";
|
|
|
|
static const char gKeyEvtName[] = "SampleCode_Key_Event";
|
2008-12-17 15:59:43 +00:00
|
|
|
static const char gTitleEvtName[] = "SampleCode_Title_Event";
|
|
|
|
static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
|
2010-12-20 18:26:13 +00:00
|
|
|
static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
|
|
|
|
|
|
|
|
bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
|
|
|
|
if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
|
|
|
|
if (outUni) {
|
|
|
|
*outUni = evt.getFast32();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
|
|
|
|
if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
|
|
|
|
if (outKey) {
|
|
|
|
*outKey = (SkKey)evt.getFast32();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
bool SampleCode::TitleQ(const SkEvent& evt) {
|
|
|
|
return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SampleCode::TitleR(SkEvent* evt, const char title[]) {
|
|
|
|
SkASSERT(evt && TitleQ(*evt));
|
|
|
|
evt->setString(gTitleEvtName, title);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SampleCode::PrefSizeQ(const SkEvent& evt) {
|
|
|
|
return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
|
|
|
|
SkASSERT(evt && PrefSizeQ(*evt));
|
|
|
|
SkScalar size[2];
|
|
|
|
size[0] = width;
|
|
|
|
size[1] = height;
|
|
|
|
evt->setScalars(gPrefSizeEvtName, 2, size);
|
|
|
|
}
|
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
bool SampleCode::FastTextQ(const SkEvent& evt) {
|
|
|
|
return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2009-11-23 21:07:51 +00:00
|
|
|
static SkMSec gAnimTime;
|
2010-12-20 18:26:13 +00:00
|
|
|
static SkMSec gAnimTimePrev;
|
|
|
|
|
2009-11-23 21:07:51 +00:00
|
|
|
SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
|
2010-12-20 18:26:13 +00:00
|
|
|
SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
|
|
|
|
SkScalar SampleCode::GetAnimSecondsDelta() {
|
|
|
|
return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
|
|
|
|
}
|
2009-11-23 21:07:51 +00:00
|
|
|
|
|
|
|
SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
|
2010-12-20 18:26:13 +00:00
|
|
|
// since gAnimTime can be up to 32 bits, we can't convert it to a float
|
|
|
|
// or we'll lose the low bits. Hence we use doubles for the intermediate
|
|
|
|
// calculations
|
|
|
|
double seconds = (double)gAnimTime / 1000.0;
|
|
|
|
double value = SkScalarToDouble(speed) * seconds;
|
2009-11-23 21:07:51 +00:00
|
|
|
if (period) {
|
2010-12-20 18:26:13 +00:00
|
|
|
value = ::fmod(value, SkScalarToDouble(period));
|
2009-11-23 21:07:51 +00:00
|
|
|
}
|
2010-12-20 18:26:13 +00:00
|
|
|
return SkDoubleToScalar(value);
|
2009-11-23 21:07:51 +00:00
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
static SkView* curr_view(SkWindow* wind) {
|
|
|
|
SkView::F2BIter iter(wind);
|
|
|
|
return iter.next();
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
class SampleWindow : public SkOSWindow {
|
2009-11-03 04:00:48 +00:00
|
|
|
SkTDArray<SkViewFactory> fSamples;
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2011-01-05 16:34:41 +00:00
|
|
|
SampleWindow(void* hwnd);
|
|
|
|
virtual ~SampleWindow();
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2009-11-23 20:10:41 +00:00
|
|
|
virtual void draw(SkCanvas* canvas);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
protected:
|
|
|
|
virtual void onDraw(SkCanvas* canvas);
|
2011-01-05 16:34:41 +00:00
|
|
|
virtual bool onHandleKey(SkKey key);
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual bool onHandleChar(SkUnichar);
|
|
|
|
virtual void onSizeChange();
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual SkCanvas* beforeChildren(SkCanvas*);
|
|
|
|
virtual void afterChildren(SkCanvas*);
|
2009-08-14 16:08:38 +00:00
|
|
|
virtual void beforeChild(SkView* child, SkCanvas* canvas);
|
|
|
|
virtual void afterChild(SkView* child, SkCanvas* canvas);
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2011-01-05 16:34:41 +00:00
|
|
|
virtual bool onEvent(const SkEvent& evt);
|
2010-12-20 18:26:13 +00:00
|
|
|
virtual bool onQuery(SkEvent* evt);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-03-21 00:53:39 +00:00
|
|
|
virtual bool onDispatchClick(int x, int y, Click::State);
|
2011-03-16 12:10:02 +00:00
|
|
|
virtual bool onClick(Click* click);
|
|
|
|
virtual Click* onFindClickHandler(SkScalar x, SkScalar y);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#if 0
|
2011-01-05 16:34:41 +00:00
|
|
|
virtual bool handleChar(SkUnichar uni);
|
|
|
|
virtual bool handleEvent(const SkEvent& evt);
|
|
|
|
virtual bool handleKey(SkKey key);
|
|
|
|
virtual bool handleKeyUp(SkKey key);
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual bool onHandleKeyUp(SkKey key);
|
|
|
|
#endif
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2009-11-03 04:00:48 +00:00
|
|
|
int fCurrIndex;
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPicture* fPicture;
|
2010-12-20 18:26:13 +00:00
|
|
|
SkGpuCanvas* fGpuCanvas;
|
2011-01-18 20:57:22 +00:00
|
|
|
GrContext* fGrContext;
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPath fClipPath;
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2011-03-16 12:10:02 +00:00
|
|
|
SkTouchGesture fGesture;
|
2011-03-21 00:53:39 +00:00
|
|
|
int fZoomLevel;
|
|
|
|
SkScalar fZoomScale;
|
2011-03-16 12:10:02 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
enum CanvasType {
|
|
|
|
kRaster_CanvasType,
|
|
|
|
kPicture_CanvasType,
|
2010-12-20 18:26:13 +00:00
|
|
|
kGPU_CanvasType
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
CanvasType fCanvasType;
|
|
|
|
|
|
|
|
bool fUseClip;
|
2009-11-23 20:10:41 +00:00
|
|
|
bool fNClip;
|
2008-12-17 15:59:43 +00:00
|
|
|
bool fRepeatDrawing;
|
|
|
|
bool fAnimating;
|
2009-08-14 16:08:38 +00:00
|
|
|
bool fRotate;
|
|
|
|
bool fScale;
|
2010-12-20 18:26:13 +00:00
|
|
|
bool fRequestGrabImage;
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2011-03-11 20:08:25 +00:00
|
|
|
LCDTextDrawFilter::Mode fLCDMode;
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
int fScrollTestX, fScrollTestY;
|
2011-01-18 20:57:22 +00:00
|
|
|
|
|
|
|
bool make3DReady();
|
2011-03-21 00:53:39 +00:00
|
|
|
void changeZoomLevel(int delta);
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void loadView(SkView*);
|
|
|
|
void updateTitle();
|
|
|
|
bool nextSample();
|
|
|
|
|
|
|
|
void postAnimatingEvent() {
|
|
|
|
if (fAnimating) {
|
|
|
|
SkEvent* evt = new SkEvent(ANIMATING_EVENTTYPE);
|
|
|
|
evt->post(this->getSinkID(), ANIMATING_DELAY);
|
|
|
|
}
|
|
|
|
}
|
2011-01-18 20:57:22 +00:00
|
|
|
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
static CanvasType cycle_canvastype(CanvasType);
|
|
|
|
|
|
|
|
typedef SkOSWindow INHERITED;
|
|
|
|
};
|
|
|
|
|
2011-01-18 20:57:22 +00:00
|
|
|
bool SampleWindow::make3DReady() {
|
|
|
|
|
|
|
|
#if defined(SK_SUPPORT_GL)
|
2011-03-10 18:24:15 +00:00
|
|
|
if (attachGL()) {
|
|
|
|
if (NULL == fGrContext) {
|
|
|
|
#if defined(SK_USE_SHADERS)
|
|
|
|
fGrContext = GrContext::Create(GrGpu::kOpenGL_Shaders_Engine, NULL);
|
|
|
|
#else
|
|
|
|
fGrContext = GrContext::Create(GrGpu::kOpenGL_Fixed_Engine, NULL);
|
|
|
|
#endif
|
|
|
|
}
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2011-03-10 18:24:15 +00:00
|
|
|
if (NULL != fGrContext) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
detachGL();
|
|
|
|
}
|
2011-01-18 20:57:22 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
SkDebugf("Failed to setup 3D");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SampleWindow::CanvasType SampleWindow::cycle_canvastype(CanvasType ct) {
|
|
|
|
static const CanvasType gCT[] = {
|
|
|
|
kPicture_CanvasType,
|
2010-12-20 18:26:13 +00:00
|
|
|
kGPU_CanvasType,
|
2008-12-17 15:59:43 +00:00
|
|
|
kRaster_CanvasType
|
|
|
|
};
|
|
|
|
return gCT[ct];
|
|
|
|
}
|
|
|
|
|
|
|
|
SampleWindow::SampleWindow(void* hwnd) : INHERITED(hwnd) {
|
|
|
|
fPicture = NULL;
|
2010-12-20 18:26:13 +00:00
|
|
|
fGpuCanvas = NULL;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-01-18 20:57:22 +00:00
|
|
|
fGrContext = NULL;
|
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
#ifdef DEFAULT_TO_GPU
|
|
|
|
fCanvasType = kGPU_CanvasType;
|
|
|
|
#else
|
2008-12-17 15:59:43 +00:00
|
|
|
fCanvasType = kRaster_CanvasType;
|
2010-12-20 18:26:13 +00:00
|
|
|
#endif
|
2008-12-17 15:59:43 +00:00
|
|
|
fUseClip = false;
|
2009-11-23 20:10:41 +00:00
|
|
|
fNClip = false;
|
2008-12-17 15:59:43 +00:00
|
|
|
fRepeatDrawing = false;
|
|
|
|
fAnimating = false;
|
2009-08-14 16:08:38 +00:00
|
|
|
fRotate = false;
|
|
|
|
fScale = false;
|
2010-12-20 18:26:13 +00:00
|
|
|
fRequestGrabImage = false;
|
2011-03-11 20:08:25 +00:00
|
|
|
fLCDMode = LCDTextDrawFilter::kNeutral_Mode;
|
2008-12-17 15:59:43 +00:00
|
|
|
fScrollTestX = fScrollTestY = 0;
|
|
|
|
|
2011-03-21 00:53:39 +00:00
|
|
|
fZoomLevel = 0;
|
|
|
|
fZoomScale = SK_Scalar1;
|
|
|
|
|
2011-01-05 16:34:41 +00:00
|
|
|
// this->setConfig(SkBitmap::kRGB_565_Config);
|
|
|
|
this->setConfig(SkBitmap::kARGB_8888_Config);
|
|
|
|
this->setVisibleP(true);
|
2010-12-20 18:26:13 +00:00
|
|
|
this->setClipToBounds(false);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2009-11-03 04:00:48 +00:00
|
|
|
{
|
|
|
|
const SkViewRegister* reg = SkViewRegister::Head();
|
|
|
|
while (reg) {
|
|
|
|
*fSamples.append() = reg->factory();
|
|
|
|
reg = reg->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fCurrIndex = 0;
|
2009-11-04 19:40:25 +00:00
|
|
|
this->loadView(fSamples[fCurrIndex]());
|
2011-03-11 20:08:25 +00:00
|
|
|
|
2011-03-14 16:58:39 +00:00
|
|
|
#ifdef SK_BUILD_FOR_MAC
|
2011-03-11 20:08:25 +00:00
|
|
|
testpdf();
|
2011-03-14 16:58:39 +00:00
|
|
|
#endif
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SampleWindow::~SampleWindow() {
|
|
|
|
delete fPicture;
|
2010-12-20 18:26:13 +00:00
|
|
|
delete fGpuCanvas;
|
2011-01-18 20:57:22 +00:00
|
|
|
if (NULL != fGrContext) {
|
|
|
|
fGrContext->unref();
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 21:46:47 +00:00
|
|
|
static SkBitmap capture_bitmap(SkCanvas* canvas) {
|
|
|
|
SkBitmap bm;
|
|
|
|
const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
|
|
|
|
src.copyTo(&bm, src.config());
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
|
|
|
|
SkBitmap* diff) {
|
|
|
|
const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2009-11-23 21:46:47 +00:00
|
|
|
SkAutoLockPixels alp0(src);
|
|
|
|
SkAutoLockPixels alp1(orig);
|
|
|
|
for (int y = 0; y < src.height(); y++) {
|
|
|
|
const void* srcP = src.getAddr(0, y);
|
|
|
|
const void* origP = orig.getAddr(0, y);
|
|
|
|
size_t bytes = src.width() * src.bytesPerPixel();
|
|
|
|
if (memcmp(srcP, origP, bytes)) {
|
|
|
|
SkDebugf("---------- difference on line %d\n", y);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-23 21:07:51 +00:00
|
|
|
#define XCLIP_N 8
|
|
|
|
#define YCLIP_N 8
|
2009-11-23 20:10:41 +00:00
|
|
|
|
|
|
|
void SampleWindow::draw(SkCanvas* canvas) {
|
2009-11-23 21:07:51 +00:00
|
|
|
// update the animation time
|
2010-12-20 18:26:13 +00:00
|
|
|
gAnimTimePrev = gAnimTime;
|
2009-11-23 21:07:51 +00:00
|
|
|
gAnimTime = SkTime::GetMSecs();
|
|
|
|
|
2011-03-21 00:53:39 +00:00
|
|
|
if (fZoomLevel) {
|
|
|
|
SkMatrix m;
|
|
|
|
SkScalar cx = SkScalarHalf(this->width());
|
|
|
|
SkScalar cy = SkScalarHalf(this->height());
|
|
|
|
SkPoint center;
|
|
|
|
m = canvas->getTotalMatrix();//.invert(&m);
|
|
|
|
m.mapXY(cx, cy, ¢er);
|
|
|
|
cx = center.fX;
|
|
|
|
cy = center.fY;
|
|
|
|
|
|
|
|
m.setTranslate(-cx, -cy);
|
|
|
|
m.postScale(fZoomScale, fZoomScale);
|
|
|
|
m.postTranslate(cx, cy);
|
|
|
|
|
|
|
|
canvas->concat(m);
|
|
|
|
}
|
|
|
|
|
2011-03-16 12:10:02 +00:00
|
|
|
// Apply any gesture matrix
|
|
|
|
if (true) {
|
|
|
|
const SkMatrix& localM = fGesture.localM();
|
|
|
|
if (localM.getType() & SkMatrix::kScale_Mask) {
|
|
|
|
canvas->setExternalMatrix(&localM);
|
|
|
|
}
|
|
|
|
canvas->concat(localM);
|
|
|
|
canvas->concat(fGesture.globalM());
|
|
|
|
|
|
|
|
if (fGesture.isActive()) {
|
|
|
|
this->inval(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-23 20:10:41 +00:00
|
|
|
if (fNClip) {
|
2009-11-23 21:46:47 +00:00
|
|
|
this->INHERITED::draw(canvas);
|
|
|
|
SkBitmap orig = capture_bitmap(canvas);
|
2009-11-23 20:10:41 +00:00
|
|
|
|
|
|
|
const SkScalar w = this->width();
|
|
|
|
const SkScalar h = this->height();
|
|
|
|
const SkScalar cw = w / XCLIP_N;
|
|
|
|
const SkScalar ch = h / YCLIP_N;
|
|
|
|
for (int y = 0; y < YCLIP_N; y++) {
|
2009-11-23 21:46:47 +00:00
|
|
|
SkRect r;
|
|
|
|
r.fTop = y * ch;
|
|
|
|
r.fBottom = (y + 1) * ch;
|
|
|
|
if (y == YCLIP_N - 1) {
|
|
|
|
r.fBottom = h;
|
|
|
|
}
|
2009-11-23 20:10:41 +00:00
|
|
|
for (int x = 0; x < XCLIP_N; x++) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
2009-11-23 21:46:47 +00:00
|
|
|
r.fLeft = x * cw;
|
|
|
|
r.fRight = (x + 1) * cw;
|
|
|
|
if (x == XCLIP_N - 1) {
|
|
|
|
r.fRight = w;
|
|
|
|
}
|
2009-11-23 20:10:41 +00:00
|
|
|
canvas->clipRect(r);
|
|
|
|
this->INHERITED::draw(canvas);
|
|
|
|
}
|
|
|
|
}
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2009-11-23 21:46:47 +00:00
|
|
|
SkBitmap diff;
|
|
|
|
if (bitmap_diff(canvas, orig, &diff)) {
|
|
|
|
}
|
2009-11-23 20:10:41 +00:00
|
|
|
} else {
|
|
|
|
this->INHERITED::draw(canvas);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void SampleWindow::onDraw(SkCanvas* canvas) {
|
|
|
|
if (fRepeatDrawing) {
|
|
|
|
this->inval(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
|
|
|
|
static void reverseRedAndBlue(const SkBitmap& bm) {
|
|
|
|
SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
|
|
|
|
uint8_t* p = (uint8_t*)bm.getPixels();
|
|
|
|
uint8_t* stop = p + bm.getSize();
|
|
|
|
while (p < stop) {
|
|
|
|
// swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
|
|
|
|
unsigned scale = SkAlpha255To256(p[3]);
|
|
|
|
unsigned r = p[2];
|
|
|
|
unsigned b = p[0];
|
|
|
|
p[0] = SkAlphaMul(r, scale);
|
|
|
|
p[1] = SkAlphaMul(p[1], scale);
|
|
|
|
p[2] = SkAlphaMul(b, scale);
|
|
|
|
p += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
|
2010-12-20 18:26:13 +00:00
|
|
|
SkIPoint viewport;
|
|
|
|
bool alreadyGPU = canvas->getViewport(&viewport);
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
if (kGPU_CanvasType != fCanvasType) {
|
2008-12-19 18:24:35 +00:00
|
|
|
#ifdef SK_SUPPORT_GL
|
2010-12-20 18:26:13 +00:00
|
|
|
detachGL();
|
2011-01-18 20:57:22 +00:00
|
|
|
#endif
|
2010-12-20 18:26:13 +00:00
|
|
|
}
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
switch (fCanvasType) {
|
|
|
|
case kRaster_CanvasType:
|
|
|
|
canvas = this->INHERITED::beforeChildren(canvas);
|
|
|
|
break;
|
|
|
|
case kPicture_CanvasType:
|
|
|
|
fPicture = new SkPicture;
|
|
|
|
canvas = fPicture->beginRecording(9999, 9999);
|
|
|
|
break;
|
2010-12-22 21:39:39 +00:00
|
|
|
case kGPU_CanvasType: {
|
2011-01-18 20:57:22 +00:00
|
|
|
if (!alreadyGPU && make3DReady()) {
|
2010-12-20 18:26:13 +00:00
|
|
|
SkDevice* device = canvas->getDevice();
|
2011-01-18 20:57:22 +00:00
|
|
|
const SkBitmap& bitmap = device->accessBitmap(true);
|
|
|
|
|
2011-01-21 21:03:59 +00:00
|
|
|
GrRenderTarget* renderTarget;
|
|
|
|
renderTarget = fGrContext->createRenderTargetFrom3DApiState();
|
|
|
|
fGpuCanvas = new SkGpuCanvas(fGrContext, renderTarget);
|
|
|
|
renderTarget->unref();
|
|
|
|
|
2011-01-05 16:34:41 +00:00
|
|
|
device = fGpuCanvas->createDevice(SkBitmap::kARGB_8888_Config,
|
|
|
|
bitmap.width(), bitmap.height(),
|
|
|
|
false, false);
|
2010-12-22 21:39:39 +00:00
|
|
|
fGpuCanvas->setDevice(device)->unref();
|
2011-03-21 00:53:39 +00:00
|
|
|
|
|
|
|
fGpuCanvas->concat(canvas->getTotalMatrix());
|
2010-12-20 18:26:13 +00:00
|
|
|
canvas = fGpuCanvas;
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
} else {
|
|
|
|
canvas = this->INHERITED::beforeChildren(canvas);
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
break;
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fUseClip) {
|
|
|
|
canvas->drawColor(0xFFFF88FF);
|
|
|
|
canvas->clipPath(fClipPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return canvas;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
|
|
|
|
const SkRegion& rgn) {
|
|
|
|
SkCanvas canvas(bm);
|
|
|
|
SkRegion inval(rgn);
|
|
|
|
|
|
|
|
inval.translate(r.fLeft, r.fTop);
|
|
|
|
canvas.clipRegion(inval);
|
|
|
|
canvas.drawColor(0xFFFF8080);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SampleWindow::afterChildren(SkCanvas* orig) {
|
2010-12-20 18:26:13 +00:00
|
|
|
if (fRequestGrabImage) {
|
|
|
|
fRequestGrabImage = false;
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
SkCanvas* canvas = fGpuCanvas ? fGpuCanvas : orig;
|
|
|
|
SkDevice* device = canvas->getDevice();
|
|
|
|
SkBitmap bitmap;
|
2011-03-10 19:40:34 +00:00
|
|
|
SkIRect bounds = {
|
|
|
|
0, 0,
|
|
|
|
SkScalarRound(this->width()),
|
|
|
|
SkScalarRound(this->height())
|
|
|
|
};
|
2010-12-20 18:26:13 +00:00
|
|
|
if (device->readPixels(bounds, &bitmap)) {
|
|
|
|
static int gSampleGrabCounter;
|
|
|
|
SkString name;
|
|
|
|
name.printf("sample_grab_%d", gSampleGrabCounter++);
|
|
|
|
SkImageEncoder::EncodeFile(name.c_str(), bitmap,
|
|
|
|
SkImageEncoder::kPNG_Type, 100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
switch (fCanvasType) {
|
|
|
|
case kRaster_CanvasType:
|
|
|
|
break;
|
|
|
|
case kPicture_CanvasType:
|
2009-03-30 21:02:14 +00:00
|
|
|
if (true) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPicture* pict = new SkPicture(*fPicture);
|
|
|
|
fPicture->unref();
|
|
|
|
orig->drawPicture(*pict);
|
|
|
|
pict->unref();
|
2009-03-30 21:02:14 +00:00
|
|
|
} else if (true) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkDynamicMemoryWStream ostream;
|
|
|
|
fPicture->serialize(&ostream);
|
|
|
|
fPicture->unref();
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkMemoryStream istream(ostream.getStream(), ostream.getOffset());
|
|
|
|
SkPicture pict(&istream);
|
|
|
|
orig->drawPicture(pict);
|
|
|
|
} else {
|
|
|
|
fPicture->draw(orig);
|
|
|
|
fPicture->unref();
|
|
|
|
}
|
|
|
|
fPicture = NULL;
|
|
|
|
break;
|
2008-12-19 18:24:35 +00:00
|
|
|
#ifdef SK_SUPPORT_GL
|
2010-12-20 18:26:13 +00:00
|
|
|
case kGPU_CanvasType:
|
|
|
|
delete fGpuCanvas;
|
|
|
|
fGpuCanvas = NULL;
|
|
|
|
presentGL();
|
|
|
|
break;
|
2008-12-19 18:24:35 +00:00
|
|
|
#endif
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// if ((fScrollTestX | fScrollTestY) != 0)
|
2010-12-20 18:26:13 +00:00
|
|
|
if (false) {
|
2008-12-17 15:59:43 +00:00
|
|
|
const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
|
|
|
|
int dx = fScrollTestX * 7;
|
|
|
|
int dy = fScrollTestY * 7;
|
|
|
|
SkIRect r;
|
|
|
|
SkRegion inval;
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
r.set(50, 50, 50+100, 50+100);
|
|
|
|
bm.scrollRect(&r, dx, dy, &inval);
|
|
|
|
paint_rgn(bm, r, inval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-14 16:08:38 +00:00
|
|
|
void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
|
|
|
|
if (fScale) {
|
|
|
|
SkScalar scale = SK_Scalar1 * 7 / 10;
|
|
|
|
SkScalar cx = this->width() / 2;
|
|
|
|
SkScalar cy = this->height() / 2;
|
|
|
|
canvas->translate(cx, cy);
|
|
|
|
canvas->scale(scale, scale);
|
|
|
|
canvas->translate(-cx, -cy);
|
|
|
|
}
|
|
|
|
if (fRotate) {
|
|
|
|
SkScalar cx = this->width() / 2;
|
|
|
|
SkScalar cy = this->height() / 2;
|
|
|
|
canvas->translate(cx, cy);
|
|
|
|
canvas->rotate(SkIntToScalar(30));
|
|
|
|
canvas->translate(-cx, -cy);
|
|
|
|
}
|
2011-03-11 20:08:25 +00:00
|
|
|
|
|
|
|
if (LCDTextDrawFilter::kNeutral_Mode != fLCDMode) {
|
|
|
|
canvas->setDrawFilter(new LCDTextDrawFilter(fLCDMode))->unref();
|
|
|
|
}
|
2009-08-14 16:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
|
2011-03-11 20:08:25 +00:00
|
|
|
canvas->setDrawFilter(NULL);
|
2009-08-14 16:08:38 +00:00
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
static SkBitmap::Config gConfigCycle[] = {
|
|
|
|
SkBitmap::kNo_Config, // none -> none
|
|
|
|
SkBitmap::kNo_Config, // a1 -> none
|
|
|
|
SkBitmap::kNo_Config, // a8 -> none
|
|
|
|
SkBitmap::kNo_Config, // index8 -> none
|
|
|
|
SkBitmap::kARGB_4444_Config, // 565 -> 4444
|
|
|
|
SkBitmap::kARGB_8888_Config, // 4444 -> 8888
|
|
|
|
SkBitmap::kRGB_565_Config // 8888 -> 565
|
|
|
|
};
|
|
|
|
|
|
|
|
static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
|
|
|
|
return gConfigCycle[c];
|
|
|
|
}
|
|
|
|
|
2011-03-21 00:53:39 +00:00
|
|
|
void SampleWindow::changeZoomLevel(int delta) {
|
|
|
|
fZoomLevel += delta;
|
|
|
|
if (fZoomLevel > 0) {
|
|
|
|
fZoomLevel = SkMin32(fZoomLevel, MAX_ZOOM_LEVEL);
|
|
|
|
fZoomScale = SkIntToScalar(fZoomLevel + 1);
|
|
|
|
} else if (fZoomLevel < 0) {
|
|
|
|
fZoomLevel = SkMax32(fZoomLevel, MIN_ZOOM_LEVEL);
|
|
|
|
fZoomScale = SK_Scalar1 / (1 - fZoomLevel);
|
|
|
|
} else {
|
|
|
|
fZoomScale = SK_Scalar1;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->inval(NULL);
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
bool SampleWindow::nextSample() {
|
2009-11-03 04:00:48 +00:00
|
|
|
fCurrIndex = (fCurrIndex + 1) % fSamples.count();
|
|
|
|
this->loadView(fSamples[fCurrIndex]());
|
|
|
|
return true;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SampleWindow::onEvent(const SkEvent& evt) {
|
|
|
|
if (evt.isType(ANIMATING_EVENTTYPE)) {
|
|
|
|
if (fAnimating) {
|
|
|
|
this->nextSample();
|
|
|
|
this->postAnimatingEvent();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-11-03 04:00:48 +00:00
|
|
|
if (evt.isType("set-curr-index")) {
|
|
|
|
fCurrIndex = evt.getFast32() % fSamples.count();
|
|
|
|
this->loadView(fSamples[fCurrIndex]());
|
|
|
|
return true;
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
return this->INHERITED::onEvent(evt);
|
|
|
|
}
|
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
bool SampleWindow::onQuery(SkEvent* query) {
|
|
|
|
if (query->isType("get-slide-count")) {
|
|
|
|
query->setFast32(fSamples.count());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (query->isType("get-slide-title")) {
|
|
|
|
SkView* view = fSamples[query->getFast32()]();
|
|
|
|
SkEvent evt(gTitleEvtName);
|
|
|
|
if (view->doQuery(&evt)) {
|
|
|
|
query->setString("title", evt.findString(gTitleEvtName));
|
|
|
|
}
|
|
|
|
SkSafeUnref(view);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (query->isType("use-fast-text")) {
|
|
|
|
SkEvent evt(gFastTextEvtName);
|
|
|
|
return curr_view(this)->doQuery(&evt);
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(query);
|
|
|
|
}
|
|
|
|
|
2008-12-23 16:49:54 +00:00
|
|
|
static void cleanup_for_filename(SkString* name) {
|
|
|
|
char* str = name->writable_str();
|
2009-12-18 21:33:39 +00:00
|
|
|
for (size_t i = 0; i < name->size(); i++) {
|
2008-12-23 16:49:54 +00:00
|
|
|
switch (str[i]) {
|
|
|
|
case ':': str[i] = '-'; break;
|
|
|
|
case '/': str[i] = '-'; break;
|
|
|
|
case ' ': str[i] = '_'; break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
bool SampleWindow::onHandleChar(SkUnichar uni) {
|
2010-12-20 18:26:13 +00:00
|
|
|
{
|
|
|
|
SkView* view = curr_view(this);
|
|
|
|
if (view) {
|
|
|
|
SkEvent evt(gCharEvtName);
|
|
|
|
evt.setFast32(uni);
|
|
|
|
if (view->doQuery(&evt)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
int dx = 0xFF;
|
|
|
|
int dy = 0xFF;
|
|
|
|
|
|
|
|
switch (uni) {
|
|
|
|
case '5': dx = 0; dy = 0; break;
|
|
|
|
case '8': dx = 0; dy = -1; break;
|
|
|
|
case '6': dx = 1; dy = 0; break;
|
|
|
|
case '2': dx = 0; dy = 1; break;
|
|
|
|
case '4': dx = -1; dy = 0; break;
|
|
|
|
case '7': dx = -1; dy = -1; break;
|
|
|
|
case '9': dx = 1; dy = -1; break;
|
|
|
|
case '3': dx = 1; dy = 1; break;
|
|
|
|
case '1': dx = -1; dy = 1; break;
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
if (0xFF != dx && 0xFF != dy) {
|
|
|
|
if ((dx | dy) == 0) {
|
|
|
|
fScrollTestX = fScrollTestY = 0;
|
|
|
|
} else {
|
|
|
|
fScrollTestX += dx;
|
|
|
|
fScrollTestY += dy;
|
|
|
|
}
|
|
|
|
this->inval(NULL);
|
|
|
|
return true;
|
|
|
|
}
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2008-12-23 16:49:54 +00:00
|
|
|
switch (uni) {
|
|
|
|
case 'a':
|
|
|
|
fAnimating = !fAnimating;
|
|
|
|
this->postAnimatingEvent();
|
|
|
|
this->updateTitle();
|
|
|
|
return true;
|
|
|
|
case 'f': {
|
|
|
|
const char* title = this->getTitle();
|
|
|
|
if (title[0] == 0) {
|
|
|
|
title = "sampleapp";
|
|
|
|
}
|
|
|
|
SkString name(title);
|
|
|
|
cleanup_for_filename(&name);
|
|
|
|
name.append(".png");
|
|
|
|
if (SkImageEncoder::EncodeFile(name.c_str(), this->getBitmap(),
|
2009-01-06 20:16:26 +00:00
|
|
|
SkImageEncoder::kPNG_Type, 100)) {
|
2008-12-23 16:49:54 +00:00
|
|
|
SkDebugf("Created %s\n", name.c_str());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-08-14 16:08:38 +00:00
|
|
|
case 'r':
|
|
|
|
fRotate = !fRotate;
|
|
|
|
this->inval(NULL);
|
|
|
|
this->updateTitle();
|
|
|
|
return true;
|
|
|
|
case 's':
|
|
|
|
fScale = !fScale;
|
|
|
|
this->inval(NULL);
|
|
|
|
this->updateTitle();
|
|
|
|
return true;
|
2011-02-10 18:47:24 +00:00
|
|
|
case 'c':
|
|
|
|
fUseClip = !fUseClip;
|
|
|
|
this->inval(NULL);
|
|
|
|
this->updateTitle();
|
|
|
|
return true;
|
2010-12-20 18:26:13 +00:00
|
|
|
case 'd':
|
|
|
|
SkGraphics::SetFontCacheUsed(0);
|
|
|
|
return true;
|
|
|
|
case 'g':
|
|
|
|
fRequestGrabImage = true;
|
|
|
|
this->inval(NULL);
|
|
|
|
break;
|
2011-03-11 20:08:25 +00:00
|
|
|
case 'l':
|
|
|
|
fLCDMode = cycle_lcdmode(fLCDMode);
|
|
|
|
this->updateTitle();
|
|
|
|
this->inval(NULL);
|
|
|
|
break;
|
2011-03-22 14:03:21 +00:00
|
|
|
#ifdef SK_BUILD_FOR_UNIX
|
|
|
|
// These methods have not been written for other platforms yet.
|
|
|
|
case 'i':
|
|
|
|
this->zoomIn();
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
this->zoomOut();
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
this->toggleZoomer();
|
|
|
|
break;
|
|
|
|
#endif
|
2008-12-23 16:49:54 +00:00
|
|
|
default:
|
|
|
|
break;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
return this->INHERITED::onHandleChar(uni);
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "SkDumpCanvas.h"
|
|
|
|
|
|
|
|
bool SampleWindow::onHandleKey(SkKey key) {
|
2010-12-20 18:26:13 +00:00
|
|
|
{
|
|
|
|
SkView* view = curr_view(this);
|
|
|
|
if (view) {
|
|
|
|
SkEvent evt(gKeyEvtName);
|
|
|
|
evt.setFast32(key);
|
|
|
|
if (view->doQuery(&evt)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
switch (key) {
|
|
|
|
case kRight_SkKey:
|
|
|
|
if (this->nextSample()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kLeft_SkKey:
|
|
|
|
fCanvasType = cycle_canvastype(fCanvasType);
|
|
|
|
this->updateTitle();
|
|
|
|
this->inval(NULL);
|
|
|
|
return true;
|
|
|
|
case kUp_SkKey:
|
2011-03-21 00:53:39 +00:00
|
|
|
if (USE_ARROWS_FOR_ZOOM) {
|
|
|
|
this->changeZoomLevel(1);
|
|
|
|
} else {
|
|
|
|
fNClip = !fNClip;
|
|
|
|
this->inval(NULL);
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
this->updateTitle();
|
|
|
|
return true;
|
|
|
|
case kDown_SkKey:
|
2011-03-21 00:53:39 +00:00
|
|
|
if (USE_ARROWS_FOR_ZOOM) {
|
|
|
|
this->changeZoomLevel(-1);
|
|
|
|
} else {
|
|
|
|
this->setConfig(cycle_configs(this->getBitmap().config()));
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
this->updateTitle();
|
|
|
|
return true;
|
|
|
|
case kOK_SkKey:
|
2010-12-20 18:26:13 +00:00
|
|
|
if (false) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkDebugfDumper dumper;
|
|
|
|
SkDumpCanvas dc(&dumper);
|
|
|
|
this->draw(&dc);
|
|
|
|
} else {
|
|
|
|
fRepeatDrawing = !fRepeatDrawing;
|
|
|
|
if (fRepeatDrawing) {
|
|
|
|
this->inval(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2009-11-03 04:00:48 +00:00
|
|
|
case kBack_SkKey:
|
|
|
|
this->loadView(NULL);
|
|
|
|
return true;
|
2008-12-17 15:59:43 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onHandleKey(key);
|
|
|
|
}
|
|
|
|
|
2011-03-16 12:10:02 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static const char gGestureClickType[] = "GestureClickType";
|
|
|
|
|
2011-03-21 00:53:39 +00:00
|
|
|
bool SampleWindow::onDispatchClick(int x, int y, Click::State state) {
|
|
|
|
int w = SkScalarRound(this->width());
|
|
|
|
int h = SkScalarRound(this->height());
|
|
|
|
|
|
|
|
// check for the resize-box
|
|
|
|
if (w - x < 16 && h - y < 16) {
|
|
|
|
return false; // let the OS handle the click
|
|
|
|
} else {
|
|
|
|
return this->INHERITED::onDispatchClick(x, y, state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-16 12:10:02 +00:00
|
|
|
class GestureClick : public SkView::Click {
|
|
|
|
public:
|
|
|
|
GestureClick(SkView* target) : SkView::Click(target) {
|
|
|
|
this->setType(gGestureClickType);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsGesture(Click* click) {
|
|
|
|
return click->isType(gGestureClickType);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
|
|
|
|
return new GestureClick(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SampleWindow::onClick(Click* click) {
|
|
|
|
if (GestureClick::IsGesture(click)) {
|
|
|
|
float x = SkScalarToFloat(click->fCurr.fX);
|
|
|
|
float y = SkScalarToFloat(click->fCurr.fY);
|
|
|
|
switch (click->fState) {
|
|
|
|
case SkView::Click::kDown_State:
|
|
|
|
fGesture.touchBegin(click, x, y);
|
|
|
|
break;
|
|
|
|
case SkView::Click::kMoved_State:
|
|
|
|
fGesture.touchMoved(click, x, y);
|
|
|
|
this->inval(NULL);
|
|
|
|
break;
|
|
|
|
case SkView::Click::kUp_State:
|
|
|
|
fGesture.touchEnd(click);
|
|
|
|
this->inval(NULL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void SampleWindow::loadView(SkView* view) {
|
|
|
|
SkView::F2BIter iter(this);
|
|
|
|
SkView* prev = iter.next();
|
|
|
|
if (prev) {
|
|
|
|
prev->detachFromParent();
|
|
|
|
}
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2009-11-03 04:00:48 +00:00
|
|
|
if (NULL == view) {
|
|
|
|
view = create_overview(fSamples.count(), fSamples.begin());
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
view->setVisibleP(true);
|
2010-12-20 18:26:13 +00:00
|
|
|
view->setClipToBounds(false);
|
2008-12-17 15:59:43 +00:00
|
|
|
this->attachChildToFront(view)->unref();
|
|
|
|
view->setSize(this->width(), this->height());
|
|
|
|
|
|
|
|
this->updateTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* gConfigNames[] = {
|
|
|
|
"unknown config",
|
|
|
|
"A1",
|
|
|
|
"A8",
|
|
|
|
"Index8",
|
|
|
|
"565",
|
|
|
|
"4444",
|
|
|
|
"8888"
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char* configToString(SkBitmap::Config c) {
|
|
|
|
return gConfigNames[c];
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* gCanvasTypePrefix[] = {
|
|
|
|
"raster: ",
|
|
|
|
"picture: ",
|
|
|
|
"opengl: "
|
|
|
|
};
|
|
|
|
|
|
|
|
void SampleWindow::updateTitle() {
|
|
|
|
SkString title;
|
|
|
|
|
|
|
|
SkView::F2BIter iter(this);
|
|
|
|
SkView* view = iter.next();
|
|
|
|
SkEvent evt(gTitleEvtName);
|
|
|
|
if (view->doQuery(&evt)) {
|
|
|
|
title.set(evt.findString(gTitleEvtName));
|
|
|
|
}
|
|
|
|
if (title.size() == 0) {
|
|
|
|
title.set("<unknown>");
|
|
|
|
}
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
title.prepend(gCanvasTypePrefix[fCanvasType]);
|
|
|
|
|
|
|
|
title.prepend(" ");
|
|
|
|
title.prepend(configToString(this->getBitmap().config()));
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
if (fAnimating) {
|
|
|
|
title.prepend("<A> ");
|
|
|
|
}
|
2009-08-14 16:08:38 +00:00
|
|
|
if (fScale) {
|
|
|
|
title.prepend("<S> ");
|
|
|
|
}
|
|
|
|
if (fRotate) {
|
|
|
|
title.prepend("<R> ");
|
|
|
|
}
|
2009-11-23 20:10:41 +00:00
|
|
|
if (fNClip) {
|
|
|
|
title.prepend("<C> ");
|
|
|
|
}
|
2011-03-11 20:08:25 +00:00
|
|
|
if (LCDTextDrawFilter::kForceOn_Mode == fLCDMode) {
|
|
|
|
title.prepend("LCD ");
|
|
|
|
} else if (LCDTextDrawFilter::kForceOff_Mode == fLCDMode) {
|
|
|
|
title.prepend("lcd ");
|
|
|
|
}
|
2011-03-21 00:53:39 +00:00
|
|
|
|
|
|
|
if (fZoomLevel) {
|
|
|
|
title.prependf("{%d} ", fZoomLevel);
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
this->setTitle(title.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SampleWindow::onSizeChange() {
|
|
|
|
this->INHERITED::onSizeChange();
|
|
|
|
|
|
|
|
SkView::F2BIter iter(this);
|
|
|
|
SkView* view = iter.next();
|
|
|
|
view->setSize(this->width(), this->height());
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// rebuild our clippath
|
|
|
|
{
|
|
|
|
const SkScalar W = this->width();
|
|
|
|
const SkScalar H = this->height();
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
fClipPath.reset();
|
|
|
|
#if 0
|
|
|
|
for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
|
|
|
|
SkRect r;
|
|
|
|
r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
|
|
|
|
for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
|
|
|
|
fClipPath.addRect(r);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
SkRect r;
|
|
|
|
r.set(0, 0, W, H);
|
|
|
|
fClipPath.addRect(r, SkPath::kCCW_Direction);
|
|
|
|
r.set(W/4, H/4, W*3/4, H*3/4);
|
|
|
|
fClipPath.addRect(r, SkPath::kCW_Direction);
|
|
|
|
#endif
|
|
|
|
}
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
this->updateTitle(); // to refresh our config
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
template <typename T> void SkTBSort(T array[], int count) {
|
|
|
|
for (int i = 1; i < count - 1; i++) {
|
|
|
|
bool didSwap = false;
|
|
|
|
for (int j = count - 1; j > i; --j) {
|
|
|
|
if (array[j] < array[j-1]) {
|
|
|
|
T tmp(array[j-1]);
|
|
|
|
array[j-1] = array[j];
|
|
|
|
array[j] = tmp;
|
|
|
|
didSwap = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!didSwap) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
for (int k = 0; k < count - 1; k++) {
|
|
|
|
SkASSERT(!(array[k+1] < array[k]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "SkRandom.h"
|
|
|
|
|
|
|
|
static void rand_rect(SkIRect* rect, SkRandom& rand) {
|
|
|
|
int bits = 8;
|
|
|
|
int shift = 32 - bits;
|
|
|
|
rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
|
|
|
|
rand.nextU() >> shift, rand.nextU() >> shift);
|
|
|
|
rect->sort();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dumpRect(const SkIRect& r) {
|
|
|
|
SkDebugf(" { %d, %d, %d, %d },\n",
|
|
|
|
r.fLeft, r.fTop,
|
|
|
|
r.fRight, r.fBottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_rects(const SkIRect rect[], int count) {
|
|
|
|
SkRegion rgn0, rgn1;
|
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
rgn0.op(rect[i], SkRegion::kUnion_Op);
|
|
|
|
// dumpRect(rect[i]);
|
|
|
|
}
|
|
|
|
rgn1.setRects(rect, count);
|
|
|
|
|
|
|
|
if (rgn0 != rgn1) {
|
|
|
|
SkDebugf("\n");
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
dumpRect(rect[i]);
|
|
|
|
}
|
|
|
|
SkDebugf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test() {
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
const SkIRect r0[] = {
|
|
|
|
{ 0, 0, 1, 1 },
|
|
|
|
{ 2, 2, 3, 3 },
|
|
|
|
};
|
|
|
|
const SkIRect r1[] = {
|
|
|
|
{ 0, 0, 1, 3 },
|
|
|
|
{ 1, 1, 2, 2 },
|
|
|
|
{ 2, 0, 3, 3 },
|
|
|
|
};
|
|
|
|
const SkIRect r2[] = {
|
|
|
|
{ 0, 0, 1, 2 },
|
|
|
|
{ 2, 1, 3, 3 },
|
|
|
|
{ 4, 0, 5, 1 },
|
|
|
|
{ 6, 0, 7, 4 },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const SkIRect* fRects;
|
|
|
|
int fCount;
|
|
|
|
} gRecs[] = {
|
|
|
|
{ r0, SK_ARRAY_COUNT(r0) },
|
|
|
|
{ r1, SK_ARRAY_COUNT(r1) },
|
|
|
|
{ r2, SK_ARRAY_COUNT(r2) },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
|
|
|
|
test_rects(gRecs[i].fRects, gRecs[i].fCount);
|
|
|
|
}
|
2011-01-18 20:57:22 +00:00
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
SkRandom rand;
|
|
|
|
for (i = 0; i < 10000; i++) {
|
|
|
|
SkRegion rgn0, rgn1;
|
|
|
|
|
|
|
|
const int N = 8;
|
|
|
|
SkIRect rect[N];
|
|
|
|
for (int j = 0; j < N; j++) {
|
|
|
|
rand_rect(&rect[j], rand);
|
|
|
|
}
|
|
|
|
test_rects(rect, N);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkOSWindow* create_sk_window(void* hwnd) {
|
2010-12-20 18:26:13 +00:00
|
|
|
// test();
|
2011-01-05 16:34:41 +00:00
|
|
|
return new SampleWindow(hwnd);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void get_preferred_size(int* x, int* y, int* width, int* height) {
|
|
|
|
*x = 10;
|
|
|
|
*y = 50;
|
|
|
|
*width = 640;
|
|
|
|
*height = 480;
|
|
|
|
}
|
|
|
|
|
|
|
|
void application_init() {
|
|
|
|
// setenv("ANDROID_ROOT", "../../../data", 0);
|
2009-12-18 21:33:39 +00:00
|
|
|
#ifdef SK_BUILD_FOR_MAC
|
2008-12-17 15:59:43 +00:00
|
|
|
setenv("ANDROID_ROOT", "/android/device/data", 0);
|
2009-12-18 21:33:39 +00:00
|
|
|
#endif
|
2011-01-05 16:34:41 +00:00
|
|
|
SkGraphics::Init();
|
|
|
|
SkEvent::Init();
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void application_term() {
|
2011-01-05 16:34:41 +00:00
|
|
|
SkEvent::Term();
|
|
|
|
SkGraphics::Term();
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|