Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef SkDevice_DEFINED
     18 #define SkDevice_DEFINED
     19 
     20 #include "SkRefCnt.h"
     21 #include "SkBitmap.h"
     22 #include "SkCanvas.h"
     23 #include "SkColor.h"
     24 
     25 class SkDraw;
     26 struct SkIRect;
     27 class SkMatrix;
     28 class SkRegion;
     29 
     30 class SkDevice : public SkRefCnt {
     31 public:
     32     SkDevice();
     33     /** Construct a new device, extracting the width/height/config/isOpaque values from
     34         the bitmap. If transferPixelOwnership is true, and the bitmap claims to own its
     35         own pixels (getOwnsPixels() == true), then transfer this responsibility to the
     36         device, and call setOwnsPixels(false) on the bitmap.
     37 
     38         Subclasses may override the destructor, which is virtual, even though this class
     39         doesn't have one. SkRefCnt does.
     40 
     41         @param bitmap   A copy of this bitmap is made and stored in the device
     42     */
     43     SkDevice(const SkBitmap& bitmap);
     44 
     45     /** Return the width of the device (in pixels).
     46     */
     47     int width() const { return fBitmap.width(); }
     48     /** Return the height of the device (in pixels).
     49     */
     50     int height() const { return fBitmap.height(); }
     51     /** Return the bitmap config of the device's pixels
     52     */
     53     SkBitmap::Config config() const { return fBitmap.getConfig(); }
     54     /** Returns true if the device's bitmap's config treats every pixels as
     55         implicitly opaque.
     56     */
     57     bool isOpaque() const { return fBitmap.isOpaque(); }
     58 
     59     /** Return the bounds of the device
     60     */
     61     void getBounds(SkIRect* bounds) const;
     62 
     63     /** Return true if the specified rectangle intersects the bounds of the
     64         device. If sect is not NULL and there is an intersection, sect returns
     65         the intersection.
     66     */
     67     bool intersects(const SkIRect& r, SkIRect* sect = NULL) const;
     68 
     69     /** Return the bitmap associated with this device. Call this each time you need
     70         to access the bitmap, as it notifies the subclass to perform any flushing
     71         etc. before you examine the pixels.
     72         @param changePixels set to true if the caller plans to change the pixels
     73         @return the device's bitmap
     74     */
     75     const SkBitmap& accessBitmap(bool changePixels);
     76 
     77     /** Helper to erase the entire device to the specified color (including
     78         alpha).
     79     */
     80     void eraseColor(SkColor eraseColor);
     81 
     82     /** Called when this device is installed into a Canvas. Balanaced by a call
     83         to unlockPixels() when the device is removed from a Canvas.
     84     */
     85     virtual void lockPixels();
     86     virtual void unlockPixels();
     87 
     88     /** Called with the correct matrix and clip before this device is drawn
     89         to using those settings. If your subclass overrides this, be sure to
     90         call through to the base class as well.
     91     */
     92     virtual void setMatrixClip(const SkMatrix&, const SkRegion&);
     93 
     94     /** Called when this device gains focus (i.e becomes the current device
     95         for drawing).
     96     */
     97     virtual void gainFocus(SkCanvas*) {}
     98 
     99     /** These are called inside the per-device-layer loop for each draw call.
    100      When these are called, we have already applied any saveLayer operations,
    101      and are handling any looping from the paint, and any effects from the
    102      DrawFilter.
    103      */
    104     virtual void drawPaint(const SkDraw&, const SkPaint& paint);
    105     virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
    106                             const SkPoint[], const SkPaint& paint);
    107     virtual void drawRect(const SkDraw&, const SkRect& r,
    108                           const SkPaint& paint);
    109     virtual void drawPath(const SkDraw&, const SkPath& path,
    110                           const SkPaint& paint);
    111     virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
    112                             const SkMatrix& matrix, const SkPaint& paint);
    113     virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
    114                             int x, int y, const SkPaint& paint);
    115     virtual void drawText(const SkDraw&, const void* text, size_t len,
    116                           SkScalar x, SkScalar y, const SkPaint& paint);
    117     virtual void drawPosText(const SkDraw&, const void* text, size_t len,
    118                              const SkScalar pos[], SkScalar constY,
    119                              int scalarsPerPos, const SkPaint& paint);
    120     virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
    121                                 const SkPath& path, const SkMatrix* matrix,
    122                                 const SkPaint& paint);
    123     virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
    124                               const SkPoint verts[], const SkPoint texs[],
    125                               const SkColor colors[], SkXfermode* xmode,
    126                               const uint16_t indices[], int indexCount,
    127                               const SkPaint& paint);
    128     virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
    129                             const SkPaint&);
    130 
    131 protected:
    132     /** Update as needed the pixel value in the bitmap, so that the caller can access
    133         the pixels directly. Note: only the pixels field should be altered. The config/width/height/rowbytes
    134         must remain unchanged.
    135     */
    136     virtual void onAccessBitmap(SkBitmap*);
    137 
    138 private:
    139     SkBitmap fBitmap;
    140 };
    141 
    142 #endif
    143