expose gpu-device-factory
authorreed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Tue, 11 Jan 2011 18:59:23 +0000 (18:59 +0000)
committerreed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Tue, 11 Jan 2011 18:59:23 +0000 (18:59 +0000)
use that factory in gpucanvas, rather than overriding createDevice

note: I think we now don't need the canvas parameter in device-factory

git-svn-id: http://skia.googlecode.com/svn/trunk@684 2bbb7eff-a529-9590-31e7-b0007b416f81

include/gpu/SkGpuCanvas.h
include/gpu/SkGpuDevice.h
include/gpu/SkGpuDeviceFactory.h [new file with mode: 0644]
src/gpu/SkGpuCanvas.cpp
src/gpu/SkGpuDevice.cpp

index e8e6e7a..8194e12 100644 (file)
@@ -29,38 +29,26 @@ class GrContext;
 class SkGpuCanvas : public SkCanvas {
 public:
     /**
-     *  The GrContext object is reference counted. When passed to our 
-     *  constructor, its reference count is incremented. In our destructor, the 
+     *  The GrContext object is reference counted. When passed to our
+     *  constructor, its reference count is incremented. In our destructor, the
      *  GrGpu's reference count will be decremented.
      */
     explicit SkGpuCanvas(GrContext*);
     virtual ~SkGpuCanvas();
 
     /**
-     *  Return our GrContext instance
-     */
-    GrContext* context() const { return fContext; }
-
-    /**
      *  Override from SkCanvas. Returns true, and if not-null, sets size to
      *  be the width/height of our viewport.
      */
     virtual bool getViewport(SkIPoint* size) const;
 
-    /**
-     *  Override from SkCanvas. Returns a new device of the correct subclass,
-     *  as determined by the GrGpu passed to our constructor.
-     */
-    virtual SkDevice* createDevice(SkBitmap::Config, int width, int height,
-                                   bool isOpaque, bool isLayer);
-
 #if 0
     virtual int saveLayer(const SkRect* bounds, const SkPaint* paint,
                           SaveFlags flags = kARGB_ClipLayer_SaveFlag) {
         return this->save(flags);
     }
 #endif
-    
+
 private:
     GrContext* fContext;
 
index e42e997..141ed96 100644 (file)
@@ -23,7 +23,6 @@
 #include "SkRegion.h"
 
 struct SkDrawProcs;
-class SkGpuCanvas;
 struct GrSkDrawProcs;
 class GrTextContext;
 
@@ -33,9 +32,9 @@ class GrTextContext;
  */
 class SkGpuDevice : public SkDevice {
 public:
-    SkGpuDevice(SkGpuCanvas*, const SkBitmap& bitmap, bool isLayer);
+    SkGpuDevice(GrContext*, const SkBitmap& bitmap, bool isLayer);
     virtual ~SkGpuDevice();
+
     GrContext* context() const { return fContext; }
 
     /**
@@ -46,7 +45,7 @@ public:
      *  is returned.
      */
     intptr_t getLayerTextureHandle() const;
-    
+
     /**
      * Attaches the device to a rendering surface. This device will then render
      * to the surface.
@@ -70,7 +69,7 @@ public:
 
     virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap);
     virtual void writePixels(const SkBitmap& bitmap, int x, int y);
-    
+
     virtual void setMatrixClip(const SkMatrix& matrix, const SkRegion& clip);
 
     virtual void drawPaint(const SkDraw&, const SkPaint& paint);
@@ -101,10 +100,10 @@ public:
                               const SkPaint& paint);
     virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
                             const SkPaint&);
-    
+
     virtual void flush() { fContext->flush(false); }
-    
-    /** 
+
+    /**
      * Make's this device's rendertarget current in the underlying 3D API.
      * Also implicitly flushes.
      */
@@ -145,14 +144,14 @@ private:
     GrRenderTarget* fRenderTarget;
     bool            fNeedClear;
     bool            fNeedPrepareRenderTarget;
-    
+
     SkDrawProcs* initDrawForText(const SkPaint&, GrTextContext*);
     bool bindDeviceAsTexture(SkPoint* max);
 
     void prepareRenderTarget(const SkDraw&);
     void internalDrawBitmap(const SkDraw&, const SkBitmap&,
                             const SkIRect&, const SkMatrix&, const SkPaint&);
-        
+
     class AutoPaintShader {
     public:
         AutoPaintShader();
diff --git a/include/gpu/SkGpuDeviceFactory.h b/include/gpu/SkGpuDeviceFactory.h
new file mode 100644 (file)
index 0000000..dd57da2
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+    Copyright 2010 Google Inc.
+
+    Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+ */
+
+#ifndef SkGpuDeviceFactory_DEFINED
+#define SkGpuDeviceFactory_DEFINED
+
+#include "SkDevice.h"
+
+class GrContext;
+
+class SkGpuDeviceFactory : public SkDeviceFactory {
+public:
+    /**
+     *  The constructor will ref() the context, passing it to each device
+     *  that it creates. It will be unref()'d in the destructor
+     */
+    SkGpuDeviceFactory(GrContext*);
+
+    virtual ~SkGpuDeviceFactory();
+
+    virtual SkDevice* newDevice(SkCanvas*, SkBitmap::Config, int width,
+                                int height, bool isOpaque, bool isLayer);
+
+private:
+    GrContext* fContext;
+};
+
+#endif
+
index 19bda4d..5ca1736 100644 (file)
 
 #include "SkGpuCanvas.h"
 #include "SkGpuDevice.h"
+#include "SkGpuDeviceFactory.h"
 
 ///////////////////////////////////////////////////////////////////////////////
 
-SkGpuCanvas::SkGpuCanvas(GrContext* context) {
+static SkDeviceFactory* make_df(GrContext* context) {
+    return SkNEW_ARGS(SkGpuDeviceFactory, (context));
+}
+
+SkGpuCanvas::SkGpuCanvas(GrContext* context) : SkCanvas(make_df(context)) {
     SkASSERT(context);
     fContext = context;
     fContext->ref();
@@ -49,12 +54,3 @@ bool SkGpuCanvas::getViewport(SkIPoint* size) const {
     return true;
 }
 
-SkDevice* SkGpuCanvas::createDevice(SkBitmap::Config config, int width, int height,
-                                    bool isOpaque, bool isLayer) {
-    SkBitmap bm;
-    bm.setConfig(config, width, height);
-    bm.setIsOpaque(isOpaque);
-    return new SkGpuDevice(this, bm, isLayer);
-}
-
-
index 2d5634c..a4fb986 100644 (file)
@@ -18,8 +18,8 @@
 #include "GrContext.h"
 #include "GrTextContext.h"
 
-#include "SkGpuCanvas.h"
 #include "SkGpuDevice.h"
+#include "SkGpuDeviceFactory.h"
 #include "SkGrTexturePixelRef.h"
 
 #include "SkDrawProcs.h"
@@ -109,13 +109,14 @@ public:
 
 ///////////////////////////////////////////////////////////////////////////////
 
-SkGpuDevice::SkGpuDevice(SkGpuCanvas* canvas, const SkBitmap& bitmap, bool isLayer)
-        : SkDevice(canvas, bitmap, false) {
+SkGpuDevice::SkGpuDevice(GrContext* context, const SkBitmap& bitmap, bool isLayer)
+        : SkDevice(NULL, bitmap, false) {
 
     fNeedPrepareRenderTarget = false;
     fDrawProcs = NULL;
 
-    fContext = canvas->context();
+    // should I ref() this, and then unref in destructor? <mrr>
+    fContext = context;
 
     fCache = NULL;
     fTexture = NULL;
@@ -1046,4 +1047,22 @@ void SkGpuDevice::unlockCachedTexture(TexCache* cache) {
     this->context()->unlockTexture((GrTextureEntry*)cache);
 }
 
+///////////////////////////////////////////////////////////////////////////////
+
+SkGpuDeviceFactory::SkGpuDeviceFactory(GrContext* context) : fContext(context) {
+    context->ref();
+}
+
+SkGpuDeviceFactory::~SkGpuDeviceFactory() {
+    fContext->unref();
+}
+
+SkDevice* SkGpuDeviceFactory::newDevice(SkCanvas*, SkBitmap::Config config,
+                                        int width, int height,
+                                        bool isOpaque, bool isLayer) {
+    SkBitmap bm;
+    bm.setConfig(config, width, height);
+    bm.setIsOpaque(isOpaque);
+    return new SkGpuDevice(fContext, bm, isLayer);
+}