Move a bunch of headers from include/gpu to src/gpu. Delete some unused files.

Review URL: http://codereview.appspot.com/5304047/



git-svn-id: http://skia.googlecode.com/svn/trunk@2506 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
bsalomon@google.com 2011-10-20 13:43:13 +00:00
parent e173992c23
commit ffa11bbbed
24 changed files with 15 additions and 1342 deletions

View File

@ -145,9 +145,6 @@
'libtess.gyp:libtess',
],
'sources': [
'../include/gpu/GrAllocator.h',
'../include/gpu/GrAllocPool.h',
'../include/gpu/GrAtlas.h',
'../include/gpu/GrClip.h',
'../include/gpu/GrClipIterator.h',
'../include/gpu/GrColor.h',
@ -158,44 +155,32 @@
'../include/gpu/GrGLConfig_chrome.h',
'../include/gpu/GrGLInterface.h',
'../include/gpu/GrGlyph.h',
'../include/gpu/GrGpuVertex.h',
'../include/gpu/GrInstanceCounter.h',
'../include/gpu/GrIPoint.h',
'../include/gpu/GrKey.h',
'../include/gpu/GrMatrix.h',
'../include/gpu/GrMesh.h',
'../include/gpu/GrNoncopyable.h',
'../include/gpu/GrPaint.h',
'../include/gpu/GrPath.h',
'../include/gpu/GrPathSink.h',
'../include/gpu/GrPlotMgr.h',
'../include/gpu/GrPoint.h',
'../include/gpu/GrRandom.h',
'../include/gpu/GrRect.h',
'../include/gpu/GrRectanizer.h',
'../include/gpu/GrRefCnt.h',
'../include/gpu/GrRenderTarget.h',
'../include/gpu/GrResource.h',
'../include/gpu/GrSamplerState.h',
'../include/gpu/GrScalar.h',
'../include/gpu/GrStencil.h',
'../include/gpu/GrStopwatch.h',
'../include/gpu/GrStringBuilder.h',
'../include/gpu/GrTBSearch.h',
'../include/gpu/GrTDArray.h',
'../include/gpu/GrTextContext.h',
'../include/gpu/GrTextStrike.h',
'../include/gpu/GrTexture.h',
'../include/gpu/GrTHashCache.h',
'../include/gpu/GrTLList.h',
'../include/gpu/GrTypes.h',
'../include/gpu/GrUserConfig.h',
'../src/gpu/GrAAHairLinePathRenderer.cpp',
'../src/gpu/GrAAHairLinePathRenderer.h',
'../src/gpu/GrAddPathRenderers_aahairline.cpp',
'../src/gpu/GrAllocator.h',
'../src/gpu/GrAllocPool.h',
'../src/gpu/GrAllocPool.cpp',
'../src/gpu/GrAtlas.cpp',
'../src/gpu/GrAtlas.h',
'../src/gpu/GrBinHashKey.h',
'../src/gpu/GrBufferAllocPool.cpp',
'../src/gpu/GrBufferAllocPool.h',
@ -234,6 +219,7 @@
'../src/gpu/GrGpuGLFixed.h',
'../src/gpu/GrGpuGLShaders.cpp',
'../src/gpu/GrGpuGLShaders.h',
'../src/gpu/GrGpuVertex.h',
'../src/gpu/GrIndexBuffer.h',
'../src/gpu/GrInOrderDrawBuffer.cpp',
'../src/gpu/GrInOrderDrawBuffer.h',
@ -245,21 +231,31 @@
'../src/gpu/GrPathRenderer.h',
'../src/gpu/GrPathUtils.cpp',
'../src/gpu/GrPathUtils.h',
'../src/gpu/GrPlotMgr.h',
'../src/gpu/GrRandom.h',
'../src/gpu/GrRectanizer.cpp',
'../src/gpu/GrRectanizer.h',
'../src/gpu/GrRedBlackTree.h',
'../src/gpu/GrRenderTarget.cpp',
'../src/gpu/GrResource.cpp',
'../src/gpu/GrResourceCache.cpp',
'../src/gpu/GrResourceCache.h',
'../src/gpu/GrStencil.cpp',
'../src/gpu/GrStencil.h',
'../src/gpu/GrStencilBuffer.cpp',
'../src/gpu/GrStencilBuffer.h',
'../src/gpu/GrStringBuilder.h',
'../src/gpu/GrTBSearch.h',
'../src/gpu/GrTDArray.h',
'../src/gpu/GrTesselatedPathRenderer.cpp',
'../src/gpu/GrTesselatedPathRenderer.h',
'../src/gpu/GrTextContext.cpp',
'../src/gpu/GrTextStrike.cpp',
'../src/gpu/GrTextStrike.h',
'../src/gpu/GrTextStrike_impl.h',
'../src/gpu/GrTexture.cpp',
'../src/gpu/GrTHashCache.h',
'../src/gpu/GrTLList.h',
'../src/gpu/GrVertexBuffer.h',
'../src/gpu/gr_unittests.cpp',

View File

@ -1,51 +0,0 @@
/*
* Copyright 2010 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkFlingState_DEFINED
#define SkFlingState_DEFINED
#include "SkScalar.h"
#include "SkPoint.h"
class SkMatrix;
struct FlingState {
FlingState() : fActive(false) {}
bool isActive() const { return fActive; }
void stop() { fActive = false; }
void reset(float sx, float sy);
bool evaluateMatrix(SkMatrix* matrix);
private:
SkPoint fDirection;
SkScalar fSpeed0;
double fTime0;
bool fActive;
};
class GrAnimateFloat {
public:
GrAnimateFloat();
void start(float v0, float v1, float duration);
bool isActive() const { return fTime0 != 0; }
void stop() { fTime0 = 0; }
float evaluate();
private:
float fValue0, fValue1, fDuration;
SkMSec fTime0;
};
#endif

View File

@ -1,28 +0,0 @@
/*
* Copyright 2010 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef GrIPoint_DEFINED
#define GrIPoint_DEFINED
#include "GrTypes.h"
struct GrIPoint {
public:
int32_t fX, fY;
GrIPoint(int32_t x, int32_t y) : fX(x), fY(y) {}
void set(int32_t x, int32_t y) {
fX = x;
fY = y;
}
};
#endif

View File

@ -1,49 +0,0 @@
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef GrMesh_DEFINED
#define GrMesh_DEFINED
#include "SkRect.h"
#include "SkPoint.h"
class SkCanvas;
class SkPaint;
class GrMesh {
public:
GrMesh();
~GrMesh();
GrMesh& operator=(const GrMesh& src);
void init(const SkRect& bounds, int rows, int cols,
const SkRect& texture);
const SkRect& bounds() const { return fBounds; }
int rows() const { return fRows; }
int cols() const { return fCols; }
SkPoint& pt(int row, int col) {
return fPts[row * (fRows + 1) + col];
}
void draw(SkCanvas*, const SkPaint&);
void drawWireframe(SkCanvas* canvas, const SkPaint& paint);
private:
SkRect fBounds;
int fRows, fCols;
SkPoint* fPts;
SkPoint* fTex; // just points into fPts, not separately allocated
int fCount;
uint16_t* fIndices;
int fIndexCount;
};
#endif

View File

@ -1,29 +0,0 @@
/*
* Copyright 2010 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef GrPathSink_DEFINED
#define GrPathSink_DEFINED
#include "GrScalar.h"
class GrPathSink {
public:
virtual ~GrPathSink() {}
virtual void moveTo(GrScalar x, GrScalar y) = 0;
virtual void lineTo(GrScalar x, GrScalar y) = 0;
virtual void quadTo(GrScalar x0, GrScalar y0, GrScalar x1, GrScalar y1) = 0;
virtual void cubicTo(GrScalar x0, GrScalar y0, GrScalar x1, GrScalar y1,
GrScalar x2, GrScalar y2) = 0;
virtual void close() = 0;
};
#endif

View File

@ -1,128 +0,0 @@
/*
* Copyright 2010 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef GrStopwatch_DEFINED
#define GrStopwatch_DEFINED
#include "GrTypes.h"
template <typename PLATFORM_TIMER>
/**
* Base class for stopwatch. Relies on PLATFORM_TIMER for platform-specific
* timer functions. PLATFORM_TIMER provides:
* - typename TIMESTAMP : a timestamp value that can be used with Diff()
* - static TIMESTAMP Now() : gets current timestamp
* - static double Diff(const TIMESTAMP& begin, const TIMESTAMP& end) :
* computes delta in seconds between two timestamps
*/
class GrStopwatchBase {
public:
/**
* Contructor - implicit reset()
*/
GrStopwatchBase() {
fRunning = false;
fTotalElapsed = 0.0;
}
/**
* begins a new lap
*/
void start() {
double lastLap = lapTime();
fTotalElapsed += lastLap;
fRunning = true;
fLastStart = PLATFORM_TIMER::Now();
}
/**
* ends current lap (or no effect if lap not started)
*/
void stop() {
double lastLap = lapTime();
fTotalElapsed += lastLap;
fRunning = false;
}
/**
* ends current lap, resets total time
*/
void reset() {
fRunning = false;
fTotalElapsed = 0.f;
}
/**
* Computes the time of all laps since last reset() including current lap
* if lap is still running.
*
* @return the sum time in seconds of all laps since last reset().
*/
double totalTime() const {
return fTotalElapsed + lapTime();
}
/**
* Current lap time.
*
* @return time in seconds of current lap if one is running otherwise 0.
*/
double lapTime() const {
if (fRunning) {
PLATFORM_TIMER::Timestamp now = PLATFORM_TIMER::Now();
return PLATFORM_TIMER::Elapsed(fLastStart, now);
}
return 0.0;
}
private:
double fTotalElapsed;
typename PLATFORM_TIMER::Timestamp fLastStart;
bool fRunning;
};
#if GR_WIN32_BUILD
#include <Windows.h>
class GrWin32Timer {
public:
typedef LARGE_INTEGER Timestamp;
static Timestamp Now() {
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
return now;
}
static double Elapsed(const Timestamp& begin, const Timestamp& end) {
double diff = (double)(end.QuadPart - begin.QuadPart);
return diff * Scale();
}
private:
static double Scale() {
static double scale;
if (0.0 == scale) {
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
GrAssert(0 != freq.QuadPart);
scale = 1 / (double) freq.QuadPart;
}
return scale;
}
};
typedef GrStopwatchBase<GrWin32Timer> GrStopwatch;
#else
#error "Implement platform timer for stopwatch"
#endif
#endif

View File

@ -1,65 +0,0 @@
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#import <UIKit/UIKit.h>
#include "SkMatrix.h"
#include "FlingState.h"
#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>
class SkOSWindow;
class SkEvent;
@interface SkUIView : UIView <UIAccelerometerDelegate> {
BOOL fRedrawRequestPending;
SkMatrix fMatrix, fLocalMatrix;
bool fNeedGestureEnded;
SkMatrix fRotateMatrix;
float fFirstPinchX, fFirstPinchY;
bool fNeedFirstPinch;
float fZoomAroundX, fZoomAroundY;
bool fZoomAround;
FlingState fFlingState;
GrAnimateFloat fWarpState;
bool fUseWarp;
struct {
EAGLContext* fContext;
GLuint fRenderbuffer;
GLuint fStencilbuffer;
GLuint fFramebuffer;
GLint fWidth;
GLint fHeight;
} fGL;
UINavigationItem* fTitle;
SkOSWindow* fWind;
}
@property (nonatomic, assign) SkOSWindow *fWind;
@property (nonatomic, retain) UINavigationItem* fTitle;
@property (nonatomic, assign) Backend fBackend;
@property (nonatomic, assign) bool fComplexClip;
@property (nonatomic, assign, setter=setWarpState) bool fUseWarp;
- (void)initGestures;
- (void)flushLocalMatrix;
- (void)setSkTitle:(const char*)title;
- (void)postInvalWithRect:(const SkIRect*)rectOrNil;
- (BOOL)onHandleEvent:(const SkEvent&)event;
@end

View File

@ -1,147 +0,0 @@
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "GrMesh.h"
#include "SkCanvas.h"
GrMesh::GrMesh() : fPts(NULL), fCount(0), fIndices(NULL), fIndexCount(0) {}
GrMesh::~GrMesh() {
delete[] fPts;
delete[] fIndices;
}
GrMesh& GrMesh::operator=(const GrMesh& src) {
delete[] fPts;
delete[] fIndices;
fBounds = src.fBounds;
fRows = src.fRows;
fCols = src.fCols;
fCount = src.fCount;
fPts = new SkPoint[fCount * 2];
fTex = fPts + fCount;
memcpy(fPts, src.fPts, fCount * 2 * sizeof(SkPoint));
delete[] fIndices;
fIndexCount = src.fIndexCount;
fIndices = new uint16_t[fIndexCount];
memcpy(fIndices, src.fIndices, fIndexCount * sizeof(uint16_t));
return *this;
}
void GrMesh::init(const SkRect& bounds, int rows, int cols,
const SkRect& texture) {
SkASSERT(rows > 0 && cols > 0);
fBounds = bounds;
fRows = rows;
fCols = cols;
delete[] fPts;
fCount = (rows + 1) * (cols + 1);
fPts = new SkPoint[fCount * 2];
fTex = fPts + fCount;
delete[] fIndices;
fIndexCount = rows * cols * 6;
fIndices = new uint16_t[fIndexCount];
SkPoint* pts = fPts;
const SkScalar dx = bounds.width() / rows;
const SkScalar dy = bounds.height() / cols;
SkPoint* tex = fTex;
const SkScalar dtx = texture.width() / rows;
const SkScalar dty = texture.height() / cols;
uint16_t* idx = fIndices;
int index = 0;
for (int y = 0; y <= cols; y++) {
for (int x = 0; x <= rows; x++) {
pts->set(bounds.fLeft + x*dx, bounds.fTop + y*dy);
pts += 1;
tex->set(texture.fLeft + x*dtx, texture.fTop + y*dty);
tex += 1;
if (y < cols && x < rows) {
*idx++ = index;
*idx++ = index + rows + 1;
*idx++ = index + 1;
*idx++ = index + 1;
*idx++ = index + rows + 1;
*idx++ = index + rows + 2;
index += 1;
}
}
index += 1;
}
}
void GrMesh::draw(SkCanvas* canvas, const SkPaint& paint) {
canvas->drawVertices(SkCanvas::kTriangles_VertexMode, fCount,
fPts, fTex, NULL, NULL, fIndices, fIndexCount,
paint);
}
/////////////////////////////////////////////
#include "SkBoundaryPatch.h"
#include "SkMeshUtils.h"
static SkPoint SkPointInterp(const SkPoint& a, const SkPoint& b, SkScalar t) {
return SkPoint::Make(SkScalarInterp(a.fX, b.fX, t),
SkScalarInterp(a.fY, b.fY, t));
}
static void set_cubic(SkPoint pts[4], SkScalar x0, SkScalar y0,
SkScalar x3, SkScalar y3, SkScalar scale = 1) {
SkPoint tmp, tmp2;
pts[0].set(x0, y0);
pts[3].set(x3, y3);
tmp = SkPointInterp(pts[0], pts[3], SK_Scalar1/3);
tmp2 = pts[0] - tmp;
tmp2.rotateCW();
tmp2.scale(scale);
pts[1] = tmp + tmp2;
tmp = SkPointInterp(pts[0], pts[3], 2*SK_Scalar1/3);
tmp2 = pts[3] - tmp;
tmp2.rotateCW();
tmp2.scale(scale);
pts[2] = tmp + tmp2;
}
void test_patch(SkCanvas* canvas, const SkBitmap& bm, SkScalar scale) {
const float w = bm.width();
const float h = bm.height();
SkCubicBoundary cubic;
set_cubic(cubic.fPts + 0, 0, 0, w, 0, scale);
set_cubic(cubic.fPts + 3, w, 0, w, h, scale);
set_cubic(cubic.fPts + 6, w, h, 0, h, -scale);
set_cubic(cubic.fPts + 9, 0, h, 0, 0, scale);
SkBoundaryPatch patch;
patch.setBoundary(&cubic);
const int Rows = 16;
const int Cols = 16;
SkPoint pts[Rows * Cols];
patch.evalPatch(pts, Rows, Cols);
SkPaint paint;
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
SkMeshUtils::Draw(canvas, bm, Rows, Cols, pts, NULL, paint);
}

View File

@ -10,6 +10,7 @@
#define GrGLShaderVar_DEFINED
#include "GrGLInterface.h"
#include "GrStringBuilder.h"
/**
* Represents a variable in a shader

View File

@ -1,827 +0,0 @@
#import "SkUIView.h"
#include <QuartzCore/QuartzCore.h>
//#include "SkGpuCanvas.h"
#include "SkGpuDevice.h"
#include "SkCGUtils.h"
#include "GrContext.h"
#define SKWIND_CONFIG SkBitmap::kRGB_565_Config
//#define SKWIND_CONFIG SkBitmap::kARGB_8888_Config
#define SKGL_CONFIG kEAGLColorFormatRGB565
//#define SKGL_CONFIG kEAGLColorFormatRGBA8
#define SHOW_FPS
#define FORCE_REDRAW
//#define DUMP_FPS_TO_PRINTF
//#define USE_ACCEL_TO_ROTATE
//#define SHOULD_COUNTER_INIT 334
static int gShouldCounter;
static bool should_draw() {
if (--gShouldCounter == 0) {
// printf("\n");
}
return true;
return gShouldCounter >= 0;
}
#ifdef SHOULD_COUNTER_INIT
bool (*gShouldDrawProc)() = should_draw;
#else
bool (*gShouldDrawProc)() = NULL;
#endif
//#define USE_GL_1
//#define USE_GL_2
#if defined(USE_GL_1) || defined(USE_GL_2)
#define USE_GL
#endif
@implementation SkUIView
@synthesize fWind;
@synthesize fTitle;
@synthesize fBackend;
@synthesize fComplexClip;
@synthesize fUseWarp;
#include "SkWindow.h"
#include "SkEvent.h"
static float gScreenScale = 1;
extern SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
#define kREDRAW_UIVIEW_GL "sk_redraw_uiview_gl_iOS"
#define TITLE_HEIGHT 0
static const float SCALE_FOR_ZOOM_LENS = 4.0;
#define Y_OFFSET_FOR_ZOOM_LENS 200
#define SIZE_FOR_ZOOM_LENS 250
static const float MAX_ZOOM_SCALE = 4.0;
static const float MIN_ZOOM_SCALE = 2.0 / MAX_ZOOM_SCALE;
extern bool gDoTraceDraw;
#define DO_TRACE_DRAW_MAX 100
#ifdef SHOW_FPS
struct FPSState {
static const int FRAME_COUNT = 60;
CFTimeInterval fNow0, fNow1;
CFTimeInterval fTime0, fTime1, fTotalTime;
int fFrameCounter;
int fDrawCounter;
FPSState() {
fTime0 = fTime1 = fTotalTime = 0;
fFrameCounter = 0;
}
void startDraw() {
fNow0 = CACurrentMediaTime();
if (0 == fDrawCounter && false) {
gDoTraceDraw = true;
SkDebugf("\n");
}
}
void endDraw() {
fNow1 = CACurrentMediaTime();
if (0 == fDrawCounter) {
gDoTraceDraw = true;
}
if (DO_TRACE_DRAW_MAX == ++fDrawCounter) {
fDrawCounter = 0;
}
}
void flush(SkOSWindow* wind) {
CFTimeInterval now2 = CACurrentMediaTime();
fTime0 += fNow1 - fNow0;
fTime1 += now2 - fNow1;
if (++fFrameCounter == FRAME_COUNT) {
CFTimeInterval totalNow = CACurrentMediaTime();
fTotalTime = totalNow - fTotalTime;
SkMSec ms0 = (int)(1000 * fTime0 / FRAME_COUNT);
SkMSec msTotal = (int)(1000 * fTotalTime / FRAME_COUNT);
SkString str;
str.printf("ms: %d [%d], fps: %3.1f", msTotal, ms0,
FRAME_COUNT / fTotalTime);
#ifdef DUMP_FPS_TO_PRINTF
SkDebugf("%s\n", str.c_str());
#else
wind->setTitle(str.c_str());
#endif
fTotalTime = totalNow;
fTime0 = fTime1 = 0;
fFrameCounter = 0;
}
}
};
static FPSState gFPS;
#define FPS_StartDraw() gFPS.startDraw()
#define FPS_EndDraw() gFPS.endDraw()
#define FPS_Flush(wind) gFPS.flush(wind)
#else
#define FPS_StartDraw()
#define FPS_EndDraw()
#define FPS_Flush(wind)
#endif
///////////////////////////////////////////////////////////////////////////////
#ifdef USE_GL
+ (Class) layerClass
{
return [CAEAGLLayer class];
}
#endif
- (id)initWithMyDefaults {
fBackend = kGL_Backend;
fUseWarp = false;
fRedrawRequestPending = false;
// FIXME: If iOS has argc & argv, pass them here.
//fWind = create_sk_window(self, 0, NULL);
//fWind->setConfig(SKWIND_CONFIG);
fMatrix.reset();
fLocalMatrix.reset();
fNeedGestureEnded = false;
fNeedFirstPinch = true;
fZoomAround = false;
fComplexClip = false;
[self initGestures];
#ifdef USE_GL
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = TRUE;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO],
kEAGLDrawablePropertyRetainedBacking,
SKGL_CONFIG,
kEAGLDrawablePropertyColorFormat,
nil];
#ifdef USE_GL_1
fGL.fContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
#else
fGL.fContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
#endif
if (!fGL.fContext || ![EAGLContext setCurrentContext:fGL.fContext])
{
[self release];
return nil;
}
// Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
glGenFramebuffersOES(1, &fGL.fFramebuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, fGL.fFramebuffer);
glGenRenderbuffersOES(1, &fGL.fRenderbuffer);
glGenRenderbuffersOES(1, &fGL.fStencilbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, fGL.fRenderbuffer);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, fGL.fRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, fGL.fStencilbuffer);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_STENCIL_ATTACHMENT_OES, GL_RENDERBUFFER_OES, fGL.fStencilbuffer);
#endif
#ifdef USE_ACCEL_TO_ROTATE
fRotateMatrix.reset();
[UIAccelerometer sharedAccelerometer].delegate = self;
[UIAccelerometer sharedAccelerometer].updateInterval = 1 / 30.0;
#endif
return self;
}
- (id)initWithCoder:(NSCoder*)coder {
if ((self = [super initWithCoder:coder])) {
self = [self initWithMyDefaults];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self = [self initWithMyDefaults];
}
return self;
}
#include "SkImageDecoder.h"
#include "SkStream_NSData.h"
static void zoom_around(SkCanvas* canvas, float cx, float cy, float zoom) {
float clipW = SIZE_FOR_ZOOM_LENS;
float clipH = SIZE_FOR_ZOOM_LENS;
SkRect r;
r.set(0, 0, clipW, clipH);
r.offset(cx - clipW/2, cy - clipH/2);
SkPaint paint;
paint.setColor(0xFF66AAEE);
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeWidth(10);
// draw our "frame" around the zoom lens
canvas->drawRect(r, paint);
// now clip and scale the lens
canvas->clipRect(r);
canvas->translate(cx, cy);
canvas->scale(zoom, zoom);
canvas->translate(-cx, -cy);
}
- (void)drawWithCanvas:(SkCanvas*)canvas {
if (fComplexClip) {
canvas->drawColor(SK_ColorBLUE);
SkPath path;
static const SkRect r[] = {
{ 50, 50, 250, 250 },
{ 150, 150, 500, 600 }
};
for (size_t i = 0; i < GR_ARRAY_COUNT(r); i++) {
path.addRect(r[i]);
}
canvas->clipPath(path);
}
// This is to consolidate multiple inval requests
fRedrawRequestPending = false;
if (fFlingState.isActive()) {
if (!fFlingState.evaluateMatrix(&fLocalMatrix)) {
[self flushLocalMatrix];
}
}
SkMatrix localMatrix = fLocalMatrix;
#ifdef USE_ACCEL_TO_ROTATE
localMatrix.preConcat(fRotateMatrix);
#endif
SkMatrix matrix;
matrix.setConcat(localMatrix, fMatrix);
const SkMatrix* localM = NULL;
if (localMatrix.getType() & SkMatrix::kScale_Mask) {
localM = &localMatrix;
}
#ifdef USE_ACCEL_TO_ROTATE
localM = &localMatrix;
#endif
canvas->setExternalMatrix(localM);
#ifdef SHOULD_COUNTER_INIT
gShouldCounter = SHOULD_COUNTER_INIT;
#endif
{
int saveCount = canvas->save();
canvas->concat(matrix);
// SkRect r = { 10, 10, 500, 600 }; canvas->clipRect(r);
fWind->draw(canvas);
canvas->restoreToCount(saveCount);
}
if (fZoomAround) {
zoom_around(canvas, fZoomAroundX, fZoomAroundY, SCALE_FOR_ZOOM_LENS);
canvas->concat(matrix);
fWind->draw(canvas);
}
#ifdef FORCE_REDRAW
fWind->inval(NULL);
#endif
}
///////////////////////////////////////////////////////////////////////////////
- (void)layoutSubviews {
int W, H;
gScreenScale = [UIScreen mainScreen].scale;
#ifdef USE_GL
CAEAGLLayer* eaglLayer = (CAEAGLLayer*)self.layer;
if ([self respondsToSelector:@selector(setContentScaleFactor:)]) {
self.contentScaleFactor = gScreenScale;
}
// Allocate color buffer backing based on the current layer size
glBindRenderbufferOES(GL_RENDERBUFFER_OES, fGL.fRenderbuffer);
[fGL.fContext renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:eaglLayer];
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &fGL.fWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &fGL.fHeight);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, fGL.fStencilbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_STENCIL_INDEX8_OES, fGL.fWidth, fGL.fHeight);
if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES)
{
NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
}
W = fGL.fWidth;
H = fGL.fHeight;
#else
CGRect rect = [self bounds];
W = (int)CGRectGetWidth(rect);
H = (int)CGRectGetHeight(rect) - TITLE_HEIGHT;
#endif
printf("---- layoutSubviews %d %d\n", W, H);
fWind->resize(W, H);
fWind->inval(NULL);
}
#ifdef USE_GL
static GrContext* gCtx;
static GrContext* get_global_grctx() {
// should be pthread-local at least
if (NULL == gCtx) {
#ifdef USE_GL_1
gCtx = GrContext::Create(kOpenGL_Fixed_GrEngine, 0);
#else
gCtx = GrContext::Create(kOpenGL_Shaders_GrEngine, 0);
#endif
}
return gCtx;
}
#include "SkDevice.h"
#include "SkShader.h"
#include "SkGrTexturePixelRef.h"
#include "GrMesh.h"
#include "SkRandom.h"
#include "GrAtlas.h"
#include "GrTextStrike.h"
static void show_fontcache(GrContext* ctx, SkCanvas* canvas) {
#if 0
SkPaint paint;
const int SIZE = 64;
GrAtlas* plot[64][64];
paint.setAntiAlias(true);
paint.setTextSize(24);
paint.setTextAlign(SkPaint::kCenter_Align);
Gr_bzero(plot, sizeof(plot));
GrFontCache* cache = ctx->getFontCache();
GrTextStrike* strike = cache->getHeadStrike();
int count = 0;
while (strike) {
GrAtlas* atlas = strike->getAtlas();
while (atlas) {
int x = atlas->getPlotX();
int y = atlas->getPlotY();
SkRandom rand((intptr_t)strike);
SkColor c = rand.nextU() | 0x80808080;
paint.setColor(c);
paint.setAlpha(0x80);
SkRect r;
r.set(x * SIZE, y * SIZE, (x + 1)*SIZE, (y+1)*SIZE);
r.inset(1, 1);
canvas->drawRect(r, paint);
paint.setColor(0xFF660000);
SkString label;
label.printf("%d", count);
canvas->drawText(label.c_str(), label.size(), r.centerX(),
r.fTop + r.height() * 2 / 3, paint);
atlas = atlas->nextAtlas();
}
strike = strike->fNext;
count += 1;
}
#endif
}
void test_patch(SkCanvas* canvas, const SkBitmap& bm, SkScalar scale);
static void draw_mesh(SkCanvas* canvas, const SkBitmap& bm) {
GrMesh fMesh;
SkRect r;
r.set(0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()));
// fMesh.init(bounds, fBitmap.width() / 40, fBitmap.height() / 40, texture);
fMesh.init(r, bm.width()/16, bm.height()/16, r);
SkPaint paint;
SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
paint.setShader(s)->unref();
fMesh.draw(canvas, paint);
}
static void scale_about(SkCanvas* canvas, float sx, float sy, float px, float py) {
canvas->translate(px, py);
canvas->scale(sx, sy);
canvas->translate(-px, -py);
}
static float grInterp(float v0, float v1, float percent) {
return v0 + percent * (v1 - v0);
}
static void draw_device(SkCanvas* canvas, SkDevice* dev, float w, float h, float warp) {
canvas->save();
float s = grInterp(1, 0.8, warp);
scale_about(canvas, s, s, w/2, h/2);
test_patch(canvas, dev->accessBitmap(false), warp);
canvas->restore();
}
- (void)drawInGL {
// printf("------ drawInGL\n");
// This application only creates a single context which is already set current at this point.
// This call is redundant, but needed if dealing with multiple contexts.
[EAGLContext setCurrentContext:fGL.fContext];
// This application only creates a single default framebuffer which is already bound at this point.
// This call is redundant, but needed if dealing with multiple framebuffers.
glBindFramebufferOES(GL_FRAMEBUFFER_OES, fGL.fFramebuffer);
GLint scissorEnable;
glGetIntegerv(GL_SCISSOR_TEST, &scissorEnable);
glDisable(GL_SCISSOR_TEST);
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);
if (scissorEnable) {
glEnable(GL_SCISSOR_TEST);
}
glViewport(0, 0, fWind->width(), fWind->height());
GrContext* ctx = get_global_grctx();
//SkGpuCanvas origCanvas(ctx);
//origCanvas.setBitmapDevice(fWind->getBitmap());
//gl->reset();
SkCanvas glCanvas;
SkGpuDevice* dev = new SkGpuDevice(ctx, SkGpuDevice::Current3DApiRenderTarget());
glCanvas.setDevice(dev)->unref();
SkCanvas rasterCanvas;
SkCanvas* canvas;
//SkDevice* dev = NULL;
switch (fBackend) {
case kRaster_Backend:
canvas = &rasterCanvas;
break;
case kGL_Backend:
canvas = &glCanvas;
break;
}
// if (fUseWarp || fWarpState.isActive()) {
// if (kGL_Backend == fBackend) {
// dev = origCanvas.createDevice(fWind->getBitmap(), true);
// canvas->setDevice(dev)->unref();
// } else {
// canvas->setBitmapDevice(fWind->getBitmap());
// dev = canvas->getDevice();
// }
// } else {
// canvas->setBitmapDevice(fWind->getBitmap());
// dev = NULL;
// }
canvas->translate(0, TITLE_HEIGHT);
// if we're not "retained", then we have to always redraw everything.
// This call forces us to ignore the fDirtyRgn, and draw everywhere.
// If we are "retained", we can skip this call (as the raster case does)
fWind->forceInvalAll();
FPS_StartDraw();
[self drawWithCanvas:canvas];
FPS_EndDraw();
// if (dev) {
// draw_device(&origCanvas, dev, fWind->width(), fWind->height(),
// fWarpState.evaluate());
// } else {
// if (kRaster_Backend == fBackend) {
// origCanvas.drawBitmap(fWind->getBitmap(), 0, 0, NULL);
// }
// // else GL - we're already on screen
// }
show_fontcache(ctx, canvas);
ctx->flush(false);
// This application only creates a single color renderbuffer which is already bound at this point.
// This call is redundant, but needed if dealing with multiple renderbuffers.
glBindRenderbufferOES(GL_RENDERBUFFER_OES, fGL.fRenderbuffer);
[fGL.fContext presentRenderbuffer:GL_RENDERBUFFER_OES];
#if GR_COLLECT_STATS
static int frame = 0;
if (!(frame % 100)) {
get_global_grctx()->printStats();
}
get_global_grctx()->resetStats();
++frame;
#endif
FPS_Flush(fWind);
#if 0
gCtx->deleteAllTextures(GrTextureCache::kAbandonTexture_DeleteMode);
gCtx->unref();
gCtx = NULL;
#endif
}
#else // raster case
- (void)drawRect:(CGRect)rect {
SkCanvas canvas;
canvas.setBitmapDevice(fWind->getBitmap());
FPS_StartDraw();
[self drawWithCanvas:&canvas];
FPS_EndDraw();
CGContextRef cg = UIGraphicsGetCurrentContext();
SkCGDrawBitmap(cg, fWind->getBitmap(), 0, TITLE_HEIGHT);
FPS_Flush(fWind);
}
#endif
- (void)setWarpState:(bool)useWarp {
fWarpState.stop(); // we should reverse from where we are if active...
const float duration = 0.5;
fUseWarp = useWarp;
if (useWarp) {
fWarpState.start(0, 1, duration);
} else {
fWarpState.start(1, 0, duration);
}
}
///////////////////////////////////////////////////////////////////////////////
- (void)flushLocalMatrix {
fMatrix.postConcat(fLocalMatrix);
fLocalMatrix.reset();
fFlingState.stop();
fNeedGestureEnded = false;
fNeedFirstPinch = true;
}
- (void)localMatrixWithGesture:(UIGestureRecognizer*)gesture {
fNeedGestureEnded = true;
switch (gesture.state) {
case UIGestureRecognizerStateCancelled:
case UIGestureRecognizerStateEnded:
[self flushLocalMatrix];
break;
case UIGestureRecognizerStateChanged: {
SkMatrix matrix;
matrix.setConcat(fLocalMatrix, fMatrix);
} break;
default:
break;
}
}
- (void)commonHandleGesture:(UIGestureRecognizer*)sender {
if (fFlingState.isActive()) {
[self flushLocalMatrix];
}
switch (sender.state) {
case UIGestureRecognizerStateBegan:
[self flushLocalMatrix];
break;
default:
break;
}
}
- (float)limitTotalZoom:(float)scale {
// this query works 'cause we know that we're square-scale w/ no skew/rotation
const float curr = fMatrix[0];
if (scale > 1 && curr * scale > MAX_ZOOM_SCALE) {
scale = MAX_ZOOM_SCALE / curr;
} else if (scale < 1 && curr * scale < MIN_ZOOM_SCALE) {
scale = MIN_ZOOM_SCALE / curr;
}
return scale;
}
- (void)handleLongPressGesture:(UILongPressGestureRecognizer*)sender {
[self commonHandleGesture:sender];
if ([sender numberOfTouches] == 0) {
fZoomAround = false;
return;
}
CGPoint pt = [sender locationOfTouch:0 inView:self];
switch (sender.state) {
case UIGestureRecognizerStateBegan:
case UIGestureRecognizerStateChanged:
fZoomAround = true;
fZoomAroundX = pt.x;
fZoomAroundY = pt.y - Y_OFFSET_FOR_ZOOM_LENS;
break;
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateCancelled:
fZoomAround = false;
break;
default:
break;
}
}
- (void)addAndReleaseGesture:(UIGestureRecognizer*)gesture {
[self addGestureRecognizer:gesture];
[gesture release];
}
//Gesture Handlers
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint loc = [touch locationInView:self];
fWind->handleClick(loc.x, loc.y, SkView::Click::kDown_State, touch);
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint loc = [touch locationInView:self];
fWind->handleClick(loc.x, loc.y, SkView::Click::kMoved_State, touch);
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint loc = [touch locationInView:self];
fWind->handleClick(loc.x, loc.y, SkView::Click::kUp_State, touch);
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint loc = [touch locationInView:self];
fWind->handleClick(loc.x, loc.y, SkView::Click::kUp_State, touch);
}
}
- (void)initGestures {
UILongPressGestureRecognizer* longG = [UILongPressGestureRecognizer alloc];
[longG initWithTarget:self action:@selector(handleLongPressGesture:)];
[self addAndReleaseGesture:longG];
}
///////////////////////////////////////////////////////////////////////////////
static float abs(float x) { return x < 0 ? -x : x; }
static bool normalize(UIAcceleration* acc, float xy[]) {
float mag2 = acc.x*acc.x + acc.y*acc.y + acc.z*acc.z;
if (mag2 < 0.000001) {
return false;
}
if (abs((float)acc.z) > 0.9 * sqrt(mag2)) {
return false;
}
mag2 = acc.x*acc.x + acc.y*acc.y;
if (mag2 < 0.000001) {
return false;
}
float scale = 1 / sqrt(mag2);
xy[0] = acc.x * scale;
xy[1] = acc.y * scale;
return true;
}
static void normalize(float xy[]) {
float scale = 1 / sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
xy[0] *= scale;
xy[1] *= scale;
}
static float weighted_average(float newv, float oldv) {
return newv * 0.25 + oldv * 0.75;
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acc {
float norm[2];
if (normalize(acc, norm)) {
float sinv = -norm[0];
float cosv = -norm[1];
// smooth
norm[0] = weighted_average(sinv, -fRotateMatrix[1]);
norm[1] = weighted_average(cosv, fRotateMatrix[0]);
normalize(norm);
fRotateMatrix.setSinCos(norm[0], norm[1], 400, 400);
}
#if 0
NSDate *now = [NSDate date];
NSTimeInterval intervalDate = [now timeIntervalSinceDate:now_prev];
velX += (acceleration.x * intervalDate);
distX += (velX * intervalDate);
//do other axis here too
// setup for next UIAccelerometer event
now_prev = now;
#endif
}
///////////////////////////////////////////////////////////////////////////////
- (void)setSkTitle:(const char *)title {
if (fTitle) {
fTitle.title = [NSString stringWithUTF8String:title];
}
}
- (BOOL)onHandleEvent:(const SkEvent&)evt {
if (evt.isType(kREDRAW_UIVIEW_GL)) {
[self drawInGL];
return true;
}
return false;
}
- (void)postInvalWithRect:(const SkIRect*)r {
#ifdef USE_GL
#if 1
if (!fRedrawRequestPending) {
fRedrawRequestPending = true;
/*
performSelectorOnMainThread seems to starve updating other views
(e.g. our FPS view in the titlebar), so we use the afterDelay
version
*/
if (0) {
[self performSelectorOnMainThread:@selector(drawInGL) withObject:nil waitUntilDone:NO];
} else {
[self performSelector:@selector(drawInGL) withObject:nil afterDelay:0];
}
}
#else
if (!fRedrawRequestPending) {
SkEvent* evt = new SkEvent(kREDRAW_UIVIEW_GL);
evt->post(fWind->getSinkID());
fRedrawRequestPending = true;
}
#endif
#else
if (r) {
[self setNeedsDisplayInRect:CGRectMake(r->fLeft, r->fTop,
r->width(), r->height())];
} else {
[self setNeedsDisplay];
}
#endif
}
@end