retool so we don't need to call SkCanvas::setDevice

Review URL: https://codereview.appspot.com/6591054

git-svn-id: http://skia.googlecode.com/svn/trunk@5759 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
reed@google.com 2012-10-01 20:31:56 +00:00
parent b4773b4802
commit 5957f47e8d
6 changed files with 47 additions and 32 deletions

View File

@ -117,32 +117,35 @@ public:
win->detach(); win->detach();
fBackend = SampleWindow::kNone_BackEndType; fBackend = SampleWindow::kNone_BackEndType;
} }
virtual bool prepareCanvas(SampleWindow::DeviceType dType, virtual SkCanvas* createCanvas(SampleWindow::DeviceType dType,
SkCanvas* canvas, SampleWindow* win) {
SampleWindow* win) SK_OVERRIDE {
switch (dType) { switch (dType) {
// use the window's bmp for these two
case SampleWindow::kRaster_DeviceType: case SampleWindow::kRaster_DeviceType:
// fallthrough
case SampleWindow::kPicture_DeviceType: case SampleWindow::kPicture_DeviceType:
canvas->setDevice(SkNEW_ARGS(SkDevice, (win->getBitmap())))->unref(); // fallthrough
#if SK_ANGLE
case SampleWindow::kANGLE_DeviceType:
#endif
break; break;
#if SK_SUPPORT_GPU #if SK_SUPPORT_GPU
// create a GPU device for these two
case SampleWindow::kGPU_DeviceType: case SampleWindow::kGPU_DeviceType:
case SampleWindow::kNullGPU_DeviceType: case SampleWindow::kNullGPU_DeviceType:
if (NULL != fCurContext) { if (fCurContext) {
canvas->setDevice(new SkGpuDevice(fCurContext, fCurRenderTarget))->unref(); SkAutoTUnref<SkDevice> device(new SkGpuDevice(fCurContext,
fCurRenderTarget));
return new SkCanvas(device);
} else { } else {
return false; return NULL;
} }
break; break;
#endif #endif
default: default:
SkASSERT(false); SkASSERT(false);
return false; return NULL;
} }
return true; return NULL;
} }
virtual void publishCanvas(SampleWindow::DeviceType dType, virtual void publishCanvas(SampleWindow::DeviceType dType,
@ -428,13 +431,13 @@ static FPSState gFPS;
glViewport(0, 0, fGL.fWidth, fGL.fHeight); glViewport(0, 0, fGL.fWidth, fGL.fHeight);
SkCanvas canvas; SkAutoTUnref<SkCanvas> canvas(fWind->createCanvas());
// if we're not "retained", then we have to always redraw everything. // if we're not "retained", then we have to always redraw everything.
// This call forces us to ignore the fDirtyRgn, and draw everywhere. // 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) // If we are "retained", we can skip this call (as the raster case does)
fWind->forceInvalAll(); fWind->forceInvalAll();
[self drawWithCanvas:&canvas]; [self drawWithCanvas:canvas];
// This application only creates a single color renderbuffer which is already bound at this point. // 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. // This call is redundant, but needed if dealing with multiple renderbuffers.
@ -444,8 +447,8 @@ static FPSState gFPS;
} }
- (void)drawInRaster { - (void)drawInRaster {
SkCanvas canvas; SkAutoTUnref<SkCanvas> canvas(fWind->createCanvas());
[self drawWithCanvas:&canvas]; [self drawWithCanvas:canvas];
CGImageRef cgimage = SkCreateCGImageRef(fWind->getBitmap()); CGImageRef cgimage = SkCreateCGImageRef(fWind->getBitmap());
fRasterLayer.contents = (id)cgimage; fRasterLayer.contents = (id)cgimage;
CGImageRelease(cgimage); CGImageRelease(cgimage);

View File

@ -61,6 +61,8 @@ public:
void preConcat(const SkMatrix&); void preConcat(const SkMatrix&);
void postConcat(const SkMatrix&); void postConcat(const SkMatrix&);
virtual SkCanvas* createCanvas();
virtual void onPDFSaved(const char title[], const char desc[], virtual void onPDFSaved(const char title[], const char desc[],
const char path[]) {} const char path[]) {}
protected: protected:

View File

@ -257,9 +257,8 @@ public:
fBackend = kNone_BackEndType; fBackend = kNone_BackEndType;
} }
virtual bool prepareCanvas(SampleWindow::DeviceType dType, virtual SkCanvas* createCanvas(SampleWindow::DeviceType dType,
SkCanvas* canvas, SampleWindow* win) {
SampleWindow* win) {
switch (dType) { switch (dType) {
case kRaster_DeviceType: case kRaster_DeviceType:
// fallthrough // fallthrough
@ -273,18 +272,19 @@ public:
case kGPU_DeviceType: case kGPU_DeviceType:
case kNullGPU_DeviceType: case kNullGPU_DeviceType:
if (fCurContext) { if (fCurContext) {
canvas->setDevice(new SkGpuDevice(fCurContext, SkAutoTUnref<SkDevice> device(new SkGpuDevice(fCurContext,
fCurRenderTarget))->unref(); fCurRenderTarget));
return new SkCanvas(device);
} else { } else {
return false; return NULL;
} }
break; break;
#endif #endif
default: default:
SkASSERT(false); SkASSERT(false);
return false; return NULL;
} }
return true; return NULL;
} }
virtual void publishCanvas(SampleWindow::DeviceType dType, virtual void publishCanvas(SampleWindow::DeviceType dType,
@ -1035,9 +1035,6 @@ static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar
#define YCLIP_N 8 #define YCLIP_N 8
void SampleWindow::draw(SkCanvas* canvas) { void SampleWindow::draw(SkCanvas* canvas) {
if (!fDevManager->prepareCanvas(fDeviceType, canvas, this)) {
return;
}
// update the animation time // update the animation time
if (!gAnimTimePrev && !gAnimTime) { if (!gAnimTimePrev && !gAnimTime) {
// first time make delta be 0 // first time make delta be 0

View File

@ -58,9 +58,7 @@ public:
// called before drawing. should install correct device // called before drawing. should install correct device
// type on the canvas. Will skip drawing if returns false. // type on the canvas. Will skip drawing if returns false.
virtual bool prepareCanvas(DeviceType dType, virtual SkCanvas* createCanvas(DeviceType dType, SampleWindow* win) = 0;
SkCanvas* canvas,
SampleWindow* win) = 0;
// called after drawing, should get the results onto the // called after drawing, should get the results onto the
// screen. // screen.
@ -84,6 +82,17 @@ public:
SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*); SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
virtual ~SampleWindow(); virtual ~SampleWindow();
virtual SkCanvas* createCanvas() SK_OVERRIDE {
SkCanvas* canvas = NULL;
if (fDevManager) {
canvas = fDevManager->createCanvas(fDeviceType, this);
}
if (NULL == canvas) {
canvas = this->INHERITED::createCanvas();
}
return canvas;
}
virtual void draw(SkCanvas* canvas); virtual void draw(SkCanvas* canvas);
void setDeviceType(DeviceType type); void setDeviceType(DeviceType type);

View File

@ -66,6 +66,10 @@ SkWindow::~SkWindow()
fMenus.deleteAll(); fMenus.deleteAll();
} }
SkCanvas* SkWindow::createCanvas() {
return new SkCanvas(this->getBitmap());
}
void SkWindow::setMatrix(const SkMatrix& matrix) { void SkWindow::setMatrix(const SkMatrix& matrix) {
if (fMatrix != matrix) { if (fMatrix != matrix) {
fMatrix = matrix; fMatrix = matrix;

View File

@ -81,8 +81,8 @@ SK_COMPILE_ASSERT(SK_SUPPORT_GPU, not_implemented_for_non_gpu_build);
- (void)drawSkia { - (void)drawSkia {
fRedrawRequestPending = false; fRedrawRequestPending = false;
if (NULL != fWind) { if (NULL != fWind) {
SkCanvas canvas(fWind->getBitmap()); SkAutoTUnref<SkCanvas> canvas(fWind->createCanvas());
fWind->draw(&canvas); fWind->draw(canvas);
#ifdef FORCE_REDRAW #ifdef FORCE_REDRAW
fWind->inval(NULL); fWind->inval(NULL);
#endif #endif