• 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 "GaneshRenderer.h"
36 #include "GLUtils.h"
37 #include "InstrumentedPlatformCanvas.h"
38 #include "RasterRenderer.h"
39 #include "SkBitmap.h"
40 #include "SkBitmapRef.h"
41 #include "SkCanvas.h"
42 #include "SkDevice.h"
43 #include "SkPicture.h"
44 #include "SkTypeface.h"
45 #include "Tile.h"
46 #include "TilesManager.h"
47 
48 #include <wtf/text/CString.h>
49 
50 #define UPDATE_COUNT_MASK 0xFF // displayed count wraps at 256
51 #define UPDATE_COUNT_ALPHA_MASK 0x1F // alpha wraps at 32
52 
53 namespace WebCore {
54 
55 BaseRenderer::RendererType BaseRenderer::g_currentType = BaseRenderer::Raster;
56 
createRenderer()57 BaseRenderer* BaseRenderer::createRenderer()
58 {
59     if (g_currentType == Raster)
60         return new RasterRenderer();
61     else if (g_currentType == Ganesh)
62         return new GaneshRenderer();
63     return NULL;
64 }
65 
swapRendererIfNeeded(BaseRenderer * & renderer)66 void BaseRenderer::swapRendererIfNeeded(BaseRenderer*& renderer)
67 {
68     if (renderer->getType() == g_currentType)
69         return;
70 
71     delete renderer;
72     renderer = createRenderer();
73 }
74 
drawTileInfo(SkCanvas * canvas,const TileRenderInfo & renderInfo,int updateCount,double renderDuration)75 void BaseRenderer::drawTileInfo(SkCanvas* canvas,
76         const TileRenderInfo& renderInfo, int updateCount, double renderDuration)
77 {
78     static SkTypeface* s_typeface = 0;
79     if (!s_typeface)
80         s_typeface = SkTypeface::CreateFromName("", SkTypeface::kBold);
81     SkPaint paint;
82     paint.setTextSize(17);
83     char str[256];
84     snprintf(str, 256, " (%d,%d)   %.2fx   %d   %.1fms", renderInfo.x, renderInfo.y,
85             renderInfo.scale, updateCount, renderDuration);
86     paint.setARGB(128, 255, 255, 255);
87     canvas->drawRectCoords(0, 0, renderInfo.tileSize.fWidth, 17, paint);
88     paint.setARGB(255, 255, 0, 0);
89     paint.setTypeface(s_typeface);
90     canvas->drawText(str, strlen(str), 20, 15, paint);
91 }
92 
renderTiledContent(TileRenderInfo & renderInfo)93 void BaseRenderer::renderTiledContent(TileRenderInfo& renderInfo)
94 {
95     const bool visualIndicator = TilesManager::instance()->getShowVisualIndicator();
96     const SkSize& tileSize = renderInfo.tileSize;
97 
98     Color *background = renderInfo.tilePainter->background();
99     InstrumentedPlatformCanvas canvas(TilesManager::instance()->tileWidth(),
100                                       TilesManager::instance()->tileHeight(),
101                                       background ? *background : Color::transparent);
102     setupCanvas(renderInfo, &canvas);
103 
104     if (!canvas.getDevice()) {
105         // TODO: consider ALOGE
106         ALOGV("Error: No Device");
107         return;
108     }
109 
110     double before;
111     if (visualIndicator) {
112         canvas.save();
113         before = currentTimeMS();
114     }
115 
116     canvas.translate(-renderInfo.x * tileSize.width(), -renderInfo.y * tileSize.height());
117     canvas.scale(renderInfo.scale, renderInfo.scale);
118     renderInfo.tilePainter->paint(&canvas);
119 
120     checkForPureColor(renderInfo, canvas);
121 
122     if (visualIndicator) {
123         double after = currentTimeMS();
124         canvas.restore();
125         unsigned int updateCount = renderInfo.tilePainter->getUpdateCount() & UPDATE_COUNT_MASK;
126         const int color = updateCount & UPDATE_COUNT_ALPHA_MASK;
127 
128         // only color the invalidated area
129         SkPaint paint;
130         paint.setARGB(color, 0, 255, 0);
131         SkIRect rect;
132         rect.set(0, 0, tileSize.width(), tileSize.height());
133         canvas.drawIRect(rect, paint);
134 
135         drawTileInfo(&canvas, renderInfo, updateCount, after - before);
136 
137         // paint the tile boundaries
138         paint.setARGB(64, 255, 0, 0);
139         paint.setStrokeWidth(3);
140         canvas.drawLine(0, 0, tileSize.width(), tileSize.height(), paint);
141         paint.setARGB(64, 0, 255, 0);
142         canvas.drawLine(0, tileSize.height(), tileSize.width(), 0, paint);
143         paint.setARGB(128, 0, 0, 255);
144         canvas.drawLine(tileSize.width(), 0, tileSize.width(), tileSize.height(), paint);
145     }
146     renderingComplete(renderInfo, &canvas);
147 }
148 
checkForPureColor(TileRenderInfo & renderInfo,InstrumentedPlatformCanvas & canvas)149 void BaseRenderer::checkForPureColor(TileRenderInfo& renderInfo, InstrumentedPlatformCanvas& canvas)
150 {
151     renderInfo.isPureColor = canvas.isSolidColor();
152     renderInfo.pureColor = canvas.solidColor();
153     deviceCheckForPureColor(renderInfo, &canvas);
154 }
155 
156 } // namespace WebCore
157 
158 #endif // USE(ACCELERATED_COMPOSITING)
159