blob: 925db303461f93a3b074fb11b1cbd27185b6ef64 [file] [log] [blame]
Stan Iliev500a0c32016-10-26 10:30:09 -04001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "SkiaOpenGLPipeline.h"
18
Stan Iliev7bc3bc62017-05-24 13:28:36 -040019#include "hwui/Bitmap.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040020#include "DeferredLayerUpdater.h"
Greg Daniel8cd3edf2017-01-09 14:15:41 -050021#include "GlLayer.h"
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050022#include "LayerDrawable.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040023#include "renderthread/EglManager.h"
Greg Danielcd558522016-11-17 13:31:40 -050024#include "renderthread/Frame.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040025#include "renderstate/RenderState.h"
Matt Sarettcf2c05c2016-10-26 11:03:23 -040026#include "SkiaPipeline.h"
27#include "SkiaProfileRenderer.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040028#include "utils/TraceUtils.h"
29
Greg Danielac2d2322017-07-12 11:30:15 -040030#include <GrBackendSurface.h>
31
Stan Iliev500a0c32016-10-26 10:30:09 -040032#include <cutils/properties.h>
33#include <strings.h>
34
35using namespace android::uirenderer::renderthread;
36
37namespace android {
38namespace uirenderer {
39namespace skiapipeline {
40
41SkiaOpenGLPipeline::SkiaOpenGLPipeline(RenderThread& thread)
42 : SkiaPipeline(thread)
43 , mEglManager(thread.eglManager()) {
44}
45
46MakeCurrentResult SkiaOpenGLPipeline::makeCurrent() {
47 // TODO: Figure out why this workaround is needed, see b/13913604
48 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
49 EGLint error = 0;
50 if (!mEglManager.makeCurrent(mEglSurface, &error)) {
51 return MakeCurrentResult::AlreadyCurrent;
52 }
53 return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded;
54}
55
56Frame SkiaOpenGLPipeline::getFrame() {
57 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
58 "drawRenderNode called on a context with no surface!");
59 return mEglManager.beginFrame(mEglSurface);
60}
61
62bool SkiaOpenGLPipeline::draw(const Frame& frame, const SkRect& screenDirty,
63 const SkRect& dirty,
64 const FrameBuilder::LightGeometry& lightGeometry,
65 LayerUpdateQueue* layerUpdateQueue,
Romain Guy07ae5052017-06-13 18:25:32 -070066 const Rect& contentDrawBounds, bool opaque, bool wideColorGamut,
Stan Iliev500a0c32016-10-26 10:30:09 -040067 const BakedOpRenderer::LightInfo& lightInfo,
68 const std::vector<sp<RenderNode>>& renderNodes,
69 FrameInfoVisualizer* profiler) {
70
71 mEglManager.damageFrame(frame, dirty);
72
73 // setup surface for fbo0
Greg Danielac2d2322017-07-12 11:30:15 -040074 GrGLFramebufferInfo fboInfo;
75 fboInfo.fFBOID = 0;
76
77 GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE,
78 kRGBA_8888_GrPixelConfig, fboInfo);
Stan Iliev500a0c32016-10-26 10:30:09 -040079
80 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
81
82 SkASSERT(mRenderThread.getGrContext() != nullptr);
83 sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(
Greg Danielac2d2322017-07-12 11:30:15 -040084 mRenderThread.getGrContext(), backendRT, kBottomLeft_GrSurfaceOrigin, nullptr, &props));
Stan Iliev500a0c32016-10-26 10:30:09 -040085
86 SkiaPipeline::updateLighting(lightGeometry, lightInfo);
Romain Guy07ae5052017-06-13 18:25:32 -070087 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, wideColorGamut,
88 contentDrawBounds, surface);
Stan Iliev500a0c32016-10-26 10:30:09 -040089 layerUpdateQueue->clear();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040090
91 // Draw visual debugging features
92 if (CC_UNLIKELY(Properties::showDirtyRegions
Matt Sarett4c9bbf42016-11-07 14:23:12 -050093 || ProfileType::None != Properties::getProfileType())) {
Matt Sarettcf2c05c2016-10-26 11:03:23 -040094 SkCanvas* profileCanvas = surface->getCanvas();
95 SkiaProfileRenderer profileRenderer(profileCanvas);
96 profiler->draw(profileRenderer);
97 profileCanvas->flush();
98 }
99
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500100 // Log memory statistics
101 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
102 dumpResourceCacheUsage();
103 }
104
Stan Iliev500a0c32016-10-26 10:30:09 -0400105 return true;
106}
107
108bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew,
109 const SkRect& screenDirty, FrameInfo* currentFrameInfo, bool* requireSwap) {
110
111 GL_CHECKPOINT(LOW);
112
113 // Even if we decided to cancel the frame, from the perspective of jank
114 // metrics the frame was swapped at this point
115 currentFrameInfo->markSwapBuffers();
116
117 *requireSwap = drew || mEglManager.damageRequiresSwap();
118
119 if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
120 return false;
121 }
122
123 return *requireSwap;
124}
125
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500126bool SkiaOpenGLPipeline::copyLayerInto(DeferredLayerUpdater* deferredLayer, SkBitmap* bitmap) {
127 if (!mRenderThread.getGrContext()) {
128 return false;
129 }
130
Chris Craik2f1aaf72017-02-14 13:01:42 -0800131 // acquire most recent buffer for drawing
132 deferredLayer->updateTexImage();
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500133 deferredLayer->apply();
134
135 SkCanvas canvas(*bitmap);
136 Layer* layer = deferredLayer->backingLayer();
137 return LayerDrawable::DrawLayer(mRenderThread.getGrContext(), &canvas, layer);
Stan Iliev500a0c32016-10-26 10:30:09 -0400138}
139
sergeyv3e9999b2017-01-19 15:37:02 -0800140static Layer* createLayer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight,
141 SkColorFilter* colorFilter, int alpha, SkBlendMode mode, bool blend) {
142 GlLayer* layer = new GlLayer(renderState, layerWidth, layerHeight, colorFilter, alpha,
143 mode, blend);
144 layer->generateTexture();
145 return layer;
146}
147
Stan Iliev500a0c32016-10-26 10:30:09 -0400148DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
149 mEglManager.initialize();
sergeyv3e9999b2017-01-19 15:37:02 -0800150 return new DeferredLayerUpdater(mRenderThread.renderState(), createLayer, Layer::Api::OpenGL);
Stan Iliev500a0c32016-10-26 10:30:09 -0400151}
152
153void SkiaOpenGLPipeline::onStop() {
154 if (mEglManager.isCurrent(mEglSurface)) {
155 mEglManager.makeCurrent(EGL_NO_SURFACE);
156 }
157}
158
Romain Guy26a2b972017-04-17 09:39:51 -0700159bool SkiaOpenGLPipeline::setSurface(Surface* surface, SwapBehavior swapBehavior,
160 ColorMode colorMode) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400161
162 if (mEglSurface != EGL_NO_SURFACE) {
163 mEglManager.destroySurface(mEglSurface);
164 mEglSurface = EGL_NO_SURFACE;
165 }
166
167 if (surface) {
Romain Guy26a2b972017-04-17 09:39:51 -0700168 const bool wideColorGamut = colorMode == ColorMode::WideColorGamut;
169 mEglSurface = mEglManager.createSurface(surface, wideColorGamut);
Stan Iliev500a0c32016-10-26 10:30:09 -0400170 }
171
172 if (mEglSurface != EGL_NO_SURFACE) {
173 const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer);
174 mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
175 return true;
176 }
177
178 return false;
179}
180
181bool SkiaOpenGLPipeline::isSurfaceReady() {
182 return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE);
183}
184
185bool SkiaOpenGLPipeline::isContextReady() {
186 return CC_LIKELY(mEglManager.hasEglContext());
187}
188
189void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
190 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
191 if (thread.eglManager().hasEglContext()) {
192 mode = DrawGlInfo::kModeProcess;
193 }
194
195 (*functor)(mode, nullptr);
196
197 // If there's no context we don't need to reset as there's no gl state to save/restore
198 if (mode != DrawGlInfo::kModeProcessNoContext) {
199 thread.getGrContext()->resetContext();
200 }
201}
202
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400203#define FENCE_TIMEOUT 2000000000
204
205class AutoEglFence {
206public:
207 AutoEglFence(EGLDisplay display)
208 : mDisplay(display) {
209 fence = eglCreateSyncKHR(mDisplay, EGL_SYNC_FENCE_KHR, NULL);
210 }
211
212 ~AutoEglFence() {
213 if (fence != EGL_NO_SYNC_KHR) {
214 eglDestroySyncKHR(mDisplay, fence);
215 }
216 }
217
218 EGLSyncKHR fence = EGL_NO_SYNC_KHR;
219private:
220 EGLDisplay mDisplay = EGL_NO_DISPLAY;
221};
222
223class AutoEglImage {
224public:
225 AutoEglImage(EGLDisplay display, EGLClientBuffer clientBuffer)
226 : mDisplay(display) {
227 EGLint imageAttrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE };
228 image = eglCreateImageKHR(display, EGL_NO_CONTEXT,
229 EGL_NATIVE_BUFFER_ANDROID, clientBuffer, imageAttrs);
230 }
231
232 ~AutoEglImage() {
233 if (image != EGL_NO_IMAGE_KHR) {
234 eglDestroyImageKHR(mDisplay, image);
235 }
236 }
237
238 EGLImageKHR image = EGL_NO_IMAGE_KHR;
239private:
240 EGLDisplay mDisplay = EGL_NO_DISPLAY;
241};
242
243class AutoSkiaGlTexture {
244public:
245 AutoSkiaGlTexture() {
246 glGenTextures(1, &mTexture);
247 glBindTexture(GL_TEXTURE_2D, mTexture);
248 }
249
250 ~AutoSkiaGlTexture() {
251 glDeleteTextures(1, &mTexture);
252 }
253
254private:
255 GLuint mTexture = 0;
256};
257
258sk_sp<Bitmap> SkiaOpenGLPipeline::allocateHardwareBitmap(renderthread::RenderThread& renderThread,
259 SkBitmap& skBitmap) {
260 renderThread.eglManager().initialize();
261
262 sk_sp<GrContext> grContext = sk_ref_sp(renderThread.getGrContext());
263 const SkImageInfo& info = skBitmap.info();
264 PixelFormat pixelFormat;
265 GLint format, type;
266 bool isSupported = false;
267
268 //TODO: add support for linear blending (when ANDROID_ENABLE_LINEAR_BLENDING is defined)
269 switch (info.colorType()) {
270 case kRGBA_8888_SkColorType:
271 isSupported = true;
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -0400272 // ARGB_4444 is upconverted to RGBA_8888
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400273 case kARGB_4444_SkColorType:
274 pixelFormat = PIXEL_FORMAT_RGBA_8888;
275 format = GL_RGBA;
276 type = GL_UNSIGNED_BYTE;
277 break;
278 case kRGBA_F16_SkColorType:
279 isSupported = grContext->caps()->isConfigTexturable(kRGBA_half_GrPixelConfig);
280 if (isSupported) {
281 type = GL_HALF_FLOAT;
282 pixelFormat = PIXEL_FORMAT_RGBA_FP16;
283 } else {
284 type = GL_UNSIGNED_BYTE;
285 pixelFormat = PIXEL_FORMAT_RGBA_8888;
286 }
287 format = GL_RGBA;
288 break;
289 case kRGB_565_SkColorType:
290 isSupported = true;
291 pixelFormat = PIXEL_FORMAT_RGB_565;
292 format = GL_RGB;
293 type = GL_UNSIGNED_SHORT_5_6_5;
294 break;
295 case kGray_8_SkColorType:
296 isSupported = true;
297 pixelFormat = PIXEL_FORMAT_RGBA_8888;
298 format = GL_LUMINANCE;
299 type = GL_UNSIGNED_BYTE;
300 break;
301 default:
302 ALOGW("unable to create hardware bitmap of colortype: %d", info.colorType());
303 return nullptr;
304 }
305
Stan Iliev0a3ff952017-07-10 17:04:03 -0400306 auto colorSpace = info.colorSpace();
307 bool convertToSRGB = false;
308 if (colorSpace && (!colorSpace->isSRGB())) {
309 isSupported = false;
310 convertToSRGB = true;
311 }
312
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400313 SkBitmap bitmap;
314 if (isSupported) {
315 bitmap = skBitmap;
316 } else {
317 bitmap.allocPixels(SkImageInfo::MakeN32(info.width(), info.height(), info.alphaType(),
318 nullptr));
319 bitmap.eraseColor(0);
Stan Iliev0a3ff952017-07-10 17:04:03 -0400320 if (info.colorType() == kRGBA_F16_SkColorType || convertToSRGB) {
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400321 // Drawing RGBA_F16 onto ARGB_8888 is not supported
322 skBitmap.readPixels(bitmap.info().makeColorSpace(SkColorSpace::MakeSRGB()),
323 bitmap.getPixels(), bitmap.rowBytes(), 0, 0);
324 } else {
325 SkCanvas canvas(bitmap);
326 canvas.drawBitmap(skBitmap, 0.0f, 0.0f, nullptr);
327 }
328 }
329
330 sp<GraphicBuffer> buffer = new GraphicBuffer(info.width(), info.height(), pixelFormat,
331 GraphicBuffer::USAGE_HW_TEXTURE |
332 GraphicBuffer::USAGE_SW_WRITE_NEVER |
333 GraphicBuffer::USAGE_SW_READ_NEVER,
334 std::string("Bitmap::allocateSkiaHardwareBitmap pid [") + std::to_string(getpid()) + "]");
335
336 status_t error = buffer->initCheck();
337 if (error < 0) {
338 ALOGW("createGraphicBuffer() failed in GraphicBuffer.create()");
339 return nullptr;
340 }
341
342 //upload the bitmap into a texture
343 EGLDisplay display = eglGetCurrentDisplay();
344 LOG_ALWAYS_FATAL_IF(display == EGL_NO_DISPLAY,
345 "Failed to get EGL_DEFAULT_DISPLAY! err=%s",
346 uirenderer::renderthread::EglManager::eglErrorString());
347 // We use an EGLImage to access the content of the GraphicBuffer
348 // The EGL image is later bound to a 2D texture
349 EGLClientBuffer clientBuffer = (EGLClientBuffer) buffer->getNativeBuffer();
350 AutoEglImage autoImage(display, clientBuffer);
351 if (autoImage.image == EGL_NO_IMAGE_KHR) {
352 ALOGW("Could not create EGL image, err =%s",
353 uirenderer::renderthread::EglManager::eglErrorString());
354 return nullptr;
355 }
356 AutoSkiaGlTexture glTexture;
357 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, autoImage.image);
358 GL_CHECKPOINT(MODERATE);
359
360 // glTexSubImage2D is synchronous in sense that it memcpy() from pointer that we provide.
361 // But asynchronous in sense that driver may upload texture onto hardware buffer when we first
362 // use it in drawing
363 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, info.width(), info.height(), format, type,
364 bitmap.getPixels());
365 GL_CHECKPOINT(MODERATE);
366
367 // The fence is used to wait for the texture upload to finish
368 // properly. We cannot rely on glFlush() and glFinish() as
369 // some drivers completely ignore these API calls
370 AutoEglFence autoFence(display);
371 if (autoFence.fence == EGL_NO_SYNC_KHR) {
372 LOG_ALWAYS_FATAL("Could not create sync fence %#x", eglGetError());
373 return nullptr;
374 }
375 // The flag EGL_SYNC_FLUSH_COMMANDS_BIT_KHR will trigger a
376 // pipeline flush (similar to what a glFlush() would do.)
377 EGLint waitStatus = eglClientWaitSyncKHR(display, autoFence.fence,
378 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, FENCE_TIMEOUT);
379 if (waitStatus != EGL_CONDITION_SATISFIED_KHR) {
380 LOG_ALWAYS_FATAL("Failed to wait for the fence %#x", eglGetError());
381 return nullptr;
382 }
383
384 grContext->resetContext(kTextureBinding_GrGLBackendState);
385
386 return sk_sp<Bitmap>(new Bitmap(buffer.get(), bitmap.info()));
387}
388
Stan Iliev500a0c32016-10-26 10:30:09 -0400389} /* namespace skiapipeline */
390} /* namespace uirenderer */
391} /* namespace android */