1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/lite/delegates/gpu/gl/egl_surface.h"
17
18 #include "tensorflow/lite/delegates/gpu/common/status.h"
19 #include "tensorflow/lite/delegates/gpu/gl/gl_call.h"
20 #include "tensorflow/lite/delegates/gpu/gl/gl_errors.h"
21
22 namespace tflite {
23 namespace gpu {
24 namespace gl {
25
EglSurface(EglSurface && other)26 EglSurface::EglSurface(EglSurface&& other)
27 : surface_(other.surface_), display_(other.display_) {
28 other.surface_ = EGL_NO_SURFACE;
29 }
30
operator =(EglSurface && other)31 EglSurface& EglSurface::operator=(EglSurface&& other) {
32 if (this != &other) {
33 display_ = other.display_;
34 Invalidate();
35 std::swap(surface_, other.surface_);
36 }
37 return *this;
38 }
39
Invalidate()40 void EglSurface::Invalidate() {
41 if (surface_ != EGL_NO_SURFACE) {
42 eglDestroySurface(display_, surface_);
43 surface_ = EGL_NO_SURFACE;
44 }
45 }
46
CreatePbufferRGBSurface(EGLConfig config,EGLDisplay display,uint32_t height,uint32_t width,EglSurface * egl_surface)47 absl::Status CreatePbufferRGBSurface(EGLConfig config, EGLDisplay display,
48 uint32_t height, uint32_t width,
49 EglSurface* egl_surface) {
50 const EGLint pbuffer_attributes[] = {EGL_WIDTH,
51 static_cast<EGLint>(width),
52 EGL_HEIGHT,
53 static_cast<EGLint>(height),
54 EGL_TEXTURE_FORMAT,
55 EGL_TEXTURE_RGB,
56 EGL_TEXTURE_TARGET,
57 EGL_TEXTURE_2D,
58 EGL_NONE};
59 EGLSurface surface =
60 eglCreatePbufferSurface(display, config, pbuffer_attributes);
61 RETURN_IF_ERROR(GetOpenGlErrors());
62 if (surface == EGL_NO_SURFACE) {
63 return absl::InternalError(
64 "No EGL error, but eglCreatePbufferSurface failed");
65 }
66 *egl_surface = EglSurface(surface, display);
67 return absl::OkStatus();
68 }
69
70 } // namespace gl
71 } // namespace gpu
72 } // namespace tflite
73