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