blob: 15277f175350a8bd1387474bc61ab26e4f295170 [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
19#include "DeferredLayerUpdater.h"
Greg Daniel8cd3edf2017-01-09 14:15:41 -050020#include "GlLayer.h"
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050021#include "LayerDrawable.h"
Matt Sarettcf2c05c2016-10-26 11:03:23 -040022#include "SkiaPipeline.h"
23#include "SkiaProfileRenderer.h"
John Reck1bcacfd2017-11-03 10:12:19 -070024#include "hwui/Bitmap.h"
25#include "renderstate/RenderState.h"
26#include "renderthread/EglManager.h"
27#include "renderthread/Frame.h"
John Reckd9d7f122018-05-03 14:40:56 -070028#include "utils/GLUtils.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040029#include "utils/TraceUtils.h"
30
Greg Danielac2d2322017-07-12 11:30:15 -040031#include <GrBackendSurface.h>
32
Stan Iliev500a0c32016-10-26 10:30:09 -040033#include <cutils/properties.h>
34#include <strings.h>
35
36using namespace android::uirenderer::renderthread;
37
38namespace android {
39namespace uirenderer {
40namespace skiapipeline {
41
42SkiaOpenGLPipeline::SkiaOpenGLPipeline(RenderThread& thread)
John Reck1bcacfd2017-11-03 10:12:19 -070043 : SkiaPipeline(thread), mEglManager(thread.eglManager()) {}
Stan Iliev500a0c32016-10-26 10:30:09 -040044
45MakeCurrentResult SkiaOpenGLPipeline::makeCurrent() {
46 // TODO: Figure out why this workaround is needed, see b/13913604
47 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
48 EGLint error = 0;
49 if (!mEglManager.makeCurrent(mEglSurface, &error)) {
50 return MakeCurrentResult::AlreadyCurrent;
51 }
52 return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded;
53}
54
55Frame SkiaOpenGLPipeline::getFrame() {
56 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
John Reck1bcacfd2017-11-03 10:12:19 -070057 "drawRenderNode called on a context with no surface!");
Stan Iliev500a0c32016-10-26 10:30:09 -040058 return mEglManager.beginFrame(mEglSurface);
59}
60
John Reck1bcacfd2017-11-03 10:12:19 -070061bool SkiaOpenGLPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
John Reckd9d7f122018-05-03 14:40:56 -070062 const LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070063 LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds,
64 bool opaque, bool wideColorGamut,
John Reckd9d7f122018-05-03 14:40:56 -070065 const LightInfo& lightInfo,
John Reck1bcacfd2017-11-03 10:12:19 -070066 const std::vector<sp<RenderNode>>& renderNodes,
67 FrameInfoVisualizer* profiler) {
Stan Iliev500a0c32016-10-26 10:30:09 -040068 mEglManager.damageFrame(frame, dirty);
69
Greg Danielc9da8e82018-03-21 10:50:24 -040070 SkColorType colorType;
Stan Iliev500a0c32016-10-26 10:30:09 -040071 // setup surface for fbo0
Greg Danielac2d2322017-07-12 11:30:15 -040072 GrGLFramebufferInfo fboInfo;
73 fboInfo.fFBOID = 0;
Greg Danielc9da8e82018-03-21 10:50:24 -040074 if (wideColorGamut) {
75 fboInfo.fFormat = GL_RGBA16F;
76 colorType = kRGBA_F16_SkColorType;
77 } else {
78 fboInfo.fFormat = GL_RGBA8;
79 colorType = kN32_SkColorType;
80 }
Greg Danielac2d2322017-07-12 11:30:15 -040081
Greg Danielc9da8e82018-03-21 10:50:24 -040082 GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE, fboInfo);
Stan Iliev500a0c32016-10-26 10:30:09 -040083
84 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
85
86 SkASSERT(mRenderThread.getGrContext() != nullptr);
87 sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(
Greg Danielc9da8e82018-03-21 10:50:24 -040088 mRenderThread.getGrContext(), backendRT, kBottomLeft_GrSurfaceOrigin, colorType,
89 nullptr, &props));
Stan Iliev500a0c32016-10-26 10:30:09 -040090
91 SkiaPipeline::updateLighting(lightGeometry, lightInfo);
John Reck1bcacfd2017-11-03 10:12:19 -070092 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, wideColorGamut, contentDrawBounds,
93 surface);
Stan Iliev500a0c32016-10-26 10:30:09 -040094 layerUpdateQueue->clear();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040095
96 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -070097 if (CC_UNLIKELY(Properties::showDirtyRegions ||
98 ProfileType::None != Properties::getProfileType())) {
Matt Sarettcf2c05c2016-10-26 11:03:23 -040099 SkCanvas* profileCanvas = surface->getCanvas();
100 SkiaProfileRenderer profileRenderer(profileCanvas);
101 profiler->draw(profileRenderer);
102 profileCanvas->flush();
103 }
104
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500105 // Log memory statistics
106 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
107 dumpResourceCacheUsage();
108 }
109
Stan Iliev500a0c32016-10-26 10:30:09 -0400110 return true;
111}
112
John Reck1bcacfd2017-11-03 10:12:19 -0700113bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
114 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400115 GL_CHECKPOINT(LOW);
116
117 // Even if we decided to cancel the frame, from the perspective of jank
118 // metrics the frame was swapped at this point
119 currentFrameInfo->markSwapBuffers();
120
121 *requireSwap = drew || mEglManager.damageRequiresSwap();
122
123 if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
124 return false;
125 }
126
127 return *requireSwap;
128}
129
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500130bool SkiaOpenGLPipeline::copyLayerInto(DeferredLayerUpdater* deferredLayer, SkBitmap* bitmap) {
131 if (!mRenderThread.getGrContext()) {
132 return false;
133 }
134
Chris Craik2f1aaf72017-02-14 13:01:42 -0800135 // acquire most recent buffer for drawing
136 deferredLayer->updateTexImage();
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500137 deferredLayer->apply();
138
Derek Sollenberger4170db32017-08-09 13:52:36 -0400139 /* This intermediate surface is present to work around a bug in SwiftShader that
140 * prevents us from reading the contents of the layer's texture directly. The
141 * workaround involves first rendering that texture into an intermediate buffer and
142 * then reading from the intermediate buffer into the bitmap.
143 */
144 sk_sp<SkSurface> tmpSurface = SkSurface::MakeRenderTarget(mRenderThread.getGrContext(),
John Reck1bcacfd2017-11-03 10:12:19 -0700145 SkBudgeted::kYes, bitmap->info());
Derek Sollenberger4170db32017-08-09 13:52:36 -0400146
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500147 Layer* layer = deferredLayer->backingLayer();
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400148 const SkRect dstRect = SkRect::MakeIWH(bitmap->width(), bitmap->height());
149 if (LayerDrawable::DrawLayer(mRenderThread.getGrContext(), tmpSurface->getCanvas(), layer,
150 &dstRect)) {
Derek Sollenberger4170db32017-08-09 13:52:36 -0400151 sk_sp<SkImage> tmpImage = tmpSurface->makeImageSnapshot();
152 if (tmpImage->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) {
153 bitmap->notifyPixelsChanged();
154 return true;
155 }
156 }
157
158 return false;
Stan Iliev500a0c32016-10-26 10:30:09 -0400159}
160
sergeyv3e9999b2017-01-19 15:37:02 -0800161static Layer* createLayer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight,
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -0400162 sk_sp<SkColorFilter> colorFilter, int alpha, SkBlendMode mode, bool blend) {
John Reck1bcacfd2017-11-03 10:12:19 -0700163 GlLayer* layer =
164 new GlLayer(renderState, layerWidth, layerHeight, colorFilter, alpha, mode, blend);
sergeyv3e9999b2017-01-19 15:37:02 -0800165 layer->generateTexture();
166 return layer;
167}
168
Stan Iliev500a0c32016-10-26 10:30:09 -0400169DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
John Reck1e510712018-04-23 08:15:03 -0700170 mRenderThread.requireGlContext();
sergeyv3e9999b2017-01-19 15:37:02 -0800171 return new DeferredLayerUpdater(mRenderThread.renderState(), createLayer, Layer::Api::OpenGL);
Stan Iliev500a0c32016-10-26 10:30:09 -0400172}
173
174void SkiaOpenGLPipeline::onStop() {
175 if (mEglManager.isCurrent(mEglSurface)) {
176 mEglManager.makeCurrent(EGL_NO_SURFACE);
177 }
178}
179
Romain Guy26a2b972017-04-17 09:39:51 -0700180bool SkiaOpenGLPipeline::setSurface(Surface* surface, SwapBehavior swapBehavior,
John Reck1bcacfd2017-11-03 10:12:19 -0700181 ColorMode colorMode) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400182 if (mEglSurface != EGL_NO_SURFACE) {
183 mEglManager.destroySurface(mEglSurface);
184 mEglSurface = EGL_NO_SURFACE;
185 }
186
187 if (surface) {
John Reck1e510712018-04-23 08:15:03 -0700188 mRenderThread.requireGlContext();
Romain Guy26a2b972017-04-17 09:39:51 -0700189 const bool wideColorGamut = colorMode == ColorMode::WideColorGamut;
190 mEglSurface = mEglManager.createSurface(surface, wideColorGamut);
Stan Iliev500a0c32016-10-26 10:30:09 -0400191 }
192
193 if (mEglSurface != EGL_NO_SURFACE) {
194 const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer);
195 mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
196 return true;
197 }
198
199 return false;
200}
201
202bool SkiaOpenGLPipeline::isSurfaceReady() {
203 return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE);
204}
205
206bool SkiaOpenGLPipeline::isContextReady() {
207 return CC_LIKELY(mEglManager.hasEglContext());
208}
209
210void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
211 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
212 if (thread.eglManager().hasEglContext()) {
213 mode = DrawGlInfo::kModeProcess;
214 }
215
216 (*functor)(mode, nullptr);
217
218 // If there's no context we don't need to reset as there's no gl state to save/restore
219 if (mode != DrawGlInfo::kModeProcessNoContext) {
220 thread.getGrContext()->resetContext();
221 }
222}
223
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400224#define FENCE_TIMEOUT 2000000000
225
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400226class AutoEglImage {
227public:
John Reck1bcacfd2017-11-03 10:12:19 -0700228 AutoEglImage(EGLDisplay display, EGLClientBuffer clientBuffer) : mDisplay(display) {
229 EGLint imageAttrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE};
230 image = eglCreateImageKHR(display, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, clientBuffer,
231 imageAttrs);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400232 }
233
234 ~AutoEglImage() {
235 if (image != EGL_NO_IMAGE_KHR) {
236 eglDestroyImageKHR(mDisplay, image);
237 }
238 }
239
240 EGLImageKHR image = EGL_NO_IMAGE_KHR;
John Reck1bcacfd2017-11-03 10:12:19 -0700241
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400242private:
243 EGLDisplay mDisplay = EGL_NO_DISPLAY;
244};
245
246class AutoSkiaGlTexture {
247public:
248 AutoSkiaGlTexture() {
249 glGenTextures(1, &mTexture);
250 glBindTexture(GL_TEXTURE_2D, mTexture);
251 }
252
John Reck1bcacfd2017-11-03 10:12:19 -0700253 ~AutoSkiaGlTexture() { glDeleteTextures(1, &mTexture); }
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400254
255private:
256 GLuint mTexture = 0;
257};
258
John Reckb90d4cb2018-04-19 17:05:35 -0700259struct FormatInfo {
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400260
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400261 PixelFormat pixelFormat;
262 GLint format, type;
263 bool isSupported = false;
John Reckb90d4cb2018-04-19 17:05:35 -0700264 bool valid = true;
265};
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400266
John Reckb90d4cb2018-04-19 17:05:35 -0700267static bool gpuSupportsHalfFloatTextures(renderthread::RenderThread& renderThread) {
268 static bool isSupported = renderThread.queue().runSync([&renderThread]() -> bool {
269 renderThread.requireGlContext();
270 sk_sp<GrContext> grContext = sk_ref_sp(renderThread.getGrContext());
271 return grContext->colorTypeSupportedAsImage(kRGBA_F16_SkColorType);
272 });
273 return isSupported;
274}
275
276static FormatInfo determineFormat(renderthread::RenderThread& renderThread,
277 const SkBitmap& skBitmap) {
278 FormatInfo formatInfo;
John Reck1bcacfd2017-11-03 10:12:19 -0700279 // TODO: add support for linear blending (when ANDROID_ENABLE_LINEAR_BLENDING is defined)
John Reckb90d4cb2018-04-19 17:05:35 -0700280 switch (skBitmap.info().colorType()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700281 case kRGBA_8888_SkColorType:
John Reckb90d4cb2018-04-19 17:05:35 -0700282 formatInfo.isSupported = true;
John Reck1bcacfd2017-11-03 10:12:19 -0700283 // ARGB_4444 is upconverted to RGBA_8888
284 case kARGB_4444_SkColorType:
John Reckb90d4cb2018-04-19 17:05:35 -0700285 formatInfo.pixelFormat = PIXEL_FORMAT_RGBA_8888;
286 formatInfo.format = GL_RGBA;
287 formatInfo.type = GL_UNSIGNED_BYTE;
John Reck1bcacfd2017-11-03 10:12:19 -0700288 break;
289 case kRGBA_F16_SkColorType:
John Reckb90d4cb2018-04-19 17:05:35 -0700290 formatInfo.isSupported = gpuSupportsHalfFloatTextures(renderThread);
291 if (formatInfo.isSupported) {
292 formatInfo.type = GL_HALF_FLOAT;
293 formatInfo.pixelFormat = PIXEL_FORMAT_RGBA_FP16;
John Reck1bcacfd2017-11-03 10:12:19 -0700294 } else {
John Reckb90d4cb2018-04-19 17:05:35 -0700295 formatInfo.type = GL_UNSIGNED_BYTE;
296 formatInfo.pixelFormat = PIXEL_FORMAT_RGBA_8888;
John Reck1bcacfd2017-11-03 10:12:19 -0700297 }
John Reckb90d4cb2018-04-19 17:05:35 -0700298 formatInfo.format = GL_RGBA;
John Reck1bcacfd2017-11-03 10:12:19 -0700299 break;
300 case kRGB_565_SkColorType:
John Reckb90d4cb2018-04-19 17:05:35 -0700301 formatInfo.isSupported = true;
302 formatInfo.pixelFormat = PIXEL_FORMAT_RGB_565;
303 formatInfo.format = GL_RGB;
304 formatInfo.type = GL_UNSIGNED_SHORT_5_6_5;
John Reck1bcacfd2017-11-03 10:12:19 -0700305 break;
306 case kGray_8_SkColorType:
John Reckb90d4cb2018-04-19 17:05:35 -0700307 formatInfo.isSupported = true;
308 formatInfo.pixelFormat = PIXEL_FORMAT_RGBA_8888;
309 formatInfo.format = GL_LUMINANCE;
310 formatInfo.type = GL_UNSIGNED_BYTE;
John Reck1bcacfd2017-11-03 10:12:19 -0700311 break;
312 default:
John Reckb90d4cb2018-04-19 17:05:35 -0700313 ALOGW("unable to create hardware bitmap of colortype: %d", skBitmap.info().colorType());
314 formatInfo.valid = false;
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400315 }
John Reckb90d4cb2018-04-19 17:05:35 -0700316 return formatInfo;
317}
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400318
John Reckb90d4cb2018-04-19 17:05:35 -0700319static SkBitmap makeHwCompatible(const FormatInfo& format, const SkBitmap& source) {
320 if (format.isSupported) {
321 return source;
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400322 } else {
John Reckb90d4cb2018-04-19 17:05:35 -0700323 SkBitmap bitmap;
324 const SkImageInfo& info = source.info();
John Reck1bcacfd2017-11-03 10:12:19 -0700325 bitmap.allocPixels(
326 SkImageInfo::MakeN32(info.width(), info.height(), info.alphaType(), nullptr));
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400327 bitmap.eraseColor(0);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400328 if (info.colorType() == kRGBA_F16_SkColorType) {
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400329 // Drawing RGBA_F16 onto ARGB_8888 is not supported
John Reckb90d4cb2018-04-19 17:05:35 -0700330 source.readPixels(bitmap.info().makeColorSpace(SkColorSpace::MakeSRGB()),
331 bitmap.getPixels(), bitmap.rowBytes(), 0, 0);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400332 } else {
333 SkCanvas canvas(bitmap);
John Reckb90d4cb2018-04-19 17:05:35 -0700334 canvas.drawBitmap(source, 0.0f, 0.0f, nullptr);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400335 }
John Reckb90d4cb2018-04-19 17:05:35 -0700336 return bitmap;
337 }
338}
339
340sk_sp<Bitmap> SkiaOpenGLPipeline::allocateHardwareBitmap(renderthread::RenderThread& thread,
341 const SkBitmap& sourceBitmap) {
342 ATRACE_CALL();
343
344 LOG_ALWAYS_FATAL_IF(thread.isCurrent(), "Must not be called on RenderThread");
345
346 FormatInfo format = determineFormat(thread, sourceBitmap);
347 if (!format.valid) {
348 return nullptr;
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400349 }
350
John Reckb90d4cb2018-04-19 17:05:35 -0700351 SkBitmap bitmap = makeHwCompatible(format, sourceBitmap);
John Reck1bcacfd2017-11-03 10:12:19 -0700352 sp<GraphicBuffer> buffer = new GraphicBuffer(
John Reckb90d4cb2018-04-19 17:05:35 -0700353 static_cast<uint32_t>(bitmap.width()), static_cast<uint32_t>(bitmap.height()),
354 format.pixelFormat,
John Reck1bcacfd2017-11-03 10:12:19 -0700355 GraphicBuffer::USAGE_HW_TEXTURE | GraphicBuffer::USAGE_SW_WRITE_NEVER |
356 GraphicBuffer::USAGE_SW_READ_NEVER,
357 std::string("Bitmap::allocateSkiaHardwareBitmap pid [") + std::to_string(getpid()) +
358 "]");
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400359
360 status_t error = buffer->initCheck();
361 if (error < 0) {
362 ALOGW("createGraphicBuffer() failed in GraphicBuffer.create()");
363 return nullptr;
364 }
365
John Reckb90d4cb2018-04-19 17:05:35 -0700366 EGLDisplay display = thread.queue().runSync([&]() -> EGLDisplay {
367 thread.requireGlContext();
368 return eglGetCurrentDisplay();
369 });
370
John Reck1bcacfd2017-11-03 10:12:19 -0700371 LOG_ALWAYS_FATAL_IF(display == EGL_NO_DISPLAY, "Failed to get EGL_DEFAULT_DISPLAY! err=%s",
372 uirenderer::renderthread::EglManager::eglErrorString());
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400373 // We use an EGLImage to access the content of the GraphicBuffer
374 // The EGL image is later bound to a 2D texture
John Reck1bcacfd2017-11-03 10:12:19 -0700375 EGLClientBuffer clientBuffer = (EGLClientBuffer)buffer->getNativeBuffer();
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400376 AutoEglImage autoImage(display, clientBuffer);
377 if (autoImage.image == EGL_NO_IMAGE_KHR) {
378 ALOGW("Could not create EGL image, err =%s",
John Reck1bcacfd2017-11-03 10:12:19 -0700379 uirenderer::renderthread::EglManager::eglErrorString());
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400380 return nullptr;
381 }
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400382
John Reckb90d4cb2018-04-19 17:05:35 -0700383 {
384 ATRACE_FORMAT("CPU -> gralloc transfer (%dx%d)", bitmap.width(), bitmap.height());
385 EGLSyncKHR fence = thread.queue().runSync([&]() -> EGLSyncKHR {
386 thread.requireGlContext();
387 sk_sp<GrContext> grContext = sk_ref_sp(thread.getGrContext());
388 AutoSkiaGlTexture glTexture;
389 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, autoImage.image);
390 GL_CHECKPOINT(MODERATE);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400391
John Reckb90d4cb2018-04-19 17:05:35 -0700392 // glTexSubImage2D is synchronous in sense that it memcpy() from pointer that we
393 // provide.
394 // But asynchronous in sense that driver may upload texture onto hardware buffer when we
395 // first
396 // use it in drawing
397 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap.width(), bitmap.height(), format.format,
398 format.type, bitmap.getPixels());
399 GL_CHECKPOINT(MODERATE);
400
401 EGLSyncKHR uploadFence =
402 eglCreateSyncKHR(eglGetCurrentDisplay(), EGL_SYNC_FENCE_KHR, NULL);
403 LOG_ALWAYS_FATAL_IF(uploadFence == EGL_NO_SYNC_KHR, "Could not create sync fence %#x",
404 eglGetError());
405 glFlush();
406 grContext->resetContext(kTextureBinding_GrGLBackendState);
407 return uploadFence;
408 });
409
410 EGLint waitStatus = eglClientWaitSyncKHR(display, fence, 0, FENCE_TIMEOUT);
411 LOG_ALWAYS_FATAL_IF(waitStatus != EGL_CONDITION_SATISFIED_KHR,
412 "Failed to wait for the fence %#x", eglGetError());
413
414 eglDestroySyncKHR(display, fence);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400415 }
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400416
417 return sk_sp<Bitmap>(new Bitmap(buffer.get(), bitmap.info()));
418}
419
Stan Iliev500a0c32016-10-26 10:30:09 -0400420} /* namespace skiapipeline */
421} /* namespace uirenderer */
422} /* namespace android */