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