Home | History | Annotate | Download | only in rendering
      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 "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 
     56 BaseRenderer* BaseRenderer::createRenderer()
     57 {
     58     if (g_currentType == Raster)
     59         return new RasterRenderer();
     60     else if (g_currentType == Ganesh)
     61         return new GaneshRenderer();
     62     return NULL;
     63 }
     64 
     65 void BaseRenderer::swapRendererIfNeeded(BaseRenderer*& renderer)
     66 {
     67     if (renderer->getType() == g_currentType)
     68         return;
     69 
     70     delete renderer;
     71     renderer = createRenderer();
     72 }
     73 
     74 void BaseRenderer::drawTileInfo(SkCanvas* canvas,
     75         const TileRenderInfo& renderInfo, int updateCount, double renderDuration)
     76 {
     77     static SkTypeface* s_typeface = 0;
     78     if (!s_typeface)
     79         s_typeface = SkTypeface::CreateFromName("", SkTypeface::kBold);
     80     SkPaint paint;
     81     paint.setTextSize(17);
     82     char str[256];
     83     snprintf(str, 256, " (%d,%d)   %.2fx   %d   %.1fms", renderInfo.x, renderInfo.y,
     84             renderInfo.scale, updateCount, renderDuration);
     85     paint.setARGB(128, 255, 255, 255);
     86     canvas->drawRectCoords(0, 0, renderInfo.tileSize.fWidth, 17, paint);
     87     paint.setARGB(255, 255, 0, 0);
     88     paint.setTypeface(s_typeface);
     89     canvas->drawText(str, strlen(str), 20, 15, paint);
     90 }
     91 
     92 void BaseRenderer::renderTiledContent(TileRenderInfo& renderInfo)
     93 {
     94     const bool visualIndicator = TilesManager::instance()->getShowVisualIndicator();
     95     const SkSize& tileSize = renderInfo.tileSize;
     96 
     97     SkCanvas canvas;
     98     setupCanvas(renderInfo, &canvas);
     99 
    100     if (!canvas.getDevice()) {
    101         // TODO: consider ALOGE
    102         ALOGV("Error: No Device");
    103         return;
    104     }
    105 
    106     double before;
    107     if (visualIndicator) {
    108         canvas.save();
    109         before = currentTimeMS();
    110     }
    111 
    112     canvas.translate(-renderInfo.x * tileSize.width(), -renderInfo.y * tileSize.height());
    113     canvas.scale(renderInfo.scale, renderInfo.scale);
    114     renderInfo.tilePainter->paint(&canvas);
    115     if (renderInfo.baseTile && renderInfo.baseTile->backTexture())
    116         checkForPureColor(renderInfo, &canvas);
    117     else
    118         renderInfo.isPureColor = false;
    119 
    120     if (visualIndicator) {
    121         double after = currentTimeMS();
    122         canvas.restore();
    123         unsigned int updateCount = renderInfo.tilePainter->getUpdateCount() & UPDATE_COUNT_MASK;
    124         const int color = updateCount & UPDATE_COUNT_ALPHA_MASK;
    125 
    126         // only color the invalidated area
    127         SkPaint paint;
    128         paint.setARGB(color, 0, 255, 0);
    129         SkIRect rect;
    130         rect.set(0, 0, tileSize.width(), tileSize.height());
    131         canvas.drawIRect(rect, paint);
    132 
    133         drawTileInfo(&canvas, renderInfo, updateCount, after - before);
    134 
    135         // paint the tile boundaries
    136         paint.setARGB(64, 255, 0, 0);
    137         paint.setStrokeWidth(3);
    138         canvas.drawLine(0, 0, tileSize.width(), tileSize.height(), paint);
    139         paint.setARGB(64, 0, 255, 0);
    140         canvas.drawLine(0, tileSize.height(), tileSize.width(), 0, paint);
    141         paint.setARGB(128, 0, 0, 255);
    142         canvas.drawLine(tileSize.width(), 0, tileSize.width(), tileSize.height(), paint);
    143     }
    144     renderingComplete(renderInfo, &canvas);
    145 }
    146 
    147 } // namespace WebCore
    148 
    149 #endif // USE(ACCELERATED_COMPOSITING)
    150