SkDevice.h revision 8d84fac294682647694b0d2d8a87ac2bd19b6aab
18a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Copyright (C) 2008 The Android Open Source Project
38a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Licensed under the Apache License, Version 2.0 (the "License");
58a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * you may not use this file except in compliance with the License.
68a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * You may obtain a copy of the License at
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *      http://www.apache.org/licenses/LICENSE-2.0
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Unless required by applicable law or agreed to in writing, software
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * distributed under the License is distributed on an "AS IS" BASIS,
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * See the License for the specific language governing permissions and
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * limitations under the License.
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkDevice_DEFINED
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkDevice_DEFINED
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRefCnt.h"
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBitmap.h"
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h"
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColor.h"
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
258d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.orgclass SkDevice;
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkDraw;
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstruct SkIRect;
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkMatrix;
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkRegion;
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
318d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org/** \class SkDeviceFactory
328d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org
338d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    Devices that extend SkDevice should also provide a SkDeviceFactory class
348d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    to pass into SkCanvas.  Doing so will eliminate the need to extend
358d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    SkCanvas as well.
368d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org*/
378d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.orgclass SkDeviceFactory : public SkRefCnt {
388d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.orgpublic:
398d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    virtual SkDevice* newDevice(SkBitmap::Config config, int width, int height,
408d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org                                bool isOpaque, bool isForLayer) = 0;
418d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org
428d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    enum Capabilities {
438d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org        kGL_Capability    = 0x1,  //!< mask to indicate this device supports GL
448d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org        kAll_Capabilities = 0x1
458d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    };
468d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    virtual uint32_t getDeviceCapabilities() = 0;
478d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org};
488d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org
498d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.orgclass SkRasterDeviceFactory : public SkDeviceFactory {
508d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.orgpublic:
518d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    virtual SkDevice* newDevice(SkBitmap::Config config, int width, int height,
528d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org                                bool isOpaque, bool isForLayer);
538d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    virtual uint32_t getDeviceCapabilities() { return 0; }
548d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org};
558d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkDevice : public SkRefCnt {
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDevice();
598d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    /** Construct a new device, extracting the width/height/config/isOpaque
608d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org        values from the bitmap.  Subclasses may override the destructor, which
618d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org        is virtual, even though this class doesn't have one. SkRefCnt does.
628d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param bitmap   A copy of this bitmap is made and stored in the device
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDevice(const SkBitmap& bitmap);
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
678d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    virtual SkDeviceFactory* getDeviceFactory() {
688d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org        return SkNEW(SkRasterDeviceFactory);
698d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org    }
708d84fac294682647694b0d2d8a87ac2bd19b6aabvandebo@chromium.org
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the width of the device (in pixels).
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int width() const { return fBitmap.width(); }
748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the height of the device (in pixels).
758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int height() const { return fBitmap.height(); }
778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the bitmap config of the device's pixels
788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBitmap::Config config() const { return fBitmap.getConfig(); }
808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns true if the device's bitmap's config treats every pixels as
818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        implicitly opaque.
828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool isOpaque() const { return fBitmap.isOpaque(); }
848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the bounds of the device
868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void getBounds(SkIRect* bounds) const;
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return true if the specified rectangle intersects the bounds of the
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        device. If sect is not NULL and there is an intersection, sect returns
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        the intersection.
928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool intersects(const SkIRect& r, SkIRect* sect = NULL) const;
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the bitmap associated with this device. Call this each time you need
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        to access the bitmap, as it notifies the subclass to perform any flushing
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        etc. before you examine the pixels.
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @param changePixels set to true if the caller plans to change the pixels
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        @return the device's bitmap
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkBitmap& accessBitmap(bool changePixels);
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Helper to erase the entire device to the specified color (including
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        alpha).
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void eraseColor(SkColor eraseColor);
1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Called when this device is installed into a Canvas. Balanaced by a call
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        to unlockPixels() when the device is removed from a Canvas.
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void lockPixels();
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void unlockPixels();
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Called with the correct matrix and clip before this device is drawn
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        to using those settings. If your subclass overrides this, be sure to
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        call through to the base class as well.
1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void setMatrixClip(const SkMatrix&, const SkRegion&);
1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Called when this device gains focus (i.e becomes the current device
1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        for drawing).
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void gainFocus(SkCanvas*) {}
1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** These are called inside the per-device-layer loop for each draw call.
1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     When these are called, we have already applied any saveLayer operations,
1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     and are handling any looping from the paint, and any effects from the
1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     DrawFilter.
1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawPaint(const SkDraw&, const SkPaint& paint);
1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            const SkPoint[], const SkPaint& paint);
1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawRect(const SkDraw&, const SkRect& r,
1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const SkPaint& paint);
1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawPath(const SkDraw&, const SkPath& path,
1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          const SkPaint& paint);
1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            const SkMatrix& matrix, const SkPaint& paint);
1398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            int x, int y, const SkPaint& paint);
1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawText(const SkDraw&, const void* text, size_t len,
1428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                          SkScalar x, SkScalar y, const SkPaint& paint);
1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawPosText(const SkDraw&, const void* text, size_t len,
1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                             const SkScalar pos[], SkScalar constY,
1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                             int scalarsPerPos, const SkPaint& paint);
1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                const SkPath& path, const SkMatrix* matrix,
1488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                const SkPaint& paint);
1498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkPoint verts[], const SkPoint texs[],
1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkColor colors[], SkXfermode* xmode,
1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const uint16_t indices[], int indexCount,
1538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                              const SkPaint& paint);
1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                            const SkPaint&);
1568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected:
1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Update as needed the pixel value in the bitmap, so that the caller can access
1598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        the pixels directly. Note: only the pixels field should be altered. The config/width/height/rowbytes
1608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        must remain unchanged.
1618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void onAccessBitmap(SkBitmap*);
1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBitmap fBitmap;
1668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
169