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 "GaneshRenderer.h"
29
30 #if USE(ACCELERATED_COMPOSITING)
31
32 #include "GaneshContext.h"
33 #include "SkCanvas.h"
34 #include "SkGpuDevice.h"
35 #include "TilesManager.h"
36
37
38 #ifdef DEBUG
39
40 #include <cutils/log.h>
41 #include <wtf/CurrentTime.h>
42 #include <wtf/text/CString.h>
43
44 #undef XLOG
45 #define XLOG(...) android_printLog(ANDROID_LOG_DEBUG, "GaneshRenderer", __VA_ARGS__)
46
47 #else
48
49 #undef XLOG
50 #define XLOG(...)
51
52 #endif // DEBUG
53
54 namespace WebCore {
55
56 static const String TAG_CREATE_FBO = "create_fbo";
57 static const String TAG_DRAW_PICTURE = "draw_picture";
58 static const String TAG_UPDATE_TEXTURE = "update_texture";
59 #define TAG_COUNT 3
60 static const String TAGS[] = {
61 TAG_CREATE_FBO,
62 TAG_DRAW_PICTURE,
63 TAG_UPDATE_TEXTURE,
64 };
65
GaneshRenderer()66 GaneshRenderer::GaneshRenderer() : BaseRenderer(BaseRenderer::Ganesh)
67 {
68 #ifdef DEBUG_COUNT
69 ClassTracker::instance()->increment("GaneshRenderer");
70 #endif
71 }
72
~GaneshRenderer()73 GaneshRenderer::~GaneshRenderer()
74 {
75 #ifdef DEBUG_COUNT
76 ClassTracker::instance()->decrement("GaneshRenderer");
77 #endif
78 }
79
setupCanvas(const TileRenderInfo & renderInfo,SkCanvas * canvas)80 void GaneshRenderer::setupCanvas(const TileRenderInfo& renderInfo, SkCanvas* canvas)
81 {
82 if (renderInfo.measurePerf)
83 m_perfMon.start(TAG_CREATE_FBO);
84
85 GaneshContext* ganesh = GaneshContext::instance();
86
87 #if !DEPRECATED_SURFACE_TEXTURE_MODE
88 if (renderInfo.textureInfo->getSharedTextureMode() == SurfaceTextureMode) {
89 TransferQueue* tileQueue = TilesManager::instance()->transferQueue();
90
91 tileQueue->lockQueue();
92
93 bool ready = tileQueue->readyForUpdate();
94 if (!ready) {
95 XLOG("!ready");
96 tileQueue->unlockQueue();
97 return;
98 }
99 }
100 #endif
101
102 SkDevice* device = NULL;
103 if (renderInfo.tileSize.width() == TilesManager::tileWidth()
104 && renderInfo.tileSize.height() == TilesManager::tileHeight()) {
105 device = ganesh->getDeviceForBaseTile(renderInfo);
106 } else {
107 // TODO support arbitrary sizes for layers
108 XLOG("ERROR: expected (%d,%d) actual (%d,%d)",
109 TilesManager::tileWidth(), TilesManager::tileHeight(),
110 renderInfo.tileSize.width(), renderInfo.tileSize.height());
111 }
112
113 if (renderInfo.measurePerf) {
114 m_perfMon.stop(TAG_CREATE_FBO);
115 m_perfMon.start(TAG_DRAW_PICTURE);
116 }
117
118 // set the GPU device to the canvas
119 canvas->setDevice(device);
120 canvas->setDeviceFactory(device->getDeviceFactory());
121
122 // invert canvas contents
123 if (renderInfo.textureInfo->getSharedTextureMode() == EglImageMode) {
124 canvas->scale(SK_Scalar1, -SK_Scalar1);
125 canvas->translate(0, -renderInfo.tileSize.height());
126 }
127 }
128
setupPartialInval(const TileRenderInfo & renderInfo,SkCanvas * canvas)129 void GaneshRenderer::setupPartialInval(const TileRenderInfo& renderInfo, SkCanvas* canvas)
130 {
131 // set the clip to our invalRect
132 SkRect clipRect = SkRect::MakeLTRB(renderInfo.invalRect->fLeft,
133 renderInfo.invalRect->fTop,
134 renderInfo.invalRect->fRight,
135 renderInfo.invalRect->fBottom);
136 canvas->clipRect(clipRect);
137 }
138
renderingComplete(const TileRenderInfo & renderInfo,SkCanvas * canvas)139 void GaneshRenderer::renderingComplete(const TileRenderInfo& renderInfo, SkCanvas* canvas)
140 {
141 if (renderInfo.measurePerf) {
142 m_perfMon.stop(TAG_DRAW_PICTURE);
143 m_perfMon.start(TAG_UPDATE_TEXTURE);
144 }
145
146 XLOG("rendered to tile (%d,%d)", renderInfo.x, renderInfo.y);
147
148 GaneshContext::instance()->flush();
149
150 // In SurfaceTextureMode we must call swapBuffers to unlock and post the
151 // tile's ANativeWindow (i.e. SurfaceTexture) buffer
152 if (renderInfo.textureInfo->getSharedTextureMode() == SurfaceTextureMode) {
153 #if !DEPRECATED_SURFACE_TEXTURE_MODE
154 TransferQueue* tileQueue = TilesManager::instance()->transferQueue();
155 eglSwapBuffers(eglGetCurrentDisplay(), tileQueue->m_eglSurface);
156 tileQueue->addItemInTransferQueue(&renderInfo, GpuUpload, 0);
157 tileQueue->unlockQueue();
158 #endif
159 }
160
161 if (renderInfo.measurePerf)
162 m_perfMon.stop(TAG_UPDATE_TEXTURE);
163 }
164
getPerformanceTags(int & tagCount)165 const String* GaneshRenderer::getPerformanceTags(int& tagCount)
166 {
167 tagCount = TAG_COUNT;
168 return TAGS;
169 }
170
171 } // namespace WebCore
172
173 #endif // USE(ACCELERATED_COMPOSITING)
174