• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011, The Android Open Source Project
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *  * Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  *  * Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #define LOG_TAG "BaseRenderer"
27 #define LOG_NDEBUG 1
28 
29 #include "config.h"
30 #include "BaseRenderer.h"
31 
32 #if USE(ACCELERATED_COMPOSITING)
33 
34 #include "AndroidLog.h"
35 #include "GLUtils.h"
36 #include "InstrumentedPlatformCanvas.h"
37 #include "RasterRenderer.h"
38 #include "SkBitmap.h"
39 #include "SkBitmapRef.h"
40 #include "SkCanvas.h"
41 #include "SkDevice.h"
42 #include "SkPicture.h"
43 #include "SkTypeface.h"
44 #include "Tile.h"
45 #include "TilesManager.h"
46 
47 #include <wtf/text/CString.h>
48 
49 #define UPDATE_COUNT_MASK 0xFF // displayed count wraps at 256
50 #define UPDATE_COUNT_ALPHA_MASK 0x1F // alpha wraps at 32
51 
52 namespace WebCore {
53 
54 BaseRenderer::RendererType BaseRenderer::g_currentType = BaseRenderer::Raster;
55 
createRenderer()56 BaseRenderer* BaseRenderer::createRenderer()
57 {
58     if (g_currentType == Raster)
59         return new RasterRenderer();
60     return NULL;
61 }
62 
swapRendererIfNeeded(BaseRenderer * & renderer)63 void BaseRenderer::swapRendererIfNeeded(BaseRenderer*& renderer)
64 {
65     if (renderer->getType() == g_currentType)
66         return;
67 
68     delete renderer;
69     renderer = createRenderer();
70 }
71 
drawTileInfo(SkCanvas * canvas,const TileRenderInfo & renderInfo,int updateCount,double renderDuration)72 void BaseRenderer::drawTileInfo(SkCanvas* canvas,
73         const TileRenderInfo& renderInfo, int updateCount, double renderDuration)
74 {
75     static SkTypeface* s_typeface = 0;
76     if (!s_typeface)
77         s_typeface = SkTypeface::CreateFromName("", SkTypeface::kBold);
78     SkPaint paint;
79     paint.setTextSize(17);
80     char str[256];
81     snprintf(str, 256, " (%d,%d)   %.2fx   %d   %.1fms", renderInfo.x, renderInfo.y,
82             renderInfo.scale, updateCount, renderDuration);
83     paint.setARGB(128, 255, 255, 255);
84     canvas->drawRectCoords(0, 0, renderInfo.tileSize.fWidth, 17, paint);
85     paint.setARGB(255, 255, 0, 0);
86     paint.setTypeface(s_typeface);
87     canvas->drawText(str, strlen(str), 20, 15, paint);
88 }
89 
renderTiledContent(TileRenderInfo & renderInfo)90 void BaseRenderer::renderTiledContent(TileRenderInfo& renderInfo)
91 {
92     const bool visualIndicator = TilesManager::instance()->getShowVisualIndicator();
93     const SkSize& tileSize = renderInfo.tileSize;
94 
95     Color *background = renderInfo.tilePainter->background();
96     InstrumentedPlatformCanvas canvas(TilesManager::instance()->tileWidth(),
97                                       TilesManager::instance()->tileHeight(),
98                                       background ? *background : Color::transparent);
99     setupCanvas(renderInfo, &canvas);
100 
101     if (!canvas.getDevice()) {
102         // TODO: consider ALOGE
103         ALOGV("Error: No Device");
104         return;
105     }
106 
107     double before;
108     if (visualIndicator) {
109         canvas.save();
110         before = currentTimeMS();
111     }
112 
113     canvas.translate(-renderInfo.x * tileSize.width(), -renderInfo.y * tileSize.height());
114     canvas.scale(renderInfo.scale, renderInfo.scale);
115     renderInfo.tilePainter->paint(&canvas);
116 
117     checkForPureColor(renderInfo, canvas);
118 
119     if (visualIndicator) {
120         double after = currentTimeMS();
121         canvas.restore();
122         unsigned int updateCount = renderInfo.tilePainter->getUpdateCount() & UPDATE_COUNT_MASK;
123         const int color = updateCount & UPDATE_COUNT_ALPHA_MASK;
124 
125         // only color the invalidated area
126         SkPaint paint;
127         paint.setARGB(color, 0, 255, 0);
128         SkIRect rect;
129         rect.set(0, 0, tileSize.width(), tileSize.height());
130         canvas.drawIRect(rect, paint);
131 
132         drawTileInfo(&canvas, renderInfo, updateCount, after - before);
133 
134         // paint the tile boundaries
135         paint.setARGB(64, 255, 0, 0);
136         paint.setStrokeWidth(3);
137         canvas.drawLine(0, 0, tileSize.width(), tileSize.height(), paint);
138         paint.setARGB(64, 0, 255, 0);
139         canvas.drawLine(0, tileSize.height(), tileSize.width(), 0, paint);
140         paint.setARGB(128, 0, 0, 255);
141         canvas.drawLine(tileSize.width(), 0, tileSize.width(), tileSize.height(), paint);
142     }
143     renderingComplete(renderInfo, &canvas);
144 }
145 
checkForPureColor(TileRenderInfo & renderInfo,InstrumentedPlatformCanvas & canvas)146 void BaseRenderer::checkForPureColor(TileRenderInfo& renderInfo, InstrumentedPlatformCanvas& canvas)
147 {
148     renderInfo.isPureColor = canvas.isSolidColor();
149     renderInfo.pureColor = canvas.solidColor();
150     deviceCheckForPureColor(renderInfo, &canvas);
151 }
152 
153 } // namespace WebCore
154 
155 #endif // USE(ACCELERATED_COMPOSITING)
156