Home | History | Annotate | Download | only in android
      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 
     27 #include "config.h"
     28 #include "BaseRenderer.h"
     29 
     30 #if USE(ACCELERATED_COMPOSITING)
     31 
     32 #include "GaneshRenderer.h"
     33 #include "GLUtils.h"
     34 #include "RasterRenderer.h"
     35 #include "SkBitmap.h"
     36 #include "SkBitmapRef.h"
     37 #include "SkCanvas.h"
     38 #include "SkDevice.h"
     39 #include "SkPicture.h"
     40 #include "TilesManager.h"
     41 
     42 #include <wtf/text/CString.h>
     43 
     44 #ifdef DEBUG
     45 
     46 #include <cutils/log.h>
     47 #include <wtf/CurrentTime.h>
     48 
     49 #undef XLOG
     50 #define XLOG(...) android_printLog(ANDROID_LOG_DEBUG, "BaseRenderer", __VA_ARGS__)
     51 
     52 #else
     53 
     54 #undef XLOG
     55 #define XLOG(...)
     56 
     57 #endif // DEBUG
     58 
     59 namespace WebCore {
     60 
     61 BaseRenderer::RendererType BaseRenderer::g_currentType = BaseRenderer::Raster;
     62 
     63 BaseRenderer* BaseRenderer::createRenderer()
     64 {
     65     if (g_currentType == Raster)
     66         return new RasterRenderer();
     67     else if (g_currentType == Ganesh)
     68         return new GaneshRenderer();
     69     return NULL;
     70 }
     71 
     72 void BaseRenderer::swapRendererIfNeeded(BaseRenderer*& renderer)
     73 {
     74     if (renderer->getType() == g_currentType)
     75         return;
     76 
     77     delete renderer;
     78     renderer = createRenderer();
     79 }
     80 
     81 void BaseRenderer::drawTileInfo(SkCanvas* canvas,
     82         const TileRenderInfo& renderInfo, int pictureCount)
     83 {
     84     SkPaint paint;
     85     char str[256];
     86     snprintf(str, 256, "(%d,%d) %.2f, tl%x p%x c%d", renderInfo.x, renderInfo.y,
     87             renderInfo.scale, this, renderInfo.tilePainter, pictureCount);
     88     paint.setARGB(255, 0, 0, 0);
     89     canvas->drawText(str, strlen(str), 0, 10, paint);
     90     paint.setARGB(255, 255, 0, 0);
     91     canvas->drawText(str, strlen(str), 0, 11, paint);
     92 
     93     int tagCount = 0;
     94     const String* tags = getPerformanceTags(tagCount);
     95 
     96     float total = 0;
     97     for (int i = 0; i < tagCount; i++) {
     98         float tagDuration = m_perfMon.getAverageDuration(tags[i]);
     99         total += tagDuration;
    100         snprintf(str, 256, "%s: %.2f", tags[i].utf8().data(), tagDuration);
    101         paint.setARGB(255, 0, 0, 0);
    102         int textY = (i * 12) + 25;
    103         canvas->drawText(str, strlen(str), 0, textY, paint);
    104         paint.setARGB(255, 255, 0, 0);
    105         canvas->drawText(str, strlen(str), 0, textY + 1, paint);
    106     }
    107     snprintf(str, 256, "total: %.2f", total);
    108     paint.setARGB(255, 0, 0, 0);
    109     int textY = (tagCount * 12) + 30;
    110     canvas->drawText(str, strlen(str), 0, textY, paint);
    111     paint.setARGB(255, 255, 0, 0);
    112     canvas->drawText(str, strlen(str), 0, textY + 1, paint);
    113 }
    114 
    115 int BaseRenderer::renderTiledContent(const TileRenderInfo& renderInfo)
    116 {
    117     const bool visualIndicator = TilesManager::instance()->getShowVisualIndicator();
    118     const SkSize& tileSize = renderInfo.tileSize;
    119 
    120     SkCanvas canvas;
    121     setupCanvas(renderInfo, &canvas);
    122 
    123     if (!canvas.getDevice()) {
    124         XLOG("Error: No Device");
    125         return 0;
    126     }
    127 
    128     if (visualIndicator)
    129         canvas.save();
    130 
    131     setupPartialInval(renderInfo, &canvas);
    132     canvas.translate(-renderInfo.x * tileSize.width(), -renderInfo.y * tileSize.height());
    133     canvas.scale(renderInfo.scale, renderInfo.scale);
    134     unsigned int pictureCount = 0;
    135     renderInfo.tilePainter->paint(renderInfo.baseTile, &canvas, &pictureCount);
    136 
    137     if (visualIndicator) {
    138         canvas.restore();
    139         const int color = 20 + (pictureCount % 100);
    140 
    141         // only color the invalidated area
    142         SkPaint invalPaint;
    143         invalPaint.setARGB(color, 0, 255, 0);
    144         canvas.drawIRect(*renderInfo.invalRect, invalPaint);
    145 
    146         // paint the tile boundaries
    147         SkPaint paint;
    148         paint.setARGB(128, 255, 0, 0);
    149         paint.setStrokeWidth(3);
    150         canvas.drawLine(0, 0, tileSize.width(), tileSize.height(), paint);
    151         paint.setARGB(128, 0, 255, 0);
    152         canvas.drawLine(0, tileSize.height(), tileSize.width(), 0, paint);
    153         paint.setARGB(128, 0, 0, 255);
    154         canvas.drawLine(0, 0, tileSize.width(), 0, paint);
    155         canvas.drawLine(tileSize.width(), 0, tileSize.width(), tileSize.height(), paint);
    156 
    157         if (renderInfo.measurePerf)
    158             drawTileInfo(&canvas, renderInfo, pictureCount);
    159     }
    160     renderInfo.textureInfo->m_pictureCount = pictureCount;
    161     renderingComplete(renderInfo, &canvas);
    162     return pictureCount;
    163 }
    164 
    165 } // namespace WebCore
    166 
    167 #endif // USE(ACCELERATED_COMPOSITING)
    168