Home | History | Annotate | Download | only in hwui
      1 /*
      2  * Copyright (C) 2013 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 ANDROID_HWUI_RENDERER_H
     18 #define ANDROID_HWUI_RENDERER_H
     19 
     20 #include <SkRegion.h>
     21 
     22 #include <utils/String8.h>
     23 
     24 #include "AssetAtlas.h"
     25 #include "SkPaint.h"
     26 
     27 namespace android {
     28 
     29 class Functor;
     30 struct Res_png_9patch;
     31 
     32 namespace uirenderer {
     33 
     34 class RenderNode;
     35 class Layer;
     36 class Matrix4;
     37 class SkiaColorFilter;
     38 class Patch;
     39 
     40 enum DrawOpMode {
     41     kDrawOpMode_Immediate,
     42     kDrawOpMode_Defer,
     43     kDrawOpMode_Flush
     44 };
     45 
     46 /**
     47  * Hwui's abstract version of Canvas.
     48  *
     49  * Provides methods for frame state operations, as well as the SkCanvas style transform/clip state,
     50  * and varied drawing operations.
     51  *
     52  * Should at some point interact with native SkCanvas.
     53  */
     54 class ANDROID_API Renderer {
     55 public:
     56     virtual ~Renderer() {}
     57 
     58     /**
     59      * Safely retrieves the mode from the specified xfermode. If the specified
     60      * xfermode is null, the mode is assumed to be SkXfermode::kSrcOver_Mode.
     61      */
     62     static inline SkXfermode::Mode getXfermode(SkXfermode* mode) {
     63         SkXfermode::Mode resultMode;
     64         if (!SkXfermode::AsMode(mode, &resultMode)) {
     65             resultMode = SkXfermode::kSrcOver_Mode;
     66         }
     67         return resultMode;
     68     }
     69 
     70     // TODO: move to a method on android:Paint
     71     static inline bool paintWillNotDraw(const SkPaint& paint) {
     72         return paint.getAlpha() == 0
     73                 && !paint.getColorFilter()
     74                 && getXfermode(paint.getXfermode()) == SkXfermode::kSrcOver_Mode;
     75     }
     76 
     77     // TODO: move to a method on android:Paint
     78     static inline bool paintWillNotDrawText(const SkPaint& paint) {
     79         return paint.getAlpha() == 0
     80                 && paint.getLooper() == NULL
     81                 && !paint.getColorFilter()
     82                 && getXfermode(paint.getXfermode()) == SkXfermode::kSrcOver_Mode;
     83     }
     84 // ----------------------------------------------------------------------------
     85 // Frame state operations
     86 // ----------------------------------------------------------------------------
     87     /**
     88      * Sets the dimension of the underlying drawing surface. This method must
     89      * be called at least once every time the drawing surface changes size.
     90      *
     91      * @param width The width in pixels of the underlysing surface
     92      * @param height The height in pixels of the underlysing surface
     93      */
     94     virtual void setViewport(int width, int height) = 0;
     95 
     96     /**
     97      * Prepares the renderer to draw a frame. This method must be invoked
     98      * at the beginning of each frame. When this method is invoked, the
     99      * entire drawing surface is assumed to be redrawn.
    100      *
    101      * @param opaque If true, the target surface is considered opaque
    102      *               and will not be cleared. If false, the target surface
    103      *               will be cleared
    104      */
    105     virtual status_t prepare(bool opaque) = 0;
    106 
    107     /**
    108      * Prepares the renderer to draw a frame. This method must be invoked
    109      * at the beginning of each frame. Only the specified rectangle of the
    110      * frame is assumed to be dirty. A clip will automatically be set to
    111      * the specified rectangle.
    112      *
    113      * @param left The left coordinate of the dirty rectangle
    114      * @param top The top coordinate of the dirty rectangle
    115      * @param right The right coordinate of the dirty rectangle
    116      * @param bottom The bottom coordinate of the dirty rectangle
    117      * @param opaque If true, the target surface is considered opaque
    118      *               and will not be cleared. If false, the target surface
    119      *               will be cleared in the specified dirty rectangle
    120      */
    121     virtual status_t prepareDirty(float left, float top, float right, float bottom,
    122             bool opaque) = 0;
    123 
    124     /**
    125      * Indicates the end of a frame. This method must be invoked whenever
    126      * the caller is done rendering a frame.
    127      */
    128     virtual void finish() = 0;
    129 
    130 // ----------------------------------------------------------------------------
    131 // Canvas state operations
    132 // ----------------------------------------------------------------------------
    133     // Save (layer)
    134     virtual int getSaveCount() const = 0;
    135     virtual int save(int flags) = 0;
    136     virtual void restore() = 0;
    137     virtual void restoreToCount(int saveCount) = 0;
    138 
    139     virtual int saveLayer(float left, float top, float right, float bottom,
    140             const SkPaint* paint, int flags) = 0;
    141 
    142     int saveLayerAlpha(float left, float top, float right, float bottom,
    143             int alpha, int flags) {
    144         SkPaint paint;
    145         paint.setAlpha(alpha);
    146         return saveLayer(left, top, right, bottom, &paint, flags);
    147     }
    148 
    149     // Matrix
    150     virtual void getMatrix(SkMatrix* outMatrix) const = 0;
    151     virtual void translate(float dx, float dy, float dz = 0.0f) = 0;
    152     virtual void rotate(float degrees) = 0;
    153     virtual void scale(float sx, float sy) = 0;
    154     virtual void skew(float sx, float sy) = 0;
    155 
    156     virtual void setMatrix(const SkMatrix& matrix) = 0;
    157     virtual void concatMatrix(const SkMatrix& matrix) = 0;
    158 
    159     // clip
    160     virtual const Rect& getLocalClipBounds() const = 0;
    161     virtual bool quickRejectConservative(float left, float top,
    162             float right, float bottom) const = 0;
    163     virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op) = 0;
    164     virtual bool clipPath(const SkPath* path, SkRegion::Op op) = 0;
    165     virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) = 0;
    166 
    167     // Misc - should be implemented with SkPaint inspection
    168     virtual void resetPaintFilter() = 0;
    169     virtual void setupPaintFilter(int clearBits, int setBits) = 0;
    170 
    171 // ----------------------------------------------------------------------------
    172 // Canvas draw operations
    173 // ----------------------------------------------------------------------------
    174     virtual status_t drawColor(int color, SkXfermode::Mode mode) = 0;
    175 
    176     // Bitmap-based
    177     virtual status_t drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) = 0;
    178     virtual status_t drawBitmap(const SkBitmap* bitmap, float srcLeft, float srcTop,
    179             float srcRight, float srcBottom, float dstLeft, float dstTop,
    180             float dstRight, float dstBottom, const SkPaint* paint) = 0;
    181     virtual status_t drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint) = 0;
    182     virtual status_t drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
    183             const float* vertices, const int* colors, const SkPaint* paint) = 0;
    184     virtual status_t drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
    185             float left, float top, float right, float bottom, const SkPaint* paint) = 0;
    186 
    187     // Shapes
    188     virtual status_t drawRect(float left, float top, float right, float bottom,
    189             const SkPaint* paint) = 0;
    190     virtual status_t drawRects(const float* rects, int count, const SkPaint* paint) = 0;
    191     virtual status_t drawRoundRect(float left, float top, float right, float bottom,
    192             float rx, float ry, const SkPaint* paint) = 0;
    193     virtual status_t drawCircle(float x, float y, float radius, const SkPaint* paint) = 0;
    194     virtual status_t drawOval(float left, float top, float right, float bottom,
    195             const SkPaint* paint) = 0;
    196     virtual status_t drawArc(float left, float top, float right, float bottom,
    197             float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) = 0;
    198     virtual status_t drawPath(const SkPath* path, const SkPaint* paint) = 0;
    199     virtual status_t drawLines(const float* points, int count, const SkPaint* paint) = 0;
    200     virtual status_t drawPoints(const float* points, int count, const SkPaint* paint) = 0;
    201 
    202     // Text
    203     virtual status_t drawText(const char* text, int bytesCount, int count, float x, float y,
    204             const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
    205             DrawOpMode drawOpMode = kDrawOpMode_Immediate) = 0;
    206     virtual status_t drawTextOnPath(const char* text, int bytesCount, int count, const SkPath* path,
    207             float hOffset, float vOffset, const SkPaint* paint) = 0;
    208     virtual status_t drawPosText(const char* text, int bytesCount, int count,
    209             const float* positions, const SkPaint* paint) = 0;
    210 
    211 // ----------------------------------------------------------------------------
    212 // Canvas draw operations - special
    213 // ----------------------------------------------------------------------------
    214     virtual status_t drawLayer(Layer* layer, float x, float y) = 0;
    215     virtual status_t drawRenderNode(RenderNode* renderNode, Rect& dirty,
    216             int32_t replayFlags) = 0;
    217 
    218     // TODO: rename for consistency
    219     virtual status_t callDrawGLFunction(Functor* functor, Rect& dirty) = 0;
    220 }; // class Renderer
    221 
    222 }; // namespace uirenderer
    223 }; // namespace android
    224 
    225 #endif // ANDROID_HWUI_RENDERER_H
    226